import dotenv from "dotenv"; import { productModel } from "../../schemas/product.schema.js"; import log4js from "log4js"; import mongoose from "mongoose"; dotenv.config(); const logger = log4js.getLogger("Products"); logger.level = process.env.LOG_LEVEL; export const listProductsRouteHandler = async ( req, res, page = 1, limit = 25, property = "", filter = {}, ) => { try { // Calculate the skip value based on the page number and limit const skip = (page - 1) * limit; let product; let aggregateCommand = []; if (filter != {}) { // use filtering if present aggregateCommand.push({ $match: filter }); } if (property != "") { aggregateCommand.push({ $group: { _id: `$${property}` } }); // group all same properties aggregateCommand.push({ $project: { _id: 0, [property]: "$_id" } }); // rename _id to the property name } else { aggregateCommand.push({ $project: { image: 0, url: 0 } }); } aggregateCommand.push({ $skip: skip }); aggregateCommand.push({ $limit: Number(limit) }); console.log(aggregateCommand); product = await productModel.aggregate(aggregateCommand); logger.trace( `List of products (Page ${page}, Limit ${limit}, Property ${property}):`, product, ); res.send(product); } catch (error) { logger.error("Error listing products:", error); res.status(500).send({ error: error }); } }; export const getProductRouteHandler = async (req, res) => { try { // Get ID from params const id = new mongoose.Types.ObjectId(req.params.id); // Fetch the product with the given remote address const product = await productModel.findOne({ _id: id, }); if (!product) { logger.warn(`Product not found with supplied id.`); return res.status(404).send({ error: "Print job not found." }); } logger.trace(`Product with ID: ${id}:`, product); res.send(product); } catch (error) { logger.error("Error fetching Product:", error); res.status(500).send({ error: error.message }); } }; export const editProductRouteHandler = async (req, res) => { try { // Get ID from params const id = new mongoose.Types.ObjectId(req.params.id); // Fetch the product with the given remote address const product = await productModel.findOne({ _id: id }); if (!product) { // Error handling logger.warn(`Product not found with supplied id.`); return res.status(404).send({ error: "Print job not found." }); } logger.trace(`Product with ID: ${id}:`, product); try { const { createdAt, updatedAt, started_at, status, ...updateData } = req.body; const result = await productModel.updateOne( { _id: id }, { $set: updateData }, ); if (result.nModified === 0) { logger.error("No Product updated."); res.status(500).send({ error: "No products updated." }); } } catch (updateError) { logger.error("Error updating product:", updateError); res.status(500).send({ error: updateError.message }); } res.send("OK"); } catch (fetchError) { logger.error("Error fetching product:", fetchError); res.status(500).send({ error: fetchError.message }); } }; export const newProductRouteHandler = async (req, res) => { try { let { ...newProduct } = req.body; newProduct = { ...newProduct, createdAt: new Date(), updatedAt: new Date(), }; const result = await productModel.create(newProduct); if (result.nCreated === 0) { logger.error("No product created."); res.status(500).send({ error: "No product created." }); } res.status(200).send(result); } catch (updateError) { logger.error("Error updating product:", updateError); res.status(500).send({ error: updateError.message }); } };