diff --git a/src/modules/payments/controllers/payments.controller.ts b/src/modules/payments/controllers/payments.controller.ts index a9d208e..dda4e94 100644 --- a/src/modules/payments/controllers/payments.controller.ts +++ b/src/modules/payments/controllers/payments.controller.ts @@ -94,7 +94,7 @@ export class PaymentsController { @ApiOperation({ summary: 'Get restaurant payment method by ID' }) @ApiParam({ name: 'paymentMethodId', type: 'string', description: 'Payment method ID' }) findOneRestaurantPaymentMethod(@Param('paymentMethodId') paymentMethodId: string) { - return this.paymentMethodService.findOne(paymentMethodId); + return this.paymentMethodService.findOneOrFail(paymentMethodId); } @UseGuards(AdminAuthGuard) diff --git a/src/modules/payments/entities/payment-method.entity.ts b/src/modules/payments/entities/payment-method.entity.ts index 63f5ad9..b41b07e 100644 --- a/src/modules/payments/entities/payment-method.entity.ts +++ b/src/modules/payments/entities/payment-method.entity.ts @@ -6,6 +6,7 @@ import { PaymentGatewayEnum, PaymentMethodEnum } from '../interface/payment'; @Entity({ tableName: 'payment_methods' }) @Unique({ properties: ['restaurant', 'method'] }) @Index({ properties: ['restaurant', 'enabled'] }) +@Index({ properties: ['restaurant'] }) export class PaymentMethod extends BaseEntity { @ManyToOne(() => Restaurant) restaurant!: Restaurant; diff --git a/src/modules/payments/entities/payment.entity.ts b/src/modules/payments/entities/payment.entity.ts index 5f2827e..0fa2e69 100644 --- a/src/modules/payments/entities/payment.entity.ts +++ b/src/modules/payments/entities/payment.entity.ts @@ -4,6 +4,7 @@ import { Order } from '../../orders/entities/order.entity'; import { PaymentGatewayEnum, PaymentMethodEnum, PaymentStatusEnum } from '../interface/payment'; @Entity({ tableName: 'payments' }) +@Index({ properties: ['order'] }) export class Payment extends BaseEntity { @ManyToOne(() => Order) @Index() diff --git a/src/modules/payments/services/payment-method.service.ts b/src/modules/payments/services/payment-method.service.ts index cb40dcf..73a6848 100644 --- a/src/modules/payments/services/payment-method.service.ts +++ b/src/modules/payments/services/payment-method.service.ts @@ -5,22 +5,20 @@ import { PaymentMethodRepository } from '../repositories/payment-method.reposito import { CreatePaymentMethodDto } from '../dto/create-payment-method.dto'; import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto'; import { RequiredEntityData } from '@mikro-orm/core'; -import { Restaurant } from '../../restaurants/entities/restaurant.entity'; -import { RestMessage, PaymentMessage } from 'src/common/enums/message.enum'; +import { PaymentMessage } from 'src/common/enums/message.enum'; +import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service'; @Injectable() export class PaymentMethodService { constructor( private readonly paymentMethodRepository: PaymentMethodRepository, + private readonly restaurantsService: RestaurantsService, private readonly em: EntityManager, - ) {} + ) { } async create(restId: string, createPaymentMethodDto: CreatePaymentMethodDto): Promise { // Check if restaurant exists - const restaurant = await this.em.findOne(Restaurant, { id: restId }); - if (!restaurant) { - throw new NotFoundException(RestMessage.NOT_FOUND); - } + const restaurant = await this.restaurantsService.findOneOrFail(restId) const paymentMethod = this.paymentMethodRepository.create({ restaurant, @@ -30,11 +28,7 @@ export class PaymentMethodService { return paymentMethod; } - async findAll(): Promise { - return this.paymentMethodRepository.findAll({ populate: ['restaurant'] }); - } - - async findOne(id: string): Promise { + async findOneOrFail(id: string): Promise { const paymentMethod = await this.paymentMethodRepository.findOne({ id }, { populate: ['restaurant'] }); if (!paymentMethod) { throw new NotFoundException(PaymentMessage.PAYMENT_METHOD_NOT_FOUND); @@ -47,14 +41,14 @@ export class PaymentMethodService { } async update(id: string, updatePaymentMethodDto: UpdatePaymentMethodDto): Promise { - const paymentMethod = await this.findOne(id); + const paymentMethod = await this.findOneOrFail(id); this.em.assign(paymentMethod, updatePaymentMethodDto); await this.em.flush(); return paymentMethod; } async remove(id: string): Promise { - const paymentMethod = await this.findOne(id); + const paymentMethod = await this.findOneOrFail(id); await this.em.removeAndFlush(paymentMethod); return paymentMethod; }