import { Injectable } from '@nestjs/common'; import { CreateBranchMenuDto } from './dto/create-branch-menu.dto.js'; import { UpdateBranchMenuDto } from './dto/update-branch-menu.dto.js'; import { BranchService } from '../branch/branch.service.js'; import { MenuItemService } from '../menu-item/menu-item.service.js'; import { BranchMenuEntity } from '../../entities/branch-menu.entity.js'; import { FilterOperator, paginate, PaginateConfig, PaginateQuery, } from 'nestjs-paginate'; import { isUUID } from 'class-validator'; @Injectable() export class BranchMenusService { constructor( private readonly branchService: BranchService, private readonly menuItemService: MenuItemService, ) {} async create(branchId: string, createBranchMenuDto: CreateBranchMenuDto) { const branch = await this.branchService.getBranchOrError(branchId); const menuItem = await this.menuItemService.getMenuItemOrError( createBranchMenuDto.menu_id, ); if (createBranchMenuDto.description) { return await BranchMenuEntity.create({ ...createBranchMenuDto, branch_id: branchId, }).save(); } else { return await BranchMenuEntity.create({ ...createBranchMenuDto, branch_id: branchId, description: menuItem.description, }).save(); } } async findAll(branchId: string, query: PaginateQuery) { const paginateConfig: PaginateConfig = { sortableColumns: ['id', 'branch_id', 'menu_id', 'description'], nullSort: 'last', defaultSortBy: [['menu_item.item_type', 'ASC']], searchableColumns: ['description'], filterableColumns: { 'menu_item.price': [ FilterOperator.LT, FilterOperator.LTE, FilterOperator.GT, FilterOperator.GTE, ], 'menu_item.item_type': [FilterOperator.EQ, FilterOperator.IN], }, relations: ['menu_item'], }; return paginate( query, BranchMenuEntity.createQueryBuilder('bm') .leftJoinAndSelect('bm.menu_item', 'menu_item') .where('bm.branch_id = :branchId', { branchId: branchId }), paginateConfig, ); } async findOne(branchId: string, id: string) { if (isUUID(id)) return await BranchMenuEntity.findOneBy({ id }); else { return await BranchMenuEntity.findOne({ where: { branch_id: branchId, menu_id: id }, relations: ['menu_item'], }); } } update(id: number, updateBranchMenuDto: UpdateBranchMenuDto) { return `This action updates a #${id} branchMenu`; } remove(id: number) { return `This action removes a #${id} branchMenu`; } }