Compare commits

10 Commits

16 changed files with 483 additions and 50 deletions
+29
View File
@@ -0,0 +1,29 @@
import { IPageFormat } from "../interfaces/IPagination";
export function buildPageFormat(
page: number,
limit: number,
totalItems: number,
baseUrl: string,
): IPageFormat {
const totalPages = Math.ceil(totalItems / limit);
const prevPage =
page > 1
? `${baseUrl}?page=${page - 1}&limit=${limit}`
: false;
const nextPage =
page < totalPages
? `${baseUrl}?page=${page + 1}&limit=${limit}`
: false;
return {
page,
limit,
totalItems,
totalPages,
prevPage,
nextPage,
};
}
@@ -0,0 +1,6 @@
import { IPageFormat } from "./IPagination";
export interface PaginatedResult<T> {
data: T[];
meta: IPageFormat;
}
@@ -0,0 +1,56 @@
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
import { AttachmentService } from '../providers/attachment.service';
import { CreateAttachmentDto } from '../dto/attachment/create-attachment.dto';
import { UpdateAttachmentDto } from '../dto/attachment/update-attachment.dto';
import { TMAttachment } from '../entities/attachment.entity';
@ApiTags('Task Manager - Attachments')
@Controller('task-manager/attachments')
export class AttachmentController {
constructor(private readonly attachmentService: AttachmentService) {}
@Get()
@ApiOperation({ summary: 'Get all attachments, optionally filtered by task' })
@ApiQuery({ name: 'taskId', required: false, description: 'Filter attachments by task ID' })
@ApiResponse({ status: 200, description: 'List of attachments', type: [TMAttachment] })
findAll(@Query('taskId') taskId?: string): Promise<TMAttachment[]> {
if (taskId) return this.attachmentService.findByTask(taskId);
return this.attachmentService.findAll();
}
@Get(':id')
@ApiOperation({ summary: 'Get an attachment by ID' })
@ApiParam({ name: 'id', description: 'Attachment ID' })
@ApiResponse({ status: 200, description: 'Attachment found', type: TMAttachment })
@ApiResponse({ status: 404, description: 'Attachment not found' })
findOne(@Param('id') id: string): Promise<TMAttachment> {
return this.attachmentService.findOne(id);
}
@Post()
@ApiOperation({ summary: 'Create a new attachment' })
@ApiResponse({ status: 201, description: 'Attachment created', type: TMAttachment })
@ApiResponse({ status: 404, description: 'Task not found' })
create(@Body() body: CreateAttachmentDto): Promise<TMAttachment> {
return this.attachmentService.create(body);
}
@Patch(':id')
@ApiOperation({ summary: 'Update an attachment' })
@ApiParam({ name: 'id', description: 'Attachment ID' })
@ApiResponse({ status: 200, description: 'Attachment updated', type: TMAttachment })
@ApiResponse({ status: 404, description: 'Attachment or Task not found' })
update(@Param('id') id: string, @Body() body: UpdateAttachmentDto): Promise<TMAttachment> {
return this.attachmentService.update(id, body);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete an attachment' })
@ApiParam({ name: 'id', description: 'Attachment ID' })
@ApiResponse({ status: 200, description: 'Attachment deleted' })
@ApiResponse({ status: 404, description: 'Attachment not found' })
remove(@Param('id') id: string): Promise<void> {
return this.attachmentService.remove(id);
}
}
@@ -1,8 +1,10 @@
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from '@nestjs/common';
import { Controller, Get, Post, Patch, Delete, Param, Body, Query, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
import { Request } from 'express';
import { ProjectService } from '../providers/project.service';
import { CreateProjectDto } from '../dto/project/create-project.dto';
import { UpdateProjectDto } from '../dto/project/update-project.dto';
import { PaginationDto } from '../../../common/DTO/pagination.dto';
import { TMProject } from '../entities/project.entity';
@ApiTags('Task Manager - Projects')
@@ -13,10 +15,15 @@ export class ProjectController {
@Get()
@ApiOperation({ summary: 'Get all projects, optionally filtered by workspace' })
@ApiQuery({ name: 'workspaceId', required: false, description: 'Filter projects by workspace ID' })
@ApiResponse({ status: 200, description: 'List of projects', type: [TMProject] })
findAll(@Query('workspaceId') workspaceId?: string): Promise<TMProject[]> {
if (workspaceId) return this.projectService.findByWorkspace(workspaceId);
return this.projectService.findAll();
@ApiResponse({ status: 200, description: 'Paginated list of projects' })
findAll(
@Query() pagination: PaginationDto,
@Query('workspaceId') workspaceId: string,
@Req() req: Request,
) {
const baseUrl = `${req.protocol}://${req.get('host')}${req.path}`;
if (workspaceId) return this.projectService.findByWorkspace(workspaceId, pagination, baseUrl);
return this.projectService.findAll(pagination, baseUrl);
}
@Get(':id')
@@ -50,7 +57,7 @@ export class ProjectController {
@Delete(':id')
@ApiOperation({ summary: 'Delete a project' })
@ApiParam({ name: 'id', description: 'Project ID' })
@ApiResponse({ status: 200, description: 'Project deleted' })
@ApiResponse({ status: 200, description: 'Project deleted', type: TMProject })
@ApiResponse({ status: 404, description: 'Project not found' })
remove(@Param('id') id: string): Promise<TMProject> {
return this.projectService.remove(id);
@@ -0,0 +1,56 @@
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from "@nestjs/common";
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from "@nestjs/swagger";
import { RemarkService } from "../providers/remark.service";
import { CreateRemarkDto } from "../dto/remark/create-remark.dto";
import { UpdateRemarkDto } from "../dto/remark/update-remark.dto";
import { TMRemark } from "../entities/remark.entity";
@ApiTags("Task Manager - Remarks")
@Controller("task-manager/remarks")
export class RemarkController {
constructor(private readonly remarkService: RemarkService) {}
@Get()
@ApiOperation({ summary: "Get all remarks, optionally filtered by task" })
@ApiQuery({ name: "taskId", required: false, description: "Filter remarks by task ID" })
@ApiResponse({ status: 200, description: "List of remarks", type: [TMRemark] })
findAll(@Query("taskId") taskId?: string): Promise<TMRemark[]> {
if (taskId) return this.remarkService.findByTask(taskId);
return this.remarkService.findAll();
}
@Get(":id")
@ApiOperation({ summary: "Get a remark by ID" })
@ApiParam({ name: "id", description: "Remark ID" })
@ApiResponse({ status: 200, description: "Remark found", type: TMRemark })
@ApiResponse({ status: 404, description: "Remark not found" })
findOne(@Param("id") id: string): Promise<TMRemark> {
return this.remarkService.findOne(id);
}
@Post()
@ApiOperation({ summary: "Create a new remark" })
@ApiResponse({ status: 201, description: "Remark created", type: TMRemark })
@ApiResponse({ status: 404, description: "Task not found" })
create(@Body() body: CreateRemarkDto): Promise<TMRemark> {
return this.remarkService.create(body);
}
@Patch(":id")
@ApiOperation({ summary: "Update a remark" })
@ApiParam({ name: "id", description: "Remark ID" })
@ApiResponse({ status: 200, description: "Remark updated", type: TMRemark })
@ApiResponse({ status: 404, description: "Remark or Task not found" })
update(@Param("id") id: string, @Body() body: UpdateRemarkDto): Promise<TMRemark> {
return this.remarkService.update(id, body);
}
@Delete(":id")
@ApiOperation({ summary: "Delete a remark" })
@ApiParam({ name: "id", description: "Remark ID" })
@ApiResponse({ status: 200, description: "Remark deleted" })
@ApiResponse({ status: 404, description: "Remark not found" })
remove(@Param("id") id: string): Promise<void> {
return this.remarkService.remove(id);
}
}
@@ -0,0 +1,26 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString, IsNotEmpty, IsUUID, IsEnum, MaxLength } from "class-validator";
import { TMAttachmentType } from "../../enums/attachment-type.enum";
export class CreateAttachmentDto {
@ApiProperty({ description: "Title of the attachment", example: "Project Requirements Doc" })
@IsString()
@IsNotEmpty()
@MaxLength(150)
title: string;
@ApiProperty({ description: "Type of the attachment", enum: TMAttachmentType, example: TMAttachmentType.FILE })
@IsEnum(TMAttachmentType)
@IsNotEmpty()
type: TMAttachmentType;
@ApiProperty({ description: "File URL or path of the attachment", example: "https://cdn.example.com/files/requirements.pdf" })
@IsString()
@IsNotEmpty()
file: string;
@ApiProperty({ description: "ID of the task this attachment belongs to", example: "a1b2c3d4-e5f6-7890-abcd-ef1234567890" })
@IsUUID()
@IsNotEmpty()
taskId: string;
}
@@ -0,0 +1,4 @@
import { PartialType } from "@nestjs/swagger";
import { CreateAttachmentDto } from "./create-attachment.dto";
export class UpdateAttachmentDto extends PartialType(CreateAttachmentDto) {}
@@ -0,0 +1,21 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString, IsNotEmpty, IsUUID, IsOptional, MaxLength } from "class-validator";
export class CreateRemarkDto {
@ApiProperty({ description: "Title of the remark", example: "This task needs review" })
@IsString()
@IsNotEmpty()
@MaxLength(150)
title: string;
@ApiProperty({ description: "Color code for the remark", required: false, example: "#F59E0B" })
@IsString()
@IsOptional()
@MaxLength(20)
color?: string;
@ApiProperty({ description: "ID of the task this remark belongs to", example: "a1b2c3d4-e5f6-7890-abcd-ef1234567890" })
@IsUUID()
@IsNotEmpty()
taskId: string;
}
@@ -0,0 +1,4 @@
import { PartialType } from "@nestjs/swagger";
import { CreateRemarkDto } from "./create-remark.dto";
export class UpdateRemarkDto extends PartialType(CreateRemarkDto) {}
@@ -0,0 +1,53 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { AttachmentRepository } from "../repositories/attachment.repository";
import { TaskRepository } from "../repositories/task.repository";
import { TMAttachment } from "../entities/attachment.entity";
import { CreateAttachmentDto } from "../dto/attachment/create-attachment.dto";
import { UpdateAttachmentDto } from "../dto/attachment/update-attachment.dto";
@Injectable()
export class AttachmentService {
constructor(
private readonly attachmentRepository: AttachmentRepository,
private readonly taskRepository: TaskRepository,
) {}
findAll(): Promise<TMAttachment[]> {
return this.attachmentRepository.findAll();
}
findByTask(taskId: string): Promise<TMAttachment[]> {
return this.attachmentRepository.findByTask(taskId);
}
async findOne(id: string): Promise<TMAttachment> {
const attachment = await this.attachmentRepository.findOneById(id);
if (!attachment) throw new NotFoundException(`Attachment #${id} not found`);
return attachment;
}
async create(dto: CreateAttachmentDto): Promise<TMAttachment> {
const task = await this.taskRepository.findOneById(dto.taskId);
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
const attachment = this.attachmentRepository.create(dto);
return this.attachmentRepository.save(attachment);
}
async update(id: string, dto: UpdateAttachmentDto): Promise<TMAttachment> {
const attachment = await this.findOne(id);
if (dto.taskId) {
const task = await this.taskRepository.findOneById(dto.taskId);
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
}
Object.assign(attachment, dto);
return this.attachmentRepository.save(attachment);
}
async remove(id: string): Promise<void> {
await this.findOne(id);
await this.attachmentRepository.delete(id);
}
}
@@ -1,12 +1,15 @@
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { In, Repository } from "typeorm";
import { ProjectRepository } from "../repositories/project.repository";
import { TMProject } from "../entities/project.entity";
import { CreateProjectDto } from "../dto/project/create-project.dto";
import { UpdateProjectDto } from "../dto/project/update-project.dto";
import { User } from "../../users/entities/user.entity";
import { WorkspaceRepository } from "../repositories/workspace.repository";
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { ProjectRepository } from '../repositories/project.repository';
import { WorkspaceRepository } from '../repositories/workspace.repository';
import { TMProject } from '../entities/project.entity';
import { CreateProjectDto } from '../dto/project/create-project.dto';
import { UpdateProjectDto } from '../dto/project/update-project.dto';
import { PaginationDto } from '../../../common/DTO/pagination.dto';
import { PaginatedResult } from '../../../common/interfaces/paginated-result.interface';
import { buildPageFormat } from '../../../common/helpers/pagination.helper';
import { User } from '../../users/entities/user.entity';
@Injectable()
export class ProjectService {
@@ -17,12 +20,34 @@ export class ProjectService {
private readonly userRepository: Repository<User>,
) {}
findAll(): Promise<TMProject[]> {
return this.projectRepository.findAll();
async findAll(pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
const page = pagination.page ?? 1;
const limit = pagination.limit ?? 10;
const skip = (page - 1) * limit;
const [data, totalItems] = await this.projectRepository.findAll(skip, limit);
return {
data,
meta: buildPageFormat(page, limit, totalItems, baseUrl),
};
}
findByWorkspace(workspaceId: string): Promise<TMProject[]> {
return this.projectRepository.findByWorkspace(workspaceId);
async findByWorkspace(
workspaceId: string,
pagination: PaginationDto,
baseUrl: string,
): Promise<PaginatedResult<TMProject>> {
const page = pagination.page ?? 1;
const limit = pagination.limit ?? 10;
const skip = (page - 1) * limit;
const [data, totalItems] = await this.projectRepository.findByWorkspace(workspaceId, skip, limit);
return {
data,
meta: buildPageFormat(page, limit, totalItems, baseUrl),
};
}
async findOne(id: string): Promise<TMProject> {
@@ -46,7 +71,9 @@ export class ProjectService {
const workspaceUserIds = workspace.users.map((u) => u.id);
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
if (invalidUsers.length) {
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
throw new BadRequestException(
`Users [${invalidUsers.join(', ')}] are not members of this workspace`,
);
}
project.users = await this.userRepository.findBy({ id: In(userIds) });
}
@@ -72,7 +99,9 @@ export class ProjectService {
const workspaceUserIds = workspace.users.map((u) => u.id);
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
if (invalidUsers.length) {
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
throw new BadRequestException(
`Users [${invalidUsers.join(', ')}] are not members of this workspace`,
);
}
project.users = await this.userRepository.findBy({ id: In(userIds) });
} else {
@@ -86,7 +115,6 @@ export class ProjectService {
async remove(id: string): Promise<TMProject> {
const project = await this.findOne(id);
await this.projectRepository.delete(id);
return project;
}
}
}
@@ -0,0 +1,53 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { RemarkRepository } from "../repositories/remark.repository";
import { TaskRepository } from "../repositories/task.repository";
import { TMRemark } from "../entities/remark.entity";
import { CreateRemarkDto } from "../dto/remark/create-remark.dto";
import { UpdateRemarkDto } from "../dto/remark/update-remark.dto";
@Injectable()
export class RemarkService {
constructor(
private readonly remarkRepository: RemarkRepository,
private readonly taskRepository: TaskRepository,
) {}
findAll(): Promise<TMRemark[]> {
return this.remarkRepository.findAll();
}
findByTask(taskId: string): Promise<TMRemark[]> {
return this.remarkRepository.findByTask(taskId);
}
async findOne(id: string): Promise<TMRemark> {
const remark = await this.remarkRepository.findOneById(id);
if (!remark) throw new NotFoundException(`Remark #${id} not found`);
return remark;
}
async create(dto: CreateRemarkDto): Promise<TMRemark> {
const task = await this.taskRepository.findOneById(dto.taskId);
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
const remark = this.remarkRepository.create(dto);
return this.remarkRepository.save(remark);
}
async update(id: string, dto: UpdateRemarkDto): Promise<TMRemark> {
const remark = await this.findOne(id);
if (dto.taskId) {
const task = await this.taskRepository.findOneById(dto.taskId);
if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`);
}
Object.assign(remark, dto);
return this.remarkRepository.save(remark);
}
async remove(id: string): Promise<void> {
await this.findOne(id);
await this.remarkRepository.delete(id);
}
}
@@ -0,0 +1,47 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TMAttachment } from '../entities/attachment.entity';
@Injectable()
export class AttachmentRepository {
constructor(
@InjectRepository(TMAttachment)
private readonly repository: Repository<TMAttachment>,
) {}
findAll() {
return this.repository.find({
relations: ['task'],
});
}
findByTask(taskId: string) {
return this.repository.find({
where: { taskId },
});
}
findOneById(id: string) {
return this.repository.findOne({
where: { id },
relations: ['task'],
});
}
create(data: Partial<TMAttachment>) {
return this.repository.create(data);
}
save(attachment: TMAttachment) {
return this.repository.save(attachment);
}
update(id: string, data: Partial<TMAttachment>) {
return this.repository.update(id, data);
}
delete(id: string) {
return this.repository.delete(id);
}
}
@@ -1,7 +1,7 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { TMProject } from "../entities/project.entity";
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TMProject } from '../entities/project.entity';
@Injectable()
export class ProjectRepository {
@@ -10,23 +10,27 @@ export class ProjectRepository {
private readonly repository: Repository<TMProject>,
) {}
findAll() {
return this.repository.find({
relations: ["workspace", "taskPhases", "users"],
findAll(skip: number, take: number) {
return this.repository.findAndCount({
relations: ['workspace', 'taskPhases', 'users'],
skip,
take,
});
}
findByWorkspace(workspaceId: string) {
return this.repository.find({
findByWorkspace(workspaceId: string, skip: number, take: number) {
return this.repository.findAndCount({
where: { workspaceId },
relations: ["taskPhases", "users"],
relations: ['taskPhases', 'users'],
skip,
take,
});
}
findOneById(id: string) {
return this.repository.findOne({
where: { id },
relations: ["workspace", "taskPhases", "users"],
relations: ['workspace', 'taskPhases', 'users'],
});
}
@@ -45,4 +49,4 @@ export class ProjectRepository {
delete(id: string) {
return this.repository.delete(id);
}
}
}
@@ -0,0 +1,47 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { TMRemark } from "../entities/remark.entity";
@Injectable()
export class RemarkRepository {
constructor(
@InjectRepository(TMRemark)
private readonly repository: Repository<TMRemark>,
) {}
findAll() {
return this.repository.find({
relations: ["task"],
});
}
findByTask(taskId: string) {
return this.repository.find({
where: { taskId },
});
}
findOneById(id: string) {
return this.repository.findOne({
where: { id },
relations: ["task"],
});
}
create(data: Partial<TMRemark>) {
return this.repository.create(data);
}
save(remark: TMRemark) {
return this.repository.save(remark);
}
update(id: string, data: Partial<TMRemark>) {
return this.repository.update(id, data);
}
delete(id: string) {
return this.repository.delete(id);
}
}
@@ -28,6 +28,9 @@ import { CheckListItemService } from "./providers/check-list-item.service";
import { TaskPhaseController } from "./controllers/task-phase.controller";
import { TaskController } from "./controllers/task.controller";
import { CheckListItemController } from "./controllers/check-list-item.controller";
import { RemarkController } from "./controllers/remark.controller";
import { RemarkRepository } from "./repositories/remark.repository";
import { RemarkService } from "./providers/remark.service";
@Module({
imports: [
@@ -40,6 +43,7 @@ import { CheckListItemController } from "./controllers/check-list-item.controlle
TaskPhaseController,
TaskController,
CheckListItemController,
RemarkController
],
providers: [
WorkspaceRepository,
@@ -54,20 +58,8 @@ import { CheckListItemController } from "./controllers/check-list-item.controlle
TaskService,
CheckListItemRepository,
CheckListItemService,
],
exports: [
WorkspaceRepository,
WorkspaceService,
WorkspaceTypeRepository,
WorkspaceTypeService,
ProjectRepository,
ProjectService,
TaskPhaseRepository,
TaskPhaseService,
TaskRepository,
TaskService,
CheckListItemRepository,
CheckListItemService,
],
RemarkRepository,
RemarkService
]
})
export class TaskManagerModule {}