File size: 2,669 Bytes
f97bd0c
 
 
 
 
 
7b1861f
 
 
 
 
 
f97bd0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b1861f
f97bd0c
 
7b1861f
 
 
 
 
 
 
f97bd0c
7b1861f
f97bd0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<BranchMenuEntity> = {
      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`;
  }
}