diff --git a/package-lock.json b/package-lock.json index ef101f2..0c9451b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@nestjs/common": "^10.3.3", "@nestjs/core": "^10.3.3", "@nestjs/jwt": "^10.2.0", + "@nestjs/passport": "^10.0.3", "@nestjs/platform-express": "^10.0.0", "@nestjs/sequelize": "^10.0.0", "@nestjs/swagger": "^7.3.0", @@ -2187,6 +2188,15 @@ } } }, + "node_modules/@nestjs/passport": { + "version": "10.0.3", + "resolved": "https://registry.npmjs.org/@nestjs/passport/-/passport-10.0.3.tgz", + "integrity": "sha512-znJ9Y4S8ZDVY+j4doWAJ8EuuVO7SkQN3yOBmzxbGaXbvcSwFDAdGJ+OMCg52NdzIO4tQoN4pYKx8W6M0ArfFRQ==", + "peerDependencies": { + "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0", + "passport": "^0.4.0 || ^0.5.0 || ^0.6.0 || ^0.7.0" + } + }, "node_modules/@nestjs/platform-express": { "version": "10.3.3", "resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-10.3.3.tgz", diff --git a/package.json b/package.json index cd75522..6baacf9 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@nestjs/common": "^10.3.3", "@nestjs/core": "^10.3.3", "@nestjs/jwt": "^10.2.0", + "@nestjs/passport": "^10.0.3", "@nestjs/platform-express": "^10.0.0", "@nestjs/sequelize": "^10.0.0", "@nestjs/swagger": "^7.3.0", diff --git a/src/i18n/en/index.ts b/src/i18n/en/index.ts index 9492db5..e1fce83 100644 --- a/src/i18n/en/index.ts +++ b/src/i18n/en/index.ts @@ -10,6 +10,7 @@ import ProjectIdea from './project-idea-messages.json'; import Updates from './updates-messages.json'; import Decisions from './decisions-messages.json'; import FollowProject from './follow-project-messages.json'; +import TaskList from './tasklist-messages.json'; export default { ...Exception, @@ -24,4 +25,5 @@ export default { ...Updates, ...Decisions, ...FollowProject, + ...TaskList, }; diff --git a/src/i18n/en/tasklist-messages.json b/src/i18n/en/tasklist-messages.json new file mode 100644 index 0000000..f9f7939 --- /dev/null +++ b/src/i18n/en/tasklist-messages.json @@ -0,0 +1,8 @@ +{ + "message.taskList.created": "Task has been created successfully", + "message.taskList.deleted": "Task has been deleted successfully", + "message.taskList.update": "Task has been updated successfully", + "message.taskList.get": "Task found successfully", + "message.taskList.userId": "All Task found successfully", + "message.taskList.priority": "All Task found by priority" +} \ No newline at end of file diff --git a/src/task-list-project/controller/task-list-project/task-list-project.controller.ts b/src/task-list-project/controller/task-list-project/task-list-project.controller.ts index afd3e5f..4e1e34f 100644 --- a/src/task-list-project/controller/task-list-project/task-list-project.controller.ts +++ b/src/task-list-project/controller/task-list-project/task-list-project.controller.ts @@ -1,4 +1,115 @@ -import { Controller } from '@nestjs/common'; +import { + Body, + Controller, + Delete, + Get, + Param, + Post, + Put, + Res, +} from '@nestjs/common'; +import { TaskListProjectService } from 'src/task-list-project/service/task-list-project/task-list-project.service'; +import { Response } from 'express'; +import { i18n } from 'src/i18n'; +import { + ApiBadRequestResponse, + ApiInternalServerErrorResponse, + ApiResponse, +} from '@nestjs/swagger'; -@Controller('task-list-project') -export class TaskListProjectController {} +@Controller('task') +export class TaskListProjectController { + constructor(private taskListService: TaskListProjectService) {} + + @ApiResponse({ + status: 201, + description: i18n()['message.taskList.get'], + }) + @ApiBadRequestResponse({ + status: 400, + description: 'Bad Request', + }) + @ApiInternalServerErrorResponse({ + status: 500, + description: 'Internal server error', + }) + @Get('/:id') + async findById(@Param('id') id: string, @Res() res: Response) { + const question = await this.taskListService.findTaskById(id); + + return res.status(200).json({ + message: i18n()['message.taskList.get'], + question, + }); + } + + @ApiResponse({ + status: 201, + description: i18n()['message.taskList.created'], + }) + @ApiBadRequestResponse({ + status: 400, + description: 'Bad Request', + }) + @ApiInternalServerErrorResponse({ + status: 500, + description: 'Internal server error', + }) + @Post('/') + async createTask(@Body() data: any, @Res() res: Response) { + const question = await this.taskListService.createTaskProject(data); + + return res.status(200).json({ + message: i18n()['message.taskList.created'], + question, + }); + } + + @ApiResponse({ + status: 201, + description: i18n()['message.taskList.update'], + }) + @ApiBadRequestResponse({ + status: 400, + description: 'Bad Request', + }) + @ApiInternalServerErrorResponse({ + status: 500, + description: 'Internal server error', + }) + @Put('/:id') + async updateTask( + @Param() id: string, + @Body() data: any, + @Res() res: Response, + ) { + const question = await this.taskListService.updateTask(id, data); + + return res.status(200).json({ + message: i18n()['message.taskList.update'], + question, + }); + } + + @ApiResponse({ + status: 201, + description: i18n()['message.taskList.deleted'], + }) + @ApiBadRequestResponse({ + status: 400, + description: 'Bad Request', + }) + @ApiInternalServerErrorResponse({ + status: 500, + description: 'Internal server error', + }) + @Delete('/:id') + async deleteTask(@Param('id') id: string, @Res() res: Response) { + const question = await this.taskListService.deleteTask(id); + + return res.status(200).json({ + message: i18n()['message.taskList.deleted'], + question, + }); + } +} diff --git a/src/task-list-project/dtos/index.ts b/src/task-list-project/dtos/index.ts new file mode 100644 index 0000000..1c861dc --- /dev/null +++ b/src/task-list-project/dtos/index.ts @@ -0,0 +1,24 @@ +import { Priority } from '@prisma/client'; +import { IsEnum, IsNotEmpty, IsString } from 'class-validator'; + +export class TaskListDTO { + @IsNotEmpty() + @IsString() + title: string; + + @IsString() + @IsNotEmpty() + description: string; + + @IsEnum(Priority) + @IsNotEmpty() + priority: string; + + @IsNotEmpty() + @IsString() + userId: string; + + @IsNotEmpty() + @IsString() + projectId: string; +} diff --git a/src/task-list-project/service/task-list-project/task-list-project.service.ts b/src/task-list-project/service/task-list-project/task-list-project.service.ts index cf8d573..dc0f0b4 100644 --- a/src/task-list-project/service/task-list-project/task-list-project.service.ts +++ b/src/task-list-project/service/task-list-project/task-list-project.service.ts @@ -1,4 +1,71 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; +import { PrismaService } from 'src/database/prisma.service'; +import { i18n } from 'src/i18n'; +import { LoggerService } from 'src/logger/logger.service'; +import { ProjectsService } from 'src/projects/services/projects/projects.service'; +import { TaskListProject } from 'src/task-list-project/types'; +import { UserService } from 'src/user/services/user/user.service'; @Injectable() -export class TaskListProjectService {} +export class TaskListProjectService { + constructor( + private prismaService: PrismaService, + private userService: UserService, + private projectService: ProjectsService, + private logger: LoggerService, + ) {} + + createTaskProject = async (data: TaskListProject) => { + this.logger.log('make all verifications before create tasklist...'); + await this.userService.checkUserExistence(data.userId); + await this.projectService.checkProjectExistence(data.projectId); + + const taskList = await this.prismaService.taskListProject.create({ + data: { + ...data, + }, + }); + return taskList; + }; + + findTaskById = async ( + task_list_id: string, + ): Promise => { + const taskList = await this.prismaService.taskListProject.findUnique({ + where: { + id: task_list_id, + }, + }); + + if (!taskList) { + throw new NotFoundException(i18n()['exception.notFound']); + } + return taskList; + }; + + deleteTask = async (task_list_id: string): Promise => { + await this.findTaskById(task_list_id); + await this.prismaService.taskListProject.delete({ + where: { + id: task_list_id, + }, + }); + }; + + updateTask = async ( + task_list_id: string, + data: TaskListProject, + ): Promise => { + this.logger.log('make all verifications before create tasklist...'); + await this.projectService.checkProjectExistence(data.projectId); + const taskList = await this.prismaService.taskListProject.update({ + where: { + id: task_list_id, + }, + data: { + ...data, + }, + }); + return taskList; + }; +} diff --git a/src/task-list-project/task-list-project.module.ts b/src/task-list-project/task-list-project.module.ts index cf75cca..0da1406 100644 --- a/src/task-list-project/task-list-project.module.ts +++ b/src/task-list-project/task-list-project.module.ts @@ -1,9 +1,17 @@ import { Module } from '@nestjs/common'; import { TaskListProjectController } from './controller/task-list-project/task-list-project.controller'; import { TaskListProjectService } from './service/task-list-project/task-list-project.service'; +import { UserService } from 'src/user/services/user/user.service'; +import { ProjectsService } from 'src/projects/services/projects/projects.service'; +import { HashService } from 'src/hash/service/hash/hash.service'; @Module({ controllers: [TaskListProjectController], - providers: [TaskListProjectService], + providers: [ + TaskListProjectService, + UserService, + ProjectsService, + HashService, + ], }) export class TaskListProjectModule {} diff --git a/src/task-list-project/types/index.ts b/src/task-list-project/types/index.ts new file mode 100644 index 0000000..9cc11ba --- /dev/null +++ b/src/task-list-project/types/index.ts @@ -0,0 +1,9 @@ +import { Priority } from '@prisma/client'; + +export type TaskListProject = { + title: string; + description: string; + priority: Priority; + userId: string; + projectId: string; +};