diff --git a/backend/src/controllers/chitGroupController.js b/backend/src/controllers/chitGroupController.js index b6366f2..6ebe11d 100644 --- a/backend/src/controllers/chitGroupController.js +++ b/backend/src/controllers/chitGroupController.js @@ -369,20 +369,14 @@ const updateChitGroup = async (req, res) => { } }; -// Delete chit group (only if forming and no members) +// Delete chit group (only if forming and no active members) const deleteChitGroup = async (req, res) => { try { const { groupId } = req.params; const managerId = req.user.id; const chitGroup = await ChitGroup.findOne({ - where: { id: groupId, manager_id: managerId }, - include: [ - { - model: GroupMember, - as: 'members' - } - ] + where: { id: groupId, manager_id: managerId } }); if (!chitGroup) { @@ -399,10 +393,18 @@ const deleteChitGroup = async (req, res) => { }); } - if (chitGroup.members.length > 0) { + // Count only ACTIVE members (not removed or inactive) + const activeMemberCount = await GroupMember.count({ + where: { + group_id: groupId, + status: 'active' + } + }); + + if (activeMemberCount > 0) { return res.status(400).json({ success: false, - message: 'Cannot delete chit group with existing members' + message: `Cannot delete chit group with ${activeMemberCount} active member(s). Please remove all active members first.` }); }