diff --git a/Config/db.js b/Config/db.js index 4842545..d42468d 100644 --- a/Config/db.js +++ b/Config/db.js @@ -5,9 +5,11 @@ var reconnectOptions = { console.log("connection lost, trying to connect (" + count + ")"); } } +// const local_db = "TD@admin168" +const prod_db = "T0n@d!g!t@l2023" -const db = new sequelize("TDDB", "sa", "TD@dmin168", { - host: "192.168.1.100", +const db = new sequelize("TDDB", "sa", prod_db, { + host: "51.79.251.248", port: 1433, dialect: "mssql", dialectOptions: { diff --git a/Controller/Category.js b/Controller/Category.js index 1615436..419ec10 100644 --- a/Controller/Category.js +++ b/Controller/Category.js @@ -58,7 +58,7 @@ exports.getAllBrand = asyncHandler(async (req, res, next) => { exports.createBrand = asyncHandler(async (req, res, next) => { - + console.log("REQ FILES ", req.files) if (!req.files) { return next(new ErrorResponse("Please upload a file", 400)) } diff --git a/Controller/DeliveryAddress.js b/Controller/DeliveryAddress.js new file mode 100644 index 0000000..d390917 --- /dev/null +++ b/Controller/DeliveryAddress.js @@ -0,0 +1,140 @@ + + +const asyncHandler = require("../Middleware/async"); +const sequelize = require('sequelize') +const db = require('../Config/db') +const SupplierModel = require('../Model/SupplierModel') +const ErrorResponse = require('../utils/errorResponse') +const SuccessResponse = require('../utils/SuccessResponse') +const fs = require('fs'); +const DeliveryAdressModel = require('../Model/DeliveryModel') + +const dateFormat = require('dateformat'); + +exports.getAllDeliveryAddress = asyncHandler(async (req, res, next) => { + try { + + const deliveryAddress = await DeliveryAdressModel.findAll({ + where: { + DEL_TYPE: "1" + } + }) + // console.log(obj); + // const supplier = await SupplierModel.findAll({ + // where: { + // DB_CODE: req.user.DB_CODE + // } + // } + // ) + res.status(200).send({ + success: true, + data: deliveryAddress + }) + } catch (e) { + console.log("ERROR : ", e) + throw new ErrorResponse(e, 400) + } +}) + +exports.getAllDeliveryAddressBySupplier = asyncHandler(async (req, res, next) => { + try { + const deliveryAddress = await DeliveryAddressModel.findAll({ + where: { + DB_CODE: req.dbcode, + DEL_CODE: req.params.id + } + }) + res.status(200).send({ + success: true, + data: deliveryAddress + }) + } + catch (e) { + throw new ErrorResponse(e, 400) + } +}) + +exports.postDeliveryAddress = asyncHandler(async (req, res, next) => { + const { DEL_ADD_1, DEL_NAME } = req.body; + + const data = req.body; + if (!DEL_ADD_1) { + return next( + new ErrorResponse("Please provide a delivery address information!", 400) + ); + } + + if (!DEL_NAME) { + return next( + new ErrorResponse("Please provide a delivery name!", 400) + ); + } + + let ADD_CODE + if (!data["ADD_CODE"]) { + ADD_CODE = await generateNextAddressCode(req.user.DB_CODE); + } + try { + await DeliveryAdressModel.sequelize.transaction(async (t) => { + await DeliveryAdressModel.create( + { + ADD_CODE: "", // if it is supplier then no need to add ADD_CODE in this table + DB_CODE: req.user.DB_CODE, + DEL_CODE: data["ADD_CODE"] ?? ADD_CODE, + DEL_NAME: data["DEL_NAME"], + DEL_ADD_1: DEL_ADD_1, + DEL_ADD_2: "", + DEL_ADD_3: "", + DEL_ADD_4: "", + DEL_ADD_5: "", + DEL_TEL: data["DEL_TEL"] ?? "", + DEL_FAX: data["DEL_FAX"] ?? "", + DEL_EMAIL: "", + DEL_WEB: "", + DEL_CONT: "", + DEL_COM_1: "", + DEL_COM_2: "", + DEL_TYPE: "0", // 1 for CUSTOMER 0 for SUPPLIER + USER_CREA: dateFormat(new Date(), "yyyy-mm-dd"), + USER_UPDT: dateFormat(new Date(), "yyyy-mm-dd"), + USER_CODE: req.user.USER_NAME, + }, + { transaction: t } + ); + res.send(new SuccessResponse("success")).status(200); + }); + } catch (error) { + + return next(new ErrorResponse(`Server Error ${error}`, 500)); + } +}) + + + + +async function generateNextAddressCode(dbCode) { + const lastSupplier = await DeliveryAddressModel.findOne( + { + where: { + DB_CODE: dbCode + }, + order: [ + [sequelize.cast(sequelize.col('ADD_CODE'), 'integer'), 'DESC'] + ] + }); + + if (!lastSupplier) { + return '0001'; // Starting point if no suppliers are present + } + console.log("LAST SUPPLIER CODE ", lastSupplier.ADD_CODE) + + let nextCode = parseInt(lastSupplier.ADD_CODE, 10) + 1; + + // 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(); + } +} \ No newline at end of file diff --git a/Controller/Supplier.js b/Controller/Supplier.js index 074e858..ed4deab 100644 --- a/Controller/Supplier.js +++ b/Controller/Supplier.js @@ -2,11 +2,12 @@ const asyncHandler = require("../Middleware/async"); const sequelize = require('sequelize') const db = require('../Config/db') const SupplierModel = require('../Model/SupplierModel') -const ErrorResponse = require('../utils/ErrorResponse') +const ErrorResponse = require('../utils/errorResponse') const fs = require('fs'); const SuccessResponse = require('../utils/SuccessResponse'); const dateFormat = require('dateformat'); +const SupplierDeliveryModel = require("../Model/SupplierDeliveryModel"); exports.getAllSupplier = asyncHandler(async (req, res, next) => { try { @@ -39,7 +40,7 @@ exports.createSupplier = asyncHandler(async (req, res, next) => { var photos = req.files; console.log("PHOTOS ", photos) - + const t = await db.transaction(); try { @@ -63,7 +64,7 @@ exports.createSupplier = asyncHandler(async (req, res, next) => { ADD_COM_1: data.company1 ?? "", ADD_COM_2: data.company2 ?? "", ADD_STAT: "A", - ADD_TYPE: "1",// NOT SURE WHAT THIS IS + ADD_TYPE: "1",// 1 for supplier 0 for Customer TRANS_PRES: "N", USER_CREA: date, USER_UPDT: date, @@ -71,6 +72,18 @@ exports.createSupplier = asyncHandler(async (req, res, next) => { ADD_EMAIL: data.email ?? "", ADD_WEB: data.website ?? "", + }, { trsanction: t }).then(async (supplier) => { + var selectedDelivery = data.selectedDelivery + for (var i = 0; i < selectedDelivery.length; i++) { + await SupplierDeliveryModel.create({ + ADD_CODE: supplier.ADD_CODE, + DEL_CODE: selectedDelivery[i].DEL_CODE, + + }, { transaction: t }) + } + + + }) if (photos) { let folderPath = process.env.SUPPLIER_IMAGE_LOCATION + "/" + supplier.ADD_CODE + "/" @@ -82,14 +95,18 @@ exports.createSupplier = asyncHandler(async (req, res, next) => { fs.writeFileSync(folderPath + supplier.ADD_CODE + '.jpg', imageData) fs.unlinkSync(req.file.path) } + + await t.commit(); res.send(new SuccessResponse("Operation Successful")); } catch (e) { + await t.rollback(); throw new ErrorResponse(e, 400) } }) + async function generateNextSupplierCode() { const lastSupplier = await SupplierModel.findOne({ order: [[sequelize.cast(sequelize.col('ADD_CODE'), 'integer'), 'DESC']] diff --git a/Controller/Warehouse.js b/Controller/Warehouse.js index 31480e0..7b6f4cb 100644 --- a/Controller/Warehouse.js +++ b/Controller/Warehouse.js @@ -31,6 +31,7 @@ exports.getAllWarehouse = asyncHandler(async (req, res, next) => { exports.createWarehouse = asyncHandler(async (req, res, next) => { try { var date = dateFormat(new Date(), "yyyy-mm-dd HH:mm:ss") + console.log("REQUEST BODY CREATE WAREHOUSE ", req.body) await db.transaction(async (t) => { const warehouse = await WarehouseModel.create({ DB_CODE: req.user.DB_CODE, diff --git a/Model/DeliveryModel.js b/Model/DeliveryModel.js new file mode 100644 index 0000000..f9e2707 --- /dev/null +++ b/Model/DeliveryModel.js @@ -0,0 +1,82 @@ +const sequelize = require('sequelize') +const db = require('../Config/db') + +const DeliveryAddressModel = db.define( + 'TDDADD', + { + ADD_CODE: { + type: sequelize.STRING, + }, + DB_CODE: { + type: sequelize.STRING + }, + DEL_CODE: { + type: sequelize.STRING + + }, + DEL_NAME: { + type: sequelize.STRING + }, + DEL_ADD_1: { + type: sequelize.STRING + }, + DEL_ADD_2: { + type: sequelize.STRING + }, + DEL_ADD_3: { + type: sequelize.STRING + }, + DEL_ADD_4: { + type: sequelize.STRING + }, + DEL_ADD_5: { + type: sequelize.STRING + }, + DEL_TEL: { + type: sequelize.STRING + }, + DEL_FAX: { + type: sequelize.STRING + }, + DEL_EMAIL: { + type: sequelize.STRING + }, + DEL_WEB: { + type: sequelize.STRING + }, + DEL_CONT: { + type: sequelize.STRING + }, + DEL_COM_1: { + type: sequelize.STRING + }, + DEL_COM_2: { + type: sequelize.STRING + }, + DEL_TYPE: { + type: sequelize.CHAR + }, + DEL_STAT: { + type: sequelize.CHAR + }, + USER_CREA: { + type: sequelize.STRING + + }, + USER_UPDT: { + type: sequelize.STRING + }, + USER_CODE: { + type: sequelize.STRING + } + + }, + { + timestamps: false, + freezeTableName: true, + } +) + + +DeliveryAddressModel.removeAttribute('id') +module.exports = DeliveryAddressModel; \ No newline at end of file diff --git a/Model/SupplierDeliveryModel.js b/Model/SupplierDeliveryModel.js new file mode 100644 index 0000000..b7c31ec --- /dev/null +++ b/Model/SupplierDeliveryModel.js @@ -0,0 +1,27 @@ +const sequelize = require('sequelize') +const db = require('../Config/db') + +const SupplierDeliveryModel = db.define( + 'TDDELA', + { + ADD_CODE: { + type: sequelize.STRING, + primaryKey: true + }, + DEL_CODE: { + type: sequelize.STRING, + primaryKey: true + + }, + + + }, + { + timestamps: false, + freezeTableName: true, + } +) + + +SupplierDeliveryModel.removeAttribute('id') +module.exports = SupplierDeliveryModel; \ No newline at end of file diff --git a/app.js b/app.js index 6494ca5..62f80e8 100644 --- a/app.js +++ b/app.js @@ -26,6 +26,7 @@ var subscriptionRouter = require('./routes/subscription') var supplierRouter = require('./routes/supplier') var inventoryRouter = require('./routes/inventory_movement') var wareouseRouter = require('./routes/warehouse') +var deliveryAddressRouter = require('./routes/delivery_address') //CORS @@ -63,6 +64,7 @@ app.use("/api/v1/category", categoryRouter) app.use("/api/v1/supplier", supplierRouter) app.use("/api/v1/inventory", inventoryRouter) app.use('/api/v1/warehouse', wareouseRouter) +app.use('/api/v1/delivery_address', deliveryAddressRouter) diff --git a/routes/delivery_address.js b/routes/delivery_address.js new file mode 100644 index 0000000..2b221f9 --- /dev/null +++ b/routes/delivery_address.js @@ -0,0 +1,27 @@ +const express = require("express"); +const route = express.Router(); +const controller = require("../Controller/DeliveryAddress"); +const { protect } = require("../Middleware/auth") + +const multer = require("multer"); +const moment = require("moment"); +var storage = multer.diskStorage({ + destination: function (req, file, cb) { + if (file.fieldname == "supplier_image") cb(null, "uploads/supplier/tmp_post"); + }, + + filename: function (req, file, cb) { + cb( + null, + moment(Date.now()).format("YYYY-MM-DD_HH-mm-ss_") + file.originalname + ); + }, +}); + +var upload = multer({ storage: storage }); + + +route.route('/').get(protect, controller.getAllDeliveryAddress) +route.route('/').post(protect, controller.postDeliveryAddress) + +module.exports = route; \ No newline at end of file