feature/IO-3255-simplified-parts-management - Expand deprovision route

This commit is contained in:
Dave
2025-09-02 15:23:44 -04:00
parent fa33b88632
commit 0883274320

View File

@@ -129,15 +129,17 @@ const partsManagementDeprovisioning = async (req, res) => {
const deletedUsers = []; const deletedUsers = [];
for (const user of associatedUsers) { for (const user of associatedUsers) {
const countResp = await client.request(GET_USER_ASSOCIATIONS_COUNT, { userEmail: user.email }); const countResp = await client.request(GET_USER_ASSOCIATIONS_COUNT, { userEmail: user.email });
const assocCount = countResp.associations_aggregate.aggregate.count; // Determine which users now have zero associations and should be deleted (defer deletion until end)
if (assocCount === 0) { const emailsToAuthId = associatedUsers.reduce((acc, u) => {
await client.request(DELETE_USER, { email: user.email }); acc[u.email] = u.authId;
await deleteFirebaseUser(user.authId); return acc;
deletedUsers.push(user.email); }, {});
} const emailsToDelete = [];
await client.request(DELETE_USER, { email: user.email });
await deleteFirebaseUser(user.authId);
deletedUsers.push(user.email);
} }
emailsToDelete.push(user.email);
// Get all job ids for this shop, then delete joblines and jobs (joblines first)
const jobIds = await getJobIdsForShop(body.shopId); const jobIds = await getJobIdsForShop(body.shopId);
const joblinesDeleted = await deleteJoblinesForJobs(jobIds); const joblinesDeleted = await deleteJoblinesForJobs(jobIds);
const jobsDeleted = await deleteJobsByIds(jobIds); const jobsDeleted = await deleteJobsByIds(jobIds);
@@ -174,6 +176,16 @@ const partsManagementDeprovisioning = async (req, res) => {
deletedJobsCount: jobsDeleted, deletedJobsCount: jobsDeleted,
deletedAuditTrailCount: auditDeleted 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) { } catch (err) {
logger.log("admin-delete-shop-error", "error", null, null, { logger.log("admin-delete-shop-error", "error", null, null, {
message: err.message, message: err.message,