Compare commits
No commits in common. "97bdf613c6a781f473b841a1b4dfca76e9cf9fac" and "0fb0493d99252773cb7452c64de4851c07117333" have entirely different histories.
97bdf613c6
...
0fb0493d99
@ -1,50 +0,0 @@
|
|||||||
import express from 'express';
|
|
||||||
import { isAuthenticated } from '../../keycloak.js';
|
|
||||||
import { getFilter, convertPropertiesString } from '../../utils.js';
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
import {
|
|
||||||
listOrderItemsRouteHandler,
|
|
||||||
getOrderItemRouteHandler,
|
|
||||||
editOrderItemRouteHandler,
|
|
||||||
newOrderItemRouteHandler,
|
|
||||||
deleteOrderItemRouteHandler,
|
|
||||||
listOrderItemsByPropertiesRouteHandler,
|
|
||||||
} from '../../services/inventory/orderitems.js';
|
|
||||||
|
|
||||||
// list of order items
|
|
||||||
router.get('/', isAuthenticated, (req, res) => {
|
|
||||||
const { page, limit, property, search, sort, order } = req.query;
|
|
||||||
const allowedFilters = ['itemType', 'item', 'item._id', 'order', 'order._id', 'orderType'];
|
|
||||||
const filter = getFilter(req.query, allowedFilters);
|
|
||||||
listOrderItemsRouteHandler(req, res, page, limit, property, filter, search, sort, order);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/properties', isAuthenticated, (req, res) => {
|
|
||||||
let properties = convertPropertiesString(req.query.properties);
|
|
||||||
const allowedFilters = ['itemType', 'item', 'item._id', 'order', 'order._id', 'orderType'];
|
|
||||||
const filter = getFilter(req.query, allowedFilters, false);
|
|
||||||
var masterFilter = {};
|
|
||||||
if (req.query.masterFilter) {
|
|
||||||
masterFilter = JSON.parse(req.query.masterFilter);
|
|
||||||
}
|
|
||||||
listOrderItemsByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/', isAuthenticated, (req, res) => {
|
|
||||||
newOrderItemRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/:id', isAuthenticated, (req, res) => {
|
|
||||||
getOrderItemRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.put('/:id', isAuthenticated, async (req, res) => {
|
|
||||||
editOrderItemRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/:id', isAuthenticated, async (req, res) => {
|
|
||||||
deleteOrderItemRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@ -1,64 +0,0 @@
|
|||||||
import express from 'express';
|
|
||||||
import { isAuthenticated } from '../../keycloak.js';
|
|
||||||
import { getFilter, convertPropertiesString } from '../../utils.js';
|
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
import {
|
|
||||||
listShipmentsRouteHandler,
|
|
||||||
getShipmentRouteHandler,
|
|
||||||
editShipmentRouteHandler,
|
|
||||||
newShipmentRouteHandler,
|
|
||||||
deleteShipmentRouteHandler,
|
|
||||||
listShipmentsByPropertiesRouteHandler,
|
|
||||||
} from '../../services/inventory/shipments.js';
|
|
||||||
|
|
||||||
// list of shipments
|
|
||||||
router.get('/', isAuthenticated, (req, res) => {
|
|
||||||
const { page, limit, property, search, sort, order } = req.query;
|
|
||||||
const allowedFilters = [
|
|
||||||
'vendor',
|
|
||||||
'purchaseOrder',
|
|
||||||
'state',
|
|
||||||
'courierService',
|
|
||||||
'vendor._id',
|
|
||||||
'purchaseOrder._id',
|
|
||||||
];
|
|
||||||
const filter = getFilter(req.query, allowedFilters);
|
|
||||||
listShipmentsRouteHandler(req, res, page, limit, property, filter, search, sort, order);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/properties', isAuthenticated, (req, res) => {
|
|
||||||
let properties = convertPropertiesString(req.query.properties);
|
|
||||||
const allowedFilters = [
|
|
||||||
'vendor',
|
|
||||||
'purchaseOrder',
|
|
||||||
'state.type',
|
|
||||||
'courierService',
|
|
||||||
'vendor._id',
|
|
||||||
'purchaseOrder._id',
|
|
||||||
];
|
|
||||||
const filter = getFilter(req.query, allowedFilters, false);
|
|
||||||
var masterFilter = {};
|
|
||||||
if (req.query.masterFilter) {
|
|
||||||
masterFilter = JSON.parse(req.query.masterFilter);
|
|
||||||
}
|
|
||||||
listShipmentsByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/', isAuthenticated, (req, res) => {
|
|
||||||
newShipmentRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/:id', isAuthenticated, (req, res) => {
|
|
||||||
getShipmentRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.put('/:id', isAuthenticated, async (req, res) => {
|
|
||||||
editShipmentRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/:id', isAuthenticated, async (req, res) => {
|
|
||||||
deleteShipmentRouteHandler(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
||||||
@ -15,14 +15,14 @@ import {
|
|||||||
// list of vendors
|
// list of vendors
|
||||||
router.get('/', isAuthenticated, (req, res) => {
|
router.get('/', isAuthenticated, (req, res) => {
|
||||||
const { page, limit, property, search, sort, order } = req.query;
|
const { page, limit, property, search, sort, order } = req.query;
|
||||||
const allowedFilters = ['country', 'active', 'createdAt', 'updatedAt'];
|
const allowedFilters = ['country'];
|
||||||
const filter = getFilter(req.query, allowedFilters);
|
const filter = getFilter(req.query, allowedFilters);
|
||||||
listVendorsRouteHandler(req, res, page, limit, property, filter, search, sort, order);
|
listVendorsRouteHandler(req, res, page, limit, property, filter, search, sort, order);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/properties', isAuthenticated, (req, res) => {
|
router.get('/properties', isAuthenticated, (req, res) => {
|
||||||
let properties = convertPropertiesString(req.query.properties);
|
let properties = convertPropertiesString(req.query.properties);
|
||||||
const allowedFilters = ['country', 'active', 'createdAt', 'updatedAt'];
|
const allowedFilters = ['country'];
|
||||||
const filter = getFilter(req.query, allowedFilters, false);
|
const filter = getFilter(req.query, allowedFilters, false);
|
||||||
listVendorsByPropertiesRouteHandler(req, res, properties, filter);
|
listVendorsByPropertiesRouteHandler(req, res, properties, filter);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,32 +0,0 @@
|
|||||||
import mongoose from 'mongoose';
|
|
||||||
import { generateId } from '../../utils.js';
|
|
||||||
const { Schema } = mongoose;
|
|
||||||
|
|
||||||
const orderItemSchema = new Schema(
|
|
||||||
{
|
|
||||||
_reference: { type: String, default: () => generateId()() },
|
|
||||||
orderType: { type: String, required: true },
|
|
||||||
order: { type: Schema.Types.ObjectId, refPath: 'orderType', required: true },
|
|
||||||
itemType: { type: String, required: true },
|
|
||||||
item: { type: Schema.Types.ObjectId, refPath: 'itemType', required: true },
|
|
||||||
syncAmount: { type: String, required: true, default: null },
|
|
||||||
itemAmount: { type: Number, required: true },
|
|
||||||
quantity: { type: Number, required: true },
|
|
||||||
totalAmount: { type: Number, required: true },
|
|
||||||
taxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
|
|
||||||
totalAmountWithTax: { type: Number, required: true },
|
|
||||||
timestamp: { type: Date, default: Date.now },
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add virtual id getter
|
|
||||||
orderItemSchema.virtual('id').get(function () {
|
|
||||||
return this._id;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Configure JSON serialization to include virtuals
|
|
||||||
orderItemSchema.set('toJSON', { virtuals: true });
|
|
||||||
|
|
||||||
// Create and export the model
|
|
||||||
export const orderItemModel = mongoose.model('orderItem', orderItemSchema);
|
|
||||||
@ -11,9 +11,8 @@ const partStockSchema = new Schema(
|
|||||||
progress: { type: Number, required: false },
|
progress: { type: Number, required: false },
|
||||||
},
|
},
|
||||||
part: { type: mongoose.Schema.Types.ObjectId, ref: 'part', required: true },
|
part: { type: mongoose.Schema.Types.ObjectId, ref: 'part', required: true },
|
||||||
|
startingQuantity: { type: Number, required: true },
|
||||||
currentQuantity: { type: Number, required: true },
|
currentQuantity: { type: Number, required: true },
|
||||||
sourceType: { type: String, required: true },
|
|
||||||
source: { type: Schema.Types.ObjectId, refPath: 'sourceType', required: true },
|
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|||||||
@ -4,7 +4,7 @@ const { Schema } = mongoose;
|
|||||||
|
|
||||||
const itemSchema = new Schema({
|
const itemSchema = new Schema({
|
||||||
itemType: { type: String, required: true },
|
itemType: { type: String, required: true },
|
||||||
item: { type: Schema.Types.ObjectId, refPath: 'items.itemType', required: true },
|
item: { type: Schema.Types.ObjectId, refPath: 'itemType', required: true },
|
||||||
quantity: { type: Number, required: true },
|
quantity: { type: Number, required: true },
|
||||||
itemCost: { type: Number, required: true },
|
itemCost: { type: Number, required: true },
|
||||||
totalCost: { type: Number, required: true },
|
totalCost: { type: Number, required: true },
|
||||||
|
|||||||
@ -1,50 +0,0 @@
|
|||||||
import mongoose from 'mongoose';
|
|
||||||
import { generateId } from '../../utils.js';
|
|
||||||
const { Schema } = mongoose;
|
|
||||||
|
|
||||||
const shipmentItemSchema = new Schema({
|
|
||||||
itemType: { type: String, required: true },
|
|
||||||
item: { type: Schema.Types.ObjectId, refPath: 'itemType', required: true },
|
|
||||||
quantity: { type: Number, required: true },
|
|
||||||
itemCost: { type: Number, required: true },
|
|
||||||
totalCost: { type: Number, required: true },
|
|
||||||
totalCostWithTax: { type: Number, required: true },
|
|
||||||
taxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
|
|
||||||
});
|
|
||||||
|
|
||||||
const shipmentSchema = new Schema(
|
|
||||||
{
|
|
||||||
_reference: { type: String, default: () => generateId()() },
|
|
||||||
purchaseOrder: { type: Schema.Types.ObjectId, ref: 'purchaseOrder', required: true },
|
|
||||||
vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true },
|
|
||||||
courierService: { type: Schema.Types.ObjectId, ref: 'courierService', required: false },
|
|
||||||
trackingNumber: { type: String, required: false },
|
|
||||||
items: [shipmentItemSchema],
|
|
||||||
cost: { net: { type: Number, required: true }, gross: { type: Number, required: true } },
|
|
||||||
shippedDate: { type: Date, required: false },
|
|
||||||
expectedDeliveryDate: { type: Date, required: false },
|
|
||||||
actualDeliveryDate: { type: Date, required: false },
|
|
||||||
state: {
|
|
||||||
type: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
default: 'pending',
|
|
||||||
enum: ['pending', 'shipped', 'in_transit', 'delivered', 'cancelled'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
notes: { type: String },
|
|
||||||
timestamp: { type: Date, default: Date.now },
|
|
||||||
},
|
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add virtual id getter
|
|
||||||
shipmentSchema.virtual('id').get(function () {
|
|
||||||
return this._id;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Configure JSON serialization to include virtuals
|
|
||||||
shipmentSchema.set('toJSON', { virtuals: true });
|
|
||||||
|
|
||||||
// Create and export the model
|
|
||||||
export const shipmentModel = mongoose.model('shipment', shipmentSchema);
|
|
||||||
@ -41,8 +41,7 @@ const deviceInfoSchema = new mongoose.Schema(
|
|||||||
{ _id: false }
|
{ _id: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
const hostSchema = new mongoose.Schema(
|
const hostSchema = new mongoose.Schema({
|
||||||
{
|
|
||||||
_reference: { type: String, default: () => generateId()() },
|
_reference: { type: String, default: () => generateId()() },
|
||||||
name: { required: true, type: String },
|
name: { required: true, type: String },
|
||||||
tags: [{ required: false, type: String }],
|
tags: [{ required: false, type: String }],
|
||||||
@ -57,9 +56,7 @@ const hostSchema = new mongoose.Schema(
|
|||||||
authCode: { type: { required: false, type: String } },
|
authCode: { type: { required: false, type: String } },
|
||||||
deviceInfo: { deviceInfoSchema },
|
deviceInfo: { deviceInfoSchema },
|
||||||
files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'file' }],
|
files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'file' }],
|
||||||
},
|
});
|
||||||
{ timestamps: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
hostSchema.virtual('id').get(function () {
|
hostSchema.virtual('id').get(function () {
|
||||||
return this._id;
|
return this._id;
|
||||||
|
|||||||
@ -1,16 +1,6 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import { generateId } from '../../utils.js';
|
import { generateId } from '../../utils.js';
|
||||||
|
|
||||||
const addressSchema = new mongoose.Schema({
|
|
||||||
building: { required: false, type: String },
|
|
||||||
addressLine1: { required: false, type: String },
|
|
||||||
addressLine2: { required: false, type: String },
|
|
||||||
city: { required: false, type: String },
|
|
||||||
state: { required: false, type: String },
|
|
||||||
postcode: { required: false, type: String },
|
|
||||||
country: { required: false, type: String },
|
|
||||||
});
|
|
||||||
|
|
||||||
const vendorSchema = new mongoose.Schema(
|
const vendorSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
_reference: { type: String, default: () => generateId()() },
|
_reference: { type: String, default: () => generateId()() },
|
||||||
@ -20,8 +10,6 @@ const vendorSchema = new mongoose.Schema(
|
|||||||
phone: { required: false, type: String },
|
phone: { required: false, type: String },
|
||||||
contact: { required: false, type: String },
|
contact: { required: false, type: String },
|
||||||
country: { required: false, type: String },
|
country: { required: false, type: String },
|
||||||
active: { required: true, type: Boolean, default: true },
|
|
||||||
address: { required: false, type: addressSchema },
|
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,209 +0,0 @@
|
|||||||
import dotenv from 'dotenv';
|
|
||||||
import { orderItemModel } from '../../schemas/inventory/orderitem.schema.js';
|
|
||||||
import log4js from 'log4js';
|
|
||||||
import mongoose from 'mongoose';
|
|
||||||
import {
|
|
||||||
deleteObject,
|
|
||||||
listObjects,
|
|
||||||
getObject,
|
|
||||||
editObject,
|
|
||||||
newObject,
|
|
||||||
listObjectsByProperties,
|
|
||||||
} from '../../database/database.js';
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const logger = log4js.getLogger('Order Items');
|
|
||||||
logger.level = process.env.LOG_LEVEL;
|
|
||||||
|
|
||||||
export const listOrderItemsRouteHandler = async (
|
|
||||||
req,
|
|
||||||
res,
|
|
||||||
page = 1,
|
|
||||||
limit = 25,
|
|
||||||
property = '',
|
|
||||||
filter = {},
|
|
||||||
search = '',
|
|
||||||
sort = '',
|
|
||||||
order = 'ascend'
|
|
||||||
) => {
|
|
||||||
const result = await listObjects({
|
|
||||||
model: orderItemModel,
|
|
||||||
page,
|
|
||||||
limit,
|
|
||||||
property,
|
|
||||||
filter,
|
|
||||||
search,
|
|
||||||
sort,
|
|
||||||
order,
|
|
||||||
populate: [
|
|
||||||
{
|
|
||||||
path: 'order',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: 'taxRate',
|
|
||||||
strictPopulate: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: 'item',
|
|
||||||
populate: { path: 'costTaxRate' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: 'item',
|
|
||||||
populate: { path: 'priceTaxRate' },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result?.error) {
|
|
||||||
logger.error('Error listing order items.');
|
|
||||||
res.status(result.code).send(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`List of order items (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const listOrderItemsByPropertiesRouteHandler = async (
|
|
||||||
req,
|
|
||||||
res,
|
|
||||||
properties = '',
|
|
||||||
filter = {},
|
|
||||||
masterFilter = {}
|
|
||||||
) => {
|
|
||||||
const result = await listObjectsByProperties({
|
|
||||||
model: orderItemModel,
|
|
||||||
properties,
|
|
||||||
filter,
|
|
||||||
populate: [],
|
|
||||||
masterFilter,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result?.error) {
|
|
||||||
logger.error('Error listing order items.');
|
|
||||||
res.status(result.code).send(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`List of order items. Count: ${result.length}`);
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getOrderItemRouteHandler = async (req, res) => {
|
|
||||||
const id = req.params.id;
|
|
||||||
const result = await getObject({
|
|
||||||
model: orderItemModel,
|
|
||||||
id,
|
|
||||||
populate: [
|
|
||||||
{
|
|
||||||
path: 'order',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: 'taxRate',
|
|
||||||
strictPopulate: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: 'item',
|
|
||||||
populate: { path: 'costTaxRate' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: 'item',
|
|
||||||
populate: { path: 'priceTaxRate' },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
if (result?.error) {
|
|
||||||
logger.warn(`Order Item not found with supplied id.`);
|
|
||||||
return res.status(result.code).send(result);
|
|
||||||
}
|
|
||||||
logger.debug(`Retreived order item with ID: ${id}`);
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const editOrderItemRouteHandler = async (req, res) => {
|
|
||||||
// Get ID from params
|
|
||||||
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
||||||
|
|
||||||
logger.trace(`Order Item with ID: ${id}`);
|
|
||||||
|
|
||||||
const updateData = {
|
|
||||||
updatedAt: new Date(),
|
|
||||||
purchaseOrder: req.body.purchaseOrder,
|
|
||||||
itemType: req.body.itemType,
|
|
||||||
item: req.body.item,
|
|
||||||
syncAmount: req.body.syncAmount,
|
|
||||||
itemAmount: req.body.itemAmount,
|
|
||||||
quantity: req.body.quantity,
|
|
||||||
totalAmount: req.body.totalAmount,
|
|
||||||
taxRate: req.body.taxRate,
|
|
||||||
totalAmountWithTax: req.body.totalAmountWithTax,
|
|
||||||
};
|
|
||||||
// Create audit log before updating
|
|
||||||
const result = await editObject({
|
|
||||||
model: orderItemModel,
|
|
||||||
id,
|
|
||||||
updateData,
|
|
||||||
user: req.user,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.error) {
|
|
||||||
logger.error('Error editing order item:', result.error);
|
|
||||||
res.status(result).send(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Edited order item with ID: ${id}`);
|
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const newOrderItemRouteHandler = async (req, res) => {
|
|
||||||
const newData = {
|
|
||||||
updatedAt: new Date(),
|
|
||||||
purchaseOrder: req.body.purchaseOrder,
|
|
||||||
itemType: req.body.itemType,
|
|
||||||
item: req.body.item,
|
|
||||||
orderType: req.body.orderType,
|
|
||||||
order: req.body.order,
|
|
||||||
syncAmount: req.body.syncAmount,
|
|
||||||
itemAmount: req.body.itemAmount,
|
|
||||||
quantity: req.body.quantity,
|
|
||||||
totalAmount: req.body.totalAmount,
|
|
||||||
taxRate: req.body.taxRate,
|
|
||||||
totalAmountWithTax: req.body.totalAmountWithTax,
|
|
||||||
};
|
|
||||||
const result = await newObject({
|
|
||||||
model: orderItemModel,
|
|
||||||
newData,
|
|
||||||
user: req.user,
|
|
||||||
});
|
|
||||||
if (result.error) {
|
|
||||||
logger.error('No order item created:', result.error);
|
|
||||||
return res.status(result.code).send(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`New order item with ID: ${result._id}`);
|
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteOrderItemRouteHandler = async (req, res) => {
|
|
||||||
// Get ID from params
|
|
||||||
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
||||||
|
|
||||||
logger.trace(`Order Item with ID: ${id}`);
|
|
||||||
|
|
||||||
const result = await deleteObject({
|
|
||||||
model: orderItemModel,
|
|
||||||
id,
|
|
||||||
user: req.user,
|
|
||||||
});
|
|
||||||
if (result.error) {
|
|
||||||
logger.error('No order item deleted:', result.error);
|
|
||||||
return res.status(result.code).send(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Deleted order item with ID: ${result._id}`);
|
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
@ -1,177 +0,0 @@
|
|||||||
import dotenv from 'dotenv';
|
|
||||||
import { shipmentModel } from '../../schemas/inventory/shipment.schema.js';
|
|
||||||
import log4js from 'log4js';
|
|
||||||
import mongoose from 'mongoose';
|
|
||||||
import {
|
|
||||||
deleteObject,
|
|
||||||
listObjects,
|
|
||||||
getObject,
|
|
||||||
editObject,
|
|
||||||
newObject,
|
|
||||||
listObjectsByProperties,
|
|
||||||
} from '../../database/database.js';
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
const logger = log4js.getLogger('Shipments');
|
|
||||||
logger.level = process.env.LOG_LEVEL;
|
|
||||||
|
|
||||||
export const listShipmentsRouteHandler = async (
|
|
||||||
req,
|
|
||||||
res,
|
|
||||||
page = 1,
|
|
||||||
limit = 25,
|
|
||||||
property = '',
|
|
||||||
filter = {},
|
|
||||||
search = '',
|
|
||||||
sort = '',
|
|
||||||
order = 'ascend'
|
|
||||||
) => {
|
|
||||||
const result = await listObjects({
|
|
||||||
model: shipmentModel,
|
|
||||||
page,
|
|
||||||
limit,
|
|
||||||
property,
|
|
||||||
filter,
|
|
||||||
search,
|
|
||||||
sort,
|
|
||||||
order,
|
|
||||||
populate: ['purchaseOrder', 'vendor', 'courierService'],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result?.error) {
|
|
||||||
logger.error('Error listing shipments.');
|
|
||||||
res.status(result.code).send(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`List of shipments (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const listShipmentsByPropertiesRouteHandler = async (
|
|
||||||
req,
|
|
||||||
res,
|
|
||||||
properties = '',
|
|
||||||
filter = {},
|
|
||||||
masterFilter = {}
|
|
||||||
) => {
|
|
||||||
const result = await listObjectsByProperties({
|
|
||||||
model: shipmentModel,
|
|
||||||
properties,
|
|
||||||
filter,
|
|
||||||
populate: ['purchaseOrder', 'vendor', 'courierService'],
|
|
||||||
masterFilter,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result?.error) {
|
|
||||||
logger.error('Error listing shipments.');
|
|
||||||
res.status(result.code).send(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`List of shipments. Count: ${result.length}`);
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getShipmentRouteHandler = async (req, res) => {
|
|
||||||
const id = req.params.id;
|
|
||||||
const result = await getObject({
|
|
||||||
model: shipmentModel,
|
|
||||||
id,
|
|
||||||
populate: ['purchaseOrder', 'vendor', 'courierService', 'items.item', 'items.taxRate'],
|
|
||||||
});
|
|
||||||
if (result?.error) {
|
|
||||||
logger.warn(`Shipment not found with supplied id.`);
|
|
||||||
return res.status(result.code).send(result);
|
|
||||||
}
|
|
||||||
logger.debug(`Retreived shipment with ID: ${id}`);
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const editShipmentRouteHandler = async (req, res) => {
|
|
||||||
// Get ID from params
|
|
||||||
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
||||||
|
|
||||||
logger.trace(`Shipment with ID: ${id}`);
|
|
||||||
|
|
||||||
const updateData = {
|
|
||||||
updatedAt: new Date(),
|
|
||||||
purchaseOrder: req.body.purchaseOrder,
|
|
||||||
vendor: req.body.vendor,
|
|
||||||
courierService: req.body.courierService,
|
|
||||||
trackingNumber: req.body.trackingNumber,
|
|
||||||
shippedDate: req.body.shippedDate,
|
|
||||||
expectedDeliveryDate: req.body.expectedDeliveryDate,
|
|
||||||
actualDeliveryDate: req.body.actualDeliveryDate,
|
|
||||||
state: req.body.state,
|
|
||||||
notes: req.body.notes,
|
|
||||||
};
|
|
||||||
// Create audit log before updating
|
|
||||||
const result = await editObject({
|
|
||||||
model: shipmentModel,
|
|
||||||
id,
|
|
||||||
updateData,
|
|
||||||
user: req.user,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.error) {
|
|
||||||
logger.error('Error editing shipment:', result.error);
|
|
||||||
res.status(result).send(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Edited shipment with ID: ${id}`);
|
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const newShipmentRouteHandler = async (req, res) => {
|
|
||||||
const newData = {
|
|
||||||
updatedAt: new Date(),
|
|
||||||
purchaseOrder: req.body.purchaseOrder,
|
|
||||||
vendor: req.body.vendor,
|
|
||||||
courierService: req.body.courierService,
|
|
||||||
trackingNumber: req.body.trackingNumber,
|
|
||||||
items: req.body.items,
|
|
||||||
cost: req.body.cost,
|
|
||||||
shippedDate: req.body.shippedDate,
|
|
||||||
expectedDeliveryDate: req.body.expectedDeliveryDate,
|
|
||||||
actualDeliveryDate: req.body.actualDeliveryDate,
|
|
||||||
state: req.body.state,
|
|
||||||
notes: req.body.notes,
|
|
||||||
};
|
|
||||||
const result = await newObject({
|
|
||||||
model: shipmentModel,
|
|
||||||
newData,
|
|
||||||
user: req.user,
|
|
||||||
});
|
|
||||||
if (result.error) {
|
|
||||||
logger.error('No shipment created:', result.error);
|
|
||||||
return res.status(result.code).send(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`New shipment with ID: ${result._id}`);
|
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteShipmentRouteHandler = async (req, res) => {
|
|
||||||
// Get ID from params
|
|
||||||
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
||||||
|
|
||||||
logger.trace(`Shipment with ID: ${id}`);
|
|
||||||
|
|
||||||
const result = await deleteObject({
|
|
||||||
model: shipmentModel,
|
|
||||||
id,
|
|
||||||
user: req.user,
|
|
||||||
});
|
|
||||||
if (result.error) {
|
|
||||||
logger.error('No shipment deleted:', result.error);
|
|
||||||
return res.status(result.code).send(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(`Deleted shipment with ID: ${result._id}`);
|
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
};
|
|
||||||
@ -97,8 +97,6 @@ export const editVendorRouteHandler = async (req, res) => {
|
|||||||
website: req.body.website,
|
website: req.body.website,
|
||||||
phone: req.body.phone,
|
phone: req.body.phone,
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
address: req.body.address,
|
|
||||||
active: req.body.active,
|
|
||||||
};
|
};
|
||||||
// Create audit log before updating
|
// Create audit log before updating
|
||||||
const result = await editObject({
|
const result = await editObject({
|
||||||
@ -128,8 +126,6 @@ export const newVendorRouteHandler = async (req, res) => {
|
|||||||
website: req.body.website,
|
website: req.body.website,
|
||||||
phone: req.body.phone,
|
phone: req.body.phone,
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
address: req.body.address,
|
|
||||||
active: req.body.active,
|
|
||||||
};
|
};
|
||||||
const result = await newObject({
|
const result = await newObject({
|
||||||
model: vendorModel,
|
model: vendorModel,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user