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' })
@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)
@@ -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;
@@ -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()
@@ -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<PaymentMethod> {
// 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<PaymentMethod[]> {
return this.paymentMethodRepository.findAll({ populate: ['restaurant'] });
}
async findOne(id: string): Promise<PaymentMethod> {
async findOneOrFail(id: string): Promise<PaymentMethod> {
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<PaymentMethod> {
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<PaymentMethod> {
const paymentMethod = await this.findOne(id);
const paymentMethod = await this.findOneOrFail(id);
await this.em.removeAndFlush(paymentMethod);
return paymentMethod;
}