feat: added item images management apis
This commit is contained in:
@@ -11,7 +11,7 @@ const {
|
||||
ReportItemTransaction,
|
||||
AdjustItemTransaction,
|
||||
} = require("../models/ItemTransaction");
|
||||
|
||||
const { S3 } = require("./../config/aws");
|
||||
const ItemAssociation = require("../models/ItemAssociation");
|
||||
const Sublevel = require("../models/Sublevel");
|
||||
const { InventoryTypes, ReportItemForTypes } = require("../config/constants");
|
||||
@@ -72,6 +72,7 @@ module.exports = {
|
||||
};
|
||||
|
||||
for (const key of Object.keys(item)) {
|
||||
if (["customAttributes"].includes(key)) continue;
|
||||
if (item[key] === undefined) {
|
||||
res.status(400).send({ success: false, error: `Missing required param: "${key}"` });
|
||||
return;
|
||||
@@ -86,6 +87,19 @@ module.exports = {
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
|
||||
const images = req.files;
|
||||
|
||||
for (let i = 0; i < images.length; i++) {
|
||||
const url = await S3.uploadFile(
|
||||
`item/${itemData._id.toString()}-${Date.now()}-${i}.${images[i].originalname.split(".").slice(-1).pop()}`,
|
||||
images[i].path
|
||||
);
|
||||
itemData.images ||= [];
|
||||
itemData.images.push({ url });
|
||||
}
|
||||
itemData.save();
|
||||
|
||||
res.send({ success: true, data: itemData });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -498,4 +512,46 @@ module.exports = {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
|
||||
addImageToItem: async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
if (!mongoose.isValidObjectId(id)) {
|
||||
res.status(400).send({ success: false, error: "invalid id" });
|
||||
}
|
||||
|
||||
const item = await Item.findById(id);
|
||||
if (!item) {
|
||||
res.status(404).send({ success: false, error: "item not found" });
|
||||
}
|
||||
|
||||
const image = req.file;
|
||||
const url = await S3.uploadFile(`item/${item._id.toString()}-${Date.now()}-0.${image.originalname.split(".").slice(-1).pop()}`, image.path);
|
||||
item.images ||= [];
|
||||
item.images.push({ url });
|
||||
await item.save();
|
||||
|
||||
res.send({ success: true, data: item });
|
||||
},
|
||||
|
||||
removeImageFromItem: async (req, res, next) => {
|
||||
const { id, image_id } = req.params;
|
||||
if (!mongoose.isValidObjectId(id)) {
|
||||
res.status(400).send({ success: false, error: "invalid id" });
|
||||
}
|
||||
if (!mongoose.isValidObjectId(image_id)) {
|
||||
res.status(400).send({ success: false, error: "invalid image_id" });
|
||||
}
|
||||
|
||||
const item = await Item.findById(id);
|
||||
if (!item) {
|
||||
res.status(404).send({ success: false, error: "item not found" });
|
||||
}
|
||||
|
||||
item.images = item.images.filter((itemImage) => {
|
||||
return (itemImage._id.toString() != image_id);
|
||||
});
|
||||
|
||||
await item.save();
|
||||
res.send({ success: true, data: item });
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
const router = require("express").Router();
|
||||
const controller = require("./item.controller");
|
||||
const { AuthenticateMiddleware, ItemTransactionCheck } = require("./utils/authorize");
|
||||
const multer = require("multer");
|
||||
const upload = multer({ dest: "tmp/uploads/" });
|
||||
|
||||
/**
|
||||
* @route /item/
|
||||
*/
|
||||
router.post("/", controller.createItem);
|
||||
router.post("/", upload.any("images"), controller.createItem);
|
||||
|
||||
/**
|
||||
* @route /item/:id/image
|
||||
*/
|
||||
router.post("/:id/image", upload.single("image"), controller.addImageToItem);
|
||||
|
||||
/**
|
||||
* @route /item/:id/image/:image_id
|
||||
*/
|
||||
router.delete("/:id/image/:image_id", controller.removeImageFromItem);
|
||||
|
||||
/**
|
||||
* @route /item/
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
const mongoose = require("mongoose");
|
||||
const { CustomAttributeTypes } = require("./../config/constants");
|
||||
|
||||
const ItemImage = new mongoose.Schema({
|
||||
url: {
|
||||
type: String,
|
||||
trim: true,
|
||||
},
|
||||
});
|
||||
|
||||
const schema = new mongoose.Schema(
|
||||
{
|
||||
commonName: {
|
||||
@@ -13,12 +20,7 @@ const schema = new mongoose.Schema(
|
||||
required: true,
|
||||
trim: true,
|
||||
},
|
||||
images: [
|
||||
{
|
||||
type: String,
|
||||
trim: true,
|
||||
},
|
||||
],
|
||||
images: [ItemImage],
|
||||
description: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
||||
Reference in New Issue
Block a user