Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/delete task #45

Open
wants to merge 31 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1ccf235
feat: add delete task
lauanegcsilva Jan 30, 2024
e2585ef
fix: change httcode and add messagem
lauanegcsilva Jan 30, 2024
1cb5633
feat: add delete task endpoint
lauanegcsilva Jan 30, 2024
91b0c56
refactor: delete task
Kauanedev Mar 13, 2024
071c298
Merge branch 'develop' of github.com:loryblu/loryblu-api into feat/de…
Kauanedev Mar 14, 2024
63f6e4d
chore: set-up rest
Kauanedev Mar 19, 2024
d4a7aeb
chore: update rest
Kauanedev Mar 20, 2024
bdca173
docs: update prisma
Kauanedev Mar 20, 2024
ca8ceae
feat: delete task
Kauanedev Mar 20, 2024
1d31317
ci: node version
viniciuscosmome Mar 21, 2024
9f110bd
fix: fix modules version
viniciuscosmome Mar 21, 2024
6ab8d2e
Merge pull request #48 from loryblu/ci/github-actions
viniciuscosmome Mar 21, 2024
1ca16e8
chore: update yarn.lock
Kauanedev Mar 21, 2024
055f443
refactor: update deleteTask func
Kauanedev Apr 5, 2024
d5471f4
fix: update types of Delete Task
Daaaiii May 9, 2024
0a3d422
fix: remove unnecessary code
Daaaiii May 9, 2024
7870d59
feat: add children Id on get task payload
Daaaiii May 9, 2024
4a583d4
fix: update childrenId on Get tasks response
Daaaiii May 9, 2024
490d68a
fix: remove unnecessary code
Daaaiii May 9, 2024
7a7241e
fix: add await
Daaaiii May 9, 2024
ac9998f
feat: add check to verify if task belong to child
Daaaiii May 9, 2024
c712921
fix: change search method to findUnique
Daaaiii May 9, 2024
af63231
fix: update response message on delete task
Daaaiii May 9, 2024
35d3341
fix: add children id to query
Daaaiii Jun 5, 2024
32b4a9b
fix: add childrenId to DeleteTaskDto
Daaaiii Jun 5, 2024
55a2f7c
fix: update task entity types to use number instead of string for tas…
Daaaiii Jun 5, 2024
9e0d02d
fix: Update deleteTask method parameter name to taskId
Daaaiii Jun 5, 2024
bed88b6
fix: Update deleteTask method parameter name to taskId
Daaaiii Jun 5, 2024
6774b5a
fix: remove parent validation func
Kauanedev Jul 3, 2024
5373258
feat: add parentId at findTaskByIdAndChildren
Kauanedev Jul 3, 2024
98e27e9
feat: add exist parent validation
Kauanedev Jul 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [18.x]
node-version: [18]

steps:
- uses: actions/checkout@v2
Expand Down
86 changes: 86 additions & 0 deletions docs/rest/api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
### Variables
@apiurl = http://localhost:5500
# @apiurl = https://loryblu-homologation.onrender.com
@authToken = {{login.response.body.data.accessToken}}

### POST /auth/register
POST {{apiurl}}/auth/register
Content-Type: application/json

{
"email": "[email protected]",
"password": "stringW#3",
"policiesAccepted": true,
"parentName": "John Doe",
"childrenName": "John Jr Doe",
"childrenBirthDate": "2009-02-28",
"childrenGender": "male"
}

### POST /login/register
# @name login
POST {{apiurl}}/auth/login HTTP/1.1
Content-Type: application/json

{
"email": "[email protected]",
"password": "stringW#3"
}

### POST /task
POST {{apiurl}}/task
Authorization: Bearer {{authToken}}
Content-Type: application/json

{
"childrenId": 2,
"categoryId": "44f29121-b7b1-4d1a-bbff-5f1cf2fc5497",
"shift": "morning",
"frequency": [
"mon"
],
"order": 0
}

### GET /task?childrenId=2&frequency=mon
GET {{apiurl}}/task?childrenId=2&frequency=mon
Authorization: Bearer {{authToken}}

### Delete /task/id
DELETE {{apiurl}}/task/5
Authorization: Bearer {{authToken}}


### PATCH /task
PATCH {{apiurl}}/task HTTP/1.1
Authorization: Bearer {{authToken}}
Content-Type: application/json

{
"childrenId": 1,
"categoryId": "44f29121-b7b1-4d1a-bbff-5f1cf2fc5497",
"shift": "morning",
"frequency": [
"mon"
],
"order": 0
}


### POST /auth/recovery
POST {{apiurl}}/login/register HTTP/1.1
Content-Type: application/json

{
"email": "[email protected]"
}

### PUT /auth/recovery
PUT {{apiurl}}/auth/set-password HTTP/1.1
Content-Type: application/json

{
"password": "stringW#3",
"recoveryToken": "string"
}

2 changes: 2 additions & 0 deletions src/globals/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type EncryptDataAsyncProps = {
salt: number;
};

type DeleteHandler = (id: string, request: Request) => Promise<void>;
Daaaiii marked this conversation as resolved.
Show resolved Hide resolved

export type ApiResponses = {
ok: ApiResponseOptions;
created: ApiResponseOptions;
Expand Down
24 changes: 24 additions & 0 deletions src/modules/task/task.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
Body,
Controller,
Delete,
Get,
HttpCode,
Param,
Patch,
Post,
Query,
Expand All @@ -17,6 +19,7 @@ import {
readTaskNewDto,
UpdateTaskDto,
ValidateIdTask,
DeleteTask,
} from './task.dto';
import { AuthorizationGuard, RequestToken } from 'src/guard';
import { iAuthTokenPayload } from '../account/account.entity';
Expand Down Expand Up @@ -107,4 +110,25 @@ export class TaskController {
message: 'Tarefas atualizadas',
};
}

@RequestToken({ type: 'access', role: 'user' })
@Delete(':id')
@HttpCode(200)
@ApiResponse(responses.badRequest)
@ApiResponse(responses.unauthorized)
@ApiResponse(responses.forbidden)
@ApiResponse(responses.unprocessable)
@ApiResponse(responses.internalError)
async delete(@Param() { id }: DeleteTask, @Req() request: Request) {
const sessionInfo = request[sessionPayloadKey] as iAuthTokenPayload;

await this.service.deleteTask({
id: id,
parentId: sessionInfo.pid,
});

return {
message: 'Tarefa excluída',
};
}
}
8 changes: 8 additions & 0 deletions src/modules/task/task.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IsInt,
IsNotEmpty,
IsNumber,
IsNumberString,
IsOptional,
IsString,
IsUUID,
Expand Down Expand Up @@ -135,3 +136,10 @@ export class ValidateIdTask {
@IsInt({ message: messages.integer })
id_task: number;
}

export class DeleteTask {
@ApiProperty({ example: 1 })
@IsNotEmpty({ message: messages.notEmpty })
@IsNumberString({ no_symbols: true }, { message: messages.integer })
id: string;
}
38 changes: 36 additions & 2 deletions src/modules/task/task.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { handleErrors } from 'src/globals/errors';
import { PrismaService } from 'src/prisma/prisma.service';
import {
iTaskRepositoryInput,
iTaskRepositoryReadManyInput,
Expand All @@ -25,7 +25,7 @@ export class TaskRepository {
parentId: parentId,
},
frequency: {
hasSome: frequency,
hasSome: Array.isArray(frequency) ? frequency : [frequency],
Daaaiii marked this conversation as resolved.
Show resolved Hide resolved
},
},
select: {
Expand Down Expand Up @@ -101,4 +101,38 @@ export class TaskRepository {
})
.catch((error) => handleErrors(error));
}

async findTaskById(id: number) {
return this.prisma.task.findFirst({
where: {
id,
},
});
}

async validateParent(parentId: string) {
return this.prisma.parentProfile.findFirst({
where: {
id: parentId,
},
});
}

async deleteTask(id: number, parentId: string) {
await this.prisma.task.findFirst({
where: {
children: {
parentId: parentId,
},
},
});

await this.prisma.task
.delete({
where: {
id: id,
},
})
.catch((error) => handleErrors(error));
}
Daaaiii marked this conversation as resolved.
Show resolved Hide resolved
}
19 changes: 18 additions & 1 deletion src/modules/task/task.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { TaskRepository } from './task.repository';
import {
iTaskRepositoryInput,
Expand Down Expand Up @@ -42,6 +42,23 @@ export class TaskService {
await this.repository.updateTask(input);
}

async deleteTask({ id, parentId }: { id: string; parentId: string }) {
const idNumber = parseInt(id);
Daaaiii marked this conversation as resolved.
Show resolved Hide resolved

const existingTask = await this.repository.findTaskById(idNumber);
const parent = await this.repository.validateParent(parentId);

if (!existingTask) {
throw new NotFoundException(`Tarefa com ID ${id} não encontrada.`);
}
Daaaiii marked this conversation as resolved.
Show resolved Hide resolved

if (!parent) {
throw new NotFoundException('Id do responsável inválida');
}

await this.repository.deleteTask(idNumber, parentId);
}

private processTasks(
result: object,
currentItem: iTaskRepositoryReadManyOutput,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
viniciuscosmome marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8451,4 +8451,4 @@ [email protected]:
yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
Loading