import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { CreateRestaurantDto } from '../dto/create-restaurant.dto'; import { UpdateRestaurantDto } from '../dto/update-restaurant.dto'; import { Restaurant } from '../entities/restaurant.entity'; import { EntityManager } from '@mikro-orm/postgresql'; import { RestRepository } from '../repositories/rest.repository'; import { RestMessage } from 'src/common/enums/message.enum'; import { FindRestaurantsDto } from '../dto/find-restaurants.dto'; import { PaginatedResult } from 'src/common/interfaces/pagination.interface'; import { PlanEnum } from '../interface/plan.interface'; @Injectable() export class RestaurantsService { constructor( private readonly em: EntityManager, private readonly restRepository: RestRepository, ) { } async create(dto: CreateRestaurantDto): Promise { const validateSlug = await this.em.findOne(Restaurant, { slug: dto.slug }) if (validateSlug) { throw new BadRequestException("Duplicate slug: " + dto.slug) } const validateSubscriptionId = await this.em.findOne(Restaurant, { subscriptionId: dto.subscriptionId }) if (validateSubscriptionId) { throw new BadRequestException("Duplicate subscriptionId: " + dto.subscriptionId) } const restaurant = this.em.create(Restaurant, { name: dto.name, slug: dto.slug, establishedYear: dto.establishedYear, phone: dto.phone, isActive: true, plan: PlanEnum.Base, domain: `https://dmenu-plus-front.dev.danakcorp.com/${dto.slug}`, subscriptionId: dto.subscriptionId, subscriptionEndDate: dto.subscriptionEndDate, subscriptionStartDate: dto.subscriptionStartDate, }); await this.em.persistAndFlush(restaurant); return restaurant; } async findAll(dto: FindRestaurantsDto): Promise> { return this.restRepository.findAllPaginated({ page: dto.page, limit: dto.limit, search: dto.search, isActive: dto.isActive, plan: dto.plan, orderBy: dto.orderBy, order: dto.order, }); } async findBySlug(slug: string): Promise { const restaurant = await this.em.findOne(Restaurant, { slug }); if (!restaurant) { throw new NotFoundException(`Restaurant with slug "${slug}" not found`); } return restaurant; } async findOne(id: string): Promise { const restaurant = await this.restRepository.findOne({ id }); if (!restaurant) { throw new NotFoundException(RestMessage.NOT_FOUND); } return restaurant; } async findOneBySubscriptionId(subscriptionId: string): Promise { const restaurant = await this.restRepository.findOne({ subscriptionId }); if (!restaurant) { throw new NotFoundException(RestMessage.NOT_FOUND); } return restaurant; } async getRestaurantSpecification(slug: string) { const restaurant = await this.findBySlug(slug); return restaurant; } async update(id: string, dto: UpdateRestaurantDto): Promise { const restaurant = await this.restRepository.findOne({ id }); if (!restaurant) { throw new NotFoundException(RestMessage.NOT_FOUND); } this.restRepository.assign(restaurant, dto); await this.em.persistAndFlush(restaurant); return restaurant; } async remove(id: string) { // Soft delete the restaurant by setting isActive to false const restaurant = await this.restRepository.findOne({ id }); if (!restaurant) { throw new NotFoundException(RestMessage.NOT_FOUND); } restaurant.isActive = false; await this.em.persistAndFlush(restaurant); return restaurant; } }