[16237c4] | 1 | import express from "express";
|
---|
| 2 | import expressAsyncHandler from "express-async-handler";
|
---|
| 3 | import Order from "../models/orderModel.js";
|
---|
| 4 | import bcrypt from "bcryptjs";
|
---|
| 5 | import { isAuth } from "../utils.js";
|
---|
| 6 |
|
---|
| 7 | const orderRouter = express.Router();
|
---|
| 8 |
|
---|
| 9 | orderRouter.post(
|
---|
| 10 | "/",
|
---|
| 11 | isAuth,
|
---|
| 12 | expressAsyncHandler(async (req, res) => {
|
---|
| 13 | const newOrder = new Order({
|
---|
| 14 | orderItems: req.body.orderItems.map((x) => ({ ...x, product: x._id })),
|
---|
| 15 | shippingAddress: req.body.shippingAddress,
|
---|
| 16 | paymentMethod: req.body.paymentMethod,
|
---|
| 17 | itemsPrice: req.body.itemsPrice,
|
---|
| 18 | shippingPrice: req.body.shippingPrice,
|
---|
| 19 | totalPrice: req.body.totalPrice,
|
---|
| 20 | user: req.user._id,
|
---|
| 21 | isPaid: req.body.isPaid,
|
---|
| 22 | paidAt: req.body.paidAt,
|
---|
[55ed171] | 23 | isConfirmed: req.body.isConfirmed,
|
---|
| 24 | contactNumber: req.body.contactNumber,
|
---|
[16237c4] | 25 | });
|
---|
| 26 | const order = await newOrder.save();
|
---|
| 27 | res.status(201).send({ message: "New Order Created", order });
|
---|
| 28 | })
|
---|
| 29 | );
|
---|
| 30 |
|
---|
| 31 | orderRouter.get(
|
---|
| 32 | "/mine",
|
---|
| 33 | isAuth,
|
---|
| 34 | expressAsyncHandler(async (req, res) => {
|
---|
| 35 | const orders = await Order.find({ user: req.user._id });
|
---|
| 36 | res.send(orders);
|
---|
| 37 | })
|
---|
| 38 | );
|
---|
[55ed171] | 39 | const PAGE_SIZE = 10;
|
---|
| 40 | orderRouter.get(
|
---|
| 41 | "/",
|
---|
| 42 | expressAsyncHandler(async (req, res) => {
|
---|
| 43 | const { query } = req;
|
---|
| 44 | const pageSize = query.pageSize || PAGE_SIZE;
|
---|
| 45 | const page = query.page || 1;
|
---|
| 46 | const isConfirmed = query.isConfirmed || "";
|
---|
| 47 | const isShipped = query.isShipped || "";
|
---|
| 48 | const searchQuery = query.query || "";
|
---|
| 49 | const isDelivered = false;
|
---|
| 50 | const queryFilter =
|
---|
| 51 | searchQuery && searchQuery !== "all"
|
---|
| 52 | ? { _: { $regex: searchQuery, $options: "i" } }
|
---|
| 53 | : {};
|
---|
| 54 | const isConfirmedFilter =
|
---|
| 55 | isConfirmed && isConfirmed !== "all" ? { isConfirmed } : {};
|
---|
| 56 | const isShippedFilter =
|
---|
| 57 | isShipped && isShipped !== "all" ? { isShipped } : {};
|
---|
| 58 |
|
---|
| 59 | const orders = await Order.find({
|
---|
| 60 | ...queryFilter,
|
---|
| 61 | ...isConfirmedFilter,
|
---|
| 62 | ...isShippedFilter,
|
---|
| 63 | ...isDelivered,
|
---|
| 64 | })
|
---|
| 65 | .skip(pageSize * (page - 1))
|
---|
| 66 | .limit(pageSize);
|
---|
| 67 | const countOrders = await Order.countDocuments({
|
---|
| 68 | ...isConfirmedFilter,
|
---|
| 69 | ...isShippedFilter,
|
---|
| 70 | });
|
---|
| 71 | res.send({
|
---|
| 72 | orders,
|
---|
| 73 | countOrders,
|
---|
| 74 | page,
|
---|
| 75 | pages: Math.ceil(countOrders / pageSize),
|
---|
| 76 | });
|
---|
| 77 | //const orders = await Order.find();
|
---|
| 78 | //res.send(orders);
|
---|
| 79 | })
|
---|
| 80 | );
|
---|
[16237c4] | 81 |
|
---|
| 82 | orderRouter.get(
|
---|
| 83 | "/:id",
|
---|
| 84 | isAuth,
|
---|
| 85 | expressAsyncHandler(async (req, res) => {
|
---|
| 86 | const order = await Order.findById(req.params.id);
|
---|
| 87 | if (order) {
|
---|
| 88 | res.send(order);
|
---|
| 89 | } else {
|
---|
| 90 | res.status(404).send({ message: "Order Not Found" });
|
---|
| 91 | }
|
---|
| 92 | })
|
---|
| 93 | );
|
---|
| 94 |
|
---|
[55ed171] | 95 | orderRouter.put(
|
---|
| 96 | "/action",
|
---|
| 97 | expressAsyncHandler(async (req, res) => {
|
---|
| 98 | if (req.body.isConfirmed) {
|
---|
| 99 | const order = await Order.updateOne(
|
---|
| 100 | { _id: req.body._id },
|
---|
| 101 | {
|
---|
| 102 | $set: {
|
---|
| 103 | isConfirmed: req.body.isConfirmed,
|
---|
| 104 | },
|
---|
| 105 | }
|
---|
| 106 | );
|
---|
| 107 | } else if (req.body.isShipped) {
|
---|
| 108 | const order = await Order.updateOne(
|
---|
| 109 | { _id: req.body._id },
|
---|
| 110 | {
|
---|
| 111 | $set: {
|
---|
| 112 | isShipped: req.body.isShipped,
|
---|
| 113 | shippedAt: req.body.shippedAt,
|
---|
| 114 | },
|
---|
| 115 | }
|
---|
| 116 | );
|
---|
| 117 | } else if (req.body.isDelivered && req.body.isPaid) {
|
---|
| 118 | const order = await Order.updateOne(
|
---|
| 119 | { _id: req.body._id },
|
---|
| 120 | {
|
---|
| 121 | $set: {
|
---|
| 122 | isDelivered: req.body.isDelivered,
|
---|
| 123 | deliveredAt: req.body.deliveredAt,
|
---|
| 124 | isPaid: req.body.isPaid,
|
---|
| 125 | paidAt: req.body.paidAt,
|
---|
| 126 | },
|
---|
| 127 | }
|
---|
| 128 | );
|
---|
| 129 | } else {
|
---|
| 130 | const order = await Order.updateOne(
|
---|
| 131 | { _id: req.body._id },
|
---|
| 132 | {
|
---|
| 133 | $set: {
|
---|
| 134 | isDelivered: req.body.isDelivered,
|
---|
| 135 | deliveredAt: req.body.deliveredAt,
|
---|
| 136 | },
|
---|
| 137 | }
|
---|
| 138 | );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | res.status(200).send({ message: "Succesful" });
|
---|
| 142 | })
|
---|
| 143 | );
|
---|
| 144 |
|
---|
[16237c4] | 145 | export default orderRouter;
|
---|