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:
Jason Woltje
2026-01-28 18:43:12 -06:00
parent 05fc1c60f4
commit 132fe6ba98
30 changed files with 3812 additions and 1 deletions

View File

@@ -0,0 +1,224 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { ActivityService } from "../activity/activity.service";
import { ProjectStatus } from "@prisma/client";
import type { CreateProjectDto, UpdateProjectDto, QueryProjectsDto } from "./dto";
/**
* Service for managing projects
*/
@Injectable()
export class ProjectsService {
constructor(
private readonly prisma: PrismaService,
private readonly activityService: ActivityService
) {}
/**
* Create a new project
*/
async create(
workspaceId: string,
userId: string,
createProjectDto: CreateProjectDto
) {
const data: any = {
...createProjectDto,
workspaceId,
creatorId: userId,
status: createProjectDto.status || ProjectStatus.PLANNING,
metadata: createProjectDto.metadata || {},
};
const project = await this.prisma.project.create({
data,
include: {
creator: {
select: { id: true, name: true, email: true },
},
_count: {
select: { tasks: true, events: true },
},
},
});
// Log activity
await this.activityService.logProjectCreated(workspaceId, userId, project.id, {
name: project.name,
});
return project;
}
/**
* Get paginated projects with filters
*/
async findAll(query: QueryProjectsDto) {
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.status) {
where.status = query.status;
}
if (query.startDateFrom || query.startDateTo) {
where.startDate = {};
if (query.startDateFrom) {
where.startDate.gte = query.startDateFrom;
}
if (query.startDateTo) {
where.startDate.lte = query.startDateTo;
}
}
// Execute queries in parallel
const [data, total] = await Promise.all([
this.prisma.project.findMany({
where,
include: {
creator: {
select: { id: true, name: true, email: true },
},
_count: {
select: { tasks: true, events: true },
},
},
orderBy: {
createdAt: "desc",
},
skip,
take: limit,
}),
this.prisma.project.count({ where }),
]);
return {
data,
meta: {
total,
page,
limit,
totalPages: Math.ceil(total / limit),
},
};
}
/**
* Get a single project by ID
*/
async findOne(id: string, workspaceId: string) {
const project = await this.prisma.project.findUnique({
where: {
id,
workspaceId,
},
include: {
creator: {
select: { id: true, name: true, email: true },
},
tasks: {
select: {
id: true,
title: true,
status: true,
priority: true,
dueDate: true,
},
orderBy: { sortOrder: "asc" },
},
events: {
select: {
id: true,
title: true,
startTime: true,
endTime: true,
},
orderBy: { startTime: "asc" },
},
_count: {
select: { tasks: true, events: true },
},
},
});
if (!project) {
throw new NotFoundException(`Project with ID ${id} not found`);
}
return project;
}
/**
* Update a project
*/
async update(
id: string,
workspaceId: string,
userId: string,
updateProjectDto: UpdateProjectDto
) {
// Verify project exists
const existingProject = await this.prisma.project.findUnique({
where: { id, workspaceId },
});
if (!existingProject) {
throw new NotFoundException(`Project with ID ${id} not found`);
}
const project = await this.prisma.project.update({
where: {
id,
workspaceId,
},
data: updateProjectDto,
include: {
creator: {
select: { id: true, name: true, email: true },
},
_count: {
select: { tasks: true, events: true },
},
},
});
// Log activity
await this.activityService.logProjectUpdated(workspaceId, userId, id, {
changes: updateProjectDto,
});
return project;
}
/**
* Delete a project
*/
async remove(id: string, workspaceId: string, userId: string) {
// Verify project exists
const project = await this.prisma.project.findUnique({
where: { id, workspaceId },
});
if (!project) {
throw new NotFoundException(`Project with ID ${id} not found`);
}
await this.prisma.project.delete({
where: {
id,
workspaceId,
},
});
// Log activity
await this.activityService.logProjectDeleted(workspaceId, userId, id, {
name: project.name,
});
}
}