Skip to content

Commit

Permalink
fix: decretment supplies when update donation order
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundesjg committed Jun 5, 2024
1 parent 0b4c1ff commit abd355f
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/donation-order/donation-order.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';
import { HttpException, Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { DonationOrderStatus, Prisma } from '@prisma/client';
import { DefaultArgs } from '@prisma/client/runtime/library';

import { PrismaService } from '../prisma/prisma.service';
Expand Down Expand Up @@ -162,22 +162,27 @@ export class DonationOrderService {
);
}

const updatePromises =
status === DonationOrderStatus.Complete
? order.donationOrderSupplies.map((d) =>
this.prismaService.shelterSupply.update({
where: {
shelterId_supplyId: {
shelterId: order.shelterId,
supplyId: d.supplyId,
},
},
data: {
quantity: {
decrement: d.quantity,
},
},
}),
)
: [];

await this.prismaService.$transaction([
...order.donationOrderSupplies.map((d) =>
this.prismaService.shelterSupply.update({
where: {
shelterId_supplyId: {
shelterId: order.shelterId,
supplyId: d.supplyId,
},
},
data: {
quantity: {
decrement: d.quantity,
},
},
}),
),
...updatePromises,
this.prismaService.donationOrder.update({
where: {
id: orderId,
Expand Down

0 comments on commit abd355f

Please sign in to comment.