fix: bug in approve of the deposit request
This commit is contained in:
@@ -11,7 +11,6 @@ import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
||||
import { CommonMessage, PaymentMessage, UserMessage, WalletMessage } from "../../../common/enums/message.enum";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { Wallet } from "../../wallets/entities/wallet.entity";
|
||||
import { WalletsService } from "../../wallets/providers/wallets.service";
|
||||
import { PAYMENT } from "../constants";
|
||||
import { CreateBankAccountDto } from "../DTO/create-bankaccount.dto";
|
||||
@@ -363,27 +362,30 @@ export class PaymentsService {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
try {
|
||||
const depositRequest = await queryRunner.manager.findOne(DepositRequest, {
|
||||
where: { id: depositId },
|
||||
lock: { mode: "pessimistic_write" },
|
||||
});
|
||||
// const depositRequest = await queryRunner.manager.findOne(DepositRequest, {
|
||||
// where: { id: depositId },
|
||||
// lock: { mode: "pessimistic_write" },
|
||||
// });
|
||||
|
||||
const depositRequest = await queryRunner.manager
|
||||
.createQueryBuilder(DepositRequest, "deposit")
|
||||
.innerJoinAndSelect("deposit.user", "user")
|
||||
.where("deposit.id = :id", { id: depositId })
|
||||
.setLock("pessimistic_write")
|
||||
.getOne();
|
||||
|
||||
if (!depositRequest) throw new BadRequestException(PaymentMessage.DEPOSIT_NOT_FOUND);
|
||||
|
||||
if (depositRequest.status === DepositRequestStatus.APPROVED) throw new BadRequestException(PaymentMessage.DEPOSIT_ALREADY_APPROVED);
|
||||
|
||||
const wallet = await queryRunner.manager.findOneBy(Wallet, { user: depositRequest.user });
|
||||
if (!wallet) throw new BadRequestException(WalletMessage.WALLET_NOT_FOUND);
|
||||
|
||||
await queryRunner.manager.update(Wallet, { id: wallet.id }, { balance: new Decimal(wallet.balance).add(depositRequest.amount) });
|
||||
|
||||
await queryRunner.manager.update(DepositRequest, { id: depositRequest.id }, { status: DepositRequestStatus.APPROVED });
|
||||
|
||||
await this.walletsService.createDepositTransaction(depositRequest.amount, wallet.id, queryRunner);
|
||||
await this.walletsService.createDepositTransaction(depositRequest.amount, depositRequest.user.id, queryRunner);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
return {
|
||||
message: PaymentMessage.DEPOSIT_APPROVED,
|
||||
depositRequest,
|
||||
depositRequest: depositRequest.id,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
|
||||
@@ -9,28 +9,25 @@ import { IFile } from "../utils/interfaces/IFile";
|
||||
|
||||
@ApiTags("Uploader")
|
||||
@Controller("uploader")
|
||||
@AuthGuards()
|
||||
export class UploaderController {
|
||||
constructor(private readonly uploaderService: UploaderService) {}
|
||||
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "Uploads a single file" })
|
||||
@ApiConsumes("multipart/form-data")
|
||||
@UseInterceptors(FileInterceptor("file"))
|
||||
@ApiBody({ type: UploadSingleFileDto })
|
||||
@Post("single-file")
|
||||
async uploadFile(@UploadedFile() file: IFile) {
|
||||
console.log(file);
|
||||
return await this.uploaderService.upload(file);
|
||||
uploadFile(@UploadedFile() file: IFile) {
|
||||
return this.uploaderService.upload(file);
|
||||
}
|
||||
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "Uploads multiple files" })
|
||||
@ApiConsumes("multipart/form-data")
|
||||
@UseInterceptors(FilesInterceptor("files"))
|
||||
@ApiBody({ type: UploadMultipleFileDto })
|
||||
@Post("multi-file")
|
||||
async uploadFiles(@UploadedFiles() files: IFile[]) {
|
||||
console.log({ files });
|
||||
return await this.uploaderService.uploadMultiple(files);
|
||||
uploadFiles(@UploadedFiles() files: IFile[]) {
|
||||
return this.uploaderService.uploadMultiple(files);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ export class UploaderService {
|
||||
}
|
||||
|
||||
async uploadMultiple(files: IFile[]) {
|
||||
const uploadPromises = files.map((file) => this.s3Service.upload(file));
|
||||
return await Promise.all(uploadPromises);
|
||||
// const uploadPromises = files.map((file) => this.s3Service.upload(file));
|
||||
|
||||
// return await Promise.all(uploadPromises);
|
||||
return this.s3Service.uploadMultiple(files);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { extname } from "node:path";
|
||||
|
||||
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
import { PutObjectCommand, PutObjectCommandInput, S3Client } from "@aws-sdk/client-s3";
|
||||
import { Inject, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
||||
|
||||
import { IS3Configs } from "../../../configs/s3.config";
|
||||
import { S3_CONFIG } from "../constants";
|
||||
@@ -10,7 +10,10 @@ import { IFile } from "../interfaces/IFile";
|
||||
|
||||
@Injectable()
|
||||
export class S3Service {
|
||||
private readonly logger = new Logger(S3Service.name);
|
||||
private client: S3Client;
|
||||
|
||||
//
|
||||
constructor(@Inject(S3_CONFIG) private s3Configs: IS3Configs) {
|
||||
this.client = new S3Client({
|
||||
endpoint: this.s3Configs.endpoint,
|
||||
@@ -24,17 +27,31 @@ export class S3Service {
|
||||
|
||||
async upload(file: IFile) {
|
||||
const extName = extname(file.originalname);
|
||||
const params = {
|
||||
const params: PutObjectCommandInput = {
|
||||
Body: file.buffer,
|
||||
Bucket: this.s3Configs.bucket,
|
||||
Key: "images/" + randomUUID() + Date.now() + extName,
|
||||
};
|
||||
await this.client.send(new PutObjectCommand(params));
|
||||
const url = `${this.s3Configs.url}/${params.Key}`;
|
||||
return {
|
||||
url,
|
||||
originalname: file.originalname,
|
||||
size: file.size,
|
||||
Key: `images/${randomUUID()}${Date.now()}${extName}`,
|
||||
ACL: "public-read",
|
||||
};
|
||||
//
|
||||
|
||||
try {
|
||||
await this.client.send(new PutObjectCommand(params));
|
||||
const url = `${this.s3Configs.url}/${params.Key}`;
|
||||
return {
|
||||
url,
|
||||
originalname: file.originalname,
|
||||
size: file.size,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
throw new InternalServerErrorException("Failed to upload file");
|
||||
}
|
||||
}
|
||||
|
||||
async uploadMultiple(files: IFile[]) {
|
||||
const uploadPromises = files.map((file) => this.upload(file));
|
||||
const results = await Promise.all(uploadPromises);
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,20 +59,20 @@ export class WalletsService {
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async createDepositTransaction(amount: Decimal, walletId: string, queryRunner: QueryRunner) {
|
||||
// const wallet = await queryRunner.manager.findOne(Wallet, { where: { user: { id: userId } }, lock: { mode: "pessimistic_write" } });
|
||||
// if (!wallet) throw new BadRequestException(WalletMessage.WALLET_NOT_FOUND);
|
||||
async createDepositTransaction(amount: Decimal, userId: string, queryRunner: QueryRunner) {
|
||||
const wallet = await queryRunner.manager.findOne(Wallet, { where: { user: { id: userId } }, lock: { mode: "pessimistic_write" } });
|
||||
if (!wallet) throw new BadRequestException(WalletMessage.WALLET_NOT_FOUND);
|
||||
|
||||
const transaction = queryRunner.manager.create(WalletTransaction, {
|
||||
amount,
|
||||
wallet: { id: walletId },
|
||||
wallet: { id: wallet.id },
|
||||
type: TransactionType.DEPOSIT,
|
||||
description: WalletMessage.DEPOSIT_WALLET_TRANSFER,
|
||||
});
|
||||
|
||||
// wallet.balance = new Decimal(wallet.balance).add(transaction.amount);
|
||||
// await queryRunner.manager.save(Wallet, wallet);
|
||||
wallet.balance = new Decimal(wallet.balance).add(transaction.amount);
|
||||
|
||||
await queryRunner.manager.save(Wallet, wallet);
|
||||
await queryRunner.manager.save(WalletTransaction, transaction);
|
||||
|
||||
return transaction;
|
||||
|
||||
Reference in New Issue
Block a user