Skip to content

Commit

Permalink
Merge pull request #63 from viniciuscosmome/feat/update-tasks
Browse files Browse the repository at this point in the history
Feat/update tasks
  • Loading branch information
viniciuscosmome authored Jan 26, 2024
2 parents 32407b8 + e69f1df commit 7cb2f54
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 21 deletions.
14 changes: 14 additions & 0 deletions prisma/migrations/20240125220420_update_task_tags/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Warnings:
- The values [excercise] on the enum `TaskTags` will be removed. If these variants are still used in the database, this will fail.
*/
-- AlterEnum
BEGIN;
CREATE TYPE "TaskTags_new" AS ENUM ('application', 'account', 'exercise', 'beauty', 'literature');
ALTER TABLE "tasks" ALTER COLUMN "tag" TYPE "TaskTags_new" USING ("tag"::text::"TaskTags_new");
ALTER TYPE "TaskTags" RENAME TO "TaskTags_old";
ALTER TYPE "TaskTags_new" RENAME TO "TaskTags";
DROP TYPE "TaskTags_old";
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "tasks" ADD COLUMN "checked" BOOLEAN NOT NULL DEFAULT false;
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ model Task {
priority TaskPriorities
tag TaskTags
category TaskCategories
checked Boolean @default(false)
accountId String @map("account_id")
account Account @relation(fields: [accountId], references: [id])
createdAt DateTime @default(now()) @map("created_at")
Expand Down Expand Up @@ -120,7 +121,7 @@ enum TaskPriorities {
enum TaskTags {
application
account
excercise
exercise
beauty
literature
}
Expand Down
37 changes: 29 additions & 8 deletions src/modules/Task/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import {
Delete,
HttpCode,
Get,
ParseIntPipe,
Query,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { TaskService } from './task.service';
import { CreateTaskInput, UpdateTaskInput } from './task.dtos';
import {
CreateTaskInput,
FindATaskControllerDto,
FindTasksControllerDto,
UpdateTaskInput,
} from './task.dtos';
import { CREDENTIALS_KEY } from 'src/utils/constants';
import { RequirePermissions, Permissions, RolesGuard } from 'src/guards';

Expand Down Expand Up @@ -44,7 +48,7 @@ export class TaskController {
@UseGuards(RolesGuard)
@RequirePermissions([Permissions['302']])
async updateById(
@Param('id') id: string,
@Param('id') id: number,
@Body() updateTaskInput: UpdateTaskInput,
@Req() req: Request
) {
Expand All @@ -69,7 +73,7 @@ export class TaskController {
@UseGuards(RolesGuard)
@RequirePermissions([Permissions['303']])
@HttpCode(200)
async deleteById(@Param('id') id: string, @Req() req: Request) {
async deleteById(@Param('id') id: number, @Req() req: Request) {
const cred = req[CREDENTIALS_KEY];

const accountId = await this.taskService.getAccountById(id);
Expand All @@ -88,15 +92,32 @@ export class TaskController {
@UseGuards(RolesGuard)
@RequirePermissions([Permissions['301']])
async accountTasks(
@Query('month', ParseIntPipe) month: number,
@Query('year', ParseIntPipe) year: number,
@Query() input: FindTasksControllerDto,
@Req() request: Request
) {
const { accountId } = request[CREDENTIALS_KEY];

return await this.taskService.findAccountTasks({
month,
year,
month: input.month,
year: input.year,
accountId,
});
}

@ApiTags('Tasks')
@ApiBearerAuth()
@Get('/:taskId')
@HttpCode(200)
@UseGuards(RolesGuard)
@RequirePermissions([Permissions['301']])
async getATaskInfo(
@Param() input: FindATaskControllerDto,
@Req() request: Request
) {
const { accountId } = request[CREDENTIALS_KEY];

return await this.taskService.findTaskByid({
taskId: input.id,
accountId,
});
}
Expand Down
37 changes: 36 additions & 1 deletion src/modules/Task/task.dtos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { IsDateString, IsEnum, IsNotEmpty, IsString } from 'class-validator';
import {
IsDateString,
IsEnum,
IsNotEmpty,
IsNumber,
IsString,
Max,
Min,
} from 'class-validator';
import { TaskPriorities, TaskTags, TaskCategories } from '@prisma/client';
import { Transform } from 'class-transformer';

export class CreateTaskInput {
@ApiProperty()
Expand Down Expand Up @@ -66,3 +75,29 @@ export class FindTasksServiceInput {
year: number;
accountId: string;
}

export class FindTasksControllerDto {
@ApiProperty()
@IsNotEmpty()
@Transform((params) => Number(params.value))
@IsNumber()
@Min(1)
@Max(12)
month: number;

@ApiProperty()
@IsNotEmpty()
@Transform((params) => Number(params.value))
@IsNumber()
@Min(2023)
year: number;
}

export class FindATaskControllerDto {
@ApiProperty()
@IsNotEmpty()
@Transform((params) => Number(params.value))
@IsNumber()
@Min(1)
id: number;
}
51 changes: 43 additions & 8 deletions src/modules/Task/task.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,36 @@ export class TaskRepository {
});
}

async updateById(id: string, updateTaskInput: UpdateTaskInput) {
async updateById(id: number, updateTaskInput: UpdateTaskInput) {
return await this.prisma.task.update({
where: { id: Number(id) },
where: {
id: id,
},
data: updateTaskInput,
});
}

async findById(id: string) {
return await this.prisma.task.findUnique({ where: { id: Number(id) } });
async findById(id: number) {
return await this.prisma.task.findUnique({
where: {
id: id,
},
});
}

async deleteById(id: string) {
return await this.prisma.task.delete({ where: { id: Number(id) } });
async deleteById(id: number) {
return await this.prisma.task.delete({
where: {
id: id,
},
});
}

async findAccountByTaskId(id: string) {
async findAccountByTaskId(id: number) {
const task = await this.prisma.task.findUnique({
where: { id: Number(id) },
where: {
id: id,
},
});

return task.accountId;
Expand All @@ -62,9 +74,32 @@ export class TaskRepository {
priority: true,
category: true,
description: true,
checked: true,
},
});

return tasks;
}

async findUserTaskById(taskId: number) {
const task = await this.prisma.task.findUnique({
where: {
id: taskId,
},
select: {
id: true,
name: true,
date: true,
hour: true,
tag: true,
priority: true,
category: true,
description: true,
checked: true,
accountId: true,
},
});

return task;
}
}
18 changes: 15 additions & 3 deletions src/modules/Task/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class TaskService {
return { ...createdTask, hour: responseHour, date: responseDate };
}

async updateById(id: string, updateTaskInput: UpdateTaskInput) {
async updateById(id: number, updateTaskInput: UpdateTaskInput) {
const taskExist = await this.repository.findById(id);
if (taskExist === null) throw new UnprocessableEntityError({});

Expand All @@ -65,11 +65,11 @@ export class TaskService {
return { ...updatedTask, hour: responseHour, date: responseDate };
}

async deleteById(id: string) {
async deleteById(id: number) {
return await this.repository.deleteById(id);
}

async getAccountById(id: string) {
async getAccountById(id: number) {
return await this.repository.findAccountByTaskId(id);
}

Expand All @@ -95,4 +95,16 @@ export class TaskService {
const tasks = await this.repository.findTasks(filters);
return tasks;
}

async findTaskByid(input: { taskId: number; accountId: string }) {
const task = await this.repository.findUserTaskById(input.taskId);

if (task && task.accountId === input.accountId) {
return task;
}

return {
message: 'Nada encontrado',
};
}
}

0 comments on commit 7cb2f54

Please sign in to comment.