fixed deletion

This commit is contained in:
Deep Koluguri 2025-11-06 15:05:02 -05:00
parent b3f064d48d
commit c855e1b434
1 changed files with 12 additions and 10 deletions

View File

@ -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) => { const deleteChitGroup = async (req, res) => {
try { try {
const { groupId } = req.params; const { groupId } = req.params;
const managerId = req.user.id; const managerId = req.user.id;
const chitGroup = await ChitGroup.findOne({ const chitGroup = await ChitGroup.findOne({
where: { id: groupId, manager_id: managerId }, where: { id: groupId, manager_id: managerId }
include: [
{
model: GroupMember,
as: 'members'
}
]
}); });
if (!chitGroup) { 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({ return res.status(400).json({
success: false, 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.`
}); });
} }