Refactored population logic in invoice, payment, order item, and other route handlers to use constants for improved readability and maintainability. This change enhances consistency across the codebase and simplifies future updates to population fields.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit is contained in:
parent
0c7469b7c1
commit
367891eb33
@ -21,6 +21,16 @@ import { shipmentModel } from '../../database/schemas/inventory/shipment.schema.
|
||||
const logger = log4js.getLogger('Invoices');
|
||||
logger.level = config.server.logLevel;
|
||||
|
||||
const INVOICE_POPULATE = [
|
||||
{ path: 'to', strictPopulate: false, ref: 'toType' },
|
||||
{ path: 'from', strictPopulate: false, ref: 'fromType' },
|
||||
{ path: 'order' },
|
||||
{ path: 'invoiceOrderItems.taxRate' },
|
||||
{ path: 'invoiceShipments.taxRate' },
|
||||
{ path: 'invoiceOrderItems.orderItem' },
|
||||
{ path: 'invoiceShipments.shipment' },
|
||||
];
|
||||
|
||||
export const listInvoicesRouteHandler = async (
|
||||
req,
|
||||
res,
|
||||
@ -93,19 +103,10 @@ export const searchInvoicesRouteHandler = async (req, res, search) => {
|
||||
|
||||
export const getInvoiceRouteHandler = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const populateFields = [
|
||||
{ path: 'to', strictPopulate: false, ref: 'toType' },
|
||||
{ path: 'from', strictPopulate: false, ref: 'fromType' },
|
||||
{ path: 'order' },
|
||||
{ path: 'invoiceOrderItems.taxRate' },
|
||||
{ path: 'invoiceShipments.taxRate' },
|
||||
{ path: 'invoiceOrderItems.orderItem' },
|
||||
{ path: 'invoiceShipments.shipment' },
|
||||
];
|
||||
const result = await getObject({
|
||||
model: invoiceModel,
|
||||
id,
|
||||
populate: populateFields,
|
||||
populate: INVOICE_POPULATE,
|
||||
});
|
||||
if (result?.error) {
|
||||
logger.warn(`Invoice not found with supplied id.`);
|
||||
@ -158,6 +159,7 @@ export const editInvoiceRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: INVOICE_POPULATE,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -20,6 +20,8 @@ import { invoiceModel } from '../../database/schemas/finance/invoice.schema.js';
|
||||
const logger = log4js.getLogger('Payments');
|
||||
logger.level = config.server.logLevel;
|
||||
|
||||
const PAYMENT_POPULATE = [{ path: 'vendor' }, { path: 'client' }, { path: 'invoice' }];
|
||||
|
||||
export const listPaymentsRouteHandler = async (
|
||||
req,
|
||||
res,
|
||||
@ -90,11 +92,10 @@ export const searchPaymentsRouteHandler = async (req, res, search) => {
|
||||
|
||||
export const getPaymentRouteHandler = async (req, res) => {
|
||||
const id = req.params.id;
|
||||
const populateFields = [{ path: 'vendor' }, { path: 'client' }, { path: 'invoice' }];
|
||||
const result = await getObject({
|
||||
model: paymentModel,
|
||||
id,
|
||||
populate: populateFields,
|
||||
populate: PAYMENT_POPULATE,
|
||||
});
|
||||
if (result?.error) {
|
||||
logger.warn(`Payment not found with supplied id.`);
|
||||
@ -140,6 +141,7 @@ export const editPaymentRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: PAYMENT_POPULATE,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -120,6 +120,11 @@ export const editFilamentStockRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [
|
||||
{ path: 'filament' },
|
||||
{ path: 'filamentSku', populate: 'filament' },
|
||||
{ path: 'stockLocation' },
|
||||
],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -18,6 +18,53 @@ import {
|
||||
const logger = log4js.getLogger('Order Items');
|
||||
logger.level = config.server.logLevel;
|
||||
|
||||
const ORDER_ITEM_POPULATE = [
|
||||
{
|
||||
path: 'order',
|
||||
},
|
||||
{
|
||||
path: 'taxRate',
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'item',
|
||||
populate: [
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'sku',
|
||||
strictPopulate: false,
|
||||
populate: [
|
||||
{
|
||||
path: 'filament',
|
||||
populate: { path: 'costTaxRate', strictPopulate: false },
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'part',
|
||||
populate: [
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'product',
|
||||
populate: [
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
strictPopulate: false,
|
||||
},
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export const listOrderItemsRouteHandler = async (
|
||||
req,
|
||||
res,
|
||||
@ -137,52 +184,7 @@ export const getOrderItemRouteHandler = async (req, res) => {
|
||||
const result = await getObject({
|
||||
model: orderItemModel,
|
||||
id,
|
||||
populate: [
|
||||
{
|
||||
path: 'order',
|
||||
},
|
||||
{
|
||||
path: 'taxRate',
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'item',
|
||||
populate: [
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'sku',
|
||||
strictPopulate: false,
|
||||
populate: [
|
||||
{
|
||||
path: 'filament',
|
||||
populate: { path: 'costTaxRate', strictPopulate: false },
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'part',
|
||||
populate: [
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
strictPopulate: false,
|
||||
},
|
||||
{
|
||||
path: 'product',
|
||||
populate: [
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
strictPopulate: false,
|
||||
},
|
||||
{ path: 'costTaxRate', strictPopulate: false },
|
||||
{ path: 'priceTaxRate', strictPopulate: false },
|
||||
],
|
||||
},
|
||||
],
|
||||
populate: ORDER_ITEM_POPULATE,
|
||||
});
|
||||
if (result?.error) {
|
||||
logger.warn(`Order Item not found with supplied id.`);
|
||||
@ -247,6 +249,7 @@ export const editOrderItemRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ORDER_ITEM_POPULATE,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -110,6 +110,7 @@ export const editPartStockRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [{ path: 'partSku' }, { path: 'stockLocation' }, { path: 'source' }],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -142,6 +142,12 @@ export const editProductStockRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [
|
||||
{ path: 'partStocks.partSku' },
|
||||
{ path: 'partStocks.partStock' },
|
||||
{ path: 'productSku' },
|
||||
{ path: 'stockLocation' },
|
||||
],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -132,6 +132,7 @@ export const editPurchaseOrderRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['vendor'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -122,6 +122,7 @@ export const editShipmentRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['order', 'courierService', 'taxRate'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -142,6 +142,10 @@ export const editStockEventRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [
|
||||
{ path: 'owner', select: 'name _id' },
|
||||
{ path: 'parent', select: 'name _id' },
|
||||
],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -362,6 +362,11 @@ export const editStockTransferRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [
|
||||
{ path: 'lines.fromStock' },
|
||||
{ path: 'lines.toStockLocation' },
|
||||
{ path: 'lines.toStock' },
|
||||
],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -123,6 +123,7 @@ export const editAppPasswordRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['user'],
|
||||
});
|
||||
|
||||
if (result?.error) {
|
||||
|
||||
@ -116,6 +116,7 @@ export const editCourierServiceRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['courier'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -106,6 +106,7 @@ export const editDocumentJobRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['documentTemplate', 'documentPrinter', 'object'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -118,6 +118,7 @@ export const editDocumentPrinterRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['currentDocumentSize', 'host'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -132,6 +132,11 @@ export const editDocumentTemplateRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [
|
||||
{ path: 'documentSize' },
|
||||
{ path: 'parent', strictPopulate: false },
|
||||
{ path: 'documentPrinters', strictPopulate: false },
|
||||
],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -122,6 +122,7 @@ export const editFilamentRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['costTaxRate', 'material'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -122,6 +122,7 @@ export const editFilamentSkuRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: [{ path: 'filament', populate: 'costTaxRate' }, 'costTaxRate'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -114,6 +114,7 @@ export const editHostRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['files'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -115,6 +115,7 @@ export const editPartRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['costTaxRate', 'priceTaxRate'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -16,6 +16,12 @@ import {
|
||||
const logger = log4js.getLogger('Part SKUs');
|
||||
logger.level = config.server.logLevel;
|
||||
|
||||
const PART_SKU_POPULATE = [
|
||||
{ path: 'part', populate: ['costTaxRate', 'priceTaxRate'] },
|
||||
'priceTaxRate',
|
||||
'costTaxRate',
|
||||
];
|
||||
|
||||
export const listPartSkusRouteHandler = async (
|
||||
req,
|
||||
res,
|
||||
@ -87,11 +93,7 @@ export const getPartSkuRouteHandler = async (req, res) => {
|
||||
const result = await getObject({
|
||||
model: partSkuModel,
|
||||
id,
|
||||
populate: [
|
||||
{ path: 'part', populate: ['costTaxRate', 'priceTaxRate'] },
|
||||
'priceTaxRate',
|
||||
'costTaxRate',
|
||||
],
|
||||
populate: PART_SKU_POPULATE,
|
||||
});
|
||||
if (result?.error) {
|
||||
logger.warn(`Part SKU not found with supplied id.`);
|
||||
@ -132,6 +134,7 @@ export const editPartSkuRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: PART_SKU_POPULATE,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -123,6 +123,7 @@ export const editProductRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['productCategory', 'vendor', 'costTaxRate', 'priceTaxRate'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -16,6 +16,15 @@ import {
|
||||
const logger = log4js.getLogger('Product SKUs');
|
||||
logger.level = config.server.logLevel;
|
||||
|
||||
const PRODUCT_SKU_POPULATE = [
|
||||
{ path: 'product', populate: ['costTaxRate', 'priceTaxRate'] },
|
||||
'priceTaxRate',
|
||||
'costTaxRate',
|
||||
'parts.partSku',
|
||||
'parts.part',
|
||||
'product',
|
||||
];
|
||||
|
||||
export const listProductSkusRouteHandler = async (
|
||||
req,
|
||||
res,
|
||||
@ -87,14 +96,7 @@ export const getProductSkuRouteHandler = async (req, res) => {
|
||||
const result = await getObject({
|
||||
model: productSkuModel,
|
||||
id,
|
||||
populate: [
|
||||
{ path: 'product', populate: ['costTaxRate', 'priceTaxRate'] },
|
||||
'priceTaxRate',
|
||||
'costTaxRate',
|
||||
'parts.partSku',
|
||||
'parts.part',
|
||||
'product',
|
||||
],
|
||||
populate: PRODUCT_SKU_POPULATE,
|
||||
});
|
||||
if (result?.error) {
|
||||
logger.warn(`Product SKU not found with supplied id.`);
|
||||
@ -136,6 +138,7 @@ export const editProductSkuRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: PRODUCT_SKU_POPULATE,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -101,6 +101,7 @@ export const editNoteRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['noteType', 'user', 'parent'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -103,6 +103,7 @@ export const editUserNotifierRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['user', 'object'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -132,6 +132,7 @@ export const editGCodeFileRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['filament', { path: 'filamentSku', populate: 'filament' }, 'parts.partSku'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -10,7 +10,7 @@ import {
|
||||
newObject,
|
||||
getModelStats,
|
||||
getModelHistory,
|
||||
searchObjects
|
||||
searchObjects,
|
||||
} from '../../database/database.js';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
@ -117,7 +117,7 @@ export const editPrinterRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: 'vendor',
|
||||
populate: ['vendor', 'host'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
@ -133,6 +133,7 @@ export const editSalesOrderRouteHandler = async (req, res) => {
|
||||
id,
|
||||
updateData,
|
||||
user: req.user,
|
||||
populate: ['client', 'marketplace'],
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user