Skip to content

Commit db45700

Browse files
committed
refact: changed relationship of shelter supplies
1 parent 9ef3c8e commit db45700

15 files changed

+257
-166
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `shelter_id` on the `supplies` table. All the data in the column will be lost.
5+
6+
*/
7+
-- DropForeignKey
8+
ALTER TABLE "supplies" DROP CONSTRAINT "supplies_shelter_id_fkey";
9+
10+
-- AlterTable
11+
ALTER TABLE "supplies" DROP COLUMN "shelter_id";

prisma/schema.prisma

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ model ShelterSupply {
7070

7171
model Supply {
7272
id String @id @default(uuid())
73-
shelterId String @map("shelter_id")
7473
supplyCategoryId String @map("supply_category_id")
7574
name String
7675
priority Int @default(value: 0)
7776
createdAt String @map("created_at") @db.VarChar(32)
7877
updatedAt String? @map("updated_at") @db.VarChar(32)
7978
8079
supplyCategory SupplyCategory @relation(fields: [supplyCategoryId], references: [id])
81-
shelter Shelter @relation(fields: [shelterId], references: [id])
8280
shelterSupplies ShelterSupply[]
8381
8482
@@map("supplies")
@@ -99,7 +97,6 @@ model Shelter {
9997
createdAt String @map("created_at") @db.VarChar(32)
10098
updatedAt String? @map("updated_at") @db.VarChar(32)
10199
102-
supplies Supply[]
103100
shelterManagers ShelterManagers[]
104101
shelterSupplies ShelterSupply[]
105102

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { UsersModule } from './users/users.module';
1010
import { SessionsModule } from './sessions/sessions.module';
1111
import { SupplyCategoriesModule } from './supply-categories/supply-categories.module';
1212
import { ShelterManagersModule } from './shelter-managers/shelter-managers.module';
13+
import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
1314

1415
@Module({
1516
imports: [
@@ -20,6 +21,7 @@ import { ShelterManagersModule } from './shelter-managers/shelter-managers.modul
2021
SupplyModule,
2122
SupplyCategoriesModule,
2223
ShelterManagersModule,
24+
ShelterSupplyModule,
2325
],
2426
controllers: [],
2527
providers: [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { ShelterSupplyController } from './shelter-supply.controller';
3+
4+
describe('ShelterSupplyController', () => {
5+
let controller: ShelterSupplyController;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
controllers: [ShelterSupplyController],
10+
}).compile();
11+
12+
controller = module.get<ShelterSupplyController>(ShelterSupplyController);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(controller).toBeDefined();
17+
});
18+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {
2+
Body,
3+
Controller,
4+
Get,
5+
HttpException,
6+
Logger,
7+
Param,
8+
Post,
9+
Put,
10+
} from '@nestjs/common';
11+
import { ApiTags } from '@nestjs/swagger';
12+
13+
import { ShelterSupplyService } from './shelter-supply.service';
14+
import { ServerResponse } from '../utils';
15+
16+
@ApiTags('Suprimento de abrigos')
17+
@Controller('shelter/supplies')
18+
export class ShelterSupplyController {
19+
private logger = new Logger(ShelterSupplyController.name);
20+
21+
constructor(private readonly shelterSupplyService: ShelterSupplyService) {}
22+
23+
@Get(':shelterId')
24+
async index(@Param('shelterId') shelterId: string) {
25+
try {
26+
const data = await this.shelterSupplyService.index(shelterId);
27+
return new ServerResponse(200, 'Successfully get shelter supplies', data);
28+
} catch (err: any) {
29+
this.logger.error(`Failed to get shelter supplies: ${err}`);
30+
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
31+
}
32+
}
33+
34+
@Post('')
35+
async store(@Body() body) {
36+
try {
37+
const data = await this.shelterSupplyService.store(body);
38+
return new ServerResponse(
39+
200,
40+
'Successfully created shelter supply',
41+
data,
42+
);
43+
} catch (err: any) {
44+
this.logger.error(`Failed to create shelter supply: ${err}`);
45+
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
46+
}
47+
}
48+
49+
@Put(':shelterId/:supplyId')
50+
async update(
51+
@Body() body,
52+
@Param('shelterId') shelterId: string,
53+
@Param('supplyId') supplyId: string,
54+
) {
55+
try {
56+
const data = await this.shelterSupplyService.update({
57+
where: { shelterId, supplyId },
58+
data: body,
59+
});
60+
return new ServerResponse(
61+
200,
62+
'Successfully updated shelter supply',
63+
data,
64+
);
65+
} catch (err: any) {
66+
this.logger.error(`Failed to update shelter supply: ${err}`);
67+
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
68+
}
69+
}
70+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Module } from '@nestjs/common';
2+
3+
import { ShelterSupplyService } from './shelter-supply.service';
4+
import { ShelterSupplyController } from './shelter-supply.controller';
5+
import { PrismaModule } from '../prisma/prisma.module';
6+
7+
@Module({
8+
imports: [PrismaModule],
9+
providers: [ShelterSupplyService],
10+
controllers: [ShelterSupplyController],
11+
})
12+
export class ShelterSupplyModule {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { ShelterSupplyService } from './shelter-supply.service';
3+
4+
describe('ShelterSupplyService', () => {
5+
let service: ShelterSupplyService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [ShelterSupplyService],
10+
}).compile();
11+
12+
service = module.get<ShelterSupplyService>(ShelterSupplyService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { z } from 'zod';
2+
import { Injectable } from '@nestjs/common';
3+
4+
import { PrismaService } from '../prisma/prisma.service';
5+
import { CreateShelterSupplySchema, UpdateShelterSupplySchema } from './types';
6+
7+
@Injectable()
8+
export class ShelterSupplyService {
9+
constructor(private readonly prismaService: PrismaService) {}
10+
11+
async store(body: z.infer<typeof CreateShelterSupplySchema>) {
12+
const payload = CreateShelterSupplySchema.parse(body);
13+
await this.prismaService.shelterSupply.create({
14+
data: {
15+
...payload,
16+
createdAt: new Date().toISOString(),
17+
},
18+
});
19+
}
20+
21+
async update(body: z.infer<typeof UpdateShelterSupplySchema>) {
22+
const { data, where } = UpdateShelterSupplySchema.parse(body);
23+
await this.prismaService.shelterSupply.update({
24+
where: {
25+
shelterId_supplyId: where,
26+
},
27+
data: {
28+
...data,
29+
createdAt: new Date().toISOString(),
30+
},
31+
});
32+
}
33+
34+
async index(shelterId: string) {
35+
return await this.prismaService.shelterSupply.findMany({
36+
where: {
37+
shelterId,
38+
},
39+
select: {
40+
priority: true,
41+
supply: {
42+
select: {
43+
id: true,
44+
name: true,
45+
priority: true,
46+
supplyCategory: {
47+
select: {
48+
id: true,
49+
name: true,
50+
},
51+
},
52+
updatedAt: true,
53+
createdAt: true,
54+
},
55+
},
56+
createdAt: true,
57+
updatedAt: true,
58+
},
59+
});
60+
}
61+
}

src/shelter-supply/types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import z from 'zod';
2+
3+
const ShelterSupplySchema = z.object({
4+
id: z.string(),
5+
shelterId: z.string(),
6+
supplyId: z.string(),
7+
priority: z.number(),
8+
createdAt: z.string(),
9+
updatedAt: z.string().nullable().optional(),
10+
});
11+
12+
const CreateShelterSupplySchema = ShelterSupplySchema.pick({
13+
shelterId: true,
14+
supplyId: true,
15+
priority: true,
16+
});
17+
18+
const UpdateShelterSupplySchema = z.object({
19+
data: z
20+
.object({
21+
shelterId: z.string(),
22+
supplyId: z.string(),
23+
})
24+
.partial(),
25+
where: z.object({
26+
shelterId: z.string(),
27+
supplyId: z.string(),
28+
}),
29+
});
30+
31+
export {
32+
ShelterSupplySchema,
33+
CreateShelterSupplySchema,
34+
UpdateShelterSupplySchema,
35+
};

src/shelter/default.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)