diff --git a/src/modules/order/entities/order.entity.ts b/src/modules/order/entities/order.entity.ts index 3394d8f..2fb4678 100644 --- a/src/modules/order/entities/order.entity.ts +++ b/src/modules/order/entities/order.entity.ts @@ -61,12 +61,17 @@ export class Order extends BaseEntity { @Property({ type: 'decimal', precision: 10, scale: 0 }) paidAmount!: number; - // balance!: number; - @Property({ type: 'decimal', precision: 10, scale: 0 }) - get balance():number{ - return this.total- this.paidAmount - } + // private _balance!: number; + + @Property({ type: 'decimal', precision: 10, scale: 0 }) + balance!: number; + // get balance(): number { + // return this._balance + // } + // set balance(value) { + // this._balance = this.total - this.paidAmount + // } // @Property({ type: 'text', nullable: true }) // description?: string; diff --git a/src/modules/order/providers/order.service.ts b/src/modules/order/providers/order.service.ts index db54879..0473565 100644 --- a/src/modules/order/providers/order.service.ts +++ b/src/modules/order/providers/order.service.ts @@ -150,6 +150,7 @@ export class OrderService { targetOrder.discount = discount targetOrder.total = subTotal - discount + targetOrder.balance = subTotal - discount // change status targetOrder.status = OrderStatusEnum.INVOICED diff --git a/src/modules/payment/controllers/payment.controller.ts b/src/modules/payment/controllers/payment.controller.ts index 160eb0e..8215693 100644 --- a/src/modules/payment/controllers/payment.controller.ts +++ b/src/modules/payment/controllers/payment.controller.ts @@ -2,6 +2,7 @@ import { Controller, Get, Post, Body, Param, UseGuards, + Query, } from '@nestjs/common'; import { PaymentService } from '../services/payments.service'; import { @@ -41,7 +42,7 @@ export class PaymentController { @ApiBearerAuth() @Get('public/payments') @ApiOperation({ summary: 'get all payments as admin' }) - findAllByUser(@UserId() userId: string, @Body() dto: FindPaymentsDto) { + findAllByUser(@UserId() userId: string, @Query() dto: FindPaymentsDto) { return this.paymentsService.findAllByUser(dto, userId); } diff --git a/src/modules/payment/services/payments.service.ts b/src/modules/payment/services/payments.service.ts index f1b3491..34012d1 100644 --- a/src/modules/payment/services/payments.service.ts +++ b/src/modules/payment/services/payments.service.ts @@ -122,7 +122,7 @@ export class PaymentService { gateway: null, order, status: PaymentStatusEnum.Paid, - paidAt: new Date() + paidAt: new Date(), }); // 3. Create Credit transaction record const newCreditTransaction = this.creditRepository.create({ @@ -130,7 +130,7 @@ export class PaymentService { amount, type: CreditTransactionType.WITHDRAW, balance: newBalance, - orderId: order.id + orderId: order.id, }); em.persist([payment, newCreditTransaction]); @@ -148,21 +148,19 @@ export class PaymentService { const gateway = this.gatewayManager.get(requestedGateway!); - const payment = this.paymentRepository.create({ - order, - amount, - method: PaymentMethodEnum.Online, - status: PaymentStatusEnum.Pending - }) - const { token, paymentUrl } = await gateway.requestPayment({ amount: ctx.amount, orderId: ctx.order.id, }); - payment.gateway = requestedGateway; - payment.token = token; - payment.status = PaymentStatusEnum.Pending; + const payment = this.paymentRepository.create({ + order, + amount, + method: PaymentMethodEnum.Online, + status: PaymentStatusEnum.Pending, + gateway: requestedGateway, + token, + }) await this.em.persistAndFlush(payment); @@ -211,6 +209,7 @@ export class PaymentService { // TODO : use decimal js // does need persist or not payment.order.paidAmount += payment.amount + payment.order.balance -= payment.amount await em.flush(); return payment; @@ -248,6 +247,7 @@ export class PaymentService { // TODO : use decimal js payment.order.paidAmount += payment.amount + payment.order.balance -= payment.amount em.persist(payment); em.persist(payment.order);