diff --git a/prisma/migrations/20240505222048_/migration.sql b/prisma/migrations/20240505222048_/migration.sql new file mode 100644 index 00000000..960f68f5 --- /dev/null +++ b/prisma/migrations/20240505222048_/migration.sql @@ -0,0 +1,11 @@ +/* + Warnings: + + - A unique constraint covering the columns `[name]` on the table `category_supplies` will be added. If there are existing duplicate values, this will fail. + +*/ +-- AlterTable +ALTER TABLE "shelters" ALTER COLUMN "pix" DROP NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "category_supplies_name_key" ON "category_supplies"("name"); diff --git a/prisma/migrations/20240505222318_/migration.sql b/prisma/migrations/20240505222318_/migration.sql new file mode 100644 index 00000000..bf6fd7f4 --- /dev/null +++ b/prisma/migrations/20240505222318_/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "shelters" ALTER COLUMN "contact" DROP NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e070c1be..51523316 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -78,12 +78,12 @@ model Supply { model Shelter { id String @id @default(uuid()) name String @unique - pix String @unique + pix String? @unique address String petFriendly Boolean? @map("pet_friendly") shelteredPeople Int? @map("sheltered_people") capacity Int? - contact String + contact String? createdAt String @map("created_at") @db.VarChar(32) updatedAt String? @map("updated_at") @db.VarChar(32) diff --git a/src/shelter/types.ts b/src/shelter/types.ts index 06fb5293..7839da28 100644 --- a/src/shelter/types.ts +++ b/src/shelter/types.ts @@ -3,12 +3,12 @@ import z from 'zod'; const ShelterSchema = z.object({ id: z.string(), name: z.string(), - pix: z.string(), + pix: z.string().nullable().optional(), address: z.string(), petFriendly: z.boolean().nullable().optional(), shelteredPeople: z.number().nullable().optional(), capacity: z.number().nullable().optional(), - contact: z.string(), + contact: z.string().nullable().optional(), createdAt: z.string(), updatedAt: z.string().nullable().optional(), }); diff --git a/src/supply/supply.controller.ts b/src/supply/supply.controller.ts index 9d17ef5c..3e0327eb 100644 --- a/src/supply/supply.controller.ts +++ b/src/supply/supply.controller.ts @@ -1,6 +1,55 @@ -import { Controller } from '@nestjs/common'; +import { + Body, + Controller, + Get, + HttpException, + Logger, + Param, + Post, + Put, +} from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; +import { ServerResponse } from '../utils'; +import { SupplyService } from './supply.service'; + @ApiTags('Suprimentos') -@Controller('supply') -export class SupplyController {} +@Controller('supplies') +export class SupplyController { + private logger = new Logger(SupplyController.name); + + constructor(private readonly supplyServices: SupplyService) {} + + @Get(':shelterId') + async index(@Param('shelterId') shelterId: string) { + try { + const data = await this.supplyServices.index(shelterId); + return new ServerResponse(200, 'Successfully get supplies', data); + } catch (err: any) { + this.logger.error(`Failed to get supplies: ${err}`); + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); + } + } + + @Post('') + async store(@Body() body) { + try { + const data = await this.supplyServices.store(body); + return new ServerResponse(200, 'Successfully created supply', data); + } catch (err: any) { + this.logger.error(`Failed to create supply: ${err}`); + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); + } + } + + @Put(':id') + async update(@Param('id') id: string, @Body() body) { + try { + const data = await this.supplyServices.update(id, body); + return new ServerResponse(200, 'Successfully updated supply', data); + } catch (err: any) { + this.logger.error(`Failed to update supply: ${err}`); + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); + } + } +} diff --git a/src/supply/supply.service.ts b/src/supply/supply.service.ts index ae529223..a4d42cdc 100644 --- a/src/supply/supply.service.ts +++ b/src/supply/supply.service.ts @@ -1,8 +1,55 @@ +import z from 'zod'; import { Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; +import { CreateSupplySchema, UpdateSupplySchema } from './types'; @Injectable() export class SupplyService { constructor(private readonly prismaService: PrismaService) {} + + async store(body: z.infer) { + const payload = CreateSupplySchema.parse(body); + await this.prismaService.supply.create({ + data: { + ...payload, + createdAt: new Date().toISOString(), + }, + }); + } + + async update(id: string, body: z.infer) { + const payload = UpdateSupplySchema.parse(body); + await this.prismaService.supply.update({ + where: { + id, + }, + data: { + ...payload, + updatedAt: new Date().toISOString(), + }, + }); + } + + async index(id: string) { + const data = await this.prismaService.supply.findMany({ + where: { + shelterId: id, + }, + select: { + id: true, + name: true, + status: true, + supplyCategory: { + select: { + id: true, + name: true, + }, + }, + createdAt: true, + updatedAt: true, + }, + }); + return data; + } } diff --git a/src/supply/types.ts b/src/supply/types.ts index 88206c70..58e3daa2 100644 --- a/src/supply/types.ts +++ b/src/supply/types.ts @@ -28,4 +28,10 @@ const CreateSupplySchema = SupplySchema.omit({ updatedAt: true, }); -export { SupplySchema, CreateSupplySchema }; +const UpdateSupplySchema = SupplySchema.pick({ + name: true, + supplyCategoryId: true, + status: true, +}).partial(); + +export { SupplySchema, CreateSupplySchema, UpdateSupplySchema };