feat: added inventory policies & modified APIs
This commit is contained in:
@@ -163,14 +163,34 @@ const createSublevels = async (subLevels, level, parent = undefined, depth = 0)
|
||||
return sub_levels_list;
|
||||
};
|
||||
|
||||
const createInventory = async ({ name, type }) => {
|
||||
const createInventory = async ({ name, type, policies }) => {
|
||||
if (!(name && type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const preferredLocations = [];
|
||||
if (policies.preferredLocations && Array.isArray(policies.preferredLocations)) {
|
||||
for (const preferredLocation of policies.preferredLocations) {
|
||||
preferredLocations.push({ id: preferredLocation.id, type: preferredLocation.type });
|
||||
}
|
||||
}
|
||||
|
||||
const verifiedPolicies = {
|
||||
orderTracking: policies.orderTracking || {},
|
||||
alerting: {
|
||||
lowestStockLevel: policies.alerting && policies.alerting.lowestStockLevel ? policies.alerting.lowestStockLevel : false,
|
||||
highestStockLevel: policies.alerting && policies.alerting.highestStockLevel ? policies.alerting.highestStockLevel : false,
|
||||
alertStockLevel: policies.alerting && policies.alerting.alertStockLevel ? policies.alerting.alertStockLevel : false,
|
||||
reOrderLevel: policies.alerting && policies.alerting.reOrderLevel ? policies.alerting.reOrderLevel : false,
|
||||
},
|
||||
replenishment: policies.replenishment || {},
|
||||
preferredLocations: preferredLocations,
|
||||
};
|
||||
|
||||
return await Inventory.create({
|
||||
name,
|
||||
type,
|
||||
policies: verifiedPolicies,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -29,17 +29,36 @@ module.exports = {
|
||||
* Create a Inventory
|
||||
*/
|
||||
createInventory: async (req, res, next) => {
|
||||
const { name, type } = req.body;
|
||||
const { name, type, policies } = req.body;
|
||||
|
||||
if (!(name && type)) {
|
||||
res.status(400).send("Missing params param");
|
||||
return;
|
||||
}
|
||||
const preferredLocations = [];
|
||||
if (policies.preferredLocations && Array.isArray(policies.preferredLocations)) {
|
||||
for (const preferredLocation of policies.preferredLocations) {
|
||||
preferredLocations.push({ id: preferredLocation.id, type: preferredLocation.type });
|
||||
}
|
||||
}
|
||||
|
||||
const verifiedPolicies = {
|
||||
orderTracking: policies.orderTracking || {},
|
||||
alerting: {
|
||||
lowestStockLevel: policies.alerting && policies.alerting.lowestStockLevel ? policies.alerting.lowestStockLevel : false,
|
||||
highestStockLevel: policies.alerting && policies.alerting.highestStockLevel ? policies.alerting.highestStockLevel : false,
|
||||
alertStockLevel: policies.alerting && policies.alerting.alertStockLevel ? policies.alerting.alertStockLevel : false,
|
||||
reOrderLevel: policies.alerting && policies.alerting.reOrderLevel ? policies.alerting.reOrderLevel : false,
|
||||
},
|
||||
replenishment: policies.replenishment || {},
|
||||
preferredLocations: preferredLocations,
|
||||
};
|
||||
|
||||
try {
|
||||
const inventoryData = new Inventory({
|
||||
name,
|
||||
type,
|
||||
policies: verifiedPolicies,
|
||||
});
|
||||
|
||||
await inventoryData.save();
|
||||
@@ -64,7 +83,7 @@ module.exports = {
|
||||
return;
|
||||
}
|
||||
|
||||
const { name, type } = req.body;
|
||||
const { name, type, policies } = req.body;
|
||||
|
||||
if (!(name || type)) {
|
||||
res.status(400).send("Missing data in body");
|
||||
@@ -81,6 +100,37 @@ module.exports = {
|
||||
if (name) inventoryData.name = name;
|
||||
if (type) inventoryData.type = type;
|
||||
|
||||
if (policies) {
|
||||
const preferredLocations = [];
|
||||
if (policies.preferredLocations && Array.isArray(policies.preferredLocations)) {
|
||||
for (const preferredLocation of policies.preferredLocations) {
|
||||
preferredLocations.push({ id: preferredLocation.id, type: preferredLocation.type });
|
||||
}
|
||||
}
|
||||
|
||||
inventoryData.policies = {
|
||||
orderTracking: policies.orderTracking || inventoryData.policies.orderTracking,
|
||||
alerting: {
|
||||
lowestStockLevel:
|
||||
policies.alerting && policies.alerting.lowestStockLevel
|
||||
? policies.alerting.lowestStockLevel
|
||||
: inventoryData.policies.alerting.lowestStockLevel,
|
||||
highestStockLevel:
|
||||
policies.alerting && policies.alerting.highestStockLevel
|
||||
? policies.alerting.highestStockLevel
|
||||
: inventoryData.policies.alerting.highestStockLevel,
|
||||
alertStockLevel:
|
||||
policies.alerting && policies.alerting.alertStockLevel
|
||||
? policies.alerting.alertStockLevel
|
||||
: inventoryData.policies.alerting.alertStockLevel,
|
||||
reOrderLevel:
|
||||
policies.alerting && policies.alerting.reOrderLevel ? policies.alerting.reOrderLevel : inventoryData.policies.alerting.reOrderLevel,
|
||||
},
|
||||
replenishment: policies.replenishment || inventoryData.policies.replenishment,
|
||||
preferredLocations: preferredLocations,
|
||||
};
|
||||
}
|
||||
|
||||
await inventoryData.save();
|
||||
res.send({ success: true, data: inventoryData });
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const mongoose = require("mongoose");
|
||||
const { InventoryTypes } = require("./../config/constants");
|
||||
const { InventoryTypes, WarehouseScopes } = require("./../config/constants");
|
||||
|
||||
const schema = new mongoose.Schema(
|
||||
{
|
||||
@@ -15,15 +15,42 @@ const schema = new mongoose.Schema(
|
||||
enum: InventoryTypes,
|
||||
},
|
||||
policies: {
|
||||
tracking: {
|
||||
orderTracking: {
|
||||
type: Object, // Create a different model and reference it here once more details available
|
||||
},
|
||||
alerting: {
|
||||
type: Object, // Create a different model and reference it here once more details available
|
||||
lowestStockLevel: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
highestStockLevel: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
alertStockLevel: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
reOrderLevel: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
replenishment: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user