import config from '../../config.js'; import { clientModel } from '../../database/schemas/sales/client.schema.js'; import log4js from 'log4js'; import mongoose from 'mongoose'; import { deleteObject, listObjects, getObject, editObject, newObject, listObjectsByProperties, getModelStats, getModelHistory, } from '../../database/database.js'; const logger = log4js.getLogger('Clients'); logger.level = config.server.logLevel; export const listClientsRouteHandler = async ( req, res, page = 1, limit = 25, property = '', filter = {}, search = '', sort = '', order = 'ascend' ) => { const result = await listObjects({ model: clientModel, page, limit, property, filter, search, sort, order, }); if (result?.error) { logger.error('Error listing clients.'); res.status(result.code).send(result); return; } logger.debug(`List of clients (Page ${page}, Limit ${limit}). Count: ${result.length}.`); res.send(result); }; export const listClientsByPropertiesRouteHandler = async ( req, res, properties = '', filter = {} ) => { const result = await listObjectsByProperties({ model: clientModel, properties, filter, }); if (result?.error) { logger.error('Error listing clients.'); res.status(result.code).send(result); return; } logger.debug(`List of clients. Count: ${result.length}`); res.send(result); }; export const getClientRouteHandler = async (req, res) => { const id = req.params.id; const result = await getObject({ model: clientModel, id, }); if (result?.error) { logger.warn(`Client not found with supplied id.`); return res.status(result.code).send(result); } logger.debug(`Retreived client with ID: ${id}`); res.send(result); }; export const editClientRouteHandler = async (req, res) => { // Get ID from params const id = new mongoose.Types.ObjectId(req.params.id); logger.trace(`Client with ID: ${id}`); const updateData = { updatedAt: new Date(), country: req.body.country, name: req.body.name, phone: req.body.phone, email: req.body.email, address: req.body.address, active: req.body.active, tags: req.body.tags, }; // Create audit log before updating const result = await editObject({ model: clientModel, id, updateData, user: req.user, }); if (result.error) { logger.error('Error editing client:', result.error); res.status(result).send(result); return; } logger.debug(`Edited client with ID: ${id}`); res.send(result); }; export const newClientRouteHandler = async (req, res) => { const newData = { updatedAt: new Date(), country: req.body.country, name: req.body.name, phone: req.body.phone, email: req.body.email, address: req.body.address, active: req.body.active, tags: req.body.tags, }; const result = await newObject({ model: clientModel, newData, user: req.user, }); if (result.error) { logger.error('No client created:', result.error); return res.status(result.code).send(result); } logger.debug(`New client with ID: ${result._id}`); res.send(result); }; export const deleteClientRouteHandler = async (req, res) => { // Get ID from params const id = new mongoose.Types.ObjectId(req.params.id); logger.trace(`Client with ID: ${id}`); const result = await deleteObject({ model: clientModel, id, user: req.user, }); if (result.error) { logger.error('No client deleted:', result.error); return res.status(result.code).send(result); } logger.debug(`Deleted client with ID: ${result._id}`); res.send(result); }; export const getClientStatsRouteHandler = async (req, res) => { const result = await getModelStats({ model: clientModel }); if (result?.error) { logger.error('Error fetching client stats:', result.error); return res.status(result.code).send(result); } logger.trace('Client stats:', result); res.send(result); }; export const getClientHistoryRouteHandler = async (req, res) => { const from = req.query.from; const to = req.query.to; const result = await getModelHistory({ model: clientModel, from, to }); if (result?.error) { logger.error('Error fetching client history:', result.error); return res.status(result.code).send(result); } logger.trace('Client history:', result); res.send(result); };