Refactor: assigned user removal when changing assigned team

This commit is contained in:
Abhinav Raut
2025-09-16 21:31:53 +05:30
parent a652f380b2
commit d63302843b
2 changed files with 14 additions and 5 deletions

View File

@@ -541,6 +541,10 @@ func (c *Manager) UpdateConversationTeamAssignee(uuid string, teamID int, actor
// Team changed?
if previousAssignedTeamID != teamID {
// Remove assigned user if team has changed.
c.RemoveConversationAssignee(uuid, models.AssigneeTypeUser, actor)
// Apply SLA policy if this new team has a SLA policy.
team, err := c.teamStore.Get(teamID)
if err != nil {
return nil
@@ -582,8 +586,6 @@ func (c *Manager) UpdateAssignee(uuid string, assigneeID int, assigneeType strin
c.lo.Error("error updating conversation assignee", "error", err)
return fmt.Errorf("updating assignee: %w", err)
}
// Clear assigned user ID.
c.BroadcastConversationUpdate(uuid, "assigned_user_id", nil)
default:
return fmt.Errorf("invalid assignee type: %s", assigneeType)
}
@@ -962,7 +964,7 @@ func (m *Manager) ApplyAction(action amodels.RuleAction, conv models.Conversatio
return nil
}
// RemoveConversationAssignee removes the assignee from the conversation.
// RemoveConversationAssignee removes assigned user from a conversation.
func (m *Manager) RemoveConversationAssignee(uuid, typ string, actor umodels.User) error {
if _, err := m.q.RemoveConversationAssignee.Exec(uuid, typ); err != nil {
m.lo.Error("error removing conversation assignee", "error", err)
@@ -977,6 +979,14 @@ func (m *Manager) RemoveConversationAssignee(uuid, typ string, actor umodels.Use
})
}
// Broadcast ws update.
switch typ {
case models.AssigneeTypeUser:
m.BroadcastConversationUpdate(uuid, "assigned_user_id", nil)
case models.AssigneeTypeTeam:
m.BroadcastConversationUpdate(uuid, "assigned_team_id", nil)
}
return nil
}

View File

@@ -215,8 +215,6 @@ WHERE uuid = $1;
-- name: update-conversation-assigned-team
UPDATE conversations
SET assigned_team_id = $2,
assigned_user_id = NULL,
assignee_last_seen_at = NULL,
updated_at = NOW()
WHERE uuid = $1;
@@ -355,6 +353,7 @@ WHERE uuid = $1;
UPDATE conversations
SET
assigned_user_id = CASE WHEN $2 = 'user' THEN NULL ELSE assigned_user_id END,
assignee_last_seen_at = CASE WHEN $2 = 'user' THEN NULL ELSE assignee_last_seen_at END,
assigned_team_id = CASE WHEN $2 = 'team' THEN NULL ELSE assigned_team_id END,
updated_at = NOW()
WHERE uuid = $1;