fix: all create apis

This commit is contained in:
Sathishkumar Krishnan
2022-02-27 00:52:02 +05:30
parent 9e0fd56300
commit 7ce1e99d84
8 changed files with 115 additions and 138 deletions

View File

@@ -1,6 +1,7 @@
const Inventory = require("../models/Inventory"); const Inventory = require("../models/Inventory");
const WidgetFamily = require("../models/WidgetFamily"); const WidgetFamily = require("../models/WidgetFamily");
const { InventoryTypes } = require("../config/constants"); const { InventoryTypes } = require("../config/constants");
const mongoose = require("mongoose");
module.exports = { module.exports = {
/** /**
@@ -37,10 +38,10 @@ module.exports = {
* Create a Inventory * Create a Inventory
*/ */
createInventory: async (req, res, next) => { createInventory: async (req, res, next) => {
const { name, type, policies } = req.body; const { name, policies, widgetName } = req.body;
if (!(name && type)) { if (!(name && widgetName)) {
res.status(400).send("Missing params param"); res.status(400).send("Missing params name");
return; return;
} }
const preferredLocations = []; const preferredLocations = [];
@@ -51,21 +52,17 @@ module.exports = {
} }
const verifiedPolicies = { const verifiedPolicies = {
orderTracking: policies.orderTracking || {}, orderTracking: policies.orderTracking || false,
alerting: { alerting: policies.alerting || false,
lowestStockLevel: policies.alerting && policies.alerting.lowestStockLevel ? policies.alerting.lowestStockLevel : false, replenishment: policies.replenishment || false,
highestStockLevel: policies.alerting && policies.alerting.highestStockLevel ? policies.alerting.highestStockLevel : false, preferredLocations: preferredLocations || false,
alertStockLevel: policies.alerting && policies.alerting.alertStockLevel ? policies.alerting.alertStockLevel : false, inventory_process: policies.inventory_process,
reOrderLevel: policies.alerting && policies.alerting.reOrderLevel ? policies.alerting.reOrderLevel : false,
},
replenishment: policies.replenishment || {},
preferredLocations: preferredLocations,
}; };
try { try {
const inventoryData = new Inventory({ const inventoryData = new Inventory({
name, name,
type, widgetName,
policies: verifiedPolicies, policies: verifiedPolicies,
}); });
@@ -74,7 +71,8 @@ module.exports = {
res.status(404); res.status(404);
return; return;
} }
res.send({ success: true, data: inventoryData }); // const widgetFamilyData = createWidgetFamiliesData(inventoryData, widgetFamily);
res.send({ success: true, data: { inventoryData } });
} catch (error) { } catch (error) {
next(error); next(error);
} }
@@ -84,65 +82,46 @@ module.exports = {
* Update a Inventory detail * Update a Inventory detail
*/ */
updateInventoryByID: async (req, res, next) => { updateInventoryByID: async (req, res, next) => {
const { id } = req.params;
if (!id) {
res.status(400).send("Missing id param");
return;
}
const { name, type, policies } = req.body;
if (!(name || type)) {
res.status(400).send("Missing data in body");
return;
}
try { try {
const inventoryData = await Inventory.findById(id); const { id } = req.params;
if (!inventoryData) {
res.status(404); if (!(id && mongoose.isValidObjectId(id))) {
res.status(400).send("Missing/Improper id param");
return; return;
} }
if (name) inventoryData.name = name; const inventory = await Inventory.findById(id);
if (type) inventoryData.type = type;
if (policies) { if (!inventory) {
const preferredLocations = []; res.status(400).send("Inventory not found");
if (policies.preferredLocations && Array.isArray(policies.preferredLocations)) { return;
for (const preferredLocation of policies.preferredLocations) { }
preferredLocations.push({ id: preferredLocation.id, type: preferredLocation.type }); let { name, policies, widgetName } = req.body;
} if (name) {
} inventory.name = name;
inventoryData.policies = {
orderTracking: policies.orderTracking || inventoryData.policies.orderTracking,
alerting: {
lowestStockLevel:
policies.alerting && policies.alerting.lowestStockLevel !== undefined
? policies.alerting.lowestStockLevel
: inventoryData.policies.alerting.lowestStockLevel,
highestStockLevel:
policies.alerting && policies.alerting.highestStockLevel !== undefined
? policies.alerting.highestStockLevel
: inventoryData.policies.alerting.highestStockLevel,
alertStockLevel:
policies.alerting && policies.alerting.alertStockLevel !== undefined
? policies.alerting.alertStockLevel
: inventoryData.policies.alerting.alertStockLevel,
reOrderLevel:
policies.alerting && policies.alerting.reOrderLevel !== undefined
? policies.alerting.reOrderLevel
: inventoryData.policies.alerting.reOrderLevel,
},
replenishment: policies.replenishment || inventoryData.policies.replenishment,
preferredLocations: preferredLocations,
};
} }
await inventoryData.save(); if (widgetName) {
res.send({ success: true, data: inventoryData }); inventory.widgetName = widgetName;
}
if (!policies) {
policies = {};
}
// const widgetFamilyData = createWidgetFamiliesData(inventory, widgetFamily);
const verifiedPolicies = {
orderTracking: policies.orderTracking !== undefined ? policies.orderTracking : inventory.policies.orderTracking,
alerting: policies.alerting !== undefined ? policies.alerting : inventory.policies.alerting,
replenishment: policies.replenishment !== undefined ? policies.replenishment : inventory.policies.replenishment,
preferredLocations: policies.replenishment !== undefined ? policies.replenishment : inventory.policies.preferredLocations,
inventory_process: policies.inventory_process || inventory.policies.inventory_process,
};
inventory.policies = verifiedPolicies;
await inventory.save();
res.send({ success: true, data: { inventory } });
} catch (error) { } catch (error) {
next(error); next(error);
} }

View File

@@ -29,7 +29,7 @@ module.exports = {
} }
try { try {
const itemData = await Item.findById(id); const itemData = await Item.findById(id).populate({ path: "widgetFamily", populate: "inventory" });
if (!itemData) { if (!itemData) {
res.status(404); res.status(404);
return; return;
@@ -63,6 +63,12 @@ module.exports = {
countPerPalletPackage: req.body.countPerPalletPackage, countPerPalletPackage: req.body.countPerPalletPackage,
customAttributes: req.body.customAttributes, customAttributes: req.body.customAttributes,
widgetFamily: widgetFamily, widgetFamily: widgetFamily,
policiesMetadata: {
underStockLevelCount: req.body.policiesMetadata.underStockLevelCount,
overStockLevelCount: req.body.policiesMetadata.overStockLevelCount,
alertStockLevelCount: req.body.policiesMetadata.alertStockLevelCount,
reorderStockLevelCount: req.body.policiesMetadata.reorderStockLevelCount,
},
}; };
for (const key of Object.keys(item)) { for (const key of Object.keys(item)) {
@@ -229,7 +235,7 @@ module.exports = {
return; return;
} }
const { putQuantity, subLevel } = req.body; const { putQuantity, subLevel, usageReason, job } = req.body;
if (!(putQuantity && putQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) { if (!(putQuantity && putQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) {
res.status(400).send("Invalid value for putQuantity/subLevel"); res.status(400).send("Invalid value for putQuantity/subLevel");
return; return;
@@ -250,6 +256,8 @@ module.exports = {
performedBy: res.locals.user, performedBy: res.locals.user,
putQuantity: putQuantity, putQuantity: putQuantity,
subLevel: subLevelObj, subLevel: subLevelObj,
usageReason: usageReason ? usageReason : "",
job: job,
}); });
res.send({ success: true, data: { itemAssociation, itemTransaction } }); res.send({ success: true, data: { itemAssociation, itemTransaction } });
res.send({ success: true, data: { itemAssociation, itemTransaction } }); res.send({ success: true, data: { itemAssociation, itemTransaction } });
@@ -272,7 +280,7 @@ module.exports = {
return; return;
} }
const { pickupQuantity, subLevel } = req.body; const { pickupQuantity, subLevel, usageReason, job } = req.body;
if (!(pickupQuantity && pickupQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) { if (!(pickupQuantity && pickupQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) {
res.status(400).send("Invalid value for pickupQuantity/subLevel"); res.status(400).send("Invalid value for pickupQuantity/subLevel");
return; return;
@@ -293,6 +301,8 @@ module.exports = {
performedBy: res.locals.user, performedBy: res.locals.user,
pickupQuantity: pickupQuantity, pickupQuantity: pickupQuantity,
subLevel: subLevelObj, subLevel: subLevelObj,
usageReason: usageReason ? usageReason : "",
job: job,
}); });
res.send({ success: true, data: { itemAssociation, itemTransaction } }); res.send({ success: true, data: { itemAssociation, itemTransaction } });
} catch (error) { } catch (error) {
@@ -314,15 +324,15 @@ module.exports = {
return; return;
} }
const { reserveQuantity, job, pickupDate } = req.body; const { reserveQuantity, job, pickupDate, usageReason } = req.body;
if (!(reserveQuantity && reserveQuantity > 0) || !(job && mongoose.isValidObjectId(job)) || !(pickupDate && Date.parse(pickupDate))) { if (!(reserveQuantity && reserveQuantity > 0) || !(job && mongoose.isValidObjectId(job))) {
res.status(400).send("Invalid value for reserveQuantity/job/pickupDate"); res.status(400).send("Invalid value for reserveQuantity/job");
return; return;
} }
const itemAssociation = await ItemAssociation.findOne({ item_id: item._id, availableQuantity: { $gte: reserveQuantity } }); const itemAssociation = await ItemAssociation.findOne({ item_id: item._id, availableQuantity: { $gte: reserveQuantity } });
if (!itemAssociation) { if (!itemAssociation) {
res.status(500).send("Quantity unavailable"); res.status(500).send({ success: false, error: "Quantity unavailable" });
return; return;
} }
itemAssociation.reservedQuantity = itemAssociation.reservedQuantity + reserveQuantity; itemAssociation.reservedQuantity = itemAssociation.reservedQuantity + reserveQuantity;
@@ -335,7 +345,8 @@ module.exports = {
performedBy: res.locals.user, performedBy: res.locals.user,
reserveQuantity: reserveQuantity, reserveQuantity: reserveQuantity,
job: job, job: job,
pickupDate: Date.parse(pickupDate), pickupDate: pickupDate ? Date.parse(pickupDate) : undefined,
usageReason: usageReason ? usageReason : "",
}); });
res.send({ success: true, data: { itemAssociation, itemTransaction } }); res.send({ success: true, data: { itemAssociation, itemTransaction } });
} catch (error) { } catch (error) {
@@ -357,19 +368,17 @@ module.exports = {
return; return;
} }
const { checkInMeterReading, hasIssue, issueDescription } = req.body; const { checkInMeterReading, hasIssue, issueDescription, usageReason, job } = req.body;
if (!(checkInMeterReading && checkInMeterReading > 0)) {
res.status(400).send("Invalid value for checkInMeterReading");
return;
}
const itemTransaction = await CheckInItemTransaction.create({ const itemTransaction = await CheckInItemTransaction.create({
type: "CHECK-IN", type: "CHECK-IN",
performedOn: item, performedOn: item,
performedBy: res.locals.user, performedBy: res.locals.user,
checkInMeterReading: checkInMeterReading, checkInMeterReading: checkInMeterReading,
hasIssue: hasIssue, hasIssue: hasIssue || false,
issueDescription: hasIssue ? issueDescription : "", issueDescription: hasIssue ? issueDescription : "",
usageReason: usageReason ? usageReason : "",
job: job,
}); });
res.send({ success: true, data: { itemTransaction } }); res.send({ success: true, data: { itemTransaction } });
} catch (error) { } catch (error) {
@@ -391,19 +400,15 @@ module.exports = {
return; return;
} }
const { checkOutMeterReading, job, usageReason } = req.body; const { checkOutMeterReading, usageReason, job } = req.body;
if (!(checkOutMeterReading && checkOutMeterReading > 0) || !(job && mongoose.isValidObjectId(job))) {
res.status(400).send("Invalid value for checkOutMeterReading/job");
return;
}
const itemTransaction = await CheckOutItemTransaction.create({ const itemTransaction = await CheckOutItemTransaction.create({
type: "CHECK-OUT", type: "CHECK-OUT",
performedOn: item, performedOn: item,
performedBy: res.locals.user, performedBy: res.locals.user,
checkOutMeterReading: checkOutMeterReading, checkOutMeterReading: checkOutMeterReading,
job: job,
usageReason: usageReason ? usageReason : "", usageReason: usageReason ? usageReason : "",
job: job,
}); });
res.send({ success: true, data: { itemTransaction } }); res.send({ success: true, data: { itemTransaction } });
} catch (error) { } catch (error) {
@@ -425,7 +430,7 @@ module.exports = {
return; return;
} }
const { reportingFor, details } = req.body; const { reportingFor, details, usageReason, job } = req.body;
if (!(reportingFor && ReportItemForTypes.includes(reportingFor))) { if (!(reportingFor && ReportItemForTypes.includes(reportingFor))) {
res.status(400).send("Invalid value for checkOutMeterReading/job"); res.status(400).send("Invalid value for checkOutMeterReading/job");
return; return;
@@ -437,6 +442,8 @@ module.exports = {
performedBy: res.locals.user, performedBy: res.locals.user,
reportingFor: reportingFor, reportingFor: reportingFor,
details: details ? details : "", details: details ? details : "",
usageReason: usageReason ? usageReason : "",
job: job,
}); });
res.send({ success: true, data: { itemTransaction } }); res.send({ success: true, data: { itemTransaction } });
} catch (error) { } catch (error) {
@@ -458,7 +465,7 @@ module.exports = {
return; return;
} }
const { recountedQuantity, damagedQuantity, subLevel } = req.body; const { recountedQuantity, damagedQuantity, subLevel, usageReason, job } = req.body;
if (!(recountedQuantity && recountedQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) { if (!(recountedQuantity && recountedQuantity > 0) || !(subLevel && mongoose.isValidObjectId(subLevel))) {
res.status(400).send("Invalid value for pickupQuantity/subLevel"); res.status(400).send("Invalid value for pickupQuantity/subLevel");
return; return;
@@ -483,6 +490,8 @@ module.exports = {
damagedQuantity, damagedQuantity,
totalAdjustment, totalAdjustment,
newAdjustedQuantity: itemAssociation.totalQuantity, newAdjustedQuantity: itemAssociation.totalQuantity,
usageReason: usageReason ? usageReason : "",
job: job,
}); });
res.send({ success: true, data: { itemAssociation, itemTransaction } }); res.send({ success: true, data: { itemAssociation, itemTransaction } });
} catch (error) { } catch (error) {

View File

@@ -70,7 +70,7 @@ module.exports = {
res.status(404); res.status(404);
return; return;
} }
req.send(sublevelData); res.send({ success: true, data: sublevelData });
} catch (error) { } catch (error) {
next(error); next(error);
} }

View File

@@ -58,7 +58,7 @@ const deleteSubLevelTreeFromRoot = async (root_sub_level_id) => {
const addSublevelToParent = async (payload, parent_id, parentIsLevel) => { const addSublevelToParent = async (payload, parent_id, parentIsLevel) => {
if (parentIsLevel) { if (parentIsLevel) {
// add sublevel to parent // add sublevel to parent
const parentData = await Sublevel.findById(parent_id); const parentData = await Level.findById(parent_id);
parentData.sub_levels.push(payload); parentData.sub_levels.push(payload);
return await parentData.save(); return await parentData.save();
} else { } else {

View File

@@ -1,5 +1,5 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const { InventoryTypes, WarehouseScopes } = require("./../config/constants"); // const { InventoryTypes } = require("./../config/constants");
const schema = new mongoose.Schema( const schema = new mongoose.Schema(
{ {
@@ -8,49 +8,41 @@ const schema = new mongoose.Schema(
required: true, required: true,
trim: true, trim: true,
}, },
type: { widgetName: {
type: String, type: String,
required: true, required: true,
trim: true, trim: true,
enum: InventoryTypes,
}, },
policies: { policies: {
orderTracking: { orderTracking: {
type: Object, // Create a different model and reference it here once more details available type: Object, // Create a different model and reference it here once more details available
}, },
alerting: { alerting: {
lowestStockLevel: { type: Boolean,
type: Boolean, required: true,
required: true,
},
highestStockLevel: {
type: Boolean,
required: true,
},
alertStockLevel: {
type: Boolean,
required: true,
},
reOrderLevel: {
type: Boolean,
required: true,
},
}, },
replenishment: { replenishment: {
type: Boolean, // Create a different model and reference it here once more details available
},
inventory_process: {
type: String,
required: true,
},
// preferredLocations: [
// {
// id: {
// type: mongoose.Schema.Types.ObjectId,
// refPath: "type",
// },
// type: {
// type: String,
// enum: WarehouseScopes,
// },
// },
// ],
preferredLocations: {
type: Object, // Create a different model and reference it here once more details available type: Object, // Create a different model and reference it here once more details available
}, },
preferredLocations: [
{
id: {
type: mongoose.Schema.Types.ObjectId,
refPath: "type",
},
type: {
type: String,
enum: WarehouseScopes,
},
},
],
}, },
}, },
{ {

View File

@@ -93,6 +93,9 @@ const schema = new mongoose.Schema(
alertStockLevelCount: { alertStockLevelCount: {
type: Number, type: Number,
}, },
reorderStockLevelCount: {
type: Number,
},
}, },
}, },
{ {

View File

@@ -17,6 +17,13 @@ const schema = new mongoose.Schema(
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "User", ref: "User",
}, },
usageReason: {
type: String,
trim: true,
},
job: {
type: mongoose.Schema.Types.ObjectId,
},
}, },
{ {
timestamps: true, timestamps: true,
@@ -35,7 +42,7 @@ const PutItemTransaction = ItemTransaction.discriminator(
}, },
subLevel: { subLevel: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Sublevel" ref: "Sublevel",
}, },
}) })
); );
@@ -61,13 +68,8 @@ const ReserveItemTransaction = ItemTransaction.discriminator(
type: Number, type: Number,
required: true, required: true,
}, },
job: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
pickupDate: { pickupDate: {
type: Date, type: Date,
required: true,
}, },
}) })
); );
@@ -77,7 +79,6 @@ const CheckInItemTransaction = ItemTransaction.discriminator(
new mongoose.Schema({ new mongoose.Schema({
checkInMeterReading: { checkInMeterReading: {
type: Number, type: Number,
required: true,
}, },
hasIssue: { hasIssue: {
type: Boolean, type: Boolean,
@@ -95,15 +96,6 @@ const CheckOutItemTransaction = ItemTransaction.discriminator(
new mongoose.Schema({ new mongoose.Schema({
checkOutMeterReading: { checkOutMeterReading: {
type: Number, type: Number,
required: true,
},
job: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
usageReason: {
type: String,
trim: true,
}, },
}) })
); );

View File

@@ -14,7 +14,7 @@ const schema = new mongoose.Schema(
inventory: { inventory: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Inventory", ref: "Inventory",
required: true required: true,
}, },
}, },
{ {
@@ -22,6 +22,8 @@ const schema = new mongoose.Schema(
} }
); );
schema.index({ name: 1, parent: 1, inventory: 1 }, { unique: true });
const WidgetFamily = mongoose.model("WidgetFamily", schema); const WidgetFamily = mongoose.model("WidgetFamily", schema);
module.exports = WidgetFamily; module.exports = WidgetFamily;