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:
227
apps/api/src/projects/projects.service.spec.ts
Normal file
227
apps/api/src/projects/projects.service.spec.ts
Normal file
@@ -0,0 +1,227 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { ProjectsService } from "./projects.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { ProjectStatus } from "@prisma/client";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("ProjectsService", () => {
|
||||
let service: ProjectsService;
|
||||
let prisma: PrismaService;
|
||||
let activityService: ActivityService;
|
||||
|
||||
const mockPrismaService = {
|
||||
project: {
|
||||
create: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const mockActivityService = {
|
||||
logProjectCreated: vi.fn(),
|
||||
logProjectUpdated: vi.fn(),
|
||||
logProjectDeleted: vi.fn(),
|
||||
};
|
||||
|
||||
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
|
||||
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
|
||||
const mockProjectId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
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({
|
||||
providers: [
|
||||
ProjectsService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrismaService,
|
||||
},
|
||||
{
|
||||
provide: ActivityService,
|
||||
useValue: mockActivityService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<ProjectsService>(ProjectsService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
activityService = module.get<ActivityService>(ActivityService);
|
||||
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a project and log activity", async () => {
|
||||
const createDto = {
|
||||
name: "New Project",
|
||||
description: "Project description",
|
||||
color: "#FF5733",
|
||||
};
|
||||
|
||||
mockPrismaService.project.create.mockResolvedValue(mockProject);
|
||||
mockActivityService.logProjectCreated.mockResolvedValue({});
|
||||
|
||||
const result = await service.create(mockWorkspaceId, mockUserId, createDto);
|
||||
|
||||
expect(result).toEqual(mockProject);
|
||||
expect(prisma.project.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
...createDto,
|
||||
workspaceId: mockWorkspaceId,
|
||||
creatorId: mockUserId,
|
||||
status: ProjectStatus.PLANNING,
|
||||
metadata: {},
|
||||
},
|
||||
include: {
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
_count: {
|
||||
select: { tasks: true, events: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(activityService.logProjectCreated).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockProject.id,
|
||||
{ name: mockProject.name }
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated projects with default pagination", async () => {
|
||||
const projects = [mockProject];
|
||||
mockPrismaService.project.findMany.mockResolvedValue(projects);
|
||||
mockPrismaService.project.count.mockResolvedValue(1);
|
||||
|
||||
const result = await service.findAll({ workspaceId: mockWorkspaceId });
|
||||
|
||||
expect(result).toEqual({
|
||||
data: projects,
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should filter by status", async () => {
|
||||
mockPrismaService.project.findMany.mockResolvedValue([mockProject]);
|
||||
mockPrismaService.project.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
status: ProjectStatus.ACTIVE,
|
||||
});
|
||||
|
||||
expect(prisma.project.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
status: ProjectStatus.ACTIVE,
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return a project by id", async () => {
|
||||
mockPrismaService.project.findUnique.mockResolvedValue(mockProject);
|
||||
|
||||
const result = await service.findOne(mockProjectId, mockWorkspaceId);
|
||||
|
||||
expect(result).toEqual(mockProject);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if project not found", async () => {
|
||||
mockPrismaService.project.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.findOne(mockProjectId, mockWorkspaceId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a project and log activity", async () => {
|
||||
const updateDto = {
|
||||
name: "Updated Project",
|
||||
status: ProjectStatus.ACTIVE,
|
||||
};
|
||||
|
||||
mockPrismaService.project.findUnique.mockResolvedValue(mockProject);
|
||||
mockPrismaService.project.update.mockResolvedValue({
|
||||
...mockProject,
|
||||
...updateDto,
|
||||
});
|
||||
mockActivityService.logProjectUpdated.mockResolvedValue({});
|
||||
|
||||
const result = await service.update(
|
||||
mockProjectId,
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
updateDto
|
||||
);
|
||||
|
||||
expect(result.name).toBe("Updated Project");
|
||||
expect(activityService.logProjectUpdated).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if project not found", async () => {
|
||||
mockPrismaService.project.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.update(mockProjectId, mockWorkspaceId, mockUserId, { name: "Test" })
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a project and log activity", async () => {
|
||||
mockPrismaService.project.findUnique.mockResolvedValue(mockProject);
|
||||
mockPrismaService.project.delete.mockResolvedValue(mockProject);
|
||||
mockActivityService.logProjectDeleted.mockResolvedValue({});
|
||||
|
||||
await service.remove(mockProjectId, mockWorkspaceId, mockUserId);
|
||||
|
||||
expect(prisma.project.delete).toHaveBeenCalled();
|
||||
expect(activityService.logProjectDeleted).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if project not found", async () => {
|
||||
mockPrismaService.project.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.remove(mockProjectId, mockWorkspaceId, mockUserId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user