payment
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { PaymentMethod } from '../entities/payment-method.entity';
|
||||
import { PaymentMethodRepository } from '../repositories/payment-method.repository';
|
||||
import { CreatePaymentMethodInput } from '../dto/create-payment-method.input';
|
||||
import { UpdatePaymentMethodInput } from '../dto/update-payment-method.input';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
|
||||
@Injectable()
|
||||
export class PaymentMethodService {
|
||||
constructor(
|
||||
private readonly paymentMethodRepository: PaymentMethodRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async create(createPaymentMethodInput: CreatePaymentMethodInput): Promise<PaymentMethod> {
|
||||
const paymentMethod = this.paymentMethodRepository.create(
|
||||
createPaymentMethodInput as unknown as RequiredEntityData<PaymentMethod>,
|
||||
);
|
||||
await this.em.persistAndFlush(paymentMethod);
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
async findAll(): Promise<PaymentMethod[]> {
|
||||
return this.paymentMethodRepository.findAll();
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<PaymentMethod> {
|
||||
const paymentMethod = await this.paymentMethodRepository.findOne({ id });
|
||||
if (!paymentMethod) {
|
||||
throw new NotFoundException(`PaymentMethod with ID ${id} not found`);
|
||||
}
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
async update(id: string, updatePaymentMethodInput: UpdatePaymentMethodInput): Promise<PaymentMethod> {
|
||||
const paymentMethod = await this.findOne(id);
|
||||
this.em.assign(paymentMethod, updatePaymentMethodInput);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreatePaymentInput } from '../dto/create-payment.input';
|
||||
import { UpdatePaymentInput } from '../dto/update-payment.input';
|
||||
|
||||
@Injectable()
|
||||
export class PaymentsService {
|
||||
create(createPaymentInput: CreatePaymentInput) {
|
||||
return 'This action adds a new payment';
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all payments`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} payment`;
|
||||
}
|
||||
|
||||
update(id: number, updatePaymentInput: UpdatePaymentInput) {
|
||||
return `This action updates a #${id} payment`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} payment`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RestaurantPaymentMethod } from '../entities/restaurant-payment-method.entity';
|
||||
import { RestaurantPaymentMethodRepository } from '../repositories/restaurant-payment-method.repository';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { PaymentMethod } from '../entities/payment-method.entity';
|
||||
import { CreateRestaurantPaymentMethodInput } from '../dto/create-restaurant-payment-method.input';
|
||||
import { UpdateRestaurantPaymentMethodInput } from '../dto/update-restaurant-payment-method.input';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
|
||||
@Injectable()
|
||||
export class RestaurantPaymentMethodService {
|
||||
constructor(
|
||||
private readonly restaurantPaymentMethodRepository: RestaurantPaymentMethodRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async create(
|
||||
createRestaurantPaymentMethodInput: CreateRestaurantPaymentMethodInput,
|
||||
): Promise<RestaurantPaymentMethod> {
|
||||
// Check if restaurant exists
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: createRestaurantPaymentMethodInput.restaurantId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(`Restaurant with ID ${createRestaurantPaymentMethodInput.restaurantId} not found`);
|
||||
}
|
||||
|
||||
// Check if payment method exists
|
||||
const paymentMethod = await this.em.findOne(PaymentMethod, { id: createRestaurantPaymentMethodInput.paymentMethodId });
|
||||
if (!paymentMethod) {
|
||||
throw new NotFoundException(`PaymentMethod with ID ${createRestaurantPaymentMethodInput.paymentMethodId} not found`);
|
||||
}
|
||||
|
||||
// Check if the relationship already exists
|
||||
const existing = await this.restaurantPaymentMethodRepository.findOne({
|
||||
restaurant: { id: createRestaurantPaymentMethodInput.restaurantId },
|
||||
paymentMethod: { id: createRestaurantPaymentMethodInput.paymentMethodId },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
throw new ConflictException('This payment method is already assigned to this restaurant');
|
||||
}
|
||||
|
||||
const restaurantPaymentMethod = this.restaurantPaymentMethodRepository.create({
|
||||
restaurant,
|
||||
paymentMethod,
|
||||
merchantId: createRestaurantPaymentMethodInput.merchantId,
|
||||
isActive: createRestaurantPaymentMethodInput.isActive ?? true,
|
||||
} as unknown as RequiredEntityData<RestaurantPaymentMethod>);
|
||||
|
||||
await this.em.persistAndFlush(restaurantPaymentMethod);
|
||||
return restaurantPaymentMethod;
|
||||
}
|
||||
|
||||
async findAll(): Promise<RestaurantPaymentMethod[]> {
|
||||
return this.restaurantPaymentMethodRepository.findAll({ populate: ['restaurant', 'paymentMethod'] });
|
||||
}
|
||||
|
||||
async findByRestaurant(restaurantId: string): Promise<RestaurantPaymentMethod[]> {
|
||||
return this.restaurantPaymentMethodRepository.find(
|
||||
{ restaurant: { id: restaurantId } },
|
||||
{ populate: ['restaurant', 'paymentMethod'] },
|
||||
);
|
||||
}
|
||||
|
||||
async findByPaymentMethod(paymentMethodId: string): Promise<RestaurantPaymentMethod[]> {
|
||||
return this.restaurantPaymentMethodRepository.find(
|
||||
{ paymentMethod: { id: paymentMethodId } },
|
||||
{ populate: ['restaurant', 'paymentMethod'] },
|
||||
);
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<RestaurantPaymentMethod> {
|
||||
const restaurantPaymentMethod = await this.restaurantPaymentMethodRepository.findOne(
|
||||
{ id },
|
||||
{ populate: ['restaurant', 'paymentMethod'] },
|
||||
);
|
||||
if (!restaurantPaymentMethod) {
|
||||
throw new NotFoundException(`RestaurantPaymentMethod with ID ${id} not found`);
|
||||
}
|
||||
return restaurantPaymentMethod;
|
||||
}
|
||||
|
||||
async update(
|
||||
id: string,
|
||||
updateRestaurantPaymentMethodInput: UpdateRestaurantPaymentMethodInput,
|
||||
): Promise<RestaurantPaymentMethod> {
|
||||
const restaurantPaymentMethod = await this.findOne(id);
|
||||
|
||||
if (updateRestaurantPaymentMethodInput.restaurantId) {
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: updateRestaurantPaymentMethodInput.restaurantId });
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(`Restaurant with ID ${updateRestaurantPaymentMethodInput.restaurantId} not found`);
|
||||
}
|
||||
restaurantPaymentMethod.restaurant = restaurant;
|
||||
}
|
||||
|
||||
if (updateRestaurantPaymentMethodInput.paymentMethodId) {
|
||||
const paymentMethod = await this.em.findOne(PaymentMethod, { id: updateRestaurantPaymentMethodInput.paymentMethodId });
|
||||
if (!paymentMethod) {
|
||||
throw new NotFoundException(`PaymentMethod with ID ${updateRestaurantPaymentMethodInput.paymentMethodId} not found`);
|
||||
}
|
||||
|
||||
// Check if the new relationship already exists
|
||||
const existing = await this.restaurantPaymentMethodRepository.findOne({
|
||||
restaurant: { id: restaurantPaymentMethod.restaurant.id },
|
||||
paymentMethod: { id: updateRestaurantPaymentMethodInput.paymentMethodId },
|
||||
});
|
||||
|
||||
if (existing && existing.id !== id) {
|
||||
throw new ConflictException('This payment method is already assigned to this restaurant');
|
||||
}
|
||||
|
||||
restaurantPaymentMethod.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
// Update merchantId and isActive if provided
|
||||
if (updateRestaurantPaymentMethodInput.merchantId !== undefined) {
|
||||
restaurantPaymentMethod.merchantId = updateRestaurantPaymentMethodInput.merchantId;
|
||||
}
|
||||
|
||||
if (updateRestaurantPaymentMethodInput.isActive !== undefined) {
|
||||
restaurantPaymentMethod.isActive = updateRestaurantPaymentMethodInput.isActive;
|
||||
}
|
||||
|
||||
await this.em.flush();
|
||||
return restaurantPaymentMethod;
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<RestaurantPaymentMethod> {
|
||||
const restaurantPaymentMethod = await this.findOne(id);
|
||||
await this.em.removeAndFlush(restaurantPaymentMethod);
|
||||
return restaurantPaymentMethod;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user