mirror of
https://github.com/Kevsl/crypto-exchange-api.git
synced 2025-07-09 14:00:12 +02:00
added token sell to bank
This commit is contained in:
parent
a5ea9e3153
commit
bbde4a2ec5
@ -51,6 +51,48 @@ export class CryptoService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async sellCryto(userId: string, dto: BuyCryptoDto) {
|
||||||
|
await checkUserHasAccount(userId);
|
||||||
|
|
||||||
|
const crypto = await this.prisma.crypto.findFirst({
|
||||||
|
where: {
|
||||||
|
id: dto.id_crypto,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!crypto || !crypto.id) {
|
||||||
|
throw new ForbiddenException('Crypto doesnt exist');
|
||||||
|
}
|
||||||
|
|
||||||
|
const userAsset = await this.prisma.userHasCrypto.findFirst({
|
||||||
|
where: {
|
||||||
|
id_user: userId,
|
||||||
|
id_crypto: dto.id_crypto,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!userAsset || userAsset.amount < dto.amount) {
|
||||||
|
throw new ForbiddenException(`Seller dont have enough asset`);
|
||||||
|
} else {
|
||||||
|
const newBalance = userAsset.amount - dto.amount;
|
||||||
|
if (newBalance > 0) {
|
||||||
|
await this.prisma.userHasCrypto.update({
|
||||||
|
where: {
|
||||||
|
id: userAsset.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
amount: newBalance,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await this.prisma.userHasCrypto.delete({
|
||||||
|
where: {
|
||||||
|
id: userAsset.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async createCrypto(userId: string, dto: CryptoDto) {
|
async createCrypto(userId: string, dto: CryptoDto) {
|
||||||
await checkuserIsAdmin(userId);
|
await checkuserIsAdmin(userId);
|
||||||
|
|
||||||
@ -151,6 +193,7 @@ export class CryptoService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async editCryptoById(userId: string, cryptoId: string, dto: CryptoDto) {
|
async editCryptoById(userId: string, cryptoId: string, dto: CryptoDto) {
|
||||||
await checkuserIsAdmin(userId);
|
await checkuserIsAdmin(userId);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user