Skip to content

Commit c9c93e0

Browse files
committed
fix: supply services
1 parent db45700 commit c9c93e0

File tree

9 files changed

+89
-44
lines changed

9 files changed

+89
-44
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 `priority` on the `supplies` table. All the data in the column will be lost.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "shelter_supplies" ALTER COLUMN "priority" SET DEFAULT 0;
9+
10+
-- AlterTable
11+
ALTER TABLE "supplies" DROP COLUMN "priority";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- A unique constraint covering the columns `[name]` on the table `supplies` will be added. If there are existing duplicate values, this will fail.
5+
6+
*/
7+
-- CreateIndex
8+
CREATE UNIQUE INDEX "supplies_name_key" ON "supplies"("name");

prisma/schema.prisma

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ model SupplyCategory {
5757
model ShelterSupply {
5858
shelterId String @map("shelter_id")
5959
supplyId String @map("supply_id")
60-
priority Int
60+
priority Int @default(value: 0)
6161
createdAt String @map("created_at") @db.VarChar(32)
6262
updatedAt String? @map("updated_at") @db.VarChar(32)
6363
@@ -72,7 +72,6 @@ model Supply {
7272
id String @id @default(uuid())
7373
supplyCategoryId String @map("supply_category_id")
7474
name String
75-
priority Int @default(value: 0)
7675
createdAt String @map("created_at") @db.VarChar(32)
7776
updatedAt String? @map("updated_at") @db.VarChar(32)
7877

src/shelter-supply/shelter-supply.service.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,59 @@ import { CreateShelterSupplySchema, UpdateShelterSupplySchema } from './types';
88
export class ShelterSupplyService {
99
constructor(private readonly prismaService: PrismaService) {}
1010

11+
private async handleUpdateShelterSum(
12+
shelterId: string,
13+
oldPriority: number,
14+
newPriority: number,
15+
) {
16+
await this.prismaService.shelter.update({
17+
where: {
18+
id: shelterId,
19+
},
20+
data: {
21+
prioritySum: {
22+
increment: newPriority - oldPriority,
23+
},
24+
updatedAt: new Date().toISOString(),
25+
},
26+
});
27+
}
28+
1129
async store(body: z.infer<typeof CreateShelterSupplySchema>) {
12-
const payload = CreateShelterSupplySchema.parse(body);
30+
const { shelterId, priority, supplyId } =
31+
CreateShelterSupplySchema.parse(body);
32+
await this.handleUpdateShelterSum(shelterId, 0, priority);
1333
await this.prismaService.shelterSupply.create({
1434
data: {
15-
...payload,
35+
shelterId,
36+
priority,
37+
supplyId,
1638
createdAt: new Date().toISOString(),
1739
},
1840
});
1941
}
2042

2143
async update(body: z.infer<typeof UpdateShelterSupplySchema>) {
2244
const { data, where } = UpdateShelterSupplySchema.parse(body);
45+
const { priority } = data;
46+
if (priority !== null && priority !== undefined) {
47+
const shelterSupply = await this.prismaService.shelterSupply.findFirst({
48+
where: {
49+
shelterId: where.shelterId,
50+
supplyId: where.supplyId,
51+
},
52+
select: {
53+
priority: true,
54+
},
55+
});
56+
if (shelterSupply)
57+
await this.handleUpdateShelterSum(
58+
where.shelterId,
59+
shelterSupply.priority,
60+
priority,
61+
);
62+
}
63+
2364
await this.prismaService.shelterSupply.update({
2465
where: {
2566
shelterId_supplyId: where,
@@ -42,7 +83,6 @@ export class ShelterSupplyService {
4283
select: {
4384
id: true,
4485
name: true,
45-
priority: true,
4686
supplyCategory: {
4787
select: {
4888
id: true,

src/shelter-supply/types.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import z from 'zod';
22

3+
import { SupplyPriority } from '../supply/types';
4+
35
const ShelterSupplySchema = z.object({
46
id: z.string(),
57
shelterId: z.string(),
68
supplyId: z.string(),
7-
priority: z.number(),
9+
priority: z.union([
10+
z.literal(SupplyPriority.UnderControl),
11+
z.literal(SupplyPriority.Remaining),
12+
z.literal(SupplyPriority.Needing),
13+
z.literal(SupplyPriority.Urgent),
14+
]),
815
createdAt: z.string(),
916
updatedAt: z.string().nullable().optional(),
1017
});
@@ -18,6 +25,12 @@ const CreateShelterSupplySchema = ShelterSupplySchema.pick({
1825
const UpdateShelterSupplySchema = z.object({
1926
data: z
2027
.object({
28+
priority: z.union([
29+
z.literal(SupplyPriority.UnderControl),
30+
z.literal(SupplyPriority.Remaining),
31+
z.literal(SupplyPriority.Needing),
32+
z.literal(SupplyPriority.Urgent),
33+
]),
2134
shelterId: z.string(),
2235
supplyId: z.string(),
2336
})

src/shelter/shelter.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ServerResponse } from '../utils';
1616
import { SearchQuery } from '../decorators';
1717
import { SeachQueryProps } from '@/decorators/search-query/types';
1818
import { StaffGuard } from '@/guards/staff.guard';
19+
import { UserGuard } from '@/guards/user.guard';
1920

2021
@ApiTags('Abrigos')
2122
@Controller('shelters')
@@ -47,7 +48,7 @@ export class ShelterController {
4748
}
4849

4950
@Post('')
50-
@UseGuards(StaffGuard)
51+
@UseGuards(UserGuard)
5152
async store(@Body() body) {
5253
try {
5354
const data = await this.shelterService.store(body);

src/shelter/shelter.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ export class ShelterService {
7272
longitude: true,
7373
shelterSupplies: {
7474
select: {
75+
priority: true,
7576
supply: {
7677
select: {
7778
id: true,
7879
name: true,
79-
priority: true,
80+
8081
supplyCategory: {
8182
select: {
8283
id: true,
@@ -125,10 +126,10 @@ export class ShelterService {
125126
},
126127
take: 10,
127128
select: {
129+
priority: true,
128130
supply: {
129131
select: {
130132
name: true,
131-
priority: true,
132133
},
133134
},
134135
},

src/supply/supply.service.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,24 @@ import { CreateSupplySchema, UpdateSupplySchema } from './types';
88
export class SupplyService {
99
constructor(private readonly prismaService: PrismaService) {}
1010

11-
private async handleUpdateShelterSum(
12-
shelterId: string,
13-
oldPriority: number,
14-
newPriority: number,
15-
) {
16-
await this.prismaService.shelter.update({
17-
where: {
18-
id: shelterId,
19-
},
20-
data: {
21-
prioritySum: {
22-
increment: newPriority - oldPriority,
23-
},
24-
updatedAt: new Date().toISOString(),
25-
},
26-
});
27-
}
28-
2911
async store(body: z.infer<typeof CreateSupplySchema>) {
30-
const { priority, ...rest } = CreateSupplySchema.parse(body);
31-
await this.prismaService.supply.create({
12+
const payload = CreateSupplySchema.parse(body);
13+
return await this.prismaService.supply.create({
3214
data: {
33-
priority,
34-
...rest,
15+
...payload,
3516
createdAt: new Date().toISOString(),
3617
},
3718
});
3819
}
3920

4021
async update(id: string, body: z.infer<typeof UpdateSupplySchema>) {
41-
const { priority, ...rest } = UpdateSupplySchema.parse(body);
42-
22+
const payload = UpdateSupplySchema.parse(body);
4323
await this.prismaService.supply.update({
4424
where: {
4525
id,
4626
},
4727
data: {
48-
priority,
49-
...rest,
28+
...payload,
5029
updatedAt: new Date().toISOString(),
5130
},
5231
});
@@ -57,7 +36,6 @@ export class SupplyService {
5736
select: {
5837
id: true,
5938
name: true,
60-
priority: true,
6139
supplyCategory: {
6240
select: {
6341
id: true,

src/supply/types.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import z from 'zod';
2+
import { capitalize } from '../utils';
23

34
enum SupplyPriority {
45
UnderControl = 0,
@@ -10,13 +11,7 @@ enum SupplyPriority {
1011
const SupplySchema = z.object({
1112
id: z.string(),
1213
supplyCategoryId: z.string(),
13-
name: z.string(),
14-
priority: z.union([
15-
z.literal(SupplyPriority.UnderControl),
16-
z.literal(SupplyPriority.Remaining),
17-
z.literal(SupplyPriority.Needing),
18-
z.literal(SupplyPriority.Urgent),
19-
]),
14+
name: z.string().transform(capitalize),
2015
createdAt: z.string(),
2116
updatedAt: z.string().nullable().optional(),
2217
});
@@ -30,7 +25,6 @@ const CreateSupplySchema = SupplySchema.omit({
3025
const UpdateSupplySchema = SupplySchema.pick({
3126
name: true,
3227
supplyCategoryId: true,
33-
priority: true,
3428
}).partial();
3529

3630
export { SupplySchema, CreateSupplySchema, UpdateSupplySchema, SupplyPriority };

0 commit comments

Comments
 (0)