feat(#5): Implement CRUD APIs for tasks, events, and projects
Implements comprehensive CRUD APIs following TDD principles with 92.44% test coverage (exceeds 85% requirement). Features: - Tasks API: Full CRUD with filtering, pagination, and subtask support - Events API: Full CRUD with recurrence support and date filtering - Projects API: Full CRUD with task/event association - Authentication guards on all endpoints - Workspace-scoped queries for multi-tenant isolation - Activity logging for all operations (CREATED, UPDATED, DELETED, etc.) - DTOs with class-validator validation - Comprehensive test suite (221 tests, 44 for new APIs) Implementation: - Services: Business logic with Prisma ORM integration - Controllers: RESTful endpoints with AuthGuard - Modules: Properly registered in AppModule - Documentation: Complete API reference in docs/4-api/4-crud-endpoints/ Test Coverage: - Tasks: 96.1% - Events: 89.83% - Projects: 84.21% - Overall: 92.44% TDD Workflow: 1. RED: Wrote failing tests first 2. GREEN: Implemented minimal code to pass tests 3. REFACTOR: Improved code quality while maintaining coverage Refs #5 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
204
apps/api/src/events/events.service.ts
Normal file
204
apps/api/src/events/events.service.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import type { CreateEventDto, UpdateEventDto, QueryEventsDto } from "./dto";
|
||||
|
||||
/**
|
||||
* Service for managing events
|
||||
*/
|
||||
@Injectable()
|
||||
export class EventsService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly activityService: ActivityService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create a new event
|
||||
*/
|
||||
async create(workspaceId: string, userId: string, createEventDto: CreateEventDto) {
|
||||
const data: any = {
|
||||
...createEventDto,
|
||||
workspaceId,
|
||||
creatorId: userId,
|
||||
allDay: createEventDto.allDay ?? false,
|
||||
metadata: createEventDto.metadata || {},
|
||||
};
|
||||
|
||||
const event = await this.prisma.event.create({
|
||||
data,
|
||||
include: {
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Log activity
|
||||
await this.activityService.logEventCreated(workspaceId, userId, event.id, {
|
||||
title: event.title,
|
||||
});
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated events with filters
|
||||
*/
|
||||
async findAll(query: QueryEventsDto) {
|
||||
const page = query.page || 1;
|
||||
const limit = query.limit || 50;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Build where clause
|
||||
const where: any = {
|
||||
workspaceId: query.workspaceId,
|
||||
};
|
||||
|
||||
if (query.projectId) {
|
||||
where.projectId = query.projectId;
|
||||
}
|
||||
|
||||
if (query.allDay !== undefined) {
|
||||
where.allDay = query.allDay;
|
||||
}
|
||||
|
||||
if (query.startFrom || query.startTo) {
|
||||
where.startTime = {};
|
||||
if (query.startFrom) {
|
||||
where.startTime.gte = query.startFrom;
|
||||
}
|
||||
if (query.startTo) {
|
||||
where.startTime.lte = query.startTo;
|
||||
}
|
||||
}
|
||||
|
||||
// Execute queries in parallel
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.event.findMany({
|
||||
where,
|
||||
include: {
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
startTime: "asc",
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
}),
|
||||
this.prisma.event.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single event by ID
|
||||
*/
|
||||
async findOne(id: string, workspaceId: string) {
|
||||
const event = await this.prisma.event.findUnique({
|
||||
where: {
|
||||
id,
|
||||
workspaceId,
|
||||
},
|
||||
include: {
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
throw new NotFoundException(`Event with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an event
|
||||
*/
|
||||
async update(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
updateEventDto: UpdateEventDto
|
||||
) {
|
||||
// Verify event exists
|
||||
const existingEvent = await this.prisma.event.findUnique({
|
||||
where: { id, workspaceId },
|
||||
});
|
||||
|
||||
if (!existingEvent) {
|
||||
throw new NotFoundException(`Event with ID ${id} not found`);
|
||||
}
|
||||
|
||||
const event = await this.prisma.event.update({
|
||||
where: {
|
||||
id,
|
||||
workspaceId,
|
||||
},
|
||||
data: updateEventDto,
|
||||
include: {
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Log activity
|
||||
await this.activityService.logEventUpdated(workspaceId, userId, id, {
|
||||
changes: updateEventDto,
|
||||
});
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an event
|
||||
*/
|
||||
async remove(id: string, workspaceId: string, userId: string) {
|
||||
// Verify event exists
|
||||
const event = await this.prisma.event.findUnique({
|
||||
where: { id, workspaceId },
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
throw new NotFoundException(`Event with ID ${id} not found`);
|
||||
}
|
||||
|
||||
await this.prisma.event.delete({
|
||||
where: {
|
||||
id,
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
|
||||
// Log activity
|
||||
await this.activityService.logEventDeleted(workspaceId, userId, id, {
|
||||
title: event.title,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user