Trần Viết Sơn
feat: order
463569a
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);
}
}