Merge pull request #16 from kfnawaz/feat/item-transaction-apis
Feat: ItemTransaction APIs
This commit is contained in:
@@ -68,6 +68,8 @@ const ItemTransactionTypes = [
|
|||||||
"RESERVE"
|
"RESERVE"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const ReportItemForTypes = ["LOCATION", "ISSUE", "INCIDENT"];
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
UserActions,
|
UserActions,
|
||||||
InventoryScopes,
|
InventoryScopes,
|
||||||
@@ -85,4 +87,5 @@ module.exports = {
|
|||||||
AUTHENTICATION_FAILURE_ERROR_MESSAGE,
|
AUTHENTICATION_FAILURE_ERROR_MESSAGE,
|
||||||
AUTHORIZATION_FAILURE_ERROR_MESSAGE,
|
AUTHORIZATION_FAILURE_ERROR_MESSAGE,
|
||||||
ItemTransactionTypes,
|
ItemTransactionTypes,
|
||||||
|
ReportItemForTypes,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,7 +2,19 @@ const mongoose = require("mongoose");
|
|||||||
const Item = require("../models/Item");
|
const Item = require("../models/Item");
|
||||||
const WidgetFamily = require("../models/WidgetFamily");
|
const WidgetFamily = require("../models/WidgetFamily");
|
||||||
const Inventory = require("../models/Inventory");
|
const Inventory = require("../models/Inventory");
|
||||||
const { InventoryTypes } = require("../config/constants");
|
const {
|
||||||
|
PickItemTransaction,
|
||||||
|
PutItemTransaction,
|
||||||
|
ReserveItemTransaction,
|
||||||
|
CheckInItemTransaction,
|
||||||
|
CheckOutItemTransaction,
|
||||||
|
ReportItemTransaction,
|
||||||
|
AdjustItemTransaction,
|
||||||
|
} = require("../models/ItemTransaction");
|
||||||
|
|
||||||
|
const ItemAssociation = require("../models/ItemAssociation");
|
||||||
|
const Sublevel = require("../models/Sublevel");
|
||||||
|
const { InventoryTypes, ReportItemForTypes } = require("../config/constants");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
/**
|
/**
|
||||||
@@ -194,4 +206,260 @@ module.exports = {
|
|||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
putItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { putQuantity, subLevel } = req.body;
|
||||||
|
if (!(putQuantity && putQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) {
|
||||||
|
res.status(400).send("Invalid value for putQuantity/subLevel");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const subLevelObj = await Sublevel.findById(subLevel);
|
||||||
|
const itemAssociation = await ItemAssociation.findOne({ item_id: item._id, sub_level_id: subLevelObj._id });
|
||||||
|
itemAssociation.totalQuantity = itemAssociation.totalQuantity + putQuantity;
|
||||||
|
itemAssociation.availableQuantity = itemAssociation.availableQuantity + putQuantity;
|
||||||
|
await itemAssociation.save();
|
||||||
|
|
||||||
|
await PutItemTransaction.create({
|
||||||
|
type: "PUT",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
putQuantity: putQuantity,
|
||||||
|
subLevel: subLevelObj,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
pickItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { pickupQuantity, subLevel } = req.body;
|
||||||
|
if (!(pickupQuantity && pickupQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) {
|
||||||
|
res.status(400).send("Invalid value for pickupQuantity/subLevel");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const subLevelObj = await Sublevel.findById(subLevel);
|
||||||
|
const itemAssociation = await ItemAssociation.findOne({ item_id: item._id, sub_level_id: subLevelObj._id });
|
||||||
|
itemAssociation.totalQuantity = itemAssociation.totalQuantity - pickupQuantity;
|
||||||
|
itemAssociation.availableQuantity = itemAssociation.availableQuantity - pickupQuantity;
|
||||||
|
await itemAssociation.save();
|
||||||
|
|
||||||
|
await PickItemTransaction.create({
|
||||||
|
type: "PICK",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
pickupQuantity: pickupQuantity,
|
||||||
|
subLevel: subLevelObj,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
reserveItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { reserveQuantity, job, pickupDate } = req.body;
|
||||||
|
if (!(reserveQuantity && reserveQuantity > 0) || !(job && mongoose.isValidObjectId(job)) || !(pickupDate && Date.parse(pickupDate))) {
|
||||||
|
res.status(400).send("Invalid value for reserveQuantity/job/pickupDate");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const itemAssociation = await ItemAssociation.findOne({ item_id: item._id, availableQuantity: { $gte: reserveQuantity } });
|
||||||
|
itemAssociation.reservedQuantity = itemAssociation.reservedQuantity + reserveQuantity;
|
||||||
|
itemAssociation.availableQuantity = itemAssociation.availableQuantity - reserveQuantity;
|
||||||
|
await itemAssociation.save();
|
||||||
|
|
||||||
|
await ReserveItemTransaction.create({
|
||||||
|
type: "RESERVE",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
reserveQuantity: reserveQuantity,
|
||||||
|
job: job,
|
||||||
|
pickupDate: Date.parse(pickupDate),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
checkInItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { checkInMeterReading, hasIssue, issueDescription } = req.body;
|
||||||
|
if (!(checkInMeterReading && checkInMeterReading > 0)) {
|
||||||
|
res.status(400).send("Invalid value for checkInMeterReading");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await CheckInItemTransaction.create({
|
||||||
|
type: "CHECK-IN",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
checkInMeterReading: checkInMeterReading,
|
||||||
|
hasIssue: hasIssue,
|
||||||
|
issueDescription: hasIssue ? issueDescription : "",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
checkOutItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { checkOutMeterReading, job, usageReason } = req.body;
|
||||||
|
if (!(checkOutMeterReading && checkOutMeterReading > 0) || !(job && mongoose.isValidObjectId(job))) {
|
||||||
|
res.status(400).send("Invalid value for checkOutMeterReading/job");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await CheckOutItemTransaction.create({
|
||||||
|
type: "CHECK-OUT",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
checkOutMeterReading: checkOutMeterReading,
|
||||||
|
job: job,
|
||||||
|
usageReason: usageReason ? usageReason : "",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
reportItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { reportingFor, details } = req.body;
|
||||||
|
if (!(reportingFor && ReportItemForTypes.includes(reportingFor))) {
|
||||||
|
res.status(400).send("Invalid value for checkOutMeterReading/job");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ReportItemTransaction.create({
|
||||||
|
type: "REPORT",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
reportingFor: reportingFor,
|
||||||
|
details: details ? details : "",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
adjustItem: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
if (!id || mongoose.isValidObjectId(id)) {
|
||||||
|
res.status(400).send("Missing/Invalid id param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = await Item.findById(id);
|
||||||
|
if (!item) {
|
||||||
|
res.status(404).send("item not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { recountedQuantity, damagedQuantity, subLevel } = req.body;
|
||||||
|
if (!(recountedQuantity && recountedQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) {
|
||||||
|
res.status(400).send("Invalid value for pickupQuantity/subLevel");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const subLevelObj = await Sublevel.findById(subLevel);
|
||||||
|
const itemAssociation = await ItemAssociation.findOne({ item_id: item._id, sub_level_id: subLevelObj._id });
|
||||||
|
const lastRecordedQuantity = itemAssociation.totalQuantity;
|
||||||
|
const varianceRecordedInQuantity = itemAssociation.totalQuantity - recountedQuantity;
|
||||||
|
const totalAdjustment = varianceRecordedInQuantity + damagedQuantity;
|
||||||
|
itemAssociation.totalQuantity = itemAssociation.totalQuantity - totalAdjustment;
|
||||||
|
itemAssociation.availableQuantity = itemAssociation.availableQuantity - totalAdjustment;
|
||||||
|
await itemAssociation.save();
|
||||||
|
|
||||||
|
await AdjustItemTransaction.create({
|
||||||
|
type: "ADJUST",
|
||||||
|
performedOn: item,
|
||||||
|
performedBy: res.locals.user,
|
||||||
|
lastRecordedQuantity,
|
||||||
|
recountedQuantity,
|
||||||
|
varianceRecordedInQuantity,
|
||||||
|
damagedQuantity,
|
||||||
|
totalAdjustment,
|
||||||
|
newAdjustedQuantity: itemAssociation.totalQuantity,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
const controller = require("./item.controller");
|
const controller = require("./item.controller");
|
||||||
|
const { AuthenticateMiddleware, ItemTransactionCheck } = require("./utils/authorize");
|
||||||
/**
|
/**
|
||||||
* @route /item/
|
* @route /item/
|
||||||
*/
|
*/
|
||||||
@@ -21,4 +21,39 @@ router.get("/filter", controller.getItemsByFilter);
|
|||||||
*/
|
*/
|
||||||
router.get("/:id", controller.getItemByID);
|
router.get("/:id", controller.getItemByID);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/pick
|
||||||
|
*/
|
||||||
|
router.post("/:id/pick", AuthenticateMiddleware, ItemTransactionCheck, controller.pickItem);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/put
|
||||||
|
*/
|
||||||
|
router.post("/:id/put", AuthenticateMiddleware, ItemTransactionCheck, controller.putItem);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/reserve
|
||||||
|
*/
|
||||||
|
router.post("/:id/reserve", AuthenticateMiddleware, ItemTransactionCheck, controller.reserveItem);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/check-in
|
||||||
|
*/
|
||||||
|
router.post("/:id/check-in", AuthenticateMiddleware, ItemTransactionCheck, controller.checkInItem);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/check-out
|
||||||
|
*/
|
||||||
|
router.post("/:id/check-out", AuthenticateMiddleware, ItemTransactionCheck, controller.checkOutItem);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/report
|
||||||
|
*/
|
||||||
|
router.post("/:id/report", AuthenticateMiddleware, ItemTransactionCheck, controller.reportItem);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /item/:id/adjust
|
||||||
|
*/
|
||||||
|
router.post("/:id/adjust", AuthenticateMiddleware, ItemTransactionCheck, controller.adjustItem);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -1,38 +1,22 @@
|
|||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
const User = require("../../models/User");
|
const User = require("../../models/User");
|
||||||
const UserRole = require("../../models/UserRole");
|
const UserRole = require("../../models/UserRole");
|
||||||
const {
|
const { SUPER_ADMIN_ROLE, AUTHORIZATION_FAILURE_ERROR_MESSAGE } = require("../../config/constants");
|
||||||
SUPER_ADMIN_ROLE,
|
|
||||||
AUTHORIZATION_FAILURE_ERROR_MESSAGE,
|
|
||||||
} = require("../../config/constants");
|
|
||||||
const { JWT_SECRET } = require("../../config/env");
|
const { JWT_SECRET } = require("../../config/env");
|
||||||
const constants = require("../../config/constants");
|
const constants = require("../../config/constants");
|
||||||
|
|
||||||
const authenticate = async (token) => {
|
const authenticate = async (token) => {
|
||||||
const decodedToken = jwt.verify(token, JWT_SECRET);
|
const decodedToken = jwt.verify(token, JWT_SECRET);
|
||||||
if (decodedToken) {
|
if (decodedToken) {
|
||||||
return await User.findById(decodedToken.id)
|
return await User.findById(decodedToken.id).populate({ path: "roles", populate: "permissions" }).populate("permissions");
|
||||||
.populate({ path: "roles", populate: "permissions" })
|
|
||||||
.populate("permissions");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const authorize = async (
|
const authorize = async (user, requiredRoles = [], requiredPermissions = []) => {
|
||||||
user,
|
|
||||||
requiredRoles = [],
|
|
||||||
requiredPermissions = []
|
|
||||||
) => {
|
|
||||||
const userRoles = user.roles.map((_) => _._id);
|
const userRoles = user.roles.map((_) => _._id);
|
||||||
const userPermissions = [
|
const userPermissions = [...user.permissions.map((_) => _._id), ...userRoles.map((_) => _.permissions).flat()];
|
||||||
...user.permissions.map((_) => _._id),
|
|
||||||
...userRoles.map((_) => _.permissions).flat(),
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
return user != undefined && requiredRoles.every((_) => userRoles.includes(_)) && requiredPermissions.every((_) => userPermissions.includes(_));
|
||||||
user != undefined &&
|
|
||||||
requiredRoles.every((_) => userRoles.includes(_)) &&
|
|
||||||
requiredPermissions.every((_) => userPermissions.includes(_))
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -41,11 +25,13 @@ module.exports = {
|
|||||||
if (authorize(res.locals.user, [SuperAdmin.id])) {
|
if (authorize(res.locals.user, [SuperAdmin.id])) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
res
|
res.status(403).send({ success: false, error: AUTHORIZATION_FAILURE_ERROR_MESSAGE });
|
||||||
.status(403)
|
|
||||||
.send({ success: false, error: AUTHORIZATION_FAILURE_ERROR_MESSAGE });
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
ItemTransactionCheck: async (req, res, next) => {
|
||||||
|
// WIP
|
||||||
|
next();
|
||||||
|
},
|
||||||
AuthenticateMiddleware: async (req, res, next) => {
|
AuthenticateMiddleware: async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const token = req.headers.authorization || "";
|
const token = req.headers.authorization || "";
|
||||||
|
|||||||
@@ -10,7 +10,15 @@ const schema = new mongoose.Schema(
|
|||||||
type: mongoose.Schema.Types.ObjectId,
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
ref: "Sublevel",
|
ref: "Sublevel",
|
||||||
},
|
},
|
||||||
quantity: {
|
totalQuantity: {
|
||||||
|
type: Number,
|
||||||
|
default: 0,
|
||||||
|
},
|
||||||
|
reservedQuantity: {
|
||||||
|
type: Number,
|
||||||
|
default: 0,
|
||||||
|
},
|
||||||
|
availableQuantity: {
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 0,
|
default: 0,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const { ItemTransactionTypes } = require("../config/constants");
|
const { ItemTransactionTypes, ReportItemForTypes } = require("../config/constants");
|
||||||
|
|
||||||
const schema = new mongoose.Schema(
|
const schema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
@@ -33,6 +33,10 @@ const PutItemTransaction = ItemTransaction.discriminator(
|
|||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
subLevel: {
|
||||||
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
|
ref: "Sublevel"
|
||||||
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -43,6 +47,10 @@ const PickItemTransaction = ItemTransaction.discriminator(
|
|||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
subLevel: {
|
||||||
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
|
ref: "Sublevel",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -103,10 +111,10 @@ const CheckOutItemTransaction = ItemTransaction.discriminator(
|
|||||||
const ReportItemTransaction = ItemTransaction.discriminator(
|
const ReportItemTransaction = ItemTransaction.discriminator(
|
||||||
"Report",
|
"Report",
|
||||||
new mongoose.Schema({
|
new mongoose.Schema({
|
||||||
for: {
|
reportingFor: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
enum: ["LOCATION", "ISSUE", "INCIDENT"]
|
enum: ReportItemForTypes,
|
||||||
},
|
},
|
||||||
details: {
|
details: {
|
||||||
type: String,
|
type: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user