/
order.controller.ts
89 lines (78 loc) · 2.5 KB
/
order.controller.ts
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
import Controller from "@/utils/interfaces/controller.interface";
import { NextFunction, Request, Response, Router } from "express";
import OrderService from "@/resources/order/order.service";
import {
CreateOrderInterface,
FetchOrderInterface,
FetchOrdersInterface,
} from "@/resources/order/order.interface";
import { StatusCodes } from "http-status-codes";
import HttpException from "@/utils/exceptions/HttpException";
import { isAdmin, loggedIn, validateResource } from "@/middlewares/index";
import {
createOrderSchema,
fetchOrderSchema,
} from "@/resources/order/order.validation";
class OrderController implements Controller {
public path = "/orders";
public router = Router();
private orderService = new OrderService();
constructor() {
this.initialiseRoutes();
}
private initialiseRoutes() {
this.router.post(
`${this.path}/create`,
[validateResource(createOrderSchema)],
this.createOrder
);
this.router.get(
`${this.path}/:orderId`,
[loggedIn, validateResource(fetchOrderSchema)],
this.fetchOrder
);
this.router.get(`${this.path}`, loggedIn, this.fetchOrders);
}
private createOrder = async (
req: Request<{}, {}, CreateOrderInterface>,
res: Response,
next: NextFunction
): Promise<Response | void> => {
const orderInput = req.body;
try {
const order = await this.orderService.createOrder(orderInput);
res.status(StatusCodes.CREATED).json(order);
} catch (e: any) {
next(new HttpException(StatusCodes.BAD_REQUEST, e.message));
}
};
private fetchOrder = async (
req: Request<FetchOrderInterface>,
res: Response,
next: NextFunction
): Promise<Response | void> => {
const { orderId } = req.params;
const { _id: userId } = res.locals.user;
try {
const order = await this.orderService.fetchOrder(orderId, userId);
res.status(StatusCodes.OK).json(order);
} catch (e: any) {
next(new HttpException(StatusCodes.BAD_REQUEST, e.message));
}
};
private fetchOrders = async (
req: Request<{}, {}, {}, FetchOrdersInterface>,
res: Response,
next: NextFunction
): Promise<Response | void> => {
const { _id: userId } = res.locals.user;
const queryOptions = req.query;
try {
const result = await this.orderService.fetchOrders(queryOptions, userId);
res.status(StatusCodes.OK).json(result);
} catch (e: any) {
next(new HttpException(StatusCodes.BAD_REQUEST, e.message));
}
};
}
export default OrderController;