feature/IO-3255-simplified-parts-management - Checkpoint

This commit is contained in:
Dave
2025-08-28 14:24:35 -04:00
parent 67002b8443
commit f071a5cc9e
6 changed files with 173 additions and 152 deletions

View File

@@ -83,46 +83,46 @@ const deleteJobsByIds = async (jobIds) => {
*/
const partsManagementDeprovisioning = async (req, res) => {
const { logger } = req;
const p = req.body; //Same as other file, can p be renamed to be more descriptive?
const body = req.body;
if (process.env.NODE_ENV === "production") {
return res.status(403).json({ error: "Deprovisioning not allowed in production environment." });
}
try {
if (!p.shopId) {
if (!body.shopId) {
throw { status: 400, message: "shopId is required." };
}
// Fetch bodyshop and check external_shop_id
const shopResp = await client.request(GET_BODYSHOP, { id: p.shopId });
const shopResp = await client.request(GET_BODYSHOP, { id: body.shopId });
const shop = shopResp.bodyshops_by_pk;
if (!shop) {
throw { status: 404, message: `Bodyshop with id ${p.shopId} not found.` };
throw { status: 404, message: `Bodyshop with id ${body.shopId} not found.` };
}
if (!shop.external_shop_id) {
throw { status: 400, message: "Cannot delete bodyshop without external_shop_id." };
}
logger.log("admin-delete-shop", "debug", null, null, {
shopId: p.shopId,
shopId: body.shopId,
shopname: shop.shopname,
ioadmin: true
});
// Get vendors
const vendorsResp = await client.request(GET_VENDORS, { shopId: p.shopId });
const vendorsResp = await client.request(GET_VENDORS, { shopId: body.shopId });
const deletedVendors = vendorsResp.vendors.map((v) => v.name);
// Get associated users
const assocResp = await client.request(GET_ASSOCIATED_USERS, { shopId: p.shopId });
const assocResp = await client.request(GET_ASSOCIATED_USERS, { shopId: body.shopId });
const associatedUsers = assocResp.associations.map((assoc) => ({
authId: assoc.user.authid,
email: assoc.user.email
}));
// Delete associations for the shop
const assocDeleteResp = await client.request(DELETE_ASSOCIATIONS_BY_SHOP, { shopId: p.shopId });
const assocDeleteResp = await client.request(DELETE_ASSOCIATIONS_BY_SHOP, { shopId: body.shopId });
const associationsDeleted = assocDeleteResp.delete_associations.affected_rows;
// For each user, check if they have remaining associations; if not, delete user and Firebase account
@@ -138,23 +138,23 @@ const partsManagementDeprovisioning = async (req, res) => {
}
// Get all job ids for this shop, then delete joblines and jobs (joblines first)
const jobIds = await getJobIdsForShop(p.shopId);
const jobIds = await getJobIdsForShop(body.shopId);
const joblinesDeleted = await deleteJoblinesForJobs(jobIds);
const jobsDeleted = await deleteJobsByIds(jobIds);
// Delete any audit trail entries tied to this bodyshop to avoid FK violations
const auditResp = await client.request(DELETE_AUDIT_TRAIL_BY_SHOP, { shopId: p.shopId });
const auditResp = await client.request(DELETE_AUDIT_TRAIL_BY_SHOP, { shopId: body.shopId });
const auditDeleted = auditResp.delete_audit_trail.affected_rows;
// Delete vendors
await deleteVendorsByShop(p.shopId);
await deleteVendorsByShop(body.shopId);
// Delete shop
await deleteBodyshop(p.shopId);
await deleteBodyshop(body.shopId);
// Summary log
logger.log("admin-delete-shop-summary", "info", null, null, {
shopId: p.shopId,
shopId: body.shopId,
shopname: shop.shopname,
associationsDeleted,
deletedUsers,
@@ -165,8 +165,8 @@ const partsManagementDeprovisioning = async (req, res) => {
});
return res.status(200).json({
message: `Bodyshop ${p.shopId} and associated resources deleted successfully.`,
deletedShop: { id: p.shopId, name: shop.shopname },
message: `Bodyshop ${body.shopId} and associated resources deleted successfully.`,
deletedShop: { id: body.shopId, name: shop.shopname },
deletedAssociationsCount: associationsDeleted,
deletedUsers: deletedUsers,
deletedVendors: deletedVendors,