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

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
const mongoose = require("mongoose");
const { InventoryTypes, WarehouseScopes } = require("./../config/constants");
// const { InventoryTypes } = require("./../config/constants");
const schema = new mongoose.Schema(
{
@@ -8,49 +8,41 @@ const schema = new mongoose.Schema(
required: true,
trim: true,
},
type: {
widgetName: {
type: String,
required: true,
trim: true,
enum: InventoryTypes,
},
policies: {
orderTracking: {
type: Object, // Create a different model and reference it here once more details available
},
alerting: {
lowestStockLevel: {
type: Boolean,
required: true,
},
highestStockLevel: {
type: Boolean,
required: true,
},
alertStockLevel: {
type: Boolean,
required: true,
},
reOrderLevel: {
type: Boolean,
required: true,
},
},
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
},
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: {
type: Number,
},
reorderStockLevelCount: {
type: Number,
},
},
},
{

View File

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