paymant clean codes

This commit is contained in:
2026-04-07 16:45:28 +03:30
parent b3cc419dd1
commit 87d0ea3792
4 changed files with 11 additions and 15 deletions
@@ -94,7 +94,7 @@ export class PaymentsController {
@ApiOperation({ summary: 'Get restaurant payment method by ID' }) @ApiOperation({ summary: 'Get restaurant payment method by ID' })
@ApiParam({ name: 'paymentMethodId', type: 'string', description: 'Payment method ID' }) @ApiParam({ name: 'paymentMethodId', type: 'string', description: 'Payment method ID' })
findOneRestaurantPaymentMethod(@Param('paymentMethodId') paymentMethodId: string) { findOneRestaurantPaymentMethod(@Param('paymentMethodId') paymentMethodId: string) {
return this.paymentMethodService.findOne(paymentMethodId); return this.paymentMethodService.findOneOrFail(paymentMethodId);
} }
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@@ -6,6 +6,7 @@ import { PaymentGatewayEnum, PaymentMethodEnum } from '../interface/payment';
@Entity({ tableName: 'payment_methods' }) @Entity({ tableName: 'payment_methods' })
@Unique({ properties: ['restaurant', 'method'] }) @Unique({ properties: ['restaurant', 'method'] })
@Index({ properties: ['restaurant', 'enabled'] }) @Index({ properties: ['restaurant', 'enabled'] })
@Index({ properties: ['restaurant'] })
export class PaymentMethod extends BaseEntity { export class PaymentMethod extends BaseEntity {
@ManyToOne(() => Restaurant) @ManyToOne(() => Restaurant)
restaurant!: Restaurant; restaurant!: Restaurant;
@@ -4,6 +4,7 @@ import { Order } from '../../orders/entities/order.entity';
import { PaymentGatewayEnum, PaymentMethodEnum, PaymentStatusEnum } from '../interface/payment'; import { PaymentGatewayEnum, PaymentMethodEnum, PaymentStatusEnum } from '../interface/payment';
@Entity({ tableName: 'payments' }) @Entity({ tableName: 'payments' })
@Index({ properties: ['order'] })
export class Payment extends BaseEntity { export class Payment extends BaseEntity {
@ManyToOne(() => Order) @ManyToOne(() => Order)
@Index() @Index()
@@ -5,22 +5,20 @@ import { PaymentMethodRepository } from '../repositories/payment-method.reposito
import { CreatePaymentMethodDto } from '../dto/create-payment-method.dto'; import { CreatePaymentMethodDto } from '../dto/create-payment-method.dto';
import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto'; import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto';
import { RequiredEntityData } from '@mikro-orm/core'; import { RequiredEntityData } from '@mikro-orm/core';
import { Restaurant } from '../../restaurants/entities/restaurant.entity'; import { PaymentMessage } from 'src/common/enums/message.enum';
import { RestMessage, PaymentMessage } from 'src/common/enums/message.enum'; import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service';
@Injectable() @Injectable()
export class PaymentMethodService { export class PaymentMethodService {
constructor( constructor(
private readonly paymentMethodRepository: PaymentMethodRepository, private readonly paymentMethodRepository: PaymentMethodRepository,
private readonly restaurantsService: RestaurantsService,
private readonly em: EntityManager, private readonly em: EntityManager,
) {} ) { }
async create(restId: string, createPaymentMethodDto: CreatePaymentMethodDto): Promise<PaymentMethod> { async create(restId: string, createPaymentMethodDto: CreatePaymentMethodDto): Promise<PaymentMethod> {
// Check if restaurant exists // Check if restaurant exists
const restaurant = await this.em.findOne(Restaurant, { id: restId }); const restaurant = await this.restaurantsService.findOneOrFail(restId)
if (!restaurant) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
const paymentMethod = this.paymentMethodRepository.create({ const paymentMethod = this.paymentMethodRepository.create({
restaurant, restaurant,
@@ -30,11 +28,7 @@ export class PaymentMethodService {
return paymentMethod; return paymentMethod;
} }
async findAll(): Promise<PaymentMethod[]> { async findOneOrFail(id: string): Promise<PaymentMethod> {
return this.paymentMethodRepository.findAll({ populate: ['restaurant'] });
}
async findOne(id: string): Promise<PaymentMethod> {
const paymentMethod = await this.paymentMethodRepository.findOne({ id }, { populate: ['restaurant'] }); const paymentMethod = await this.paymentMethodRepository.findOne({ id }, { populate: ['restaurant'] });
if (!paymentMethod) { if (!paymentMethod) {
throw new NotFoundException(PaymentMessage.PAYMENT_METHOD_NOT_FOUND); throw new NotFoundException(PaymentMessage.PAYMENT_METHOD_NOT_FOUND);
@@ -47,14 +41,14 @@ export class PaymentMethodService {
} }
async update(id: string, updatePaymentMethodDto: UpdatePaymentMethodDto): Promise<PaymentMethod> { async update(id: string, updatePaymentMethodDto: UpdatePaymentMethodDto): Promise<PaymentMethod> {
const paymentMethod = await this.findOne(id); const paymentMethod = await this.findOneOrFail(id);
this.em.assign(paymentMethod, updatePaymentMethodDto); this.em.assign(paymentMethod, updatePaymentMethodDto);
await this.em.flush(); await this.em.flush();
return paymentMethod; return paymentMethod;
} }
async remove(id: string): Promise<PaymentMethod> { async remove(id: string): Promise<PaymentMethod> {
const paymentMethod = await this.findOne(id); const paymentMethod = await this.findOneOrFail(id);
await this.em.removeAndFlush(paymentMethod); await this.em.removeAndFlush(paymentMethod);
return paymentMethod; return paymentMethod;
} }