Skip to content

Commit

Permalink
Merge pull request #44 from Keeeyaan/master
Browse files Browse the repository at this point in the history
sync repo
  • Loading branch information
Keeeyaan authored Feb 2, 2024
2 parents ca79384 + 5e648f6 commit e3cf49a
Show file tree
Hide file tree
Showing 23 changed files with 677 additions and 154 deletions.
23 changes: 23 additions & 0 deletions backend/psits_backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/psits_backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"jsonwebtoken": "^9.0.1",
"mongoose": "^7.4.2",
"multer": "^1.4.5-lts.1",
"nanoid": "^5.0.4",
"nodemailer": "^6.9.4",
"qrcode": "^1.5.3",
"sharp": "^0.32.5"
Expand Down
72 changes: 56 additions & 16 deletions backend/psits_backend/src/controllers/orderController.js
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!" });
}
};
16 changes: 13 additions & 3 deletions backend/psits_backend/src/controllers/statController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ import Event from "../models/EventModel.js";
import Merchandise from "../models/MerchandiseModel.js";

export const getAllStats = async (req, res) => {
if (!req.user.isAdmin) throw new UnauthorizedError("Unauthorized!");

const users = await User.countDocuments();
const orders = await Order.countDocuments();
const announcements = await Announcement.countDocuments();
const events = await Event.countDocuments();
const merchandise = await Merchandise.countDocuments();

const claimedOrders = await Order.find({ orderStatus: "CLAIMED" }).select(
"cartItems"
);

let totalEarnings = 0;

claimedOrders.forEach((order) => {
order.cartItems.forEach((item) => {
totalEarnings += parseFloat(item.price) * item.quantity;
});
});

res
.status(StatusCodes.OK)
.json({ users, orders, announcements, events, merchandise });
.json({ users, orders, announcements, events, merchandise, totalEarnings });
};
26 changes: 26 additions & 0 deletions backend/psits_backend/src/models/ItemModel.js
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);
20 changes: 14 additions & 6 deletions backend/psits_backend/src/models/MerchandiseModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import mongoose from "mongoose";

const StockSchema = new mongoose.Schema({
size: {
type: String,
enum: ["XS", "SM", "MD", "LG", "XL", "XXL"],
required: true,
},
quantity: {
type: Number,
default: 0,
},
});

const ImageSchema = new mongoose.Schema({
image: {
type: String,
Expand Down Expand Up @@ -28,12 +40,8 @@ const MerchandiseModel = new mongoose.Schema({
type: Number,
default: 0,
},
stocks: {
type: Number,
default: 0,
},
stocks: [StockSchema],
images: [ImageSchema],
size: String,
color: String,
ratings: {
type: Number,
Expand All @@ -44,4 +52,4 @@ const MerchandiseModel = new mongoose.Schema({
},
});

export default mongoose.model("Merchandise", MerchandiseModel);
export default mongoose.model("Merchandise", MerchandiseModel);
7 changes: 5 additions & 2 deletions backend/psits_backend/src/models/OrderModel.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import mongoose from "mongoose";
import mongoose, { Schema } from "mongoose";
import { CartSchema } from "./UserModel.js";
const Schema = mongoose.Schema;

const OrderModel = new mongoose.Schema({
userId: {
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
orderId: {
type: String,
required: true,
},
orderDate: {
type: Date,
default: Date.now,
Expand Down
18 changes: 13 additions & 5 deletions backend/psits_backend/src/routes/orderRouter.js
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;
10 changes: 10 additions & 0 deletions backend/psits_backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

fsevents@~2.3.2:
version "2.3.2"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==

function-bind@^1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"
Expand Down Expand Up @@ -1077,6 +1082,11 @@ multer@^1.4.5-lts.1:
type-is "^1.6.4"
xtend "^4.0.0"

nanoid@^5.0.4:
version "5.0.4"
resolved "https://registry.npmjs.org/nanoid/-/nanoid-5.0.4.tgz"
integrity sha512-vAjmBf13gsmhXSgBrtIclinISzFFy22WwCYoyilZlsrRXNIHSwgFQ1bEdjRwMT3aoadeIF6HMuDRlOxzfXV8ig==

napi-build-utils@^1.0.1:
version "1.0.2"
resolved "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-1.0.2.tgz"
Expand Down
56 changes: 56 additions & 0 deletions frontend/psits_frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3cf49a

Please sign in to comment.