Skip to content

Commit

Permalink
Merge pull request #32 from viniciuscosmome/feature/create-tasks
Browse files Browse the repository at this point in the history
[LOR 144] feature: create tasks
  • Loading branch information
Daaaiii committed Nov 1, 2023
2 parents a090e36 + c35ceb0 commit a1581ae
Show file tree
Hide file tree
Showing 21 changed files with 468 additions and 25 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"prisma": {
"seed": "ts-node prisma/seed/index.ts"
},
"name": "loryblu-api",
"description": "API para o aplicativo LoryBlu. Um aplicativo pensado para ajudar pais e crianças diagnosticadas com TEA.",
"author": "LoryBlu"
Expand Down
42 changes: 42 additions & 0 deletions prisma/migrations/20231030230934_format_lowercases/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Warnings:
- The values [MALE,FEMALE] on the enum `Genders` will be removed. If these variants are still used in the database, this will fail.
- The values [ADMIN,USER] on the enum `Roles` will be removed. If these variants are still used in the database, this will fail.
- The values [ACTIVE,DISABLED,BLOCKED] on the enum `Status` will be removed. If these variants are still used in the database, this will fail.
*/
-- AlterEnum
BEGIN;
CREATE TYPE "Genders_new" AS ENUM ('male', 'female');
ALTER TABLE "children_profiles" ALTER COLUMN "gender" TYPE "Genders_new" USING ("gender"::text::"Genders_new");
ALTER TYPE "Genders" RENAME TO "Genders_old";
ALTER TYPE "Genders_new" RENAME TO "Genders";
DROP TYPE "Genders_old";
COMMIT;

-- AlterEnum
BEGIN;
CREATE TYPE "Roles_new" AS ENUM ('admin', 'user');
ALTER TABLE "credentials" ALTER COLUMN "role" DROP DEFAULT;
ALTER TABLE "credentials" ALTER COLUMN "role" TYPE "Roles_new" USING ("role"::text::"Roles_new");
ALTER TYPE "Roles" RENAME TO "Roles_old";
ALTER TYPE "Roles_new" RENAME TO "Roles";
DROP TYPE "Roles_old";
ALTER TABLE "credentials" ALTER COLUMN "role" SET DEFAULT 'user';
COMMIT;

-- AlterEnum
BEGIN;
CREATE TYPE "Status_new" AS ENUM ('active', 'disabled', 'blocked');
ALTER TABLE "credentials" ALTER COLUMN "status" DROP DEFAULT;
ALTER TABLE "credentials" ALTER COLUMN "status" TYPE "Status_new" USING ("status"::text::"Status_new");
ALTER TYPE "Status" RENAME TO "Status_old";
ALTER TYPE "Status_new" RENAME TO "Status";
DROP TYPE "Status_old";
ALTER TABLE "credentials" ALTER COLUMN "status" SET DEFAULT 'disabled';
COMMIT;

-- AlterTable
ALTER TABLE "credentials" ALTER COLUMN "status" SET DEFAULT 'disabled',
ALTER COLUMN "role" SET DEFAULT 'user';
11 changes: 11 additions & 0 deletions prisma/migrations/20231030232151_task_categories/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- CreateEnum
CREATE TYPE "TaskGroup" AS ENUM ('study', 'routine');

-- CreateTable
CREATE TABLE "task_categories" (
"id" TEXT NOT NULL,
"group" "TaskGroup" NOT NULL,
"category" TEXT NOT NULL,

CONSTRAINT "task_categories_pkey" PRIMARY KEY ("id")
);
25 changes: 25 additions & 0 deletions prisma/migrations/20231031024812_task_model/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- CreateEnum
CREATE TYPE "TaskFrequency" AS ENUM ('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');

-- CreateEnum
CREATE TYPE "TaskShift" AS ENUM ('morning', 'afternoon', 'night');

-- CreateTable
CREATE TABLE "tasks" (
"id" SERIAL NOT NULL,
"shift" "TaskShift" NOT NULL,
"frequency" "TaskFrequency" NOT NULL,
"order" INTEGER NOT NULL,
"category_id" TEXT NOT NULL,
"children_id" INTEGER NOT NULL,
"updated_at" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "tasks_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "task_categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_children_id_fkey" FOREIGN KEY ("children_id") REFERENCES "children_profiles"("id") ON DELETE CASCADE ON UPDATE CASCADE;
64 changes: 55 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ model Credential {
email String @unique
password String
policiesAcceptedAt DateTime @map("policies_accepted_at")
status Status @default(DISABLED)
role Roles @default(USER)
status Status @default(disabled)
role Roles @default(user)
parentProfile ParentProfile?
resetPasswordInfo ResetPasswordInfo?
updatedAt DateTime @updatedAt @map("updated_at")
Expand Down Expand Up @@ -52,6 +52,7 @@ model ChildrenProfile {
fullname String
birthdate DateTime
gender Genders
tasks Task[]
parentId String @map("parent_id")
parent ParentProfile @relation(fields: [parentId], references: [id], onDelete: Cascade)
updatedAt DateTime @updatedAt @map("updated_at")
Expand All @@ -60,18 +61,63 @@ model ChildrenProfile {
@@map("children_profiles")
}

model TaskCategory {
id String @id @default(uuid())
group TaskGroup
category String
tasks Task[]
@@map("task_categories")
}

model Task {
id Int @id @default(autoincrement())
shift TaskShift
frequency TaskFrequency
order Int
categoryId String @map("category_id")
category TaskCategory @relation(fields: [categoryId], references: [id], onDelete: Cascade)
childrenId Int @map("children_id")
children ChildrenProfile @relation(fields: [childrenId], references: [id], onDelete: Cascade)
updatedAt DateTime @updatedAt @map("updated_at")
createdAt DateTime @default(now()) @map("created_at")
@@map("tasks")
}

enum TaskFrequency {
sun
mon
tue
wed
thu
fri
sat
}

enum TaskShift {
morning
afternoon
night
}

enum TaskGroup {
study
routine
}

enum Roles {
ADMIN
USER
admin
user
}

enum Status {
ACTIVE
DISABLED
BLOCKED
active
disabled
blocked
}

enum Genders {
MALE
FEMALE
male
female
}
123 changes: 123 additions & 0 deletions prisma/seed/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { PrismaClient, TaskCategory } from '@prisma/client';

const taskCategories: Array<Partial<TaskCategory>> = [
{
id: '44f29121-b7b1-4d1a-bbff-5f1cf2fc5497',
group: 'study',
category: 'school',
},
{
id: 'e9f17f88-7c94-4953-a1eb-bce035c483d8',
group: 'study',
category: 'reinforcement',
},
{
id: '8c004aa0-961d-4ec4-b6fe-cb70cbc0c4c1',
group: 'study',
category: 'languages',
},
{
id: '648d5ce9-6880-476e-bf98-0d7139aadb2c',
group: 'study',
category: 'sport',
},
{
id: '549a70f6-3f9c-4640-9b1e-b23d69f1d213',
group: 'study',
category: 'reading',
},
{
id: '6166d506-b5f0-4feb-b900-e78d23f1e6ac',
group: 'study',
category: 'musicTherapy',
},
{
id: '49f9aed1-e583-427c-82bf-a596513a6707',
group: 'study',
category: 'psychologist',
},
{
id: 'aa61d985-93a9-4ee0-b465-2e73848c84a8',
group: 'study',
category: 'speechTherapist',
},
{
id: '1b2f2a80-9b3e-455d-aab4-66ba68e6dac0',
group: 'study',
category: 'pedagogue',
},
{
id: '7da68252-143d-4e98-b5b0-17c7ea9953a3',
group: 'study',
category: 'occupationalTherapist',
},
{
id: '6dfc15bb-f422-4c75-b2cc-bf3e9806c76a',
group: 'routine',
category: 'bathTime',
},
{
id: '61123e8a-22af-421f-8e0d-6356c920803b',
group: 'routine',
category: 'brushTeeth',
},
{
id: '15d2ceb0-ab06-4cf6-8e46-54ccf8c1e556',
group: 'routine',
category: 'breakfast',
},
{
id: '5431e6bf-c1d3-43b6-9289-843ca6455ee3',
group: 'routine',
category: 'lunch',
},
{
id: '27e4089a-8643-487d-9634-2332419c2adc',
group: 'routine',
category: 'afternoonLunch',
},
{
id: 'a6284261-134d-4c96-ad1c-4afd50ae493d',
group: 'routine',
category: 'dinner',
},
{
id: '161bf998-4bf3-4c9f-bd72-1542bad40ed9',
group: 'routine',
category: 'drinkWater',
},
{
id: '86008ded-8ba1-4719-a55e-ef6d8d4cb8b1',
group: 'routine',
category: 'playTime',
},
{
id: 'bc920d36-d024-481b-9923-3d2d6a478a81',
group: 'routine',
category: 'tvReleased',
},
{
id: '63631065-7485-42d1-a9c1-523787954f56',
group: 'routine',
category: 'videoGameReleased',
},
];

export default async (prisma: PrismaClient) => {
for await (const { id, group, category } of taskCategories) {
await prisma.taskCategory.upsert({
where: {
id,
},
update: {
group,
category,
},
create: {
id,
group,
category,
},
});
}
};
22 changes: 22 additions & 0 deletions prisma/seed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { PrismaClient } from '@prisma/client';
import seedCategories from './categories';

const prisma = new PrismaClient();

async function main() {
await seedCategories(prisma);
}

main()
.then(() => {
console.info('# -------------------- #');
console.info('# --- SEED SUCCESS --- #');
console.info('# -------------------- #');
})
.catch(async (error) => {
console.error(error);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module } from '@nestjs/common';
import configModule from 'src/globals/constants';

import { AccountModule } from './modules';
import { AccountModule, TaskModule } from './modules';

@Module({
imports: [configModule, AccountModule],
imports: [configModule, TaskModule, AccountModule],
})
export class AppModule {}
4 changes: 3 additions & 1 deletion src/globals/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ export type Messages = {
notEmpty: Validator;
boolean: Validator;
string: Validator;
number: Validator;
integer: Validator;
enum: Validator;
minLength: Validator;
email: Validator;
strongPassword: Validator;
fullnamePattern: Validator;
birthDatePattern: Validator;
genderPattern: Validator;
recoveryTokenPattern: Validator;
};
14 changes: 11 additions & 3 deletions src/globals/errors.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { Prisma } from '@prisma/client';
import { constants } from './constants';
import { UnknownErrorException, P2002Exception } from './responses/exceptions';
import {
UnknownErrorException,
P2002Exception,
P2025Exception,
} from './responses/exceptions';

import { UnauthorizedException } from '@nestjs/common';
import { formatException } from './utils';

export function prismaKnownRequestErrors(
error: Prisma.PrismaClientKnownRequestError,
) {
const target = (error.meta?.target as Array<string>) || ['unknow_meta'];
let target: string;

switch (error.code) {
case 'P2002':
throw new P2002Exception(target[0]);
[target] = (error.meta?.target as Array<string>) || ['Unknow meta'];
throw new P2002Exception(target);
case 'P2025':
target = (error.meta.cause as string).split("'")[1];
throw new P2025Exception(target);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/globals/responses/exceptions/database.exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ export class P2002Exception extends UnprocessableEntityException {
super(formatException(`O ${target} informado já está em uso.`, target));
}
}

export class P2025Exception extends UnprocessableEntityException {
constructor(target: string) {
super(formatException(`O campo ${target} não pode ser acessado.`, target));
}
}
Loading

0 comments on commit a1581ae

Please sign in to comment.