Skip to content

Commit

Permalink
feat: apply update product price logic
Browse files Browse the repository at this point in the history
  • Loading branch information
suk-6 committed Sep 25, 2024
1 parent 3f5f06b commit fc6a587
Showing 1 changed file with 73 additions and 71 deletions.
144 changes: 73 additions & 71 deletions src/modules/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit fc6a587

Please sign in to comment.