Working on delivery address

development
Visoth 8 months ago
parent 3a57cd0a0e
commit 719c2388be
  1. 6
      Config/db.js
  2. 2
      Controller/Category.js
  3. 140
      Controller/DeliveryAddress.js
  4. 23
      Controller/Supplier.js
  5. 1
      Controller/Warehouse.js
  6. 82
      Model/DeliveryModel.js
  7. 27
      Model/SupplierDeliveryModel.js
  8. 2
      app.js
  9. 27
      routes/delivery_address.js

@ -5,9 +5,11 @@ var reconnectOptions = {
console.log("connection lost, trying to connect (" + count + ")"); 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", { const db = new sequelize("TDDB", "sa", prod_db, {
host: "192.168.1.100", host: "51.79.251.248",
port: 1433, port: 1433,
dialect: "mssql", dialect: "mssql",
dialectOptions: { dialectOptions: {

@ -58,7 +58,7 @@ exports.getAllBrand = asyncHandler(async (req, res, next) => {
exports.createBrand = asyncHandler(async (req, res, next) => { exports.createBrand = asyncHandler(async (req, res, next) => {
console.log("REQ FILES ", req.files)
if (!req.files) { if (!req.files) {
return next(new ErrorResponse("Please upload a file", 400)) return next(new ErrorResponse("Please upload a file", 400))
} }

@ -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();
}
}

@ -2,11 +2,12 @@ const asyncHandler = require("../Middleware/async");
const sequelize = require('sequelize') const sequelize = require('sequelize')
const db = require('../Config/db') const db = require('../Config/db')
const SupplierModel = require('../Model/SupplierModel') const SupplierModel = require('../Model/SupplierModel')
const ErrorResponse = require('../utils/ErrorResponse') const ErrorResponse = require('../utils/errorResponse')
const fs = require('fs'); const fs = require('fs');
const SuccessResponse = require('../utils/SuccessResponse'); const SuccessResponse = require('../utils/SuccessResponse');
const dateFormat = require('dateformat'); const dateFormat = require('dateformat');
const SupplierDeliveryModel = require("../Model/SupplierDeliveryModel");
exports.getAllSupplier = asyncHandler(async (req, res, next) => { exports.getAllSupplier = asyncHandler(async (req, res, next) => {
try { try {
@ -39,7 +40,7 @@ exports.createSupplier = asyncHandler(async (req, res, next) => {
var photos = req.files; var photos = req.files;
console.log("PHOTOS ", photos) console.log("PHOTOS ", photos)
const t = await db.transaction();
try { try {
@ -63,7 +64,7 @@ exports.createSupplier = asyncHandler(async (req, res, next) => {
ADD_COM_1: data.company1 ?? "", ADD_COM_1: data.company1 ?? "",
ADD_COM_2: data.company2 ?? "", ADD_COM_2: data.company2 ?? "",
ADD_STAT: "A", ADD_STAT: "A",
ADD_TYPE: "1",// NOT SURE WHAT THIS IS ADD_TYPE: "1",// 1 for supplier 0 for Customer
TRANS_PRES: "N", TRANS_PRES: "N",
USER_CREA: date, USER_CREA: date,
USER_UPDT: date, USER_UPDT: date,
@ -71,6 +72,18 @@ exports.createSupplier = asyncHandler(async (req, res, next) => {
ADD_EMAIL: data.email ?? "", ADD_EMAIL: data.email ?? "",
ADD_WEB: data.website ?? "", 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) { if (photos) {
let folderPath = process.env.SUPPLIER_IMAGE_LOCATION + "/" + supplier.ADD_CODE + "/" 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.writeFileSync(folderPath + supplier.ADD_CODE + '.jpg', imageData)
fs.unlinkSync(req.file.path) fs.unlinkSync(req.file.path)
} }
await t.commit();
res.send(new SuccessResponse("Operation Successful")); res.send(new SuccessResponse("Operation Successful"));
} catch (e) { } catch (e) {
await t.rollback();
throw new ErrorResponse(e, 400) throw new ErrorResponse(e, 400)
} }
}) })
async function generateNextSupplierCode() { async function generateNextSupplierCode() {
const lastSupplier = await SupplierModel.findOne({ const lastSupplier = await SupplierModel.findOne({
order: [[sequelize.cast(sequelize.col('ADD_CODE'), 'integer'), 'DESC']] order: [[sequelize.cast(sequelize.col('ADD_CODE'), 'integer'), 'DESC']]

@ -31,6 +31,7 @@ exports.getAllWarehouse = asyncHandler(async (req, res, next) => {
exports.createWarehouse = asyncHandler(async (req, res, next) => { exports.createWarehouse = asyncHandler(async (req, res, next) => {
try { try {
var date = dateFormat(new Date(), "yyyy-mm-dd HH:mm:ss") var date = dateFormat(new Date(), "yyyy-mm-dd HH:mm:ss")
console.log("REQUEST BODY CREATE WAREHOUSE ", req.body)
await db.transaction(async (t) => { await db.transaction(async (t) => {
const warehouse = await WarehouseModel.create({ const warehouse = await WarehouseModel.create({
DB_CODE: req.user.DB_CODE, DB_CODE: req.user.DB_CODE,

@ -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;

@ -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;

@ -26,6 +26,7 @@ var subscriptionRouter = require('./routes/subscription')
var supplierRouter = require('./routes/supplier') var supplierRouter = require('./routes/supplier')
var inventoryRouter = require('./routes/inventory_movement') var inventoryRouter = require('./routes/inventory_movement')
var wareouseRouter = require('./routes/warehouse') var wareouseRouter = require('./routes/warehouse')
var deliveryAddressRouter = require('./routes/delivery_address')
//CORS //CORS
@ -63,6 +64,7 @@ app.use("/api/v1/category", categoryRouter)
app.use("/api/v1/supplier", supplierRouter) app.use("/api/v1/supplier", supplierRouter)
app.use("/api/v1/inventory", inventoryRouter) app.use("/api/v1/inventory", inventoryRouter)
app.use('/api/v1/warehouse', wareouseRouter) app.use('/api/v1/warehouse', wareouseRouter)
app.use('/api/v1/delivery_address', deliveryAddressRouter)

@ -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;
Loading…
Cancel
Save