64 lines
2.4 KiB
TypeScript
64 lines
2.4 KiB
TypeScript
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 { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
|
|
|
@Injectable()
|
|
export class PaymentMethodService {
|
|
constructor(
|
|
private readonly paymentMethodRepository: PaymentMethodRepository,
|
|
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(`Restaurant with ID ${restId} not found`);
|
|
}
|
|
|
|
const paymentMethod = this.paymentMethodRepository.create({
|
|
restaurant,
|
|
...createPaymentMethodDto,
|
|
} as unknown as RequiredEntityData<PaymentMethod>);
|
|
await this.em.persistAndFlush(paymentMethod);
|
|
return paymentMethod;
|
|
}
|
|
|
|
async findAll(): Promise<PaymentMethod[]> {
|
|
return this.paymentMethodRepository.findAll({ populate: ['restaurant'] });
|
|
}
|
|
|
|
async findOne(id: string): Promise<PaymentMethod> {
|
|
const paymentMethod = await this.paymentMethodRepository.findOne({ id }, { populate: ['restaurant'] });
|
|
if (!paymentMethod) {
|
|
throw new NotFoundException(`PaymentMethod with ID ${id} not found`);
|
|
}
|
|
return paymentMethod;
|
|
}
|
|
|
|
async findByRestaurant(restaurantId: string): Promise<PaymentMethod[]> {
|
|
return this.paymentMethodRepository.find(
|
|
{ restaurant: { id: restaurantId } },
|
|
{ populate: ['restaurant'] },
|
|
);
|
|
}
|
|
|
|
async update(id: string, updatePaymentMethodDto: UpdatePaymentMethodDto): Promise<PaymentMethod> {
|
|
const paymentMethod = await this.findOne(id);
|
|
this.em.assign(paymentMethod, updatePaymentMethodDto);
|
|
await this.em.flush();
|
|
return paymentMethod;
|
|
}
|
|
|
|
async remove(id: string): Promise<PaymentMethod> {
|
|
const paymentMethod = await this.findOne(id);
|
|
await this.em.removeAndFlush(paymentMethod);
|
|
return paymentMethod;
|
|
}
|
|
}
|