fix: remove queries using conversation.applied_sla_id

as this column is removed
- fix sql query
This commit is contained in:
Abhinav Raut
2025-05-25 10:56:31 +05:30
parent a9fd4fe2b6
commit 9226063db3
6 changed files with 23 additions and 17 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ type Manager struct {
type slaStore interface {
ApplySLA(startTime time.Time, conversationID, assignedTeamID, slaID int) (slaModels.SLAPolicy, error)
CreateNextResponseSLAEvent(conversationID, assignedTeamID int) (time.Time, error)
SetLatestSLAEventMetAt(appliedSLAID int, metric string) (time.Time, error)
SetLatestSLAEventMetAt(conversationID int, metric string) (time.Time, error)
}
type statusStore interface {
+6 -8
View File
@@ -189,14 +189,12 @@ func (m *Manager) sendOutgoingMessage(message models.Message) {
if systemUser, err := m.userStore.GetSystemUser(); err == nil && message.SenderID != systemUser.ID {
m.UpdateConversationFirstReplyAt(message.ConversationUUID, message.ConversationID, time.Now())
m.UpdateConversationLastReplyAt(message.ConversationUUID, message.ConversationID, time.Now())
// Set `met_at` timestamp for next response SLA event if event exists and not already met.
if message.ConversationAppliedSLAID.Int > 0 {
metAt, err := m.slaStore.SetLatestSLAEventMetAt(message.ConversationAppliedSLAID.Int, sla.MetricNextResponse)
if err != nil {
m.lo.Error("error setting next response SLA event met at", "conversation_id", message.ConversationID, "error", err)
} else if !metAt.IsZero() {
m.BroadcastConversationUpdate(message.ConversationUUID, "next_response_met_at", metAt.Format(time.RFC3339))
}
// Set the `met_at` timestamp for the latest next response SLA event if the event exists and is not already met.
metAt, err := m.slaStore.SetLatestSLAEventMetAt(message.ConversationID, sla.MetricNextResponse)
if err != nil {
m.lo.Error("error setting next response SLA event met at", "conversation_id", message.ConversationID, "error", err)
} else if !metAt.IsZero() {
m.BroadcastConversationUpdate(message.ConversationUUID, "next_response_met_at", metAt.Format(time.RFC3339))
}
} else if err != nil {
m.lo.Error("error fetching system user for updating first reply time", "error", err)
-1
View File
@@ -137,7 +137,6 @@ type Message struct {
Meta json.RawMessage `db:"meta" json:"meta"`
Attachments attachment.Attachments `db:"attachments" json:"attachments"`
ConversationUUID string `db:"conversation_uuid" json:"-"`
ConversationAppliedSLAID null.Int `db:"conversation_applied_sla_id" json:"-"`
From string `db:"from" json:"-"`
Subject string `db:"subject" json:"-"`
Channel string `db:"channel" json:"-"`
-1
View File
@@ -426,7 +426,6 @@ SELECT
ARRAY(SELECT jsonb_array_elements_text(m.meta->'to')) AS to,
c.inbox_id,
c.uuid as conversation_uuid,
c.applied_sla_id as conversation_applied_sla_id,
c.subject
FROM conversation_messages m
INNER JOIN conversations c ON c.id = m.conversation_id
+1 -1
View File
@@ -77,7 +77,7 @@ SET next_sla_deadline_at = CASE
-- If an external timestamp ($3) is provided (e.g. next_response), use the earliest of $3.
WHEN $3 IS NOT NULL THEN LEAST(
$3,
$3::TIMESTAMPTZ,
CASE
WHEN c.first_reply_at IS NOT NULL AND c.resolved_at IS NULL AND a.resolution_deadline_at IS NOT NULL THEN a.resolution_deadline_at
WHEN c.first_reply_at IS NULL AND c.resolved_at IS NULL AND a.first_response_deadline_at IS NOT NULL THEN a.first_response_deadline_at
+15 -5
View File
@@ -349,12 +349,22 @@ func (m *Manager) CreateNextResponseSLAEvent(conversationID, assignedTeamID int)
}
// SetLatestSLAEventMetAt marks the latest SLA event as met for a given applied SLA.
func (m *Manager) SetLatestSLAEventMetAt(appliedSLAID int, metric string) (time.Time, error) {
var metAt time.Time
if err := m.q.SetLatestSLAEventMetAt.QueryRow(appliedSLAID, metric).Scan(&metAt); err != nil {
func (m *Manager) SetLatestSLAEventMetAt(conversationID int, metric string) (time.Time, error) {
var appliedSLA models.AppliedSLA
if err := m.q.GetLatestAppliedSLAForConversation.Get(&appliedSLA, conversationID); err != nil {
if err == sql.ErrNoRows {
m.lo.Warn("no SLA event found for applied SLA ID and metric to update met at", "applied_sla_id", appliedSLAID, "metric", metric)
return metAt, fmt.Errorf("no SLA event found for applied SLA ID: %d and metric: %s to update met at", appliedSLAID, metric)
m.lo.Warn("no applied SLA found for conversation to update met at", "conversation_id", conversationID)
return time.Time{}, fmt.Errorf("no applied SLA found for conversation: %d to update met at", conversationID)
}
m.lo.Error("error fetching latest applied SLA for conversation", "error", err)
return time.Time{}, fmt.Errorf("fetching latest applied SLA for conversation: %w", err)
}
var metAt time.Time
if err := m.q.SetLatestSLAEventMetAt.QueryRow(appliedSLA.ID, metric).Scan(&metAt); err != nil {
if err == sql.ErrNoRows {
m.lo.Warn("no SLA event found for applied SLA and metric to update met at", "applied_sla_id", appliedSLA.ID, "metric", metric)
return metAt, fmt.Errorf("no SLA event found for applied SLA ID: %d and metric: %s to update met at", appliedSLA.ID, metric)
}
m.lo.Error("error marking SLA event as met", "error", err)
return metAt, fmt.Errorf("marking SLA event as met: %w", err)