import config from '../../config.js'; import { fulfillmentPolicyModel } from '../../database/schemas/sales/fulfillmentpolicy.schema.js'; import log4js from 'log4js'; import mongoose from 'mongoose'; import { deleteObject, listObjects, getObject, editObject, newObject, listObjectsByProperties, getModelStats, getModelHistory, searchObjects, getPropertyValues, getObjectNeighbors, } from '../../database/database.js'; import { syncFulfillmentPolicy } from '../../integrations/marketplace.js'; import { startPolicyMarketplaceSync } from '../misc/syncPolicyMarketplaces.js'; const logger = log4js.getLogger('FulfillmentPolicies'); logger.level = config.server.logLevel; export const FULFILLMENT_POLICY_POPULATE = ['courierServices', 'marketplaces.marketplace']; const policyFields = (body = {}) => ({ name: body.name, description: body.description, handlingTime: body.handlingTime, localPickup: body.localPickup, globalShipping: body.globalShipping, freightShipping: body.freightShipping, pickupDropOff: body.pickupDropOff, courierServices: body.courierServices, marketplaces: body.marketplaces, }); export const listFulfillmentPoliciesRouteHandler = async ( req, res, page = 1, limit = 25, property = '', filter = {}, search = '', sort = '', order = 'ascend' ) => { const result = await listObjects({ model: fulfillmentPolicyModel, page, limit, property, filter, search, sort, order, populate: FULFILLMENT_POLICY_POPULATE, }); if (result?.error) { logger.error('Error listing fulfillment policies.'); res.status(result.code).send(result); return; } logger.debug( `List of fulfillment policies (Page ${page}, Limit ${limit}). Count: ${result.length}.` ); res.send(result); }; export const listFulfillmentPoliciesByPropertiesRouteHandler = async ( req, res, properties = '', filter = {}, masterFilter = {} ) => { const result = await listObjectsByProperties({ model: fulfillmentPolicyModel, properties, filter, masterFilter, populate: FULFILLMENT_POLICY_POPULATE, }); if (result?.error) { logger.error('Error listing fulfillment policies.'); res.status(result.code).send(result); return; } logger.debug(`List of fulfillment policies. Count: ${result.length}`); res.send(result); }; export const getFulfillmentPolicyPropertyValuesRouteHandler = async ( req, res, property, filter, masterFilter ) => { const result = await getPropertyValues({ model: fulfillmentPolicyModel, property, filter: { ...filter, ...masterFilter }, }); res.send(result); }; export const searchFulfillmentPoliciesRouteHandler = async (req, res, search) => { const result = await searchObjects({ model: fulfillmentPolicyModel, search, populate: FULFILLMENT_POLICY_POPULATE, }); res.send(result); }; export const getFulfillmentPolicyRouteHandler = async (req, res) => { const id = req.params.id; const result = await getObject({ model: fulfillmentPolicyModel, id, populate: FULFILLMENT_POLICY_POPULATE, }); if (result?.error) { logger.warn('Fulfillment policy not found with supplied id.'); return res.status(result.code).send(result); } logger.debug(`Retrieved fulfillment policy with ID: ${id}`); res.send(result); }; export const editFulfillmentPolicyRouteHandler = async (req, res) => { const id = new mongoose.Types.ObjectId(req.params.id); const result = await editObject({ model: fulfillmentPolicyModel, id, updateData: { updatedAt: new Date(), ...policyFields(req.body) }, user: req.user, populate: FULFILLMENT_POLICY_POPULATE, }); if (result.error) { logger.error('Error editing fulfillment policy:', result.error); res.status(result.code).send(result); return; } logger.debug(`Edited fulfillment policy with ID: ${id}`); const synced = await startPolicyMarketplaceSync({ policy: result, user: req.user, syncFn: syncFulfillmentPolicy, logger, model: fulfillmentPolicyModel, populate: FULFILLMENT_POLICY_POPULATE, }); res.send(synced || result); }; export const newFulfillmentPolicyRouteHandler = async (req, res) => { const result = await newObject({ model: fulfillmentPolicyModel, newData: { updatedAt: new Date(), ...policyFields(req.body) }, user: req.user, }); if (result.error) { logger.error('No fulfillment policy created:', result.error); return res.status(result.code).send(result); } logger.debug(`New fulfillment policy with ID: ${result._id}`); res.send(result); }; export const deleteFulfillmentPolicyRouteHandler = async (req, res) => { const id = new mongoose.Types.ObjectId(req.params.id); const result = await deleteObject({ model: fulfillmentPolicyModel, id, user: req.user, }); if (result.error) { logger.error('No fulfillment policy deleted:', result.error); return res.status(result.code).send(result); } logger.debug(`Deleted fulfillment policy with ID: ${result._id}`); res.send(result); }; export const getFulfillmentPolicyStatsRouteHandler = async (req, res) => { const result = await getModelStats({ model: fulfillmentPolicyModel }); if (result?.error) { logger.error('Error fetching fulfillment policy stats:', result.error); return res.status(result.code).send(result); } res.send(result); }; export const getFulfillmentPolicyHistoryRouteHandler = async (req, res) => { const from = req.query.from; const to = req.query.to; const result = await getModelHistory({ model: fulfillmentPolicyModel, from, to }); if (result?.error) { logger.error('Error fetching fulfillment policy history:', result.error); return res.status(result.code).send(result); } res.send(result); }; export const getFulfillmentPolicyNeighborsRouteHandler = async ( req, res, property = '', filter = {}, search = '', sort = '', order = 'ascend', id ) => { if (!id) { return res.status(400).send({ error: 'Missing id parameter', code: 400 }); } const result = await getObjectNeighbors({ model: fulfillmentPolicyModel, id, filter, search, sort, order, }); if (result?.error) { logger.error('Error fetching fulfillmentPolicy neighbors.'); return res.status(result.code).send(result); } res.send(result); };