Skip to content

Commit

Permalink
fix: priority sum update where (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundesjg authored May 11, 2024
1 parent b3b6544 commit 375418e
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/shelter-supply/shelter-supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,45 @@ export class ShelterSupplyService {
async updateMany(body: z.infer<typeof UpdateManyShelterSupplySchema>) {
const { ids, shelterId } = UpdateManyShelterSupplySchema.parse(body);

await this.prismaService.shelterSupply.updateMany({
const supplies = await this.prismaService.shelterSupply.findMany({
where: {
shelterId: shelterId,
shelterId,
supplyId: {
in: ids,
},
},
data: {
priority: SupplyPriority.UnderControl,
updatedAt: new Date().toISOString(),
},
});

const prioritySum = supplies.reduce(
(prev, current) => prev + current.priority,
0,
);

await this.prismaService.$transaction([
this.prismaService.shelter.update({
where: {
id: shelterId,
},
data: {
prioritySum: {
decrement: prioritySum,
},
updatedAt: new Date().toISOString(),
},
}),
this.prismaService.shelterSupply.updateMany({
where: {
shelterId,
supplyId: {
in: ids,
},
},
data: {
priority: SupplyPriority.UnderControl,
updatedAt: new Date().toISOString(),
},
}),
]);
}

async index(shelterId: string) {
Expand Down

0 comments on commit 375418e

Please sign in to comment.