diff --git a/src/auth/dto/auth.register.dto.ts b/src/auth/dto/auth.register.dto.ts index e9eb1d7..1d533d6 100644 --- a/src/auth/dto/auth.register.dto.ts +++ b/src/auth/dto/auth.register.dto.ts @@ -1,5 +1,11 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { + IsCreditCard, + IsEmail, + IsNotEmpty, + IsOptional, + IsString, +} from 'class-validator'; export class AuthRegisterDto { @ApiProperty({ type: String, diff --git a/src/crypto/crypto.service.ts b/src/crypto/crypto.service.ts index 1322189..304b637 100644 --- a/src/crypto/crypto.service.ts +++ b/src/crypto/crypto.service.ts @@ -103,7 +103,7 @@ export class CryptoService { } } const newCryptoValue = crypto.value * 1.1; - await this.prisma.crypto.update({ + return this.prisma.crypto.update({ where: { id: dto.id_crypto, }, diff --git a/src/trade/dto/trade.dto.ts b/src/trade/dto/trade.dto.ts index 65c5859..7ffaaa3 100644 --- a/src/trade/dto/trade.dto.ts +++ b/src/trade/dto/trade.dto.ts @@ -1,48 +1,12 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsDecimal, IsNotEmpty, IsString } from 'class-validator'; +import { IsNotEmpty, IsString } from 'class-validator'; export class TradeDto { - @ApiProperty({ - type: String, - description: 'UUID of the user that hold tokens before trade', - example: '121212-DSDZ1-21212DJDZ-31313', - }) - @IsNotEmpty() - @IsString() - id_giver: string; - - @ApiProperty({ - type: String, - description: 'UUID of the user that will receive tokens after trade', - example: '121212-DSDZ1-21212DJDZ-31313', - }) - @IsNotEmpty() - @IsString() - id_receiver: string; - - @ApiProperty({ - type: String, - description: 'UUID of the crypto traded', - example: '121212-DSDZ1-21212DJDZ-31313', - }) - @IsNotEmpty() - @IsString() - id_crypto: string; - - @ApiProperty({ - type: Number, - description: 'Amount of tokens traded ', - example: 2, - }) - @IsNotEmpty() - @IsDecimal() - amount_traded: number; - @ApiProperty({ type: String, description: 'Offer UUID ', example: '121212-DSDZ1-21212DJDZ-31313', }) @IsNotEmpty() - @IsDecimal() + @IsString() id_offer: string; } diff --git a/src/trade/trade.service.ts b/src/trade/trade.service.ts index 00b12be..f2379ec 100644 --- a/src/trade/trade.service.ts +++ b/src/trade/trade.service.ts @@ -35,17 +35,24 @@ export class TradeService { async createTrade(userId: string, dto: TradeDto) { await checkUserHasAccount(userId); + const offer = await this.prisma.offer.findUnique({ + where: { + id: dto.id_offer, + }, + }); const crypto = await this.prisma.crypto.findFirst({ where: { - id: dto.id_crypto, + id: offer.id_crypto, }, }); + const buyer = await this.prisma.user.findFirst({ where: { - id: dto.id_receiver, + id: offer.id_user, }, }); - const price = crypto.value * dto.amount_traded; + + const price = crypto.value * offer.amount; if (buyer.dollarAvailables < price) { throw new ForbiddenException( `Acqueror ${buyer.pseudo} doesnt have enough money to make this trade`, @@ -54,26 +61,26 @@ export class TradeService { const asset = await this.prisma.userHasCrypto.findFirst({ where: { - id_crypto: dto.id_crypto, - id_user: dto.id_giver, + id_crypto: offer.id_crypto, + id_user: offer.id_user, }, }); - if (!asset || asset.amount < dto.amount_traded) { + if (!asset || asset.amount < offer.amount) { throw new ForbiddenException(`Seller doesnt have enough ${crypto.name} `); } const trade = await this.prisma.trade.create({ data: { - id_giver: dto.id_giver, - id_receiver: dto.id_receiver, - id_crypto: dto.id_crypto, - amount_traded: dto.amount_traded, - id_offer: dto.id_offer, + id_giver: offer.id_user, + id_receiver: userId, + id_crypto: offer.id_crypto, + amount_traded: offer.amount, + id_offer: offer.id, }, }); - const newBalanceGiver = (asset.amount -= dto.amount_traded); + const newBalanceGiver = (asset.amount -= offer.amount); await this.prisma.userHasCrypto.update({ where: { id: asset.id, @@ -85,21 +92,21 @@ export class TradeService { const receiverAssets = await this.prisma.userHasCrypto.findFirst({ where: { - id_user: dto.id_receiver, - id_crypto: dto.id_crypto, + id_user: userId, + id_crypto: offer.id_crypto, }, }); if (!receiverAssets) { await this.prisma.userHasCrypto.create({ data: { - id_user: dto.id_receiver, - id_crypto: dto.id_crypto, - amount: dto.amount_traded, + id_user: userId, + id_crypto: offer.id_crypto, + amount: offer.amount, createdAt: new Date(), }, }); } else { - const newBalanceReceiver = receiverAssets.amount + dto.amount_traded; + const newBalanceReceiver = receiverAssets.amount + offer.amount; await this.prisma.userHasCrypto.update({ where: { id: receiverAssets.id, @@ -125,12 +132,17 @@ export class TradeService { await this.prisma.user.update({ where: { - id: dto.id_receiver, + id: userId, }, data: { dollarAvailables: prevAmount - price, }, }); + await this.prisma.offer.delete({ + where: { + id: offer.id, + }, + }); return trade; }