import { Injectable, NotFoundException, ConflictException } from '@nestjs/common'; import { EntityManager } from '@mikro-orm/postgresql'; import { PaymentMethod } from '../entities/payment-method.entity'; import { PaymentMethodRepository } from '../repositories/payment-method.repository'; import { CreatePaymentMethodDto } from '../dto/create-payment-method.dto'; import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto'; import { RequiredEntityData } from '@mikro-orm/core'; import { Shop } from ../../..shops/entities/shop.entity'; import { RestMessage, PaymentMessage } from 'src/common/enums/message.enum'; @Injectable() export class PaymentMethodService { constructor( private readonly paymentMethodRepository: PaymentMethodRepository, private readonly em: EntityManager, ) {} async create(restId: string, createPaymentMethodDto: CreatePaymentMethodDto): Promise { // Check if shop exists const shop = await this.em.findOne(Shop, { id: restId }); if (!shop) { throw new NotFoundException(RestMessage.NOT_FOUND); } const paymentMethod = this.paymentMethodRepository.create({ shop, ...createPaymentMethodDto, } as unknown as RequiredEntityData); await this.em.persistAndFlush(paymentMethod); return paymentMethod; } async findAll(): Promise { return this.paymentMethodRepository.findAll({ populate: ['shop'] }); } async findOne(id: string): Promise { const paymentMethod = await this.paymentMethodRepository.findOne({ id }, { populate: ['shop'] }); if (!paymentMethod) { throw new NotFoundException(PaymentMessage.PAYMENT_METHOD_NOT_FOUND); } return paymentMethod; } async findByRestaurant(restaurantId: string): Promise { return this.paymentMethodRepository.find({ shop: { id: restaurantId } }, { populate: ['shop'] }); } async update(id: string, updatePaymentMethodDto: UpdatePaymentMethodDto): Promise { const paymentMethod = await this.findOne(id); this.em.assign(paymentMethod, updatePaymentMethodDto); await this.em.flush(); return paymentMethod; } async remove(id: string): Promise { const paymentMethod = await this.findOne(id); await this.em.removeAndFlush(paymentMethod); return paymentMethod; } }