feature/IO-3255-simplified-parts-management - Expand deprovision route
This commit is contained in:
@@ -129,15 +129,17 @@ const partsManagementDeprovisioning = async (req, res) => {
|
||||
const deletedUsers = [];
|
||||
for (const user of associatedUsers) {
|
||||
const countResp = await client.request(GET_USER_ASSOCIATIONS_COUNT, { userEmail: user.email });
|
||||
const assocCount = countResp.associations_aggregate.aggregate.count;
|
||||
if (assocCount === 0) {
|
||||
await client.request(DELETE_USER, { email: user.email });
|
||||
await deleteFirebaseUser(user.authId);
|
||||
deletedUsers.push(user.email);
|
||||
}
|
||||
// Determine which users now have zero associations and should be deleted (defer deletion until end)
|
||||
const emailsToAuthId = associatedUsers.reduce((acc, u) => {
|
||||
acc[u.email] = u.authId;
|
||||
return acc;
|
||||
}, {});
|
||||
const emailsToDelete = [];
|
||||
await client.request(DELETE_USER, { email: user.email });
|
||||
await deleteFirebaseUser(user.authId);
|
||||
deletedUsers.push(user.email);
|
||||
}
|
||||
|
||||
// Get all job ids for this shop, then delete joblines and jobs (joblines first)
|
||||
emailsToDelete.push(user.email);
|
||||
const jobIds = await getJobIdsForShop(body.shopId);
|
||||
const joblinesDeleted = await deleteJoblinesForJobs(jobIds);
|
||||
const jobsDeleted = await deleteJobsByIds(jobIds);
|
||||
@@ -174,6 +176,16 @@ const partsManagementDeprovisioning = async (req, res) => {
|
||||
deletedJobsCount: jobsDeleted,
|
||||
deletedAuditTrailCount: auditDeleted
|
||||
});
|
||||
// Now delete users that have no remaining associations and their Firebase accounts
|
||||
const deletedUsers = [];
|
||||
for (const email of emailsToDelete) {
|
||||
await client.request(DELETE_USER, { email });
|
||||
const authId = emailsToAuthId[email];
|
||||
if (authId) {
|
||||
await deleteFirebaseUser(authId);
|
||||
}
|
||||
deletedUsers.push(email);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.log("admin-delete-shop-error", "error", null, null, {
|
||||
message: err.message,
|
||||
|
||||
Reference in New Issue
Block a user