Skip to content

Commit 375418e

Browse files
authored
fix: priority sum update where (#72)
1 parent b3b6544 commit 375418e

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,45 @@ export class ShelterSupplyService {
8282
async updateMany(body: z.infer<typeof UpdateManyShelterSupplySchema>) {
8383
const { ids, shelterId } = UpdateManyShelterSupplySchema.parse(body);
8484

85-
await this.prismaService.shelterSupply.updateMany({
85+
const supplies = await this.prismaService.shelterSupply.findMany({
8686
where: {
87-
shelterId: shelterId,
87+
shelterId,
8888
supplyId: {
8989
in: ids,
9090
},
9191
},
92-
data: {
93-
priority: SupplyPriority.UnderControl,
94-
updatedAt: new Date().toISOString(),
95-
},
9692
});
93+
94+
const prioritySum = supplies.reduce(
95+
(prev, current) => prev + current.priority,
96+
0,
97+
);
98+
99+
await this.prismaService.$transaction([
100+
this.prismaService.shelter.update({
101+
where: {
102+
id: shelterId,
103+
},
104+
data: {
105+
prioritySum: {
106+
decrement: prioritySum,
107+
},
108+
updatedAt: new Date().toISOString(),
109+
},
110+
}),
111+
this.prismaService.shelterSupply.updateMany({
112+
where: {
113+
shelterId,
114+
supplyId: {
115+
in: ids,
116+
},
117+
},
118+
data: {
119+
priority: SupplyPriority.UnderControl,
120+
updatedAt: new Date().toISOString(),
121+
},
122+
}),
123+
]);
97124
}
98125

99126
async index(shelterId: string) {

0 commit comments

Comments
 (0)