mirror of
https://github.com/Kevsl/crypto-exchange-api.git
synced 2025-07-09 14:00:12 +02:00
fixed: trade received amount by seller
This commit is contained in:
parent
f6dddf8aef
commit
ebb017c07c
@ -2,6 +2,7 @@ import { ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { checkUserHasAccount, checkuserIsAdmin } from 'src/utils/checkUser';
|
||||
import { TradeDto } from './dto';
|
||||
import { of } from 'rxjs';
|
||||
@Injectable()
|
||||
export class TradeService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
@ -73,6 +74,12 @@ export class TradeService {
|
||||
},
|
||||
});
|
||||
|
||||
const seller = await this.prisma.user.findFirst({
|
||||
where: {
|
||||
id: offer.id_user,
|
||||
},
|
||||
});
|
||||
|
||||
const price = crypto.value * offer.amount;
|
||||
if (buyer.dollarAvailables < price) {
|
||||
throw new ForbiddenException(
|
||||
@ -164,6 +171,18 @@ export class TradeService {
|
||||
dollarAvailables: prevAmount - price,
|
||||
},
|
||||
});
|
||||
|
||||
const newBalanceSeller = seller.dollarAvailables + price;
|
||||
|
||||
await this.prisma.user.update({
|
||||
where: {
|
||||
id: seller.id,
|
||||
},
|
||||
data: {
|
||||
dollarAvailables: newBalanceSeller,
|
||||
},
|
||||
});
|
||||
|
||||
await this.prisma.offer.delete({
|
||||
where: {
|
||||
id: offer.id,
|
||||
|
Loading…
x
Reference in New Issue
Block a user