diff --git a/controllers/customDiscount.controller.js b/controllers/customDiscount.controller.js index 1b200c81463c9027acbbaee0730aa1c4d620c530..0941c6aa9c0eefdea531498e72f8b60fc72f9360 100644 --- a/controllers/customDiscount.controller.js +++ b/controllers/customDiscount.controller.js @@ -35,6 +35,10 @@ const { countCategoriesService, } = require("../services/category.service"); const { getAllBrandsService } = require("../services/brand.service"); +const { + findAndCountAllCustomerService, + findCustomerWithRoleStatsService, +} = require("../services/customer.services"); /** * Helper function to convert array of objects to object of objects with indexed keys @@ -502,7 +506,7 @@ const toggleCustomDiscountStatus = async (req, res) => { }; /** - * Get entities (products/categories/brands) for discount configuration + * Get entities (products/categories/brands/customers) for discount configuration * GET /api/custom-discount/entities */ const getEntitiesForDiscount = async (req, res) => { @@ -517,12 +521,12 @@ const getEntitiesForDiscount = async (req, res) => { } = req.query; // Validate entity parameter - const validEntities = ["product", "category", "brand"]; + const validEntities = ["product", "category", "brand", "customer"]; if (!validEntities.includes(entity.toLowerCase())) { return res.status(400).json({ status: false, message: - "Invalid entity type. Must be 'product', 'category', or 'brand'", + "Invalid entity type. Must be 'product', 'category', 'brand', or 'customer'", }); } @@ -760,6 +764,38 @@ const getEntitiesForDiscount = async (req, res) => { name: brand.brandName, })); break; + + case "customer": + const customerWhereClause = { + isDeleted: false, + status: "active", + ...(entitySearch && { + [Op.or]: [ + { firstName: { [Op.like]: `%${entitySearch}%` } }, + { lastName: { [Op.like]: `%${entitySearch}%` } }, + { email: { [Op.like]: `%${entitySearch}%` } }, + { userName: { [Op.like]: `%${entitySearch}%` } }, + ], + }), + }; + + const customers = await findAndCountAllCustomerService({ + where: customerWhereClause, + attributes: ["ID", "firstName", "lastName", "email", "userName"], + limit: limitNum, + offset: offset, + order: [["firstName", "ASC"]], + }); + + totalCount = customers.count; + + result = customers.rows.map((customer) => ({ + customerId: customer.ID, + name: `${customer.firstName} ${customer.lastName}`.trim(), + email: customer.email, + userName: customer.userName, + })); + break; } return res.status(200).json({