Skip to content

Commit

Permalink
feat: supplies
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundesjg committed May 5, 2024
1 parent d95d816 commit 574d0c9
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 8 deletions.
11 changes: 11 additions & 0 deletions prisma/migrations/20240505222048_/migration.sql
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 2 additions & 0 deletions prisma/migrations/20240505222318_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "shelters" ALTER COLUMN "contact" DROP NOT NULL;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/shelter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand Down
55 changes: 52 additions & 3 deletions src/supply/supply.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
47 changes: 47 additions & 0 deletions src/supply/supply.service.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CreateSupplySchema>) {
const payload = CreateSupplySchema.parse(body);
await this.prismaService.supply.create({
data: {
...payload,
createdAt: new Date().toISOString(),
},
});
}

async update(id: string, body: z.infer<typeof UpdateSupplySchema>) {
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;
}
}
8 changes: 7 additions & 1 deletion src/supply/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 574d0c9

Please sign in to comment.