|
| 1 | +/** |
| 2 | + * Deletion functions for removing project-related records |
| 3 | + */ |
| 4 | + |
| 5 | +const { ObjectId } = require('mongodb'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Delete check-ins |
| 9 | + * @param {Db} db - MongoDB database instance |
| 10 | + * @param {Array} checkIns - Array of check-in documents |
| 11 | + * @param {boolean} isMock - If true, only print what would be deleted |
| 12 | + * @returns {Promise<void>} |
| 13 | + */ |
| 14 | +async function deleteCheckIns(db, checkIns, isMock) { |
| 15 | + if (checkIns.length === 0) { |
| 16 | + console.log('[INFO] No check-ins to delete.'); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + if (isMock) { |
| 21 | + console.log(`[MOCK] Would delete ${checkIns.length} check-in(s).`); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const checkInIds = checkIns.map((ci) => ci._id); |
| 26 | + const result = await db.collection('checkins').deleteMany({ _id: { $in: checkInIds } }); |
| 27 | + |
| 28 | + console.log(`[SUCCESS] Deleted ${result.deletedCount} check-in(s).`); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Delete project team members |
| 33 | + * @param {Db} db - MongoDB database instance |
| 34 | + * @param {string} projectId - The project ID |
| 35 | + * @param {boolean} isMock - If true, only print what would be deleted |
| 36 | + * @returns {Promise<void>} |
| 37 | + */ |
| 38 | +async function deleteProjectTeamMembers(db, projectId, isMock) { |
| 39 | + const count = await db.collection('projectteammembers').countDocuments({ projectId: projectId }); |
| 40 | + |
| 41 | + if (count === 0) { |
| 42 | + console.log('[INFO] No project team members to delete.'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (isMock) { |
| 47 | + console.log(`[MOCK] Would delete ${count} project team member(s).`); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const result = await db.collection('projectteammembers').deleteMany({ projectId: projectId }); |
| 52 | + |
| 53 | + console.log(`[SUCCESS] Deleted ${result.deletedCount} project team member(s).`); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Update users to remove project references |
| 58 | + * @param {Db} db - MongoDB database instance |
| 59 | + * @param {string} projectId - The project ID |
| 60 | + * @param {Array} users - Array of user documents |
| 61 | + * @param {boolean} isMock - If true, only print what would be updated |
| 62 | + * @returns {Promise<void>} |
| 63 | + */ |
| 64 | +async function updateUsersRemoveProject(db, projectId, users, isMock) { |
| 65 | + if (users.length === 0) { |
| 66 | + console.log('[INFO] No users to update.'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (isMock) { |
| 71 | + console.log(`[MOCK] Would update ${users.length} user(s) to remove project references.`); |
| 72 | + users.forEach((user) => { |
| 73 | + const hasInProjects = user.projects?.some((pid) => String(pid) === projectId); |
| 74 | + const hasInManagedProjects = user.managedProjects?.includes(projectId); |
| 75 | + console.log(` - User: ${user.name?.firstName} ${user.name?.lastName} (${user.email})`); |
| 76 | + if (hasInProjects) console.log(` * Remove from 'projects' array`); |
| 77 | + if (hasInManagedProjects) console.log(` * Remove from 'managedProjects' array`); |
| 78 | + }); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const operations = users.map((user) => ({ |
| 83 | + updateOne: { |
| 84 | + filter: { _id: user._id }, |
| 85 | + update: { |
| 86 | + $pull: { |
| 87 | + projects: new ObjectId(projectId), |
| 88 | + managedProjects: projectId, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + })); |
| 93 | + |
| 94 | + const result = await db.collection('users').bulkWrite(operations, { ordered: false }); |
| 95 | + |
| 96 | + console.log(`[SUCCESS] Updated ${result.modifiedCount} user(s) to remove project references.`); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Delete events |
| 101 | + * @param {Db} db - MongoDB database instance |
| 102 | + * @param {string} projectId - The project ID |
| 103 | + * @param {boolean} isMock - If true, only print what would be deleted |
| 104 | + * @returns {Promise<void>} |
| 105 | + */ |
| 106 | +async function deleteEvents(db, projectId, isMock) { |
| 107 | + const count = await db.collection('events').countDocuments({ project: new ObjectId(projectId) }); |
| 108 | + |
| 109 | + if (count === 0) { |
| 110 | + console.log('[INFO] No events to delete.'); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (isMock) { |
| 115 | + console.log(`[MOCK] Would delete ${count} event(s).`); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const result = await db.collection('events').deleteMany({ project: new ObjectId(projectId) }); |
| 120 | + |
| 121 | + console.log(`[SUCCESS] Deleted ${result.deletedCount} event(s).`); |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Delete recurring events |
| 126 | + * @param {Db} db - MongoDB database instance |
| 127 | + * @param {Array} recurringEvents - Array of recurring event documents |
| 128 | + * @param {boolean} isMock - If true, only print what would be deleted |
| 129 | + * @returns {Promise<void>} |
| 130 | + */ |
| 131 | +async function deleteRecurringEvents(db, recurringEvents, isMock) { |
| 132 | + if (recurringEvents.length === 0) { |
| 133 | + console.log('[INFO] No recurring events to delete.'); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (isMock) { |
| 138 | + console.log(`[MOCK] Would delete ${recurringEvents.length} recurring event(s).`); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const recurringEventIds = recurringEvents.map((re) => re._id); |
| 143 | + const result = await db |
| 144 | + .collection('recurringevents') |
| 145 | + .deleteMany({ _id: { $in: recurringEventIds } }); |
| 146 | + |
| 147 | + console.log(`[SUCCESS] Deleted ${result.deletedCount} recurring event(s).`); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Delete the project |
| 152 | + * @param {Db} db - MongoDB database instance |
| 153 | + * @param {string} projectId - The project ID |
| 154 | + * @param {boolean} isMock - If true, only print what would be deleted |
| 155 | + * @returns {Promise<void>} |
| 156 | + */ |
| 157 | +async function deleteProject(db, projectId, isMock) { |
| 158 | + if (isMock) { |
| 159 | + console.log(`[MOCK] Would delete project: ${projectId}`); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + const result = await db.collection('projects').deleteOne({ _id: new ObjectId(projectId) }); |
| 164 | + |
| 165 | + console.log(`[SUCCESS] Deleted ${result.deletedCount} project.`); |
| 166 | +} |
| 167 | + |
| 168 | +module.exports = { |
| 169 | + deleteCheckIns, |
| 170 | + deleteProjectTeamMembers, |
| 171 | + updateUsersRemoveProject, |
| 172 | + deleteEvents, |
| 173 | + deleteRecurringEvents, |
| 174 | + deleteProject, |
| 175 | +}; |
0 commit comments