-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Keeeyaan/master
sync repo
- Loading branch information
Showing
23 changed files
with
677 additions
and
154 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,91 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import Order from '../models/OrderModel.js' | ||
import User from '../models/UserModel.js' | ||
import Merchandise from '../models/MerchandiseModel.js'; | ||
import { StatusCodes } from "http-status-codes"; | ||
import Order from "../models/OrderModel.js"; | ||
import User from "../models/UserModel.js"; | ||
import Merchandise from "../models/MerchandiseModel.js"; | ||
import { nanoid } from "nanoid"; | ||
|
||
export const getAllOrders = async (req, res) => { | ||
const orders = await Order.find({}).sort({ orderDate: "desc" }); | ||
const page = parseInt(req.query.page) - 1 || 0; | ||
const limit = parseInt(req.query.limit) || 15; | ||
const search = req.query.search || ""; | ||
|
||
res.status(StatusCodes.OK).json({ orders }); | ||
const orders = await Order.find({ | ||
$or: [ | ||
{ orderId: { $regex: search, $options: "i" } }, | ||
{ orderStatus: { $regex: search, $options: "i" } }, | ||
], | ||
}) | ||
.skip(page * limit) | ||
.limit(limit) | ||
.populate("userId") | ||
.sort({ orderDate: "desc" }); | ||
|
||
const total = await User.countDocuments({ | ||
orderId: { $regex: search, $options: "i" }, | ||
orderStatus: { $regex: search, $options: "i" }, | ||
}); | ||
|
||
const response = { | ||
error: false, | ||
total, | ||
page: page + 1, | ||
limit, | ||
orders, | ||
}; | ||
|
||
res.status(StatusCodes.OK).json(response); | ||
}; | ||
|
||
export const getOrderById = async (req, res) => { | ||
if (!req.user.isAdmin) throw new UnauthorizedError("Unauthorized!"); | ||
|
||
const order = await Order.findOne({ orderId: req.params.orderId }); | ||
|
||
res.status(StatusCodes.OK).json({ order }); | ||
}; | ||
|
||
export const getCurrentUserOrders = async (req, res) => { | ||
const userOrders = await Order.find({ userId: req.params.userId }).sort({ orderDate: "desc" }); | ||
|
||
const userOrders = await Order.find({ userId: req.params.userId }).sort({ | ||
orderDate: "desc", | ||
}); | ||
|
||
res.status(StatusCodes.OK).json({ userOrders }); | ||
} | ||
}; | ||
|
||
export const createOrder = async (req, res) => { | ||
let newBody = {...req.body}; | ||
let newBody = { ...req.body }; | ||
const userId = req.params.userId; | ||
const randomID = nanoid(5); | ||
const user = await User.findOne({ _id: req.params.userId }); | ||
|
||
user.cart = []; | ||
|
||
await user.save(); | ||
|
||
newBody.userId = userId; | ||
newBody.orderId = randomID; | ||
|
||
await Order.create(newBody); | ||
res.status(StatusCodes.OK).json({ msg: "Order created!" }); | ||
} | ||
}; | ||
|
||
export const updateOrder = async (req, res) => { | ||
const { orderStatus, additionalInfo, orderId } = req.body; | ||
|
||
const updatedOrder = await Order.findOne({ _id: orderId }); | ||
|
||
if (orderStatus) updatedOrder.orderStatus = orderStatus; | ||
if (additionalInfo) updatedOrder.additionalInfo = additionalInfo; | ||
|
||
if (updatedOrder.orderStatus == "CLAIMED") { | ||
let merch = null | ||
updatedOrder.cartItems.forEach(async item => { | ||
let merch = null; | ||
updatedOrder.cartItems.forEach(async (item) => { | ||
merch = await Merchandise.findOne({ _id: item.merchId }); | ||
merch.stocks = merch.stocks - 1; | ||
await merch.save(); | ||
}) | ||
}); | ||
} | ||
|
||
await updatedOrder.save(); | ||
res.status(StatusCodes.OK).json({ msg: "Order Updated!" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// import mongoose from "mongoose"; | ||
|
||
// const ItemModel = new mongoose.Schema({ | ||
// merchId: { | ||
// type: Schema.Types.ObjectId, | ||
// ref: "Merchandise", | ||
// required: true, | ||
// }, | ||
// price: { | ||
// type: String, | ||
// require: true, | ||
// }, | ||
// quantity: { | ||
// type: Number, | ||
// require: true, | ||
// }, | ||
// size: { | ||
// type: String, | ||
// require: true, | ||
// }, | ||
// color: String, | ||
// image: String, | ||
// name: String, | ||
// }); | ||
|
||
// export default mongoose.model("Item", ItemModel); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
import { getCurrentUserOrders, getAllOrders, createOrder, updateOrder } from "../controllers/orderController.js"; | ||
import { | ||
getCurrentUserOrders, | ||
getAllOrders, | ||
createOrder, | ||
updateOrder, | ||
getOrderById, | ||
} from "../controllers/orderController.js"; | ||
import { authenticateUser } from "../middlewares/authMiddleware.js"; | ||
import { Router } from "express"; | ||
const router = Router(); | ||
|
||
router.route("/").get(authenticateUser,getAllOrders) | ||
router.route("/").get(authenticateUser, getAllOrders); | ||
|
||
router | ||
.route("/:userId") | ||
.get(authenticateUser,getCurrentUserOrders) | ||
.patch(authenticateUser,updateOrder) | ||
.post(authenticateUser,createOrder) | ||
.get(authenticateUser, getCurrentUserOrders) | ||
.patch(authenticateUser, updateOrder) | ||
.post(authenticateUser, createOrder); | ||
|
||
router.route("/single/:orderId").get(authenticateUser, getOrderById); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.