import { ClientModel, VAModel, ProjectModel } from "../models/vaModel.js"; /** * Generic function to update relationships between Client, VA, and Project * @param {Object} data - The update data object * @returns {Object} - Result of the update operations */ async function updateEntityRelationships(data) { try { let results = {}; // Handle Project updates if (data.project) { const { projectId, vaId, clientId } = data.project; if (!projectId) { throw new Error('Project ID is required for project updates'); } // Update Client with projectId if (clientId) { const clientUpdate = await ClientModel.findByIdAndUpdate( clientId, { $addToSet: { projectIds: projectId, vaIds: vaId } }, { new: true } ); results.clientUpdate = clientUpdate ? 'Client updated successfully' : 'Client not found'; } // Update VAs with projectId if (vaId && Array.isArray(vaId) && vaId.length > 0) { const vaUpdateResult = await VAModel.updateMany( { _id: { $in: vaId } }, { $addToSet: { projectIds: projectId, clientIds: clientId } } ); results.vaUpdate = `Updated ${vaUpdateResult.modifiedCount} VAs`; } // // Update Project with clientId and vaId // const projectUpdateData = {}; // if (clientId) { // projectUpdateData.clientId = clientId; // } // // 🐞🐞🐞 Here Duplicate Entry of Va is Created if SF pass same va multiple times in same req // if (vaId && Array.isArray(vaId)) { // projectUpdateData.vaAssigned = vaId; // } // if (Object.keys(projectUpdateData).length > 0) { // const projectUpdate = await ProjectModel.findByIdAndUpdate( // projectId, // projectUpdateData, // { new: true } // ); // results.projectUpdate = projectUpdate ? 'Project updated successfully' : 'Project not found'; // } if (vaId && Array.isArray(vaId)) { await ProjectModel.findByIdAndUpdate( projectId, { $addToSet: { vaAssigned: { $each: vaId } } } ); } // Update other fields normally const projectUpdateData = {}; if (clientId) { projectUpdateData.clientId = clientId; } if (Object.keys(projectUpdateData).length > 0) { const projectUpdate = await ProjectModel.findByIdAndUpdate( projectId, projectUpdateData, { new: true } ); results.projectUpdate = projectUpdate ? 'Project updated successfully' : 'Project not found'; } } // Handle VA updates if (data.va) { const { vaId, projectId, clientId } = data.va; if (!vaId) { throw new Error('VA ID is required for VA updates'); } // Update Projects with vaId if (projectId && Array.isArray(projectId) && projectId.length > 0) { const projectUpdateResult = await ProjectModel.updateMany( { _id: { $in: projectId } }, { $addToSet: { vaAssigned: vaId } } ); results.projectUpdate = `Updated ${projectUpdateResult.modifiedCount} projects`; } // Update Clients with vaId if (clientId && Array.isArray(clientId) && clientId.length > 0) { const clientUpdateResult = await ClientModel.updateMany( { _id: { $in: clientId } }, { $addToSet: { vaIds: vaId } } ); results.clientUpdate = `Updated ${clientUpdateResult.modifiedCount} clients`; } // Update VA with clientId and projectId const vaUpdateData = {}; if (clientId && Array.isArray(clientId)) { vaUpdateData.clientIds = clientId; } if (projectId && Array.isArray(projectId)) { vaUpdateData.projectIds = projectId; } if (Object.keys(vaUpdateData).length > 0) { const vaUpdate = await VAModel.findByIdAndUpdate( vaId, { $addToSet: vaUpdateData }, { new: true } ); results.vaUpdate = vaUpdate ? 'VA updated successfully' : 'VA not found'; } } // Handle Client updates if (data.client) { const { clientId, projectId, vaId } = data.client; if (!clientId) { throw new Error('Client ID is required for client updates'); } // Update Projects with clientId if (projectId && Array.isArray(projectId) && projectId.length > 0) { const projectUpdateResult = await ProjectModel.updateMany( { _id: { $in: projectId } }, { clientId: clientId } ); results.projectUpdate = `Updated ${projectUpdateResult.modifiedCount} projects`; } // Update VAs with clientId if (vaId && Array.isArray(vaId) && vaId.length > 0) { const vaUpdateResult = await VAModel.updateMany( { _id: { $in: vaId } }, { $addToSet: { clientIds: clientId } } ); results.vaUpdate = `Updated ${vaUpdateResult.modifiedCount} VAs`; } // Update Client with projectId and vaId const clientUpdateData = {}; if (projectId && Array.isArray(projectId)) { clientUpdateData.projectIds = projectId; } if (vaId && Array.isArray(vaId)) { clientUpdateData.vaIds = vaId; } if (Object.keys(clientUpdateData).length > 0) { const clientUpdate = await ClientModel.findByIdAndUpdate( clientId, { $addToSet: clientUpdateData }, { new: true } ); results.clientUpdate = clientUpdate ? 'Client updated successfully' : 'Client not found'; } } return { success: true, message: 'Relationships updated successfully', results }; } catch (error) { console.error('Error updating entity relationships:', error); return { success: false, message: error.message, results: {} }; } } /** * Remove relationships (for deletion cases) * @param {Object} data - The removal data object * @returns {Object} - Result of the removal operations */ async function removeEntityRelationships(data) { try { let results = {}; // Handle Project removal if (data.project) { const { projectId, vaId, clientId } = data.project; if (!projectId) { throw new Error('Project ID is required for project removal'); } // Remove projectId from Client if (clientId) { await ClientModel.findByIdAndUpdate( clientId, {$pull: { projectIds: projectId, vaIds: vaId }} ); } // Remove projectId from VAs if (vaId && Array.isArray(vaId)) { // await VAModel.updateMany( // { _id: { $in: vaId } }, // { $pull: { // projectIds: projectId, // // 🐞🐞🐞 Here before removing we need to check if VA is still assigned to other projects of the same client // clientIds: clientId // }} // ); for (const va of vaId) { // Check if VA is still assigned to other projects of the same client const otherProjectsWithSameClient = await ProjectModel.countDocuments({ _id: { $ne: projectId }, clientId: clientId, vaAssigned: va }); // Prepare update operation const updateOperation = { $pull: { projectIds: projectId } }; // Only remove clientId if VA is not assigned to any other projects from this client if (!otherProjectsWithSameClient) { updateOperation.$pull = { ...updateOperation.$pull, clientIds: clientId }; } await VAModel.findByIdAndUpdate(va, updateOperation); } } results.projectRemoval = 'Project relationships removed successfully'; } // Handle VA removal if (data.va) { const { vaId, projectId, clientId } = data.va; if (!vaId) { throw new Error('VA ID is required for VA removal'); } // Remove vaId from Projects if (projectId && Array.isArray(projectId)) { await ProjectModel.updateMany( { _id: { $in: projectId } }, { $pull: { vaAssigned: vaId } } ); } // Remove vaId from Clients if (clientId && Array.isArray(clientId)) { await ClientModel.updateMany( { _id: { $in: clientId } }, { $pull: { vaIds: vaId } } ); } results.vaRemoval = 'VA relationships removed successfully'; } // Handle Client removal if (data.client) { const { clientId, projectId, vaId } = data.client; if (!clientId) { throw new Error('Client ID is required for client removal'); } // Remove clientId from Projects if (projectId && Array.isArray(projectId)) { await ProjectModel.updateMany( { _id: { $in: projectId } }, { clientId: null } ); } // Remove clientId from VAs if (vaId && Array.isArray(vaId)) { await VAModel.updateMany( { _id: { $in: vaId } }, { $pull: { clientIds: clientId } } ); } results.clientRemoval = 'Client relationships removed successfully'; } return { success: true, message: 'Relationships removed successfully', results }; } catch (error) { console.error('Error removing entity relationships:', error); return { success: false, message: error.message, results: {} }; } } export { updateEntityRelationships, removeEntityRelationships };