added token sell to bank

This commit is contained in:
Kevsl 2024-06-14 12:18:58 +02:00
parent 7f00cfb382
commit def7d6ed69

View File

@ -60,6 +60,10 @@ export class CryptoService {
},
});
const user = await this.prisma.user.findFirst({
where: { id: userId },
});
if (!crypto || !crypto.id) {
throw new ForbiddenException('Crypto doesnt exist');
}
@ -82,6 +86,17 @@ export class CryptoService {
},
});
const price = crypto.value * dto.amount;
const newBalanceSeller = user.dollarAvailables + price;
await this.prisma.user.update({
where: {
id: userId,
},
data: {
dollarAvailables: newBalanceSeller,
},
});
const newBalance = userAsset.amount - dto.amount;
if (newBalance > 0) {
return this.prisma.userHasCrypto.update({