From 1d2c6f1ad81d0ee0b5e7c2b54134ac3ca8d3fba0 Mon Sep 17 00:00:00 2001 From: Kenneth Obsequio Date: Mon, 31 Aug 2026 11:00:29 +0800 Subject: [PATCH] tiers controller acid missing Signed-off-by: Kenneth Obsequio --- apps/api/controllers/admin/tiers.controller.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/api/controllers/admin/tiers.controller.js b/apps/api/controllers/admin/tiers.controller.js index f60e3cb..4d9367e 100644 --- a/apps/api/controllers/admin/tiers.controller.js +++ b/apps/api/controllers/admin/tiers.controller.js @@ -10,6 +10,9 @@ ***********************************************************************************************************************************************************************/ const { Op, ForeignKeyConstraintError } = require('sequelize'); +const sequelize = require('../../config/db.config'); +const { withTransactionRetry } = require('../../utils/withTransactionRetry.util'); + const mdl_TierCategories = require('../../models/tiers/tier_categories.mdl'); const mdl_TierPlans = require('../../models/tiers/tier_plans.mdl'); const mdl_UserTiers = require('../../models/tiers/user_tiers.mdl'); @@ -348,9 +351,12 @@ exports.permanentlyDeletePlan = async (req, res) => { // by default) — a plan can't be force-destroyed while payment rows still // reference it, so those rows are force-destroyed first. This permanently // erases that plan's payment/billing history; there is no undo. - const deleted_payment_count = await mdl_Payments.destroy({ where: { plan_id: plan.plan_id }, force: true }); + const deleted_payment_count = await withTransactionRetry(sequelize, async (t) => { + const count = await mdl_Payments.destroy({ where: { plan_id: plan.plan_id }, force: true, transaction: t }); + await plan.destroy({ force: true, transaction: t }); + return count; + }); - await plan.destroy({ force: true }); logActivity(req.user?.user_id, 'permanently_delete_tier_plan', { entityType: 'tier_plan', details: { label: plan.label, deleted_payment_count, revoked_user_count } }); return R.success(res, 'Plan permanently deleted.', { deleted_payment_count, revoked_user_count }); } catch (err) { @@ -387,9 +393,11 @@ exports.bulkPermanentlyDeletePlans = async (req, res) => { // by default) — plans can't be force-destroyed while payment rows still // reference them, so those rows are force-destroyed first. This permanently // erases these plans' payment/billing history; there is no undo. - const deleted_payment_count = await mdl_Payments.destroy({ where: { plan_id: archivedIds }, force: true }); - - await mdl_TierPlans.destroy({ where: { plan_id: archivedIds }, force: true }); + const deleted_payment_count = await withTransactionRetry(sequelize, async (t) => { + const count = await mdl_Payments.destroy({ where: { plan_id: archivedIds }, force: true, transaction: t }); + await mdl_TierPlans.destroy({ where: { plan_id: archivedIds }, force: true, transaction: t }); + return count; + }); logActivity(req.user?.user_id, 'bulk_permanently_delete_tier_plans', { entityType: 'tier_plan', details: { ids: archivedIds, count: archivedIds.length, deleted_payment_count, revoked_user_count } }); return R.success(res, `${archivedIds.length} plan(s) permanently deleted.`, {