This commit is contained in:
2026-01-18 17:28:53 +03:30
parent a10b6129b9
commit 8db3b8d1ac
8 changed files with 55 additions and 97 deletions
@@ -1,6 +1,6 @@
import { Controller, Get, Post, Param, UseGuards, Patch, Query, Body } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiHeader, ApiBody, ApiQuery } from '@nestjs/swagger';
import { OrdersService } from '../providers/orders.service';
import { OrderService } from '../providers/order.service';
import { AuthGuard } from '../../auth/guards/auth.guard';
import { UserId } from '../../../common/decorators/user-id.decorator';
import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
@@ -15,22 +15,22 @@ import { CreateInvoiceDto } from '../dto/create-invoice.dto';
@ApiTags('orders')
@ApiBearerAuth()
@Controller()
export class OrdersController {
constructor(private readonly ordersService: OrdersService) { }
export class OrderController {
constructor(private readonly orderService: OrderService) { }
@Post('public/checkout')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Checkout : create order ' })
@ApiBody({ type: CreateOrderDto })
checkout(@UserId() userId: string, @Body() body: CreateOrderDto) {
return this.ordersService.createOrder(userId, body);
return this.orderService.createOrder(userId, body);
}
@Get('public/orders')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
async findAll(@Query() dto: FindOrdersDto, @UserId() userId: string) {
const orders = await this.ordersService.findAllForUser(userId, dto);
const orders = await this.orderService.findAllForUser(userId, dto);
return orders
}
@@ -42,19 +42,19 @@ export class OrdersController {
@Param('orderItemId') orderItemId: string,
@UserId() userId: string
) {
return this.ordersService.confirmOrderItem(userId, orderId, +orderItemId);
return this.orderService.confirmOrderItem(userId, orderId, +orderItemId);
}
/*========================== Admin Routes =====================*/
/*========================== Admin Routes =====================*/
@Post('admin/orders/:orderId/create-invoice')
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Create invoice for new order' })
createInvoice(@Param('orderId') orderId: string, @Body() body: CreateInvoiceDto) {
return this.ordersService.createInvoice(orderId, body);
return this.orderService.createInvoice(orderId, body);
}
// @UseGuards(AdminAuthGuard)
// @Permissions(Permission.MANAGE_ORDERS)
+4 -1
View File
@@ -61,8 +61,11 @@ export class Order extends BaseEntity {
@Property({ type: 'decimal', precision: 10, scale: 0 })
paidAmount!: number;
// balance!: number;
@Property({ type: 'decimal', precision: 10, scale: 0 })
balance!: number;
get balance():number{
return this.total- this.paidAmount
}
// @Property({ type: 'text', nullable: true })
// description?: string;
+5 -5
View File
@@ -1,7 +1,7 @@
import { Module, forwardRef } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { OrdersService } from './providers/orders.service';
import { OrdersController } from './controllers/orders.controller';
import { OrderService } from './providers/order.service';
import { OrderController } from './controllers/order.controller';
import { Order } from './entities/order.entity';
import { OrderItem } from './entities/order-item.entity';
import { AuthModule } from '../auth/auth.module';
@@ -31,9 +31,9 @@ import { OrderItemRepository } from './repositories/order-item.repository';
productModule,
TicketModule
],
controllers: [OrdersController],
providers: [OrdersService, OrderRepository, OrderListeners,
controllers: [OrderController],
providers: [OrderService, OrderRepository, OrderListeners,
OrdersCrone, OrderItemRepository],
exports: [OrderRepository, OrdersService],
exports: [OrderRepository, OrderService],
})
export class OrderModule { }
+4 -7
View File
@@ -1,10 +1,7 @@
import { Injectable, NotFoundException, BadRequestException, Logger } from '@nestjs/common';
import { EntityManager } from '@mikro-orm/postgresql';
import { Order } from '../entities/order.entity';
import { OrderItem } from '../entities/order-item.entity';
import { User } from '../../user/entities/user.entity';
import { PaymentMethodEnum, PaymentStatusEnum } from '../../payment/interface/payment';
import { PaymentsService } from '../../payment/services/payments.service';
import { PaymentService } from '../../payment/services/payments.service';
import { OrderRepository } from '../repositories/order.repository';
import { FindOrdersDto } from '../dto/find-orders.dto';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
@@ -32,7 +29,7 @@ export class OrderService {
private readonly em: EntityManager,
private readonly orderRepository: OrderRepository,
private readonly orderItemRepository: OrderItemRepository,
private readonly paymentsService: PaymentsService,
// private readonly paymentsService: PaymentService,
private readonly userService: UserService,
private readonly productService: ProductService,
private readonly productRepository: ProductRepository,
@@ -167,7 +164,7 @@ export class OrderService {
}
async confirmOrderItem(userId: string, orderId: string, orderItemId: number) {
const orderItem = await this.orderItemRepository.findOne({ id: orderItemId, order: { id: orderId } },
{ populate: ['order', 'order.user'] })
@@ -179,7 +176,7 @@ export class OrderService {
throw new BadRequestException("Order Item does not belong to you")
}
orderItem.status=OrderItemStatus.CONFIRMED
orderItem.status = OrderItemStatus.CONFIRMED
this.em.persistAndFlush(OrderItem)
@@ -1,5 +1,5 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Query } from '@nestjs/common';
import { PaymentsService } from '../services/payments.service';
import { PaymentService } from '../services/payments.service';
import {
ApiTags,
ApiOperation,
@@ -23,9 +23,9 @@ import { PayOrderDto } from '../dto/pay-order.dto';
@ApiTags('payments')
@Controller()
export class PaymentsController {
export class PaymentController {
constructor(
private readonly paymentsService: PaymentsService,
private readonly paymentsService: PaymentService,
) { }
@@ -33,8 +33,8 @@ export class PaymentsController {
@UseGuards(AuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Pay for an order' })
payAnOrder(@Param('orderId') orderId: string,@Body() dto :PayOrderDto) {
return this.paymentsService.payOrder(orderId,dto);
payAnOrder(@Param('orderId') orderId: string, @Body() dto: PayOrderDto) {
return this.paymentsService.payOrder(orderId, dto);
}
// @Post('public/payments/verify')
@@ -6,7 +6,7 @@ import { Permission } from 'src/common/enums/permission.enum';
import { NotifTitleEnum } from 'src/modules/notification/interfaces/notification.interface';
import { NotificationService } from 'src/modules/notification/services/notification.service';
import { ConfigService } from '@nestjs/config';
import { OrdersService } from 'src/modules/order/providers/orders.service';
import { OrderService } from 'src/modules/order/providers/order.service';
@Injectable()
export class PaymentListeners {
@@ -17,7 +17,7 @@ export class PaymentListeners {
private readonly adminService: AdminRepository,
private readonly notificationService: NotificationService,
private readonly configService: ConfigService,
private readonly orderService: OrdersService,
private readonly orderService: OrderService,
) {
this.orderCreatedSmsTemplateId = this.configService.get<string>('SMS_PATTERN_ORDER_CREATED') ?? '123';
this.paymentSucceedSmsTemplateId = this.configService.get<string>('SMS_PATTERN_ONLINE_PAYMENT_SUCCEED') ?? '123';
+21 -9
View File
@@ -1,7 +1,7 @@
import { Module, forwardRef } from '@nestjs/common';
import { PaymentsService } from './services/payments.service';
import { PaymentService } from './services/payments.service';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { PaymentsController } from './controllers/payments.controller';
import { PaymentController } from './controllers/payment.controller';
import { AuthModule } from '../auth/auth.module';
import { JwtModule } from '@nestjs/jwt';
import { Payment } from './entities/payment.entity';
@@ -12,17 +12,29 @@ import { PaymentListeners } from './listeners/payment.listeners';
import { AdminModule } from '../admin/admin.module';
import { NotificationsModule } from '../notification/notifications.module';
import { OrderModule } from '../order/order.module';
import { UserModule } from '../user/user.module';
@Module({
imports: [MikroOrmModule.forFeature([Payment]),
AuthModule, JwtModule, AdminModule, NotificationsModule,
forwardRef(() => OrderModule)],
controllers: [PaymentsController],
providers: [PaymentsService,
PaymentRepository, ZarinpalGateway, GatewayManager, PaymentListeners],
imports: [
MikroOrmModule.forFeature([Payment]),
AuthModule,
JwtModule,
AdminModule,
NotificationsModule,
forwardRef(() => OrderModule),
UserModule
],
controllers: [PaymentController],
providers: [
PaymentService,
PaymentRepository,
ZarinpalGateway,
GatewayManager,
PaymentListeners
],
exports: [
PaymentRepository,
PaymentsService,
PaymentService,
// PaymentGatewayService,
],
})
@@ -19,8 +19,8 @@ import { Payment } from '../entities/payment.entity';
@Injectable()
export class PaymentsService {
private readonly logger = new Logger(PaymentsService.name);
export class PaymentService {
private readonly logger = new Logger(PaymentService.name);
constructor(
private readonly em: EntityManager,
@@ -211,7 +211,6 @@ export class PaymentsService {
// TODO : use decimal js
// does need persist or not
payment.order.paidAmount += payment.amount
payment.order.balance = payment.order.total - payment.order.paidAmount
await em.flush();
return payment;
@@ -249,7 +248,6 @@ export class PaymentsService {
// TODO : use decimal js
payment.order.paidAmount += payment.amount
payment.order.balance = payment.order.total - payment.order.paidAmount
em.persist(payment);
em.persist(payment.order);
@@ -259,57 +257,5 @@ export class PaymentsService {
return payment;
}
// private markPaid(payment: Payment, referenceId: string) {
// payment.status = PaymentStatusEnum.Paid;
// payment.paidAt = new Date();
// payment.referenceId = referenceId;
// }
// private failPayment(payment: Payment) {
// payment.status = PaymentStatusEnum.Failed;
// payment.failedAt = new Date();
// payment.order.status = OrderStatus.CANCELED;
// }
// private async getOrCreateLatestPendingPayment(
// orderId: string,
// params: {
// amount: number;
// method: PaymentMethodEnum;
// gateway: PaymentGatewayEnum | null;
// em?: EntityManager;
// },
// ): Promise<Payment> {
// const em = params.em ?? this.em;
// const existing = await em.findOne(
// Payment,
// { order: { id: orderId }, status: PaymentStatusEnum.Pending },
// { orderBy: { createdAt: 'DESC' } },
// );
// if (existing) {
// if (existing.method !== params.method) {
// throw new BadRequestException(PaymentMessage.EXISTING_PENDING_PAYMENT_MISMATCH);
// }
// return existing;
// }
// const orderRef = em.getReference(Order, orderId);
// const payment = em.create(Payment, {
// amount: params.amount,
// order: orderRef,
// method: params.method,
// gateway: params.gateway,
// transactionId: null,
// status: PaymentStatusEnum.Pending,
// });
// em.persist(payment);
// await em.flush();
// return payment;
// }
}