Skip to content

Commit 0248869

Browse files
committed
feat: update many supplies
1 parent 2ccf427 commit 0248869

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

src/shelter-supply/shelter-supply.controller.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,22 @@ export class ShelterSupplyController {
6767
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
6868
}
6969
}
70+
71+
@Put(':shelterId/supplies/many')
72+
async updateMany(@Body() body, @Param('shelterId') shelterId: string) {
73+
try {
74+
const data = await this.shelterSupplyService.updateMany({
75+
shelterId,
76+
...body,
77+
});
78+
return new ServerResponse(
79+
200,
80+
'Successfully updated many shelter supplies',
81+
data,
82+
);
83+
} catch (err: any) {
84+
this.logger.error(`Failed to update many shelter supplies: ${err}`);
85+
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
86+
}
87+
}
7088
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { z } from 'zod';
22
import { Injectable } from '@nestjs/common';
33

44
import { PrismaService } from '../prisma/prisma.service';
5-
import { CreateShelterSupplySchema, UpdateShelterSupplySchema } from './types';
5+
import {
6+
CreateShelterSupplySchema,
7+
UpdateManyShelterSupplySchema,
8+
UpdateShelterSupplySchema,
9+
} from './types';
610
import { SupplyPriority } from '../supply/types';
711

812
@Injectable()
@@ -70,7 +74,24 @@ export class ShelterSupplyService {
7074
data: {
7175
...data,
7276
quantity: priority !== SupplyPriority.UnderControl ? quantity : null,
73-
createdAt: new Date().toISOString(),
77+
updatedAt: new Date().toISOString(),
78+
},
79+
});
80+
}
81+
82+
async updateMany(body: z.infer<typeof UpdateManyShelterSupplySchema>) {
83+
const { ids, shelterId } = UpdateManyShelterSupplySchema.parse(body);
84+
85+
await this.prismaService.shelterSupply.updateMany({
86+
where: {
87+
shelterId: shelterId,
88+
supplyId: {
89+
in: ids,
90+
},
91+
},
92+
data: {
93+
priority: SupplyPriority.UnderControl,
94+
updatedAt: new Date().toISOString(),
7495
},
7596
});
7697
}

src/shelter-supply/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ const UpdateShelterSupplySchema = z.object({
4444
}),
4545
});
4646

47+
const UpdateManyShelterSupplySchema = z.object({
48+
ids: z.array(z.string()),
49+
shelterId: z.string(),
50+
});
51+
4752
export {
4853
ShelterSupplySchema,
4954
CreateShelterSupplySchema,
5055
UpdateShelterSupplySchema,
56+
UpdateManyShelterSupplySchema,
5157
};

src/shelter/ShelterSearch.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ class ShelterSearch {
2727
this.formProps = { ...props };
2828
}
2929

30-
get priority(): Prisma.ShelterWhereInput[] {
30+
priority(supplyIds: string[] = []): Prisma.ShelterWhereInput {
3131
if (this.formProps.priority) {
32-
return [
33-
{
34-
shelterSupplies: {
35-
some: {
36-
priority: +this.formProps.priority,
37-
},
32+
return {
33+
shelterSupplies: {
34+
some: {
35+
priority: +this.formProps.priority,
36+
supplyId:
37+
supplyIds.length > 0
38+
? {
39+
in: supplyIds,
40+
}
41+
: undefined,
3842
},
3943
},
40-
];
41-
}
42-
return [];
44+
};
45+
} else return {};
4346
}
4447

4548
get shelterStatus(): Prisma.ShelterWhereInput[] {
@@ -66,11 +69,14 @@ class ShelterSearch {
6669
}
6770
}
6871

69-
get supplyCategoryIds(): Prisma.ShelterWhereInput {
72+
supplyCategoryIds(
73+
priority?: SupplyPriority | null,
74+
): Prisma.ShelterWhereInput {
7075
if (!this.formProps.supplyCategoryIds) return {};
7176
return {
7277
shelterSupplies: {
7378
some: {
79+
priority: priority ? +priority : undefined,
7480
supply: {
7581
supplyCategoryId: {
7682
in: this.formProps.supplyCategoryIds,
@@ -116,14 +122,14 @@ class ShelterSearch {
116122
}
117123

118124
get query(): Prisma.ShelterWhereInput {
125+
console.log(this.formProps);
119126
if (Object.keys(this.formProps).length === 0) return {};
120127
const queryData = {
121128
AND: [
122129
{ OR: this.search },
123-
...this.priority,
124-
...this.shelterStatus,
125-
this.supplyCategoryIds,
126-
this.supplyIds,
130+
{ OR: this.shelterStatus },
131+
this.priority(this.formProps.supplyIds),
132+
this.supplyCategoryIds(this.formProps.priority),
127133
],
128134
};
129135

@@ -173,7 +179,10 @@ function parseTagResponse(
173179
}
174180
if (
175181
tags.NeedVolunteers &&
176-
voluntaryIds.includes(shelterSupply.supply.supplyCategoryId)
182+
voluntaryIds.includes(shelterSupply.supply.supplyCategoryId) &&
183+
[SupplyPriority.Urgent, SupplyPriority.Needing].includes(
184+
shelterSupply.priority,
185+
)
177186
) {
178187
if (qtd.NeedVolunteers < tags.NeedVolunteers) {
179188
qtd.NeedVolunteers++;

0 commit comments

Comments
 (0)