Trần Viết Sơn commited on
Commit
3367e34
1 Parent(s): f97bd0c

feat: all thing about order

Browse files
backend/src/entities/order-item.entity.ts CHANGED
@@ -16,7 +16,7 @@ export class OrderItemEntity extends BaseEntity {
16
  id: number;
17
 
18
  @Column()
19
- order_id: string;
20
 
21
  @ManyToOne(() => OrderEntity, (a) => a.order_items)
22
  @JoinColumn({ name: 'order_id' })
@@ -26,7 +26,7 @@ export class OrderItemEntity extends BaseEntity {
26
  branch_menu_id: string;
27
 
28
  @ManyToOne(() => BranchMenuEntity)
29
- @JoinColumn({ name: 'menu_id' })
30
  branch_menu: Relation<BranchMenuEntity>;
31
 
32
  @Column()
 
16
  id: number;
17
 
18
  @Column()
19
+ order_id: number;
20
 
21
  @ManyToOne(() => OrderEntity, (a) => a.order_items)
22
  @JoinColumn({ name: 'order_id' })
 
26
  branch_menu_id: string;
27
 
28
  @ManyToOne(() => BranchMenuEntity)
29
+ @JoinColumn({ name: 'branch_menu_id' })
30
  branch_menu: Relation<BranchMenuEntity>;
31
 
32
  @Column()
backend/src/entities/order.entity.ts CHANGED
@@ -26,6 +26,7 @@ export class OrderEntity extends BaseEntity {
26
  customer_id: string;
27
 
28
  @ManyToOne(() => UserEntity, { nullable: true })
 
29
  customer: Relation<UserEntity>;
30
 
31
  @Column()
@@ -35,12 +36,12 @@ export class OrderEntity extends BaseEntity {
35
  @JoinColumn({ name: 'branch_id' })
36
  branch: Relation<BranchEntity>;
37
 
38
- @Column()
39
- employee_id: string;
40
 
41
- @ManyToOne(() => UserEntity)
42
- @JoinColumn({ name: 'employee_id' })
43
- employee: Relation<UserEntity>;
44
 
45
  @Column({ nullable: true })
46
  table_number: number;
@@ -64,6 +65,6 @@ export class OrderEntity extends BaseEntity {
64
  payment_id: number;
65
 
66
  @OneToOne(() => PaymentEntity, (a) => a.order)
67
- @JoinColumn()
68
  payment: Relation<PaymentEntity>;
69
  }
 
26
  customer_id: string;
27
 
28
  @ManyToOne(() => UserEntity, { nullable: true })
29
+ @JoinColumn({ name: 'customer_id' })
30
  customer: Relation<UserEntity>;
31
 
32
  @Column()
 
36
  @JoinColumn({ name: 'branch_id' })
37
  branch: Relation<BranchEntity>;
38
 
39
+ @Column({ nullable: true })
40
+ staff_id: string;
41
 
42
+ @ManyToOne(() => UserEntity, { nullable: true })
43
+ @JoinColumn({ name: 'staff_id' })
44
+ staff: Relation<UserEntity>;
45
 
46
  @Column({ nullable: true })
47
  table_number: number;
 
65
  payment_id: number;
66
 
67
  @OneToOne(() => PaymentEntity, (a) => a.order)
68
+ @JoinColumn({ name: 'payment_id' })
69
  payment: Relation<PaymentEntity>;
70
  }
backend/src/migrations/{1730432721785-RefactorAll.ts → 1730474673934-RefactorAll.ts} RENAMED
@@ -1,7 +1,7 @@
1
  import { MigrationInterface, QueryRunner } from "typeorm";
2
 
3
- export class RefactorAll1730432721785 implements MigrationInterface {
4
- name = 'RefactorAll1730432721785'
5
 
6
  public async up(queryRunner: QueryRunner): Promise<void> {
7
  await queryRunner.query(`CREATE TABLE "feeds" ("id" SERIAL NOT NULL, "author_id" character varying, "image_url" character varying, "title" character varying NOT NULL, "description" character varying, "create_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_3dafbf766ecbb1eb2017732153f" PRIMARY KEY ("id"))`);
@@ -15,26 +15,26 @@ export class RefactorAll1730432721785 implements MigrationInterface {
15
  await queryRunner.query(`CREATE TABLE "payments" ("id" SERIAL NOT NULL, "payment_method" "public"."payments_payment_method_enum" NOT NULL DEFAULT 'cash', "value" integer NOT NULL, CONSTRAINT "PK_197ab7af18c93fbb0c9b28b4a59" PRIMARY KEY ("id"))`);
16
  await queryRunner.query(`CREATE TYPE "public"."orders_order_type_enum" AS ENUM('take_away', 'offline', 'online')`);
17
  await queryRunner.query(`CREATE TYPE "public"."orders_order_status_enum" AS ENUM('pending', 'confirmed', 'preparing', 'delivering', 'done')`);
18
- await queryRunner.query(`CREATE TABLE "orders" ("id" SERIAL NOT NULL, "customer_id" character varying, "branch_id" character varying NOT NULL, "employee_id" uuid NOT NULL, "table_number" integer, "total_value" integer NOT NULL, "create_at" TIMESTAMP NOT NULL DEFAULT now(), "order_type" "public"."orders_order_type_enum" NOT NULL DEFAULT 'online', "order_status" "public"."orders_order_status_enum" NOT NULL DEFAULT 'pending', "payment_id" integer, "customerId" uuid, "paymentId" integer, CONSTRAINT "REL_06a051324c76276ca2a9d1feb0" UNIQUE ("paymentId"), CONSTRAINT "PK_710e2d4957aa5878dfe94e4ac2f" PRIMARY KEY ("id"))`);
19
- await queryRunner.query(`CREATE TABLE "order_items" ("id" SERIAL NOT NULL, "order_id" integer NOT NULL, "branch_menu_id" character varying NOT NULL, "quantity" integer NOT NULL, "price" integer NOT NULL, "menu_id" uuid, CONSTRAINT "PK_005269d8574e6fac0493715c308" PRIMARY KEY ("id"))`);
20
  await queryRunner.query(`ALTER TABLE "branches" ADD CONSTRAINT "FK_8c6ae9f9c654c4fac71bccbb7ed" FOREIGN KEY ("ownerId") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
21
  await queryRunner.query(`ALTER TABLE "branch_menu" ADD CONSTRAINT "FK_96fd74bed807987cf2ee5d8f168" FOREIGN KEY ("branch_id") REFERENCES "branches"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
22
  await queryRunner.query(`ALTER TABLE "branch_menu" ADD CONSTRAINT "FK_703aa953158d2e80f3fbb0eb9ea" FOREIGN KEY ("menu_id") REFERENCES "menu_items"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
23
- await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_e5de51ca888d8b1f5ac25799dd1" FOREIGN KEY ("customerId") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
24
  await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_17b723da2c12837f4bc21e33398" FOREIGN KEY ("branch_id") REFERENCES "branches"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
25
- await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_f8a7411077c731327ca6e0b93b6" FOREIGN KEY ("employee_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
26
- await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_06a051324c76276ca2a9d1feb08" FOREIGN KEY ("paymentId") REFERENCES "payments"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
27
  await queryRunner.query(`ALTER TABLE "order_items" ADD CONSTRAINT "FK_145532db85752b29c57d2b7b1f1" FOREIGN KEY ("order_id") REFERENCES "orders"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
28
- await queryRunner.query(`ALTER TABLE "order_items" ADD CONSTRAINT "FK_564d2ba92c6bc3ec79c874e1458" FOREIGN KEY ("menu_id") REFERENCES "branch_menu"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
29
  }
30
 
31
  public async down(queryRunner: QueryRunner): Promise<void> {
32
- await queryRunner.query(`ALTER TABLE "order_items" DROP CONSTRAINT "FK_564d2ba92c6bc3ec79c874e1458"`);
33
  await queryRunner.query(`ALTER TABLE "order_items" DROP CONSTRAINT "FK_145532db85752b29c57d2b7b1f1"`);
34
- await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_06a051324c76276ca2a9d1feb08"`);
35
- await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_f8a7411077c731327ca6e0b93b6"`);
36
  await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_17b723da2c12837f4bc21e33398"`);
37
- await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_e5de51ca888d8b1f5ac25799dd1"`);
38
  await queryRunner.query(`ALTER TABLE "branch_menu" DROP CONSTRAINT "FK_703aa953158d2e80f3fbb0eb9ea"`);
39
  await queryRunner.query(`ALTER TABLE "branch_menu" DROP CONSTRAINT "FK_96fd74bed807987cf2ee5d8f168"`);
40
  await queryRunner.query(`ALTER TABLE "branches" DROP CONSTRAINT "FK_8c6ae9f9c654c4fac71bccbb7ed"`);
 
1
  import { MigrationInterface, QueryRunner } from "typeorm";
2
 
3
+ export class RefactorAll1730474673934 implements MigrationInterface {
4
+ name = 'RefactorAll1730474673934'
5
 
6
  public async up(queryRunner: QueryRunner): Promise<void> {
7
  await queryRunner.query(`CREATE TABLE "feeds" ("id" SERIAL NOT NULL, "author_id" character varying, "image_url" character varying, "title" character varying NOT NULL, "description" character varying, "create_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_3dafbf766ecbb1eb2017732153f" PRIMARY KEY ("id"))`);
 
15
  await queryRunner.query(`CREATE TABLE "payments" ("id" SERIAL NOT NULL, "payment_method" "public"."payments_payment_method_enum" NOT NULL DEFAULT 'cash', "value" integer NOT NULL, CONSTRAINT "PK_197ab7af18c93fbb0c9b28b4a59" PRIMARY KEY ("id"))`);
16
  await queryRunner.query(`CREATE TYPE "public"."orders_order_type_enum" AS ENUM('take_away', 'offline', 'online')`);
17
  await queryRunner.query(`CREATE TYPE "public"."orders_order_status_enum" AS ENUM('pending', 'confirmed', 'preparing', 'delivering', 'done')`);
18
+ await queryRunner.query(`CREATE TABLE "orders" ("id" SERIAL NOT NULL, "customer_id" uuid, "branch_id" character varying NOT NULL, "staff_id" uuid, "table_number" integer, "total_value" integer NOT NULL, "create_at" TIMESTAMP NOT NULL DEFAULT now(), "order_type" "public"."orders_order_type_enum" NOT NULL DEFAULT 'online', "order_status" "public"."orders_order_status_enum" NOT NULL DEFAULT 'pending', "payment_id" integer, CONSTRAINT "REL_5b3e94bd2aedc184f9ad8c1043" UNIQUE ("payment_id"), CONSTRAINT "PK_710e2d4957aa5878dfe94e4ac2f" PRIMARY KEY ("id"))`);
19
+ await queryRunner.query(`CREATE TABLE "order_items" ("id" SERIAL NOT NULL, "order_id" integer NOT NULL, "branch_menu_id" uuid NOT NULL, "quantity" integer NOT NULL, "price" integer NOT NULL, CONSTRAINT "PK_005269d8574e6fac0493715c308" PRIMARY KEY ("id"))`);
20
  await queryRunner.query(`ALTER TABLE "branches" ADD CONSTRAINT "FK_8c6ae9f9c654c4fac71bccbb7ed" FOREIGN KEY ("ownerId") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
21
  await queryRunner.query(`ALTER TABLE "branch_menu" ADD CONSTRAINT "FK_96fd74bed807987cf2ee5d8f168" FOREIGN KEY ("branch_id") REFERENCES "branches"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
22
  await queryRunner.query(`ALTER TABLE "branch_menu" ADD CONSTRAINT "FK_703aa953158d2e80f3fbb0eb9ea" FOREIGN KEY ("menu_id") REFERENCES "menu_items"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
23
+ await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_772d0ce0473ac2ccfa26060dbe9" FOREIGN KEY ("customer_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
24
  await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_17b723da2c12837f4bc21e33398" FOREIGN KEY ("branch_id") REFERENCES "branches"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
25
+ await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_40337bbb0e0cc7113dc3037fc60" FOREIGN KEY ("staff_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
26
+ await queryRunner.query(`ALTER TABLE "orders" ADD CONSTRAINT "FK_5b3e94bd2aedc184f9ad8c10439" FOREIGN KEY ("payment_id") REFERENCES "payments"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
27
  await queryRunner.query(`ALTER TABLE "order_items" ADD CONSTRAINT "FK_145532db85752b29c57d2b7b1f1" FOREIGN KEY ("order_id") REFERENCES "orders"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
28
+ await queryRunner.query(`ALTER TABLE "order_items" ADD CONSTRAINT "FK_927879f38b3098216737427d2f0" FOREIGN KEY ("branch_menu_id") REFERENCES "branch_menu"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
29
  }
30
 
31
  public async down(queryRunner: QueryRunner): Promise<void> {
32
+ await queryRunner.query(`ALTER TABLE "order_items" DROP CONSTRAINT "FK_927879f38b3098216737427d2f0"`);
33
  await queryRunner.query(`ALTER TABLE "order_items" DROP CONSTRAINT "FK_145532db85752b29c57d2b7b1f1"`);
34
+ await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_5b3e94bd2aedc184f9ad8c10439"`);
35
+ await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_40337bbb0e0cc7113dc3037fc60"`);
36
  await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_17b723da2c12837f4bc21e33398"`);
37
+ await queryRunner.query(`ALTER TABLE "orders" DROP CONSTRAINT "FK_772d0ce0473ac2ccfa26060dbe9"`);
38
  await queryRunner.query(`ALTER TABLE "branch_menu" DROP CONSTRAINT "FK_703aa953158d2e80f3fbb0eb9ea"`);
39
  await queryRunner.query(`ALTER TABLE "branch_menu" DROP CONSTRAINT "FK_96fd74bed807987cf2ee5d8f168"`);
40
  await queryRunner.query(`ALTER TABLE "branches" DROP CONSTRAINT "FK_8c6ae9f9c654c4fac71bccbb7ed"`);
backend/src/modules/order/dto/create-order.dto.ts CHANGED
@@ -1,24 +1,26 @@
1
- import { IsEnum, IsNumber, IsOptional, IsString } from 'class-validator';
 
 
 
 
 
 
 
 
2
  import { OrderType } from '../../../common/enums/OrderType.enum.js';
3
  import { OrderItemsDto } from './order-items.dto.js';
 
4
 
5
  export class CreateOrderDto {
6
- @IsString()
7
- customer_id: string;
8
-
9
- @IsString()
10
- branch_id: string;
11
-
12
- @IsString()
13
- employee_id: string;
14
-
15
  @IsOptional()
16
- @IsString()
17
  table_number?: number;
18
 
19
  @IsEnum(OrderType)
20
  order_type: OrderType;
21
 
22
-
 
 
23
  order_items: OrderItemsDto[];
24
  }
 
1
+ import {
2
+ IsArray,
3
+ IsEnum,
4
+ IsNumber,
5
+ IsObject,
6
+ IsOptional,
7
+ IsString,
8
+ ValidateNested,
9
+ } from 'class-validator';
10
  import { OrderType } from '../../../common/enums/OrderType.enum.js';
11
  import { OrderItemsDto } from './order-items.dto.js';
12
+ import { Type } from 'class-transformer';
13
 
14
  export class CreateOrderDto {
 
 
 
 
 
 
 
 
 
15
  @IsOptional()
16
+ @IsNumber()
17
  table_number?: number;
18
 
19
  @IsEnum(OrderType)
20
  order_type: OrderType;
21
 
22
+ @IsArray()
23
+ @ValidateNested()
24
+ @Type(() => OrderItemsDto)
25
  order_items: OrderItemsDto[];
26
  }
backend/src/modules/order/dto/order-items.dto.ts CHANGED
@@ -1,3 +1,9 @@
 
 
1
  export class OrderItemsDto {
2
-
 
 
 
 
3
  }
 
1
+ import { IsNumber, IsString } from 'class-validator';
2
+
3
  export class OrderItemsDto {
4
+ @IsString()
5
+ menu_id: string;
6
+
7
+ @IsNumber()
8
+ quantity: number;
9
  }
backend/src/modules/order/order.controller.ts CHANGED
@@ -6,21 +6,43 @@ import {
6
  Patch,
7
  Param,
8
  Delete,
 
9
  } from '@nestjs/common';
10
  import { OrderService } from './order.service.js';
11
  import { CreateOrderDto } from './dto/create-order.dto.js';
 
12
 
13
- @Controller('order')
14
  export class OrderController {
15
  constructor(private readonly orderService: OrderService) {}
16
 
17
  @Post()
18
- async create(@Body() createOrderDto: CreateOrderDto) {
19
- return this.orderService.create(createOrderDto);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  }
21
 
22
  @Get()
23
- async findAll() {
 
 
24
  return this.orderService.findAll();
25
  }
26
 
 
6
  Patch,
7
  Param,
8
  Delete,
9
+ Req,
10
  } from '@nestjs/common';
11
  import { OrderService } from './order.service.js';
12
  import { CreateOrderDto } from './dto/create-order.dto.js';
13
+ import { Role } from '../../common/enums/role.enum.js';
14
 
15
+ @Controller('branchs/:branchId/orders')
16
  export class OrderController {
17
  constructor(private readonly orderService: OrderService) {}
18
 
19
  @Post()
20
+ async create(
21
+ @Param('branchId') branchId: string,
22
+ @Req() req: Request,
23
+ @Body() createOrderDto: CreateOrderDto,
24
+ ) {
25
+ const userId = req['user'].sub;
26
+ const role = req['user'].roles;
27
+ console.log(req['user']);
28
+ if (role == Role.CUSTOMER)
29
+ return this.orderService.createFromCustomer(
30
+ branchId,
31
+ userId,
32
+ createOrderDto,
33
+ );
34
+ else
35
+ return this.orderService.createFromStaff(
36
+ branchId,
37
+ userId,
38
+ createOrderDto,
39
+ );
40
  }
41
 
42
  @Get()
43
+ async findAll(@Req() req: Request) {
44
+ const userId = req['user'].sub;
45
+ console.log(req['user']);
46
  return this.orderService.findAll();
47
  }
48
 
backend/src/modules/order/order.module.ts CHANGED
@@ -1,8 +1,11 @@
1
  import { Module } from '@nestjs/common';
2
  import { OrderService } from './order.service.js';
3
  import { OrderController } from './order.controller.js';
 
 
4
 
5
  @Module({
 
6
  controllers: [OrderController],
7
  providers: [OrderService],
8
  })
 
1
  import { Module } from '@nestjs/common';
2
  import { OrderService } from './order.service.js';
3
  import { OrderController } from './order.controller.js';
4
+ import { BranchModule } from '../branch/branch.module.js';
5
+ import { BranchMenusModule } from '../branch-menus/branch-menus.module.js';
6
 
7
  @Module({
8
+ imports: [BranchModule, BranchMenusModule],
9
  controllers: [OrderController],
10
  providers: [OrderService],
11
  })
backend/src/modules/order/order.service.ts CHANGED
@@ -1,10 +1,126 @@
1
- import { Injectable } from '@nestjs/common';
2
  import { CreateOrderDto } from './dto/create-order.dto.js';
 
 
 
 
 
 
 
 
3
 
4
  @Injectable()
5
  export class OrderService {
6
- create(createOrderDto: CreateOrderDto) {
7
- return 'This action adds a new order';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  }
9
 
10
  findAll() {
 
1
+ import { BadRequestException, Injectable } from '@nestjs/common';
2
  import { CreateOrderDto } from './dto/create-order.dto.js';
3
+ import { UserEntity } from '../../entities/user.entity.js';
4
+ import { OrderEntity } from '../../entities/order.entity.js';
5
+ import { BranchService } from '../branch/branch.service.js';
6
+ import { OrderType } from '../../common/enums/OrderType.enum.js';
7
+ import { isUUID } from 'class-validator';
8
+ import { OrderItemEntity } from '../../entities/order-item.entity.js';
9
+ import { BranchMenuEntity } from '../../entities/branch-menu.entity.js';
10
+ import { OrderStatus } from '../../common/enums/OrderStatus.enum.js';
11
 
12
  @Injectable()
13
  export class OrderService {
14
+ constructor(private readonly branchService: BranchService) {}
15
+ async createFromCustomer(
16
+ branchId: string,
17
+ userId: string,
18
+ createOrderDto: CreateOrderDto,
19
+ ) {
20
+ console.log('??');
21
+ if (createOrderDto.order_type != OrderType.ONLINE) {
22
+ throw new BadRequestException('customer cannot create offline order');
23
+ }
24
+
25
+ const user = await UserEntity.findOneBy({ id: userId });
26
+ if (!user) {
27
+ throw new BadRequestException('User not found');
28
+ }
29
+ const branch = await this.branchService.getBranchOrError(branchId);
30
+ if (!branch) {
31
+ throw new BadRequestException('Branch not found');
32
+ }
33
+ const order = OrderEntity.create();
34
+ order.branch = branch;
35
+ order.customer = user;
36
+ order.order_type = createOrderDto.order_type;
37
+ order.order_status = OrderStatus.PENDING;
38
+ order.total_value = 0;
39
+ await order.save();
40
+
41
+ let orderItems: OrderItemEntity[] = [];
42
+ let totalValue = 0;
43
+ for (const item of createOrderDto.order_items) {
44
+ let branchMenu: BranchMenuEntity;
45
+ if (!isUUID(item.menu_id)) {
46
+ branchMenu = await BranchMenuEntity.findOne({
47
+ where: { branch_id: branchId, menu_id: item.menu_id },
48
+ relations: ['menu_item'],
49
+ });
50
+ } else {
51
+ branchMenu = await BranchMenuEntity.findOne({
52
+ where: { branch_id: branchId, id: item.menu_id },
53
+ relations: ['menu_item'],
54
+ });
55
+ }
56
+ if (!branchMenu) {
57
+ throw new BadRequestException('Item not found in branch menu');
58
+ }
59
+ const orderItem = OrderItemEntity.create();
60
+ orderItem.branch_menu = branchMenu;
61
+ orderItem.price = branchMenu.menu_item.price;
62
+ orderItem.quantity = item.quantity;
63
+ orderItem.order_id = order.id;
64
+ totalValue += orderItem.price * orderItem.quantity;
65
+ orderItems.push(orderItem);
66
+ }
67
+ await order.save();
68
+ order.total_value = totalValue;
69
+ await OrderItemEntity.save(orderItems);
70
+ return { ...order, order_items: orderItems };
71
+ }
72
+
73
+ async createFromStaff(
74
+ branchId: string,
75
+ userId: string,
76
+ createOrderDto: CreateOrderDto,
77
+ ) {
78
+ if (createOrderDto.order_type == OrderType.ONLINE) {
79
+ throw new BadRequestException('staff cannot create online order');
80
+ }
81
+ // staff
82
+ const staff = await UserEntity.findOneBy({ id: userId });
83
+ const branch = await this.branchService.getBranchOrError(branchId);
84
+ const order = OrderEntity.create();
85
+
86
+ order.branch = branch;
87
+ order.staff = staff;
88
+ order.order_type = createOrderDto.order_type;
89
+ order.table_number = createOrderDto.table_number;
90
+ order.order_status = OrderStatus.PREPARING;
91
+ order.total_value = 0;
92
+ await order.save();
93
+
94
+ let orderItems: OrderItemEntity[] = [];
95
+ let totalValue = 0;
96
+ for (const item of createOrderDto.order_items) {
97
+ let branchMenu: BranchMenuEntity;
98
+ if (!isUUID(item.menu_id)) {
99
+ branchMenu = await BranchMenuEntity.findOne({
100
+ where: { branch_id: branchId, menu_id: item.menu_id },
101
+ relations: ['menu_item'],
102
+ });
103
+ } else {
104
+ branchMenu = await BranchMenuEntity.findOne({
105
+ where: { branch_id: branchId, id: item.menu_id },
106
+ relations: ['menu_item'],
107
+ });
108
+ }
109
+ if (!branchMenu) {
110
+ throw new BadRequestException('Item not found in branch menu');
111
+ }
112
+ const orderItem = OrderItemEntity.create();
113
+ orderItem.branch_menu = branchMenu;
114
+ orderItem.price = branchMenu.menu_item.price;
115
+ orderItem.quantity = item.quantity;
116
+ orderItem.order_id = order.id;
117
+ totalValue += orderItem.price * orderItem.quantity;
118
+ orderItems.push(orderItem);
119
+ }
120
+ await order.save();
121
+ order.total_value = totalValue;
122
+ await OrderItemEntity.save(orderItems);
123
+ return { ...order, order_items: orderItems };
124
  }
125
 
126
  findAll() {