update and delete item api

development
hengvisoth 8 months ago
parent 719c2388be
commit 404c516022
  1. 407
      Controller/Items.js

@ -1,218 +1,261 @@
const asyncHandler = require("../Middleware/async"); const asyncHandler = require("../Middleware/async");
const sequelize = require('sequelize') const sequelize = require("sequelize");
const DBInfo = require("../Model/Database"); const DBInfo = require("../Model/Database");
const ItemModel = require("../Model/ItemModel"); const ItemModel = require("../Model/ItemModel");
const CategoryModel = require("../Model/CategoryModel"); const CategoryModel = require("../Model/CategoryModel");
const SupplierItemModel = require("../Model/SupplierItemModel"); const SupplierItemModel = require("../Model/SupplierItemModel");
const ErrorResponse = require("../utils/ErrorResponse"); const ErrorResponse = require("../utils/ErrorResponse");
const SuccessResponse = require("../utils/SuccessResponse"); const SuccessResponse = require("../utils/SuccessResponse");
const fs = require('fs') const fs = require("fs");
const dateFormat = require("dateformat"); const dateFormat = require("dateformat");
const path = require('path'); const path = require("path");
const { log } = require("console"); const { log } = require("console");
const CategoryDetail = require("../Model/CategoryDetailModel"); const CategoryDetail = require("../Model/CategoryDetailModel");
exports.getAllItemsByStore = asyncHandler(async (req, res, next) => { exports.getAllItemsByStore = asyncHandler(async (req, res, next) => {
try { try {
let items = await ItemModel.findAll({ let items = await ItemModel.findAll({
where: {
DB_CODE: req.user.DB_CODE,
},
});
console.log("DB CODE ", req.user.DB_CODE);
const folderPath = path.join(
process.env.ITEM_IMAGE_LOCATION,
req.user.DB_CODE
);
// Use Promise.all to wait for all promises to resolve
items = await Promise.all(
items.map(async (item) => {
let itemObject = item.get({ plain: true });
try {
// Get category name
var category = await CategoryDetail.findOne({
where: { where: {
DB_CODE: req.user.DB_CODE ID: item.CAT_CODE,
} // DB_CODE: req.user.DB_CODE
}); },
});
console.log("DB CODE ", req.user.DB_CODE); if (category) {
itemObject.CAT_DESC_EN = category.DESC_EN;
itemObject.CAT_DESC_KH = category.DESC_KH;
itemObject.CAT_DESC_CN = category.DESC_CN;
}
} catch (catErr) {
console.error("Error fetching category", catErr);
// Optionally handle specific category fetch error
}
// Check existence of the image file
const imagePath = path.join(folderPath, `${item.ITEM_CODE}.jpg`);
itemObject.ITEM_IMG = fs.existsSync(imagePath) ? imagePath : "";
return itemObject;
})
);
res.status(200).send({
success: true,
data: items,
});
} catch (err) {
console.error("ERROR GET ALL ITEMS", err);
return next(new ErrorResponse(err.message, 400));
}
});
exports.createItemByStore = asyncHandler(async (req, res, next) => {
try {
var date = dateFormat(new Date(), "yyyy-mm-dd HH:mm:ss");
const data = JSON.parse(req.body["data"]);
const items = data["items"];
var photo = req.file.path;
let imageData = fs.readFileSync(req.file.path);
const itemCode = items["ITEM_CODE"];
const itemBarCode = items["ITEM_BCODE"];
const itemDes = items["ITEM_DESC"];
const unitStock = items["UNIT_STOCK"] ?? "1";
const unitSale = items["UNIT_SALE"] ?? "1";
const catCode = items["CATE_CODE"]; // CAT_CODE -> CAT_DETAIL_ID
const supplierCode = items["SUPP_CODE"];
const itemCost1 = items["ITEM_COST1"];
const itemPrice1 = items["ITEM_PRICE1"];
if (!itemBarCode || !itemDes || !catCode || !itemCost1 || !itemPrice1) {
throw "Please provide all required fields";
}
// var itemCode = await generateNextItemCode();
const folderPath = path.join(process.env.ITEM_IMAGE_LOCATION, req.user.DB_CODE); const existingItem = await ItemModel.findOne({
where: {
ITEM_CODE: itemCode,
DB_CODE: req.user.DB_CODE,
},
});
// Use Promise.all to wait for all promises to resolve if (existingItem) {
items = await Promise.all(items.map(async (item) => { throw `ITEM_CODE ${itemCode} already exists`;
let itemObject = item.get({ plain: true }); }
try { await ItemModel.sequelize.transaction(async (t) => {
// Get category name const item = await ItemModel.create({
var category = await CategoryDetail.findOne({ ITEM_CODE: itemCode,
where: { ITEM_BCODE: itemBarCode,
ID: item.CAT_CODE, ITEM_DESC: itemDes,
// DB_CODE: req.user.DB_CODE DB_CODE: req.user.DB_CODE,
} UNIT_STOCK: unitStock,
}); UNIT_SALE: unitSale,
if (category) { CAT_CODE: catCode,
itemObject.CAT_DESC_EN = category.DESC_EN; ITEM_STAT: "A",
itemObject.CAT_DESC_KH = category.DESC_KH; USER_CREA: req.user.USER_NAME,
itemObject.CAT_DESC_CN = category.DESC_CN; DATE_CREA: date.toString(),
} ITEM_TYPE: "S", // S -> Kit Stock N -> Min Kit Stock I-> ដរករទ
} catch (catErr) { ITEM_PRICE1: itemPrice1,
console.error("Error fetching category", catErr); ITEM_COST1: itemCost1,
// Optionally handle specific category fetch error ITEM_COST2: 0,
} ITEM_PRICE2: 0,
ITEM_CUS1: "",
ITEM_CUS2: "",
ITEM_CUS3: "",
ITEM_CUS4: "",
ITEM_CUS5: "",
ITEM_CUS6: "",
ITEM_CUS7: "",
ITEM_CUS8: "",
ITEM_CUS9_KH: "",
ITEM_CUS10_KH: "",
USER_UPDT: req.user.USER_NAME,
DATE_UPDT: date,
});
// add to TDITEMSP table
await SupplierItemModel.create({
ITEM_CODE: itemCode,
DB_CODE: req.user.DB_CODE,
SUPP_CODE: supplierCode,
SUPP_COST: itemCost1,
SUPP_UNIT: "1",
});
let folderPath =
process.env.ITEM_IMAGE_LOCATION + "/" + req.user.DB_CODE + "/";
if (!fs.existsSync(folderPath))
fs.mkdirSync(folderPath, {
recursive: true,
});
log("ITEM CODE ", item.ITEM_CODE);
fs.writeFileSync(folderPath + item.ITEM_CODE + ".jpg", imageData);
fs.unlinkSync(req.file.path);
res.send(new SuccessResponse("Operation Successful"));
});
} catch (err) {
console.log("ERROR CREATE ITEM", err);
fs.unlinkSync(req.file.path);
return next(new ErrorResponse(err, 400));
}
});
// Check existence of the image file exports.updateByItemCode = asyncHandler(async (req, res, next) => {
const imagePath = path.join(folderPath, `${item.ITEM_CODE}.jpg`); try {
itemObject.ITEM_IMG = fs.existsSync(imagePath) ? imagePath : ""; const itemCode = req.params.item_code;
const updateData = req.body;
return itemObject; const item = await ItemModel.findOne({
})); where: { ITEM_CODE: itemCode, DB_CODE: req.user.DB_CODE },
});
res.status(200).send({ if (!item) {
success: true, return next(new ErrorResponse("Item not found", 404));
data: items
});
} catch (err) {
console.error("ERROR GET ALL ITEMS", err);
return next(new ErrorResponse(err.message, 400));
} }
});
exports.createItemByStore = asyncHandler(async (req, res, next) => {
try {
var date = dateFormat(new Date(), "yyyy-mm-dd HH:mm:ss")
const data = JSON.parse(req.body['data']);
const items = data['items'];
var photo = req.file.path
let imageData = fs.readFileSync(req.file.path)
const itemCode = items['ITEM_CODE'];
const itemBarCode = items['ITEM_BCODE']
const itemDes = items['ITEM_DESC'];
const unitStock = items['UNIT_STOCK'] ?? '1';
const unitSale = items['UNIT_SALE'] ?? '1';
const catCode = items['CATE_CODE']; // CAT_CODE -> CAT_DETAIL_ID
const supplierCode = items['SUPP_CODE'];
const itemCost1 = items['ITEM_COST1'];
const itemPrice1 = items['ITEM_PRICE1'];
if (!itemBarCode || !itemDes || !catCode || !itemCost1 || !itemPrice1) {
throw ("Please provide all required fields");
}
// var itemCode = await generateNextItemCode();
await item.update(updateData);
const existingItem = await ItemModel.findOne({ res.status(200).send({
where: { success: true,
ITEM_CODE: itemCode, data: item,
DB_CODE: req.user.DB_CODE });
} } catch (err) {
}); console.error("ERROR UPDATE ITEM", err);
return next(new ErrorResponse(err.message, 400));
}
});
if (existingItem) { exports.deleteByItemCode = asyncHandler(async (req, res, next) => {
throw (`ITEM_CODE ${itemCode} already exists`); try {
} const itemCode = req.params.item_code;
const item = await ItemModel.findOne({
where: { ITEM_CODE: itemCode, DB_CODE: req.user.DB_CODE },
});
await ItemModel.sequelize.transaction(async (t) => { if (!item) {
const item = await ItemModel.create({ return next(new ErrorResponse("Item not found", 404));
ITEM_CODE: itemCode,
ITEM_BCODE: itemBarCode,
ITEM_DESC: itemDes,
DB_CODE: req.user.DB_CODE,
UNIT_STOCK: unitStock,
UNIT_SALE: unitSale,
CAT_CODE: catCode,
ITEM_STAT: "A",
USER_CREA: req.user.USER_NAME,
DATE_CREA: date.toString(),
ITEM_TYPE: "S", // S -> Kit Stock N -> Min Kit Stock I-> ដរករទ
ITEM_PRICE1: itemPrice1,
ITEM_COST1: itemCost1,
ITEM_COST2: 0,
ITEM_PRICE2: 0,
ITEM_CUS1: "",
ITEM_CUS2: "",
ITEM_CUS3: "",
ITEM_CUS4: "",
ITEM_CUS5: "",
ITEM_CUS6: "",
ITEM_CUS7: "",
ITEM_CUS8: "",
ITEM_CUS9_KH: "",
ITEM_CUS10_KH: "",
USER_UPDT: req.user.USER_NAME,
DATE_UPDT: date,
})
// add to TDITEMSP table
await SupplierItemModel.create({
ITEM_CODE: itemCode,
DB_CODE: req.user.DB_CODE,
SUPP_CODE: supplierCode,
SUPP_COST: itemCost1,
SUPP_UNIT: "1"
})
let folderPath = process.env.ITEM_IMAGE_LOCATION + "/" + req.user.DB_CODE + "/"
if (!fs.existsSync(folderPath)) fs.mkdirSync(folderPath, {
recursive: true,
})
log("ITEM CODE ", item.ITEM_CODE)
fs.writeFileSync(folderPath + item.ITEM_CODE + '.jpg', imageData)
fs.unlinkSync(req.file.path)
res.send(new SuccessResponse("Operation Successful"));
})
} catch (err) {
console.log("ERROR CREATE ITEM", err)
fs.unlinkSync(req.file.path)
return next(new ErrorResponse(err, 400));
} }
})
// check user permission to delete item
await item.destroy();
res.status(200).send({
success: true,
data: {},
message: `Item with ITEM_CODE ${itemCode} deleted successfully`,
});
} catch (err) {
console.error("ERROR DELETE ITEM", err);
return next(new ErrorResponse(err.message, 400));
}
});
exports.checkIfItemExist = asyncHandler(async (req, res, next) => { exports.checkIfItemExist = asyncHandler(async (req, res, next) => {
try { try {
const item = await ItemModel.findOne({ const item = await ItemModel.findOne({
where: { where: {
ITEM_CODE: req.params.item_code, ITEM_CODE: req.params.item_code,
DB_CODE: req.user.DB_CODE DB_CODE: req.user.DB_CODE,
} },
}) });
if (!item) { if (!item) {
return next(new ErrorResponse("Item not found", 400)); return next(new ErrorResponse("Item not found", 400));
}
res.status(200).send({
success: true,
data: item
})
} catch (err) {
console.log("ERROR CHECK ITEM", err)
return next(new ErrorResponse(err, 400));
} }
} res.status(200).send({
) success: true,
data: item,
});
} catch (err) {
console.log("ERROR CHECK ITEM", err);
return next(new ErrorResponse(err, 400));
}
});
async function generateNextItemCode() { async function generateNextItemCode() {
try { try {
const lastItem = await ItemModel.findOne({
order: [["ITEM_CODE", "DESC"]],
const lastItem = await ItemModel.findOne({ });
order: [['ITEM_CODE', 'DESC']] console.log("LAST ITEM ", lastItem);
}); if (!lastItem) {
console.log("LAST ITEM ", lastItem) return "0001"; // Starting point if no suppliers are present
if (!lastItem) {
return '0001'; // Starting point if no suppliers are present
}
console.log("LAST ITEM CODE ", lastItem.ITEM_CODE)
let nextCode = parseInt(lastItem.ITEM_CODE, 10) + 1;
console.log("NEXT CODE ", nextCode)
// Check if the number is less than 10000 for zero-padding
if (nextCode < 10000) {
return nextCode.toString().padStart(4, '0');
} else {
// If it's 10000 or more, just return the number
return nextCode.toString();
}
} }
catch (e) { console.log("LAST ITEM CODE ", lastItem.ITEM_CODE);
console.log("CATCH ERROR IN GENERATE ITEM CODE ", e) let nextCode = parseInt(lastItem.ITEM_CODE, 10) + 1;
console.log("NEXT CODE ", nextCode);
// Check if the number is less than 10000 for zero-padding
if (nextCode < 10000) {
return nextCode.toString().padStart(4, "0");
} else {
// If it's 10000 or more, just return the number
return nextCode.toString();
} }
} } catch (e) {
console.log("CATCH ERROR IN GENERATE ITEM CODE ", e);
}
}

Loading…
Cancel
Save