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:
@@ -4,9 +4,21 @@ import { AppService } from "./app.service";
|
||||
import { PrismaModule } from "./prisma/prisma.module";
|
||||
import { DatabaseModule } from "./database/database.module";
|
||||
import { AuthModule } from "./auth/auth.module";
|
||||
import { ActivityModule } from "./activity/activity.module";
|
||||
import { TasksModule } from "./tasks/tasks.module";
|
||||
import { EventsModule } from "./events/events.module";
|
||||
import { ProjectsModule } from "./projects/projects.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, DatabaseModule, AuthModule],
|
||||
imports: [
|
||||
PrismaModule,
|
||||
DatabaseModule,
|
||||
AuthModule,
|
||||
ActivityModule,
|
||||
TasksModule,
|
||||
EventsModule,
|
||||
ProjectsModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
|
||||
53
apps/api/src/events/dto/create-event.dto.ts
Normal file
53
apps/api/src/events/dto/create-event.dto.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsUUID,
|
||||
IsDateString,
|
||||
IsBoolean,
|
||||
IsObject,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* DTO for creating a new event
|
||||
*/
|
||||
export class CreateEventDto {
|
||||
@IsString({ message: "title must be a string" })
|
||||
@MinLength(1, { message: "title must not be empty" })
|
||||
@MaxLength(255, { message: "title must not exceed 255 characters" })
|
||||
title!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "description must be a string" })
|
||||
@MaxLength(10000, { message: "description must not exceed 10000 characters" })
|
||||
description?: string;
|
||||
|
||||
@IsDateString({}, { message: "startTime must be a valid ISO 8601 date string" })
|
||||
startTime!: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "endTime must be a valid ISO 8601 date string" })
|
||||
endTime?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean({ message: "allDay must be a boolean" })
|
||||
allDay?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "location must be a string" })
|
||||
@MaxLength(500, { message: "location must not exceed 500 characters" })
|
||||
location?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "recurrence must be an object" })
|
||||
recurrence?: Record<string, unknown>;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "projectId must be a valid UUID" })
|
||||
projectId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "metadata must be an object" })
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
3
apps/api/src/events/dto/index.ts
Normal file
3
apps/api/src/events/dto/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { CreateEventDto } from "./create-event.dto";
|
||||
export { UpdateEventDto } from "./update-event.dto";
|
||||
export { QueryEventsDto } from "./query-events.dto";
|
||||
48
apps/api/src/events/dto/query-events.dto.ts
Normal file
48
apps/api/src/events/dto/query-events.dto.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import {
|
||||
IsUUID,
|
||||
IsOptional,
|
||||
IsInt,
|
||||
Min,
|
||||
Max,
|
||||
IsDateString,
|
||||
IsBoolean,
|
||||
} from "class-validator";
|
||||
import { Type } from "class-transformer";
|
||||
|
||||
/**
|
||||
* DTO for querying events with filters and pagination
|
||||
*/
|
||||
export class QueryEventsDto {
|
||||
@IsUUID("4", { message: "workspaceId must be a valid UUID" })
|
||||
workspaceId!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "projectId must be a valid UUID" })
|
||||
projectId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startFrom must be a valid ISO 8601 date string" })
|
||||
startFrom?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startTo must be a valid ISO 8601 date string" })
|
||||
startTo?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean({ message: "allDay must be a boolean" })
|
||||
@Type(() => Boolean)
|
||||
allDay?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt({ message: "page must be an integer" })
|
||||
@Min(1, { message: "page must be at least 1" })
|
||||
page?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt({ message: "limit must be an integer" })
|
||||
@Min(1, { message: "limit must be at least 1" })
|
||||
@Max(100, { message: "limit must not exceed 100" })
|
||||
limit?: number;
|
||||
}
|
||||
56
apps/api/src/events/dto/update-event.dto.ts
Normal file
56
apps/api/src/events/dto/update-event.dto.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsUUID,
|
||||
IsDateString,
|
||||
IsBoolean,
|
||||
IsObject,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* DTO for updating an existing event
|
||||
* All fields are optional to support partial updates
|
||||
*/
|
||||
export class UpdateEventDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: "title must be a string" })
|
||||
@MinLength(1, { message: "title must not be empty" })
|
||||
@MaxLength(255, { message: "title must not exceed 255 characters" })
|
||||
title?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "description must be a string" })
|
||||
@MaxLength(10000, { message: "description must not exceed 10000 characters" })
|
||||
description?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startTime must be a valid ISO 8601 date string" })
|
||||
startTime?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "endTime must be a valid ISO 8601 date string" })
|
||||
endTime?: Date | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean({ message: "allDay must be a boolean" })
|
||||
allDay?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "location must be a string" })
|
||||
@MaxLength(500, { message: "location must not exceed 500 characters" })
|
||||
location?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "recurrence must be an object" })
|
||||
recurrence?: Record<string, unknown> | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "projectId must be a valid UUID" })
|
||||
projectId?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "metadata must be an object" })
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
165
apps/api/src/events/events.controller.spec.ts
Normal file
165
apps/api/src/events/events.controller.spec.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { EventsController } from "./events.controller";
|
||||
import { EventsService } from "./events.service";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
import { ExecutionContext } from "@nestjs/common";
|
||||
|
||||
describe("EventsController", () => {
|
||||
let controller: EventsController;
|
||||
let service: EventsService;
|
||||
|
||||
const mockEventsService = {
|
||||
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 mockEventId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
const mockRequest = {
|
||||
user: {
|
||||
id: mockUserId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
};
|
||||
|
||||
const mockEvent = {
|
||||
id: mockEventId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
title: "Test Event",
|
||||
description: "Test Description",
|
||||
startTime: new Date("2026-02-01T10:00:00Z"),
|
||||
endTime: new Date("2026-02-01T11:00:00Z"),
|
||||
allDay: false,
|
||||
location: "Conference Room A",
|
||||
recurrence: null,
|
||||
creatorId: mockUserId,
|
||||
projectId: null,
|
||||
metadata: {},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [EventsController],
|
||||
providers: [
|
||||
{
|
||||
provide: EventsService,
|
||||
useValue: mockEventsService,
|
||||
},
|
||||
],
|
||||
})
|
||||
.overrideGuard(AuthGuard)
|
||||
.useValue(mockAuthGuard)
|
||||
.compile();
|
||||
|
||||
controller = module.get<EventsController>(EventsController);
|
||||
service = module.get<EventsService>(EventsService);
|
||||
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create an event", async () => {
|
||||
const createDto = {
|
||||
title: "New Event",
|
||||
startTime: new Date("2026-02-01T10:00:00Z"),
|
||||
};
|
||||
|
||||
mockEventsService.create.mockResolvedValue(mockEvent);
|
||||
|
||||
const result = await controller.create(createDto, mockRequest);
|
||||
|
||||
expect(result).toEqual(mockEvent);
|
||||
expect(service.create).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
createDto
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated events", async () => {
|
||||
const query = {
|
||||
workspaceId: mockWorkspaceId,
|
||||
};
|
||||
|
||||
const paginatedResult = {
|
||||
data: [mockEvent],
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
},
|
||||
};
|
||||
|
||||
mockEventsService.findAll.mockResolvedValue(paginatedResult);
|
||||
|
||||
const result = await controller.findAll(query, mockRequest);
|
||||
|
||||
expect(result).toEqual(paginatedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return an event by id", async () => {
|
||||
mockEventsService.findOne.mockResolvedValue(mockEvent);
|
||||
|
||||
const result = await controller.findOne(mockEventId, mockRequest);
|
||||
|
||||
expect(result).toEqual(mockEvent);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update an event", async () => {
|
||||
const updateDto = {
|
||||
title: "Updated Event",
|
||||
};
|
||||
|
||||
const updatedEvent = { ...mockEvent, ...updateDto };
|
||||
mockEventsService.update.mockResolvedValue(updatedEvent);
|
||||
|
||||
const result = await controller.update(mockEventId, updateDto, mockRequest);
|
||||
|
||||
expect(result).toEqual(updatedEvent);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete an event", async () => {
|
||||
mockEventsService.remove.mockResolvedValue(undefined);
|
||||
|
||||
await controller.remove(mockEventId, mockRequest);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(
|
||||
mockEventId,
|
||||
mockWorkspaceId,
|
||||
mockUserId
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
100
apps/api/src/events/events.controller.ts
Normal file
100
apps/api/src/events/events.controller.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
Request,
|
||||
} from "@nestjs/common";
|
||||
import { EventsService } from "./events.service";
|
||||
import { CreateEventDto, UpdateEventDto, QueryEventsDto } from "./dto";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
|
||||
/**
|
||||
* Controller for event endpoints
|
||||
* All endpoints require authentication
|
||||
*/
|
||||
@Controller("events")
|
||||
@UseGuards(AuthGuard)
|
||||
export class EventsController {
|
||||
constructor(private readonly eventsService: EventsService) {}
|
||||
|
||||
/**
|
||||
* POST /api/events
|
||||
* Create a new event
|
||||
*/
|
||||
@Post()
|
||||
async create(@Body() createEventDto: CreateEventDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId or userId not found");
|
||||
}
|
||||
|
||||
return this.eventsService.create(workspaceId, userId, createEventDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/events
|
||||
* Get paginated events with optional filters
|
||||
*/
|
||||
@Get()
|
||||
async findAll(@Query() query: QueryEventsDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId || query.workspaceId;
|
||||
return this.eventsService.findAll({ ...query, workspaceId });
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/events/:id
|
||||
* Get a single event by ID
|
||||
*/
|
||||
@Get(":id")
|
||||
async findOne(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
if (!workspaceId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
return this.eventsService.findOne(id, workspaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/events/:id
|
||||
* Update an event
|
||||
*/
|
||||
@Patch(":id")
|
||||
async update(
|
||||
@Param("id") id: string,
|
||||
@Body() updateEventDto: UpdateEventDto,
|
||||
@Request() req: any
|
||||
) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
|
||||
return this.eventsService.update(id, workspaceId, userId, updateEventDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/events/:id
|
||||
* Delete an event
|
||||
*/
|
||||
@Delete(":id")
|
||||
async remove(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
|
||||
return this.eventsService.remove(id, workspaceId, userId);
|
||||
}
|
||||
}
|
||||
14
apps/api/src/events/events.module.ts
Normal file
14
apps/api/src/events/events.module.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { EventsController } from "./events.controller";
|
||||
import { EventsService } from "./events.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
import { ActivityModule } from "../activity/activity.module";
|
||||
import { AuthModule } from "../auth/auth.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, ActivityModule, AuthModule],
|
||||
controllers: [EventsController],
|
||||
providers: [EventsService],
|
||||
exports: [EventsService],
|
||||
})
|
||||
export class EventsModule {}
|
||||
236
apps/api/src/events/events.service.spec.ts
Normal file
236
apps/api/src/events/events.service.spec.ts
Normal file
@@ -0,0 +1,236 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { EventsService } from "./events.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("EventsService", () => {
|
||||
let service: EventsService;
|
||||
let prisma: PrismaService;
|
||||
let activityService: ActivityService;
|
||||
|
||||
const mockPrismaService = {
|
||||
event: {
|
||||
create: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const mockActivityService = {
|
||||
logEventCreated: vi.fn(),
|
||||
logEventUpdated: vi.fn(),
|
||||
logEventDeleted: vi.fn(),
|
||||
};
|
||||
|
||||
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
|
||||
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
|
||||
const mockEventId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
const mockEvent = {
|
||||
id: mockEventId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
title: "Test Event",
|
||||
description: "Test Description",
|
||||
startTime: new Date("2026-02-01T10:00:00Z"),
|
||||
endTime: new Date("2026-02-01T11:00:00Z"),
|
||||
allDay: false,
|
||||
location: "Conference Room A",
|
||||
recurrence: null,
|
||||
creatorId: mockUserId,
|
||||
projectId: null,
|
||||
metadata: {},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
EventsService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrismaService,
|
||||
},
|
||||
{
|
||||
provide: ActivityService,
|
||||
useValue: mockActivityService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<EventsService>(EventsService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
activityService = module.get<ActivityService>(ActivityService);
|
||||
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create an event and log activity", async () => {
|
||||
const createDto = {
|
||||
title: "New Event",
|
||||
description: "Event description",
|
||||
startTime: new Date("2026-02-01T10:00:00Z"),
|
||||
allDay: false,
|
||||
};
|
||||
|
||||
mockPrismaService.event.create.mockResolvedValue(mockEvent);
|
||||
mockActivityService.logEventCreated.mockResolvedValue({});
|
||||
|
||||
const result = await service.create(mockWorkspaceId, mockUserId, createDto);
|
||||
|
||||
expect(result).toEqual(mockEvent);
|
||||
expect(prisma.event.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
...createDto,
|
||||
workspaceId: mockWorkspaceId,
|
||||
creatorId: mockUserId,
|
||||
allDay: false,
|
||||
metadata: {},
|
||||
},
|
||||
include: {
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(activityService.logEventCreated).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockEvent.id,
|
||||
{ title: mockEvent.title }
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated events with default pagination", async () => {
|
||||
const events = [mockEvent];
|
||||
mockPrismaService.event.findMany.mockResolvedValue(events);
|
||||
mockPrismaService.event.count.mockResolvedValue(1);
|
||||
|
||||
const result = await service.findAll({ workspaceId: mockWorkspaceId });
|
||||
|
||||
expect(result).toEqual({
|
||||
data: events,
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should filter by date range", async () => {
|
||||
const startFrom = new Date("2026-02-01");
|
||||
const startTo = new Date("2026-02-28");
|
||||
|
||||
mockPrismaService.event.findMany.mockResolvedValue([mockEvent]);
|
||||
mockPrismaService.event.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
startFrom,
|
||||
startTo,
|
||||
});
|
||||
|
||||
expect(prisma.event.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
startTime: {
|
||||
gte: startFrom,
|
||||
lte: startTo,
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return an event by id", async () => {
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(mockEvent);
|
||||
|
||||
const result = await service.findOne(mockEventId, mockWorkspaceId);
|
||||
|
||||
expect(result).toEqual(mockEvent);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if event not found", async () => {
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.findOne(mockEventId, mockWorkspaceId)).rejects.toThrow(
|
||||
NotFoundException
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update an event and log activity", async () => {
|
||||
const updateDto = {
|
||||
title: "Updated Event",
|
||||
location: "New Location",
|
||||
};
|
||||
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(mockEvent);
|
||||
mockPrismaService.event.update.mockResolvedValue({
|
||||
...mockEvent,
|
||||
...updateDto,
|
||||
});
|
||||
mockActivityService.logEventUpdated.mockResolvedValue({});
|
||||
|
||||
const result = await service.update(
|
||||
mockEventId,
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
updateDto
|
||||
);
|
||||
|
||||
expect(result.title).toBe("Updated Event");
|
||||
expect(activityService.logEventUpdated).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if event not found", async () => {
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.update(mockEventId, mockWorkspaceId, mockUserId, { title: "Test" })
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete an event and log activity", async () => {
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(mockEvent);
|
||||
mockPrismaService.event.delete.mockResolvedValue(mockEvent);
|
||||
mockActivityService.logEventDeleted.mockResolvedValue({});
|
||||
|
||||
await service.remove(mockEventId, mockWorkspaceId, mockUserId);
|
||||
|
||||
expect(prisma.event.delete).toHaveBeenCalled();
|
||||
expect(activityService.logEventDeleted).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if event not found", async () => {
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.remove(mockEventId, mockWorkspaceId, mockUserId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
});
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
49
apps/api/src/projects/dto/create-project.dto.ts
Normal file
49
apps/api/src/projects/dto/create-project.dto.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { ProjectStatus } from "@prisma/client";
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsEnum,
|
||||
IsDateString,
|
||||
IsObject,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
Matches,
|
||||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* DTO for creating a new project
|
||||
*/
|
||||
export class CreateProjectDto {
|
||||
@IsString({ message: "name must be a string" })
|
||||
@MinLength(1, { message: "name must not be empty" })
|
||||
@MaxLength(255, { message: "name must not exceed 255 characters" })
|
||||
name!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "description must be a string" })
|
||||
@MaxLength(10000, { message: "description must not exceed 10000 characters" })
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(ProjectStatus, { message: "status must be a valid ProjectStatus" })
|
||||
status?: ProjectStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startDate must be a valid ISO 8601 date string" })
|
||||
startDate?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "endDate must be a valid ISO 8601 date string" })
|
||||
endDate?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "color must be a string" })
|
||||
@Matches(/^#[0-9A-F]{6}$/i, {
|
||||
message: "color must be a valid hex color code (e.g., #FF5733)",
|
||||
})
|
||||
color?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "metadata must be an object" })
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
3
apps/api/src/projects/dto/index.ts
Normal file
3
apps/api/src/projects/dto/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { CreateProjectDto } from "./create-project.dto";
|
||||
export { UpdateProjectDto } from "./update-project.dto";
|
||||
export { QueryProjectsDto } from "./query-projects.dto";
|
||||
44
apps/api/src/projects/dto/query-projects.dto.ts
Normal file
44
apps/api/src/projects/dto/query-projects.dto.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { ProjectStatus } from "@prisma/client";
|
||||
import {
|
||||
IsUUID,
|
||||
IsEnum,
|
||||
IsOptional,
|
||||
IsInt,
|
||||
Min,
|
||||
Max,
|
||||
IsDateString,
|
||||
} from "class-validator";
|
||||
import { Type } from "class-transformer";
|
||||
|
||||
/**
|
||||
* DTO for querying projects with filters and pagination
|
||||
*/
|
||||
export class QueryProjectsDto {
|
||||
@IsUUID("4", { message: "workspaceId must be a valid UUID" })
|
||||
workspaceId!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(ProjectStatus, { message: "status must be a valid ProjectStatus" })
|
||||
status?: ProjectStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startDateFrom must be a valid ISO 8601 date string" })
|
||||
startDateFrom?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startDateTo must be a valid ISO 8601 date string" })
|
||||
startDateTo?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt({ message: "page must be an integer" })
|
||||
@Min(1, { message: "page must be at least 1" })
|
||||
page?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt({ message: "limit must be an integer" })
|
||||
@Min(1, { message: "limit must be at least 1" })
|
||||
@Max(100, { message: "limit must not exceed 100" })
|
||||
limit?: number;
|
||||
}
|
||||
51
apps/api/src/projects/dto/update-project.dto.ts
Normal file
51
apps/api/src/projects/dto/update-project.dto.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { ProjectStatus } from "@prisma/client";
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsEnum,
|
||||
IsDateString,
|
||||
IsObject,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
Matches,
|
||||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* DTO for updating an existing project
|
||||
* All fields are optional to support partial updates
|
||||
*/
|
||||
export class UpdateProjectDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: "name must be a string" })
|
||||
@MinLength(1, { message: "name must not be empty" })
|
||||
@MaxLength(255, { message: "name must not exceed 255 characters" })
|
||||
name?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "description must be a string" })
|
||||
@MaxLength(10000, { message: "description must not exceed 10000 characters" })
|
||||
description?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(ProjectStatus, { message: "status must be a valid ProjectStatus" })
|
||||
status?: ProjectStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "startDate must be a valid ISO 8601 date string" })
|
||||
startDate?: Date | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "endDate must be a valid ISO 8601 date string" })
|
||||
endDate?: Date | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "color must be a string" })
|
||||
@Matches(/^#[0-9A-F]{6}$/i, {
|
||||
message: "color must be a valid hex color code (e.g., #FF5733)",
|
||||
})
|
||||
color?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "metadata must be an object" })
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
164
apps/api/src/projects/projects.controller.spec.ts
Normal file
164
apps/api/src/projects/projects.controller.spec.ts
Normal 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
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
100
apps/api/src/projects/projects.controller.ts
Normal file
100
apps/api/src/projects/projects.controller.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
Request,
|
||||
} from "@nestjs/common";
|
||||
import { ProjectsService } from "./projects.service";
|
||||
import { CreateProjectDto, UpdateProjectDto, QueryProjectsDto } from "./dto";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
|
||||
/**
|
||||
* Controller for project endpoints
|
||||
* All endpoints require authentication
|
||||
*/
|
||||
@Controller("projects")
|
||||
@UseGuards(AuthGuard)
|
||||
export class ProjectsController {
|
||||
constructor(private readonly projectsService: ProjectsService) {}
|
||||
|
||||
/**
|
||||
* POST /api/projects
|
||||
* Create a new project
|
||||
*/
|
||||
@Post()
|
||||
async create(@Body() createProjectDto: CreateProjectDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId or userId not found");
|
||||
}
|
||||
|
||||
return this.projectsService.create(workspaceId, userId, createProjectDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/projects
|
||||
* Get paginated projects with optional filters
|
||||
*/
|
||||
@Get()
|
||||
async findAll(@Query() query: QueryProjectsDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId || query.workspaceId;
|
||||
return this.projectsService.findAll({ ...query, workspaceId });
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/projects/:id
|
||||
* Get a single project by ID
|
||||
*/
|
||||
@Get(":id")
|
||||
async findOne(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
if (!workspaceId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
return this.projectsService.findOne(id, workspaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/projects/:id
|
||||
* Update a project
|
||||
*/
|
||||
@Patch(":id")
|
||||
async update(
|
||||
@Param("id") id: string,
|
||||
@Body() updateProjectDto: UpdateProjectDto,
|
||||
@Request() req: any
|
||||
) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
|
||||
return this.projectsService.update(id, workspaceId, userId, updateProjectDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/projects/:id
|
||||
* Delete a project
|
||||
*/
|
||||
@Delete(":id")
|
||||
async remove(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
|
||||
return this.projectsService.remove(id, workspaceId, userId);
|
||||
}
|
||||
}
|
||||
14
apps/api/src/projects/projects.module.ts
Normal file
14
apps/api/src/projects/projects.module.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ProjectsController } from "./projects.controller";
|
||||
import { ProjectsService } from "./projects.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
import { ActivityModule } from "../activity/activity.module";
|
||||
import { AuthModule } from "../auth/auth.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, ActivityModule, AuthModule],
|
||||
controllers: [ProjectsController],
|
||||
providers: [ProjectsService],
|
||||
exports: [ProjectsService],
|
||||
})
|
||||
export class ProjectsModule {}
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
224
apps/api/src/projects/projects.service.ts
Normal file
224
apps/api/src/projects/projects.service.ts
Normal 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
61
apps/api/src/tasks/dto/create-task.dto.ts
Normal file
61
apps/api/src/tasks/dto/create-task.dto.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { TaskStatus, TaskPriority } from "@prisma/client";
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsUUID,
|
||||
IsEnum,
|
||||
IsDateString,
|
||||
IsInt,
|
||||
IsObject,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
Min,
|
||||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* DTO for creating a new task
|
||||
*/
|
||||
export class CreateTaskDto {
|
||||
@IsString({ message: "title must be a string" })
|
||||
@MinLength(1, { message: "title must not be empty" })
|
||||
@MaxLength(255, { message: "title must not exceed 255 characters" })
|
||||
title!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "description must be a string" })
|
||||
@MaxLength(10000, { message: "description must not exceed 10000 characters" })
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(TaskStatus, { message: "status must be a valid TaskStatus" })
|
||||
status?: TaskStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(TaskPriority, { message: "priority must be a valid TaskPriority" })
|
||||
priority?: TaskPriority;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "dueDate must be a valid ISO 8601 date string" })
|
||||
dueDate?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "assigneeId must be a valid UUID" })
|
||||
assigneeId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "projectId must be a valid UUID" })
|
||||
projectId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "parentId must be a valid UUID" })
|
||||
parentId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt({ message: "sortOrder must be an integer" })
|
||||
@Min(0, { message: "sortOrder must be at least 0" })
|
||||
sortOrder?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "metadata must be an object" })
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
3
apps/api/src/tasks/dto/index.ts
Normal file
3
apps/api/src/tasks/dto/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { CreateTaskDto } from "./create-task.dto";
|
||||
export { UpdateTaskDto } from "./update-task.dto";
|
||||
export { QueryTasksDto } from "./query-tasks.dto";
|
||||
60
apps/api/src/tasks/dto/query-tasks.dto.ts
Normal file
60
apps/api/src/tasks/dto/query-tasks.dto.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { TaskStatus, TaskPriority } from "@prisma/client";
|
||||
import {
|
||||
IsUUID,
|
||||
IsEnum,
|
||||
IsOptional,
|
||||
IsInt,
|
||||
Min,
|
||||
Max,
|
||||
IsDateString,
|
||||
} from "class-validator";
|
||||
import { Type } from "class-transformer";
|
||||
|
||||
/**
|
||||
* DTO for querying tasks with filters and pagination
|
||||
*/
|
||||
export class QueryTasksDto {
|
||||
@IsUUID("4", { message: "workspaceId must be a valid UUID" })
|
||||
workspaceId!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(TaskStatus, { message: "status must be a valid TaskStatus" })
|
||||
status?: TaskStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(TaskPriority, { message: "priority must be a valid TaskPriority" })
|
||||
priority?: TaskPriority;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "assigneeId must be a valid UUID" })
|
||||
assigneeId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "projectId must be a valid UUID" })
|
||||
projectId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "parentId must be a valid UUID" })
|
||||
parentId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "dueDateFrom must be a valid ISO 8601 date string" })
|
||||
dueDateFrom?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "dueDateTo must be a valid ISO 8601 date string" })
|
||||
dueDateTo?: Date;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt({ message: "page must be an integer" })
|
||||
@Min(1, { message: "page must be at least 1" })
|
||||
page?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt({ message: "limit must be an integer" })
|
||||
@Min(1, { message: "limit must be at least 1" })
|
||||
@Max(100, { message: "limit must not exceed 100" })
|
||||
limit?: number;
|
||||
}
|
||||
63
apps/api/src/tasks/dto/update-task.dto.ts
Normal file
63
apps/api/src/tasks/dto/update-task.dto.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { TaskStatus, TaskPriority } from "@prisma/client";
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsUUID,
|
||||
IsEnum,
|
||||
IsDateString,
|
||||
IsInt,
|
||||
IsObject,
|
||||
MinLength,
|
||||
MaxLength,
|
||||
Min,
|
||||
} from "class-validator";
|
||||
|
||||
/**
|
||||
* DTO for updating an existing task
|
||||
* All fields are optional to support partial updates
|
||||
*/
|
||||
export class UpdateTaskDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: "title must be a string" })
|
||||
@MinLength(1, { message: "title must not be empty" })
|
||||
@MaxLength(255, { message: "title must not exceed 255 characters" })
|
||||
title?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString({ message: "description must be a string" })
|
||||
@MaxLength(10000, { message: "description must not exceed 10000 characters" })
|
||||
description?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(TaskStatus, { message: "status must be a valid TaskStatus" })
|
||||
status?: TaskStatus;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(TaskPriority, { message: "priority must be a valid TaskPriority" })
|
||||
priority?: TaskPriority;
|
||||
|
||||
@IsOptional()
|
||||
@IsDateString({}, { message: "dueDate must be a valid ISO 8601 date string" })
|
||||
dueDate?: Date | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "assigneeId must be a valid UUID" })
|
||||
assigneeId?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "projectId must be a valid UUID" })
|
||||
projectId?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID("4", { message: "parentId must be a valid UUID" })
|
||||
parentId?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt({ message: "sortOrder must be an integer" })
|
||||
@Min(0, { message: "sortOrder must be at least 0" })
|
||||
sortOrder?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject({ message: "metadata must be an object" })
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
229
apps/api/src/tasks/tasks.controller.spec.ts
Normal file
229
apps/api/src/tasks/tasks.controller.spec.ts
Normal file
@@ -0,0 +1,229 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { TasksController } from "./tasks.controller";
|
||||
import { TasksService } from "./tasks.service";
|
||||
import { TaskStatus, TaskPriority } from "@prisma/client";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
import { ExecutionContext } from "@nestjs/common";
|
||||
|
||||
describe("TasksController", () => {
|
||||
let controller: TasksController;
|
||||
let service: TasksService;
|
||||
|
||||
const mockTasksService = {
|
||||
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 mockTaskId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
const mockRequest = {
|
||||
user: {
|
||||
id: mockUserId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
};
|
||||
|
||||
const mockTask = {
|
||||
id: mockTaskId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
title: "Test Task",
|
||||
description: "Test Description",
|
||||
status: TaskStatus.NOT_STARTED,
|
||||
priority: TaskPriority.MEDIUM,
|
||||
dueDate: new Date("2026-02-01T12:00:00Z"),
|
||||
assigneeId: null,
|
||||
creatorId: mockUserId,
|
||||
projectId: null,
|
||||
parentId: null,
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
completedAt: null,
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [TasksController],
|
||||
providers: [
|
||||
{
|
||||
provide: TasksService,
|
||||
useValue: mockTasksService,
|
||||
},
|
||||
],
|
||||
})
|
||||
.overrideGuard(AuthGuard)
|
||||
.useValue(mockAuthGuard)
|
||||
.compile();
|
||||
|
||||
controller = module.get<TasksController>(TasksController);
|
||||
service = module.get<TasksService>(TasksService);
|
||||
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a task", async () => {
|
||||
const createDto = {
|
||||
title: "New Task",
|
||||
description: "Task description",
|
||||
};
|
||||
|
||||
mockTasksService.create.mockResolvedValue(mockTask);
|
||||
|
||||
const result = await controller.create(createDto, mockRequest);
|
||||
|
||||
expect(result).toEqual(mockTask);
|
||||
expect(service.create).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
createDto
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated tasks", async () => {
|
||||
const query = {
|
||||
workspaceId: mockWorkspaceId,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
};
|
||||
|
||||
const paginatedResult = {
|
||||
data: [mockTask],
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
},
|
||||
};
|
||||
|
||||
mockTasksService.findAll.mockResolvedValue(paginatedResult);
|
||||
|
||||
const result = await controller.findAll(query, mockRequest);
|
||||
|
||||
expect(result).toEqual(paginatedResult);
|
||||
expect(service.findAll).toHaveBeenCalledWith({
|
||||
...query,
|
||||
workspaceId: mockWorkspaceId,
|
||||
});
|
||||
});
|
||||
|
||||
it("should extract workspaceId from request.user if not in query", async () => {
|
||||
const query = {};
|
||||
|
||||
mockTasksService.findAll.mockResolvedValue({
|
||||
data: [],
|
||||
meta: { total: 0, page: 1, limit: 50, totalPages: 0 },
|
||||
});
|
||||
|
||||
await controller.findAll(query as any, mockRequest);
|
||||
|
||||
expect(service.findAll).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
workspaceId: mockWorkspaceId,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return a task by id", async () => {
|
||||
mockTasksService.findOne.mockResolvedValue(mockTask);
|
||||
|
||||
const result = await controller.findOne(mockTaskId, mockRequest);
|
||||
|
||||
expect(result).toEqual(mockTask);
|
||||
expect(service.findOne).toHaveBeenCalledWith(mockTaskId, mockWorkspaceId);
|
||||
});
|
||||
|
||||
it("should throw error if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.findOne(mockTaskId, requestWithoutWorkspace)
|
||||
).rejects.toThrow("User workspaceId not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a task", async () => {
|
||||
const updateDto = {
|
||||
title: "Updated Task",
|
||||
status: TaskStatus.IN_PROGRESS,
|
||||
};
|
||||
|
||||
const updatedTask = { ...mockTask, ...updateDto };
|
||||
mockTasksService.update.mockResolvedValue(updatedTask);
|
||||
|
||||
const result = await controller.update(mockTaskId, updateDto, mockRequest);
|
||||
|
||||
expect(result).toEqual(updatedTask);
|
||||
expect(service.update).toHaveBeenCalledWith(
|
||||
mockTaskId,
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
updateDto
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.update(mockTaskId, { title: "Test" }, requestWithoutWorkspace)
|
||||
).rejects.toThrow("User workspaceId not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a task", async () => {
|
||||
mockTasksService.remove.mockResolvedValue(undefined);
|
||||
|
||||
await controller.remove(mockTaskId, mockRequest);
|
||||
|
||||
expect(service.remove).toHaveBeenCalledWith(
|
||||
mockTaskId,
|
||||
mockWorkspaceId,
|
||||
mockUserId
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.remove(mockTaskId, requestWithoutWorkspace)
|
||||
).rejects.toThrow("User workspaceId not found");
|
||||
});
|
||||
});
|
||||
});
|
||||
100
apps/api/src/tasks/tasks.controller.ts
Normal file
100
apps/api/src/tasks/tasks.controller.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
Request,
|
||||
} from "@nestjs/common";
|
||||
import { TasksService } from "./tasks.service";
|
||||
import { CreateTaskDto, UpdateTaskDto, QueryTasksDto } from "./dto";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
|
||||
/**
|
||||
* Controller for task endpoints
|
||||
* All endpoints require authentication
|
||||
*/
|
||||
@Controller("tasks")
|
||||
@UseGuards(AuthGuard)
|
||||
export class TasksController {
|
||||
constructor(private readonly tasksService: TasksService) {}
|
||||
|
||||
/**
|
||||
* POST /api/tasks
|
||||
* Create a new task
|
||||
*/
|
||||
@Post()
|
||||
async create(@Body() createTaskDto: CreateTaskDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId or userId not found");
|
||||
}
|
||||
|
||||
return this.tasksService.create(workspaceId, userId, createTaskDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/tasks
|
||||
* Get paginated tasks with optional filters
|
||||
*/
|
||||
@Get()
|
||||
async findAll(@Query() query: QueryTasksDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId || query.workspaceId;
|
||||
return this.tasksService.findAll({ ...query, workspaceId });
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/tasks/:id
|
||||
* Get a single task by ID
|
||||
*/
|
||||
@Get(":id")
|
||||
async findOne(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
if (!workspaceId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
return this.tasksService.findOne(id, workspaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/tasks/:id
|
||||
* Update a task
|
||||
*/
|
||||
@Patch(":id")
|
||||
async update(
|
||||
@Param("id") id: string,
|
||||
@Body() updateTaskDto: UpdateTaskDto,
|
||||
@Request() req: any
|
||||
) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
|
||||
return this.tasksService.update(id, workspaceId, userId, updateTaskDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/tasks/:id
|
||||
* Delete a task
|
||||
*/
|
||||
@Delete(":id")
|
||||
async remove(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
}
|
||||
|
||||
return this.tasksService.remove(id, workspaceId, userId);
|
||||
}
|
||||
}
|
||||
14
apps/api/src/tasks/tasks.module.ts
Normal file
14
apps/api/src/tasks/tasks.module.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TasksController } from "./tasks.controller";
|
||||
import { TasksService } from "./tasks.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
import { ActivityModule } from "../activity/activity.module";
|
||||
import { AuthModule } from "../auth/auth.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, ActivityModule, AuthModule],
|
||||
controllers: [TasksController],
|
||||
providers: [TasksService],
|
||||
exports: [TasksService],
|
||||
})
|
||||
export class TasksModule {}
|
||||
446
apps/api/src/tasks/tasks.service.spec.ts
Normal file
446
apps/api/src/tasks/tasks.service.spec.ts
Normal file
@@ -0,0 +1,446 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { TasksService } from "./tasks.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { TaskStatus, TaskPriority } from "@prisma/client";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("TasksService", () => {
|
||||
let service: TasksService;
|
||||
let prisma: PrismaService;
|
||||
let activityService: ActivityService;
|
||||
|
||||
const mockPrismaService = {
|
||||
task: {
|
||||
create: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const mockActivityService = {
|
||||
logTaskCreated: vi.fn(),
|
||||
logTaskUpdated: vi.fn(),
|
||||
logTaskDeleted: vi.fn(),
|
||||
logTaskCompleted: vi.fn(),
|
||||
logTaskAssigned: vi.fn(),
|
||||
};
|
||||
|
||||
const mockWorkspaceId = "550e8400-e29b-41d4-a716-446655440001";
|
||||
const mockUserId = "550e8400-e29b-41d4-a716-446655440002";
|
||||
const mockTaskId = "550e8400-e29b-41d4-a716-446655440003";
|
||||
|
||||
const mockTask = {
|
||||
id: mockTaskId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
title: "Test Task",
|
||||
description: "Test Description",
|
||||
status: TaskStatus.NOT_STARTED,
|
||||
priority: TaskPriority.MEDIUM,
|
||||
dueDate: new Date("2026-02-01T12:00:00Z"),
|
||||
assigneeId: null,
|
||||
creatorId: mockUserId,
|
||||
projectId: null,
|
||||
parentId: null,
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
completedAt: null,
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
TasksService,
|
||||
{
|
||||
provide: PrismaService,
|
||||
useValue: mockPrismaService,
|
||||
},
|
||||
{
|
||||
provide: ActivityService,
|
||||
useValue: mockActivityService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<TasksService>(TasksService);
|
||||
prisma = module.get<PrismaService>(PrismaService);
|
||||
activityService = module.get<ActivityService>(ActivityService);
|
||||
|
||||
// Clear all mocks before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a task and log activity", async () => {
|
||||
const createDto = {
|
||||
title: "New Task",
|
||||
description: "Task description",
|
||||
priority: TaskPriority.HIGH,
|
||||
};
|
||||
|
||||
mockPrismaService.task.create.mockResolvedValue(mockTask);
|
||||
mockActivityService.logTaskCreated.mockResolvedValue({});
|
||||
|
||||
const result = await service.create(mockWorkspaceId, mockUserId, createDto);
|
||||
|
||||
expect(result).toEqual(mockTask);
|
||||
expect(prisma.task.create).toHaveBeenCalledWith({
|
||||
data: {
|
||||
...createDto,
|
||||
workspaceId: mockWorkspaceId,
|
||||
creatorId: mockUserId,
|
||||
status: TaskStatus.NOT_STARTED,
|
||||
priority: TaskPriority.HIGH,
|
||||
sortOrder: 0,
|
||||
metadata: {},
|
||||
},
|
||||
include: {
|
||||
assignee: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(activityService.logTaskCreated).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockTask.id,
|
||||
{ title: mockTask.title }
|
||||
);
|
||||
});
|
||||
|
||||
it("should set completedAt when status is COMPLETED", async () => {
|
||||
const createDto = {
|
||||
title: "Completed Task",
|
||||
status: TaskStatus.COMPLETED,
|
||||
};
|
||||
|
||||
mockPrismaService.task.create.mockResolvedValue({
|
||||
...mockTask,
|
||||
status: TaskStatus.COMPLETED,
|
||||
completedAt: new Date(),
|
||||
});
|
||||
|
||||
await service.create(mockWorkspaceId, mockUserId, createDto);
|
||||
|
||||
expect(prisma.task.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
completedAt: expect.any(Date),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
it("should return paginated tasks with default pagination", async () => {
|
||||
const tasks = [mockTask];
|
||||
mockPrismaService.task.findMany.mockResolvedValue(tasks);
|
||||
mockPrismaService.task.count.mockResolvedValue(1);
|
||||
|
||||
const result = await service.findAll({ workspaceId: mockWorkspaceId });
|
||||
|
||||
expect(result).toEqual({
|
||||
data: tasks,
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
limit: 50,
|
||||
totalPages: 1,
|
||||
},
|
||||
});
|
||||
expect(prisma.task.findMany).toHaveBeenCalledWith({
|
||||
where: { workspaceId: mockWorkspaceId },
|
||||
include: expect.any(Object),
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip: 0,
|
||||
take: 50,
|
||||
});
|
||||
});
|
||||
|
||||
it("should filter by status", async () => {
|
||||
mockPrismaService.task.findMany.mockResolvedValue([mockTask]);
|
||||
mockPrismaService.task.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
status: TaskStatus.IN_PROGRESS,
|
||||
});
|
||||
|
||||
expect(prisma.task.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
status: TaskStatus.IN_PROGRESS,
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should filter by priority", async () => {
|
||||
mockPrismaService.task.findMany.mockResolvedValue([mockTask]);
|
||||
mockPrismaService.task.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
priority: TaskPriority.HIGH,
|
||||
});
|
||||
|
||||
expect(prisma.task.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
priority: TaskPriority.HIGH,
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should filter by assigneeId", async () => {
|
||||
mockPrismaService.task.findMany.mockResolvedValue([mockTask]);
|
||||
mockPrismaService.task.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
assigneeId: mockUserId,
|
||||
});
|
||||
|
||||
expect(prisma.task.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
assigneeId: mockUserId,
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should filter by date range", async () => {
|
||||
const dueDateFrom = new Date("2026-02-01");
|
||||
const dueDateTo = new Date("2026-02-28");
|
||||
|
||||
mockPrismaService.task.findMany.mockResolvedValue([mockTask]);
|
||||
mockPrismaService.task.count.mockResolvedValue(1);
|
||||
|
||||
await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
dueDateFrom,
|
||||
dueDateTo,
|
||||
});
|
||||
|
||||
expect(prisma.task.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: {
|
||||
workspaceId: mockWorkspaceId,
|
||||
dueDate: {
|
||||
gte: dueDateFrom,
|
||||
lte: dueDateTo,
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle pagination correctly", async () => {
|
||||
mockPrismaService.task.findMany.mockResolvedValue([mockTask]);
|
||||
mockPrismaService.task.count.mockResolvedValue(100);
|
||||
|
||||
const result = await service.findAll({
|
||||
workspaceId: mockWorkspaceId,
|
||||
page: 2,
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
expect(result.meta).toEqual({
|
||||
total: 100,
|
||||
page: 2,
|
||||
limit: 10,
|
||||
totalPages: 10,
|
||||
});
|
||||
expect(prisma.task.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
skip: 10,
|
||||
take: 10,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
it("should return a task by id", async () => {
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(mockTask);
|
||||
|
||||
const result = await service.findOne(mockTaskId, mockWorkspaceId);
|
||||
|
||||
expect(result).toEqual(mockTask);
|
||||
expect(prisma.task.findUnique).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: mockTaskId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
include: expect.any(Object),
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if task not found", async () => {
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.findOne(mockTaskId, mockWorkspaceId)).rejects.toThrow(
|
||||
NotFoundException
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update a task and log activity", async () => {
|
||||
const updateDto = {
|
||||
title: "Updated Task",
|
||||
status: TaskStatus.IN_PROGRESS,
|
||||
};
|
||||
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(mockTask);
|
||||
mockPrismaService.task.update.mockResolvedValue({
|
||||
...mockTask,
|
||||
...updateDto,
|
||||
});
|
||||
mockActivityService.logTaskUpdated.mockResolvedValue({});
|
||||
|
||||
const result = await service.update(
|
||||
mockTaskId,
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
updateDto
|
||||
);
|
||||
|
||||
expect(result.title).toBe("Updated Task");
|
||||
expect(activityService.logTaskUpdated).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockTaskId,
|
||||
{ changes: updateDto }
|
||||
);
|
||||
});
|
||||
|
||||
it("should set completedAt when status changes to COMPLETED", async () => {
|
||||
const updateDto = { status: TaskStatus.COMPLETED };
|
||||
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(mockTask);
|
||||
mockPrismaService.task.update.mockResolvedValue({
|
||||
...mockTask,
|
||||
status: TaskStatus.COMPLETED,
|
||||
completedAt: new Date(),
|
||||
});
|
||||
|
||||
await service.update(mockTaskId, mockWorkspaceId, mockUserId, updateDto);
|
||||
|
||||
expect(prisma.task.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
completedAt: expect.any(Date),
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(activityService.logTaskCompleted).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should clear completedAt when status changes from COMPLETED", async () => {
|
||||
const completedTask = {
|
||||
...mockTask,
|
||||
status: TaskStatus.COMPLETED,
|
||||
completedAt: new Date(),
|
||||
};
|
||||
const updateDto = { status: TaskStatus.IN_PROGRESS };
|
||||
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(completedTask);
|
||||
mockPrismaService.task.update.mockResolvedValue({
|
||||
...completedTask,
|
||||
status: TaskStatus.IN_PROGRESS,
|
||||
completedAt: null,
|
||||
});
|
||||
|
||||
await service.update(mockTaskId, mockWorkspaceId, mockUserId, updateDto);
|
||||
|
||||
expect(prisma.task.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
completedAt: null,
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should log assignment when assigneeId changes", async () => {
|
||||
const updateDto = { assigneeId: "550e8400-e29b-41d4-a716-446655440099" };
|
||||
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(mockTask);
|
||||
mockPrismaService.task.update.mockResolvedValue({
|
||||
...mockTask,
|
||||
assigneeId: updateDto.assigneeId,
|
||||
});
|
||||
|
||||
await service.update(mockTaskId, mockWorkspaceId, mockUserId, updateDto);
|
||||
|
||||
expect(activityService.logTaskAssigned).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockTaskId,
|
||||
updateDto.assigneeId
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if task not found", async () => {
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.update(mockTaskId, mockWorkspaceId, mockUserId, { title: "Test" })
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("should delete a task and log activity", async () => {
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(mockTask);
|
||||
mockPrismaService.task.delete.mockResolvedValue(mockTask);
|
||||
mockActivityService.logTaskDeleted.mockResolvedValue({});
|
||||
|
||||
await service.remove(mockTaskId, mockWorkspaceId, mockUserId);
|
||||
|
||||
expect(prisma.task.delete).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: mockTaskId,
|
||||
workspaceId: mockWorkspaceId,
|
||||
},
|
||||
});
|
||||
expect(activityService.logTaskDeleted).toHaveBeenCalledWith(
|
||||
mockWorkspaceId,
|
||||
mockUserId,
|
||||
mockTaskId,
|
||||
{ title: mockTask.title }
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if task not found", async () => {
|
||||
mockPrismaService.task.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.remove(mockTaskId, mockWorkspaceId, mockUserId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
});
|
||||
});
|
||||
281
apps/api/src/tasks/tasks.service.ts
Normal file
281
apps/api/src/tasks/tasks.service.ts
Normal file
@@ -0,0 +1,281 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { TaskStatus } from "@prisma/client";
|
||||
import type { CreateTaskDto, UpdateTaskDto, QueryTasksDto } from "./dto";
|
||||
|
||||
/**
|
||||
* Service for managing tasks
|
||||
*/
|
||||
@Injectable()
|
||||
export class TasksService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly activityService: ActivityService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create a new task
|
||||
*/
|
||||
async create(workspaceId: string, userId: string, createTaskDto: CreateTaskDto) {
|
||||
const data: any = {
|
||||
...createTaskDto,
|
||||
workspaceId,
|
||||
creatorId: userId,
|
||||
status: createTaskDto.status || TaskStatus.NOT_STARTED,
|
||||
priority: createTaskDto.priority || createTaskDto.priority,
|
||||
sortOrder: createTaskDto.sortOrder ?? 0,
|
||||
metadata: createTaskDto.metadata || {},
|
||||
};
|
||||
|
||||
// Set completedAt if status is COMPLETED
|
||||
if (data.status === TaskStatus.COMPLETED) {
|
||||
data.completedAt = new Date();
|
||||
}
|
||||
|
||||
const task = await this.prisma.task.create({
|
||||
data,
|
||||
include: {
|
||||
assignee: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Log activity
|
||||
await this.activityService.logTaskCreated(workspaceId, userId, task.id, {
|
||||
title: task.title,
|
||||
});
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated tasks with filters
|
||||
*/
|
||||
async findAll(query: QueryTasksDto) {
|
||||
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.priority) {
|
||||
where.priority = query.priority;
|
||||
}
|
||||
|
||||
if (query.assigneeId) {
|
||||
where.assigneeId = query.assigneeId;
|
||||
}
|
||||
|
||||
if (query.projectId) {
|
||||
where.projectId = query.projectId;
|
||||
}
|
||||
|
||||
if (query.parentId) {
|
||||
where.parentId = query.parentId;
|
||||
}
|
||||
|
||||
if (query.dueDateFrom || query.dueDateTo) {
|
||||
where.dueDate = {};
|
||||
if (query.dueDateFrom) {
|
||||
where.dueDate.gte = query.dueDateFrom;
|
||||
}
|
||||
if (query.dueDateTo) {
|
||||
where.dueDate.lte = query.dueDateTo;
|
||||
}
|
||||
}
|
||||
|
||||
// Execute queries in parallel
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.task.findMany({
|
||||
where,
|
||||
include: {
|
||||
assignee: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
}),
|
||||
this.prisma.task.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single task by ID
|
||||
*/
|
||||
async findOne(id: string, workspaceId: string) {
|
||||
const task = await this.prisma.task.findUnique({
|
||||
where: {
|
||||
id,
|
||||
workspaceId,
|
||||
},
|
||||
include: {
|
||||
assignee: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
subtasks: {
|
||||
include: {
|
||||
assignee: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!task) {
|
||||
throw new NotFoundException(`Task with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a task
|
||||
*/
|
||||
async update(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
updateTaskDto: UpdateTaskDto
|
||||
) {
|
||||
// Verify task exists
|
||||
const existingTask = await this.prisma.task.findUnique({
|
||||
where: { id, workspaceId },
|
||||
});
|
||||
|
||||
if (!existingTask) {
|
||||
throw new NotFoundException(`Task with ID ${id} not found`);
|
||||
}
|
||||
|
||||
const data: any = { ...updateTaskDto };
|
||||
|
||||
// Handle completedAt based on status changes
|
||||
if (updateTaskDto.status) {
|
||||
if (
|
||||
updateTaskDto.status === TaskStatus.COMPLETED &&
|
||||
existingTask.status !== TaskStatus.COMPLETED
|
||||
) {
|
||||
data.completedAt = new Date();
|
||||
} else if (
|
||||
updateTaskDto.status !== TaskStatus.COMPLETED &&
|
||||
existingTask.status === TaskStatus.COMPLETED
|
||||
) {
|
||||
data.completedAt = null;
|
||||
}
|
||||
}
|
||||
|
||||
const task = await this.prisma.task.update({
|
||||
where: {
|
||||
id,
|
||||
workspaceId,
|
||||
},
|
||||
data,
|
||||
include: {
|
||||
assignee: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
creator: {
|
||||
select: { id: true, name: true, email: true },
|
||||
},
|
||||
project: {
|
||||
select: { id: true, name: true, color: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Log activities
|
||||
await this.activityService.logTaskUpdated(workspaceId, userId, id, {
|
||||
changes: updateTaskDto,
|
||||
});
|
||||
|
||||
// Log completion if status changed to COMPLETED
|
||||
if (
|
||||
updateTaskDto.status === TaskStatus.COMPLETED &&
|
||||
existingTask.status !== TaskStatus.COMPLETED
|
||||
) {
|
||||
await this.activityService.logTaskCompleted(workspaceId, userId, id);
|
||||
}
|
||||
|
||||
// Log assignment if assigneeId changed
|
||||
if (
|
||||
updateTaskDto.assigneeId !== undefined &&
|
||||
updateTaskDto.assigneeId !== existingTask.assigneeId
|
||||
) {
|
||||
await this.activityService.logTaskAssigned(
|
||||
workspaceId,
|
||||
userId,
|
||||
id,
|
||||
updateTaskDto.assigneeId || ""
|
||||
);
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a task
|
||||
*/
|
||||
async remove(id: string, workspaceId: string, userId: string) {
|
||||
// Verify task exists
|
||||
const task = await this.prisma.task.findUnique({
|
||||
where: { id, workspaceId },
|
||||
});
|
||||
|
||||
if (!task) {
|
||||
throw new NotFoundException(`Task with ID ${id} not found`);
|
||||
}
|
||||
|
||||
await this.prisma.task.delete({
|
||||
where: {
|
||||
id,
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
|
||||
// Log activity
|
||||
await this.activityService.logTaskDeleted(workspaceId, userId, id, {
|
||||
title: task.title,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user