File size: 2,590 Bytes
f97bd0c
 
 
 
 
 
 
 
3367e34
f97bd0c
 
 
3367e34
95a812a
 
 
463569a
f97bd0c
3367e34
95a812a
f97bd0c
 
 
3367e34
 
 
 
 
463569a
3367e34
eb9e504
3367e34
95a812a
 
3367e34
 
 
 
463569a
3367e34
95a812a
3367e34
 
 
 
 
f97bd0c
 
95a812a
 
 
 
 
 
 
3367e34
 
95a812a
f97bd0c
 
 
 
 
 
95a812a
 
 
 
f97bd0c
 
 
 
 
 
95a812a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
} from '@nestjs/common';
import { OrderService } from './order.service.js';
import { CreateOrderDto } from './dto/create-order.dto.js';
import { Role } from '../../common/enums/role.enum.js';
import { paginate, Paginate, PaginateQuery } from 'nestjs-paginate';
import { Roles } from '../authentication/authorization/roles.decorator.js';
import { UpdateOrderDto } from './dto/update-order.dto.js';
import { Request } from 'express';

@Controller('branchs/:branchId/orders')
export class BranchOrderController {
  constructor(private readonly orderService: OrderService) {}

  @Post()
  async create(
    @Param('branchId') branchId: string,
    @Req() req: Request,
    @Body() createOrderDto: CreateOrderDto,
  ) {
    const ipAddr = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
    const userId = req['user'].sub;
    const role = req['user'].role;
    console.log(req['user']);
    if (role === Role.CUSTOMER) {
      console.log('customer');
      return this.orderService.createFromCustomer(
        branchId,
        userId,
        createOrderDto,
        ipAddr as string,
      );
    } else
      return this.orderService.createFromStaff(
        branchId,
        userId,
        createOrderDto,
      );
  }

  @Get('history')
  async findHistory(
    @Req() req: Request,
    @Param('branchId') branchId: string,
    @Paginate() paginateQuery: PaginateQuery,
  ) {
    // order history of user.
    const userId = req['user'].sub;
    console.log(req['user']);
    return this.orderService.findHistory(paginateQuery, userId, branchId);
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    return this.orderService.findOne(+id);
  }
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
    return this.orderService.updateOrder(+id, updateOrderDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.orderService.remove(+id);
  }
}

@Roles(Role.ADMIN, Role.STAFF, Role.BRANCH_MANAGER, Role.SHIPPER)
@Controller('orders')
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @Get()
  async findAll(@Paginate() paginateQuery: PaginateQuery) {
    return this.orderService.findAll(paginateQuery);
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    return this.orderService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
    return this.orderService.updateOrder(+id, updateOrderDto);
  }
}