feat: get all inventories & image for inventory apis

This commit is contained in:
Sathishkumar Krishnan
2022-02-27 12:59:58 +05:30
parent 80af7080ca
commit 208430cb53
9 changed files with 317 additions and 13 deletions

36
src/config/aws.js Normal file
View File

@@ -0,0 +1,36 @@
const AWS = require("aws-sdk");
const fs = require("fs");
const { AWS_S3_ACCESS_KEY_ID, AWS_S3_SECRET_ACCESS_KEY, AWS_S3_BUCKET, AWS_S3_REGION } = require("./env");
AWS.config.update({
maxRetries: 3,
accessKeyId: AWS_S3_ACCESS_KEY_ID,
secretAccessKey: AWS_S3_SECRET_ACCESS_KEY,
region: AWS_S3_REGION,
});
const S3 = new AWS.S3();
module.exports = {
S3: {
uploadFile: async (key, filepath) => {
const fileReadStream = fs.createReadStream(filepath);
const params = {
Bucket: AWS_S3_BUCKET,
Key: key,
Body: fileReadStream,
};
try {
const response = await S3.upload(params).promise();
console.log("S3 Upload success", response);
fs.rmSync(filepath);
return response.Location;
} catch (error) {
console.log("S3 Upload Error", error);
fs.rmSync(filepath);
return false;
}
},
},
};

View File

@@ -4,9 +4,12 @@ const envVariables = {
API_PORT: process.env.API_PORT || "3000",
MONGODB_URI: process.env.MONGODB_URI || "mongodb://localhost:12017",
JWT_SECRET: process.env.JWT_SECRET || "secret123",
JWT_REFRESH_EXPIRY_TIME:
parseInt(process.env.JWT_REFRESH_EXPIRY_TIME) || 3600,
JWT_REFRESH_EXPIRY_TIME: parseInt(process.env.JWT_REFRESH_EXPIRY_TIME) || 3600,
JWT_ACCESS_EXPIRY_TIME: parseInt(process.env.JWT_ACCESS_EXPIRY_TIME) || 86400,
AWS_S3_BUCKET: process.env.AWS_S3_BUCKET,
AWS_S3_ACCESS_KEY_ID: process.env.AWS_S3_ACCESS_KEY_ID,
AWS_S3_SECRET_ACCESS_KEY: process.env.AWS_S3_SECRET_ACCESS_KEY,
AWS_S3_REGION: process.env.AWS_S3_REGION || "us-east-2",
};
module.exports = envVariables;

View File

@@ -1,7 +1,9 @@
const mongoose = require("mongoose");
const Inventory = require("../models/Inventory");
const WidgetFamily = require("../models/WidgetFamily");
const { InventoryTypes } = require("../config/constants");
const mongoose = require("mongoose");
const { S3 } = require("./../config/aws");
module.exports = {
/**
@@ -34,11 +36,36 @@ module.exports = {
}
},
getInventories: async (req, res, next) => {
let { page, perPage } = req.query;
page = page ? parseInt(page) : 0;
perPage = perPage ? parseInt(perPage) : 10;
try {
const inventoryData = await Inventory.find()
.skip(parseInt(page) * parseInt(perPage))
.limit(parseInt(perPage));
if (!inventoryData) {
res.status(404);
return;
}
for (const inventory of inventoryData) {
inventory["widgetFamilies"] = await WidgetFamily.find({ inventory: inventory._id });
}
res.send({ success: true, data: inventoryData });
} catch (error) {
next(error);
}
},
/**
* Create a Inventory
*/
createInventory: async (req, res, next) => {
const { name, policies, widgetName } = req.body;
let { name, policies, widgetName } = req.body;
const image = req.file;
if (!(name && widgetName)) {
res.status(400).send("Missing params name");
@@ -49,6 +76,8 @@ module.exports = {
for (const preferredLocation of policies.preferredLocations) {
preferredLocations.push({ id: preferredLocation.id, type: preferredLocation.type });
}
} else if (!policies) {
policies = {};
}
const verifiedPolicies = {
@@ -66,12 +95,20 @@ module.exports = {
policies: verifiedPolicies,
});
if (image) {
const url = await S3.uploadFile(
`inventory/${inventoryData._id.toString()}.${image.originalname.split(".").slice(-1).pop()}`,
image.path
);
inventoryData.image_url = url;
}
await inventoryData.save();
if (!inventoryData) {
res.status(404);
return;
}
// const widgetFamilyData = createWidgetFamiliesData(inventoryData, widgetFamily);
res.send({ success: true, data: { inventoryData } });
} catch (error) {
next(error);
@@ -120,6 +157,15 @@ module.exports = {
};
inventory.policies = verifiedPolicies;
const image = req.file;
if (image) {
const url = await S3.uploadFile(
`inventory/${inventory._id.toString()}.${image.originalname.split(".").slice(-1).pop()}`,
image.path
);
inventory.image_url = url;
}
await inventory.save();
res.send({ success: true, data: { inventory } });
} catch (error) {

View File

@@ -1,21 +1,27 @@
const router = require("express").Router();
const controller = require("./inventory.controller");
const multer = require("multer");
const upload = multer({ dest: "tmp/uploads/" });
/**
* @route /inventory/
*/
router.post("/", upload.single("image"), controller.createInventory);
/**
* @route /inventory/
*/
router.post("/", controller.createInventory);
/**
* @route /inventory/
*/
router.patch("/:id", controller.updateInventoryByID);
router.patch("/:id", upload.single("image"), controller.updateInventoryByID);
/**
* @route /inventory/types
*/
router.get("/types", controller.getInventoryTypes);
/**
* @route /inventory/all
*/
router.get("/all", controller.getInventories);
/**
* @route /inventory/filter-by-type
*/

View File

@@ -8,6 +8,10 @@ const schema = new mongoose.Schema(
required: true,
trim: true,
},
image_url: {
type: String,
trim: true,
},
widgetName: {
type: String,
required: true,