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,164 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { Test, TestingModule } from "@nestjs/testing";
import { ProjectsController } from "./projects.controller";
import { ProjectsService } from "./projects.service";
import { ProjectStatus } from "@prisma/client";
import { AuthGuard } from "../auth/guards/auth.guard";
import { ExecutionContext } from "@nestjs/common";
describe("ProjectsController", () => {
let controller: ProjectsController;
let service: ProjectsService;
const mockProjectsService = {
create: vi.fn(),
findAll: vi.fn(),
findOne: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
};
const mockAuthGuard = {
canActivate: vi.fn((context: ExecutionContext) => {
const request = context.switchToHttp().getRequest();
request.user = {
id: "550e8400-e29b-41d4-a716-446655440002",
workspaceId: "550e8400-e29b-41d4-a716-446655440001",
};
return true;
}),
};
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
const mockProjectId = "550e8400-e29b-41d4-a716-446655440003";
const mockRequest = {
user: {
id: mockUserId,
workspaceId: mockWorkspaceId,
},
};
const mockProject = {
id: mockProjectId,
workspaceId: mockWorkspaceId,
name: "Test Project",
description: "Test Description",
status: ProjectStatus.PLANNING,
startDate: new Date("2026-02-01"),
endDate: new Date("2026-03-01"),
creatorId: mockUserId,
color: "#FF5733",
metadata: {},
createdAt: new Date(),
updatedAt: new Date(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ProjectsController],
providers: [
{
provide: ProjectsService,
useValue: mockProjectsService,
},
],
})
.overrideGuard(AuthGuard)
.useValue(mockAuthGuard)
.compile();
controller = module.get<ProjectsController>(ProjectsController);
service = module.get<ProjectsService>(ProjectsService);
vi.clearAllMocks();
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
describe("create", () => {
it("should create a project", async () => {
const createDto = {
name: "New Project",
description: "Project description",
};
mockProjectsService.create.mockResolvedValue(mockProject);
const result = await controller.create(createDto, mockRequest);
expect(result).toEqual(mockProject);
expect(service.create).toHaveBeenCalledWith(
mockWorkspaceId,
mockUserId,
createDto
);
});
});
describe("findAll", () => {
it("should return paginated projects", async () => {
const query = {
workspaceId: mockWorkspaceId,
};
const paginatedResult = {
data: [mockProject],
meta: {
total: 1,
page: 1,
limit: 50,
totalPages: 1,
},
};
mockProjectsService.findAll.mockResolvedValue(paginatedResult);
const result = await controller.findAll(query, mockRequest);
expect(result).toEqual(paginatedResult);
});
});
describe("findOne", () => {
it("should return a project by id", async () => {
mockProjectsService.findOne.mockResolvedValue(mockProject);
const result = await controller.findOne(mockProjectId, mockRequest);
expect(result).toEqual(mockProject);
});
});
describe("update", () => {
it("should update a project", async () => {
const updateDto = {
name: "Updated Project",
};
const updatedProject = { ...mockProject, ...updateDto };
mockProjectsService.update.mockResolvedValue(updatedProject);
const result = await controller.update(mockProjectId, updateDto, mockRequest);
expect(result).toEqual(updatedProject);
});
});
describe("remove", () => {
it("should delete a project", async () => {
mockProjectsService.remove.mockResolvedValue(undefined);
await controller.remove(mockProjectId, mockRequest);
expect(service.remove).toHaveBeenCalledWith(
mockProjectId,
mockWorkspaceId,
mockUserId
);
});
});
});