From fc6a5879b375ef4d7f8b0d6d25c588ce74ca3850 Mon Sep 17 00:00:00 2001 From: suk-6 Date: Wed, 25 Sep 2024 20:46:33 +0900 Subject: [PATCH] feat: apply update product price logic --- src/modules/product/product.service.ts | 144 +++++++++++++------------ 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/src/modules/product/product.service.ts b/src/modules/product/product.service.ts index 68cc4ce..9c912b0 100644 --- a/src/modules/product/product.service.ts +++ b/src/modules/product/product.service.ts @@ -710,91 +710,93 @@ export class ProductService { if (product.currentAmount * amount > (user.userAccount.credit ?? 0)) throw new BadRequestException('잔액이 부족합니다.'); - return this.prisma.$transaction(async (tx) => { - await tx.userAccount.update({ - where: { - id: user.userAccount!.id, - }, - data: { - credit: { - decrement: product.currentAmount * amount, - }, - }, - }); - - const userTokenBalance = await tx.userTokenBalancesOnProduct - .findFirstOrThrow({ + return this.prisma + .$transaction(async (tx) => { + await tx.userAccount.update({ where: { - AND: [ - { - productId, - }, - { - userAccountId: user.userAccount!.id, - }, - ], + id: user.userAccount!.id, }, - }) - .catch(() => - tx.userTokenBalancesOnProduct.create({ - data: { - product: { - connect: { - id: productId, + data: { + credit: { + decrement: product.currentAmount * amount, + }, + }, + }); + + const userTokenBalance = await tx.userTokenBalancesOnProduct + .findFirstOrThrow({ + where: { + AND: [ + { + productId, }, - }, - userAccount: { - connect: { - id: user.userAccount!.id, + { + userAccountId: user.userAccount!.id, }, - }, + ], }, - }), - ); + }) + .catch(() => + tx.userTokenBalancesOnProduct.create({ + data: { + product: { + connect: { + id: productId, + }, + }, + userAccount: { + connect: { + id: user.userAccount!.id, + }, + }, + }, + }), + ); - await tx.userTokenBalancesOnProduct.update({ - where: { - id: userTokenBalance.id, - }, - data: { - token: { - increment: Number(amount), + await tx.userTokenBalancesOnProduct.update({ + where: { + id: userTokenBalance.id, }, - }, - }); + data: { + token: { + increment: Number(amount), + }, + }, + }); - await tx.product.update({ - where: { - id: productId, - }, - data: { - collectedAmount: { - increment: product.currentAmount * amount, + await tx.product.update({ + where: { + id: productId, }, - fundingLog: { - create: { - amount: Number(amount), - price: product.currentAmount, - type: FundingType.DEPOSIT, - userTokenBalance: { - connect: { - id: userTokenBalance.id, + data: { + collectedAmount: { + increment: product.currentAmount * amount, + }, + fundingLog: { + create: { + amount: Number(amount), + price: product.currentAmount, + type: FundingType.DEPOSIT, + userTokenBalance: { + connect: { + id: userTokenBalance.id, + }, }, }, }, }, - }, - }); + }); - await this.blockchain.getRemainingTokens(product.tokenAddress!).then((remaining) => { - if (remaining < amount) throw new BadRequestException('남은 토큰이 부족합니다.'); - }); + await this.blockchain.getRemainingTokens(product.tokenAddress!).then((remaining) => { + if (remaining < amount) throw new BadRequestException('남은 토큰이 부족합니다.'); + }); - await this.blockchain.transfer( - user.userAccount!.walletAddress!, - amount, - product.tokenAddress!, - ); - }); + await this.blockchain.transfer( + user.userAccount!.walletAddress!, + amount, + product.tokenAddress!, + ); + }) + .then(() => this.updateProductPrice(productId)); } }