From abd355f1f9d34205e41b851ca022d026081a78a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Fagundes?= Date: Wed, 5 Jun 2024 14:52:45 -0300 Subject: [PATCH] fix: decretment supplies when update donation order --- src/donation-order/donation-order.service.ts | 37 +++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/donation-order/donation-order.service.ts b/src/donation-order/donation-order.service.ts index 5738c54..cc7fb2f 100644 --- a/src/donation-order/donation-order.service.ts +++ b/src/donation-order/donation-order.service.ts @@ -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'; @@ -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,