From 9d122ad358e00f5758e45e9956df37ca5e932708 Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 10 Jan 2025 12:03:38 +0530 Subject: [PATCH] staff phone unique --- src/controllers/userController.js | 33 +++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index d0b9b406..6ea4854f 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -825,7 +825,6 @@ exports.updateTeamMember = async (req, reply) => { - exports.createstaff = async (request, reply) => { try { const { customerId } = request.params; @@ -842,13 +841,30 @@ exports.createstaff = async (request, reply) => { return reply.status(404).send({ error: 'Customer not found' }); } - // Validate each staff entry and append it to the user's staff array - const newStaff = staff.map((member) => ({ - name: member.name || null, - phone: member.phone || null, - password: member.password || null, - status: "active", // Default status - })); + // Check for duplicate phone numbers + const existingPhones = new Set(user.staff.staff.map((member) => member.phone)); + const newStaff = []; + const duplicatePhones = []; + + staff.forEach((member) => { + if (member.phone && existingPhones.has(member.phone)) { + duplicatePhones.push(member.phone); + } else { + if (member.phone) { + existingPhones.add(member.phone); + } + newStaff.push({ + name: member.name || null, + phone: member.phone || null, + password: member.password || null, + status: "active", // Default status + }); + } + }); + + if (duplicatePhones.length > 0) { + return reply.status(400).send({ error: 'Duplicate phone numbers found', duplicatePhones }); + } // Update the user document with the new staff members user.staff.staff.push(...newStaff); @@ -864,6 +880,7 @@ exports.createstaff = async (request, reply) => { }; + exports.editStaff = async (request, reply) => { try { const { customerId, phone } = request.params;