Skip to content

Commit 9226063

Browse files
committed
fix: remove queries using conversation.applied_sla_id
as this column is removed - fix sql query
1 parent a9fd4fe commit 9226063

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

internal/conversation/conversation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Manager struct {
8383
type slaStore interface {
8484
ApplySLA(startTime time.Time, conversationID, assignedTeamID, slaID int) (slaModels.SLAPolicy, error)
8585
CreateNextResponseSLAEvent(conversationID, assignedTeamID int) (time.Time, error)
86-
SetLatestSLAEventMetAt(appliedSLAID int, metric string) (time.Time, error)
86+
SetLatestSLAEventMetAt(conversationID int, metric string) (time.Time, error)
8787
}
8888

8989
type statusStore interface {

internal/conversation/message.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,12 @@ func (m *Manager) sendOutgoingMessage(message models.Message) {
189189
if systemUser, err := m.userStore.GetSystemUser(); err == nil && message.SenderID != systemUser.ID {
190190
m.UpdateConversationFirstReplyAt(message.ConversationUUID, message.ConversationID, time.Now())
191191
m.UpdateConversationLastReplyAt(message.ConversationUUID, message.ConversationID, time.Now())
192-
// Set `met_at` timestamp for next response SLA event if event exists and not already met.
193-
if message.ConversationAppliedSLAID.Int > 0 {
194-
metAt, err := m.slaStore.SetLatestSLAEventMetAt(message.ConversationAppliedSLAID.Int, sla.MetricNextResponse)
195-
if err != nil {
196-
m.lo.Error("error setting next response SLA event met at", "conversation_id", message.ConversationID, "error", err)
197-
} else if !metAt.IsZero() {
198-
m.BroadcastConversationUpdate(message.ConversationUUID, "next_response_met_at", metAt.Format(time.RFC3339))
199-
}
192+
// Set the `met_at` timestamp for the latest next response SLA event if the event exists and is not already met.
193+
metAt, err := m.slaStore.SetLatestSLAEventMetAt(message.ConversationID, sla.MetricNextResponse)
194+
if err != nil {
195+
m.lo.Error("error setting next response SLA event met at", "conversation_id", message.ConversationID, "error", err)
196+
} else if !metAt.IsZero() {
197+
m.BroadcastConversationUpdate(message.ConversationUUID, "next_response_met_at", metAt.Format(time.RFC3339))
200198
}
201199
} else if err != nil {
202200
m.lo.Error("error fetching system user for updating first reply time", "error", err)

internal/conversation/models/models.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ type Message struct {
137137
Meta json.RawMessage `db:"meta" json:"meta"`
138138
Attachments attachment.Attachments `db:"attachments" json:"attachments"`
139139
ConversationUUID string `db:"conversation_uuid" json:"-"`
140-
ConversationAppliedSLAID null.Int `db:"conversation_applied_sla_id" json:"-"`
141140
From string `db:"from" json:"-"`
142141
Subject string `db:"subject" json:"-"`
143142
Channel string `db:"channel" json:"-"`

internal/conversation/queries.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ SELECT
426426
ARRAY(SELECT jsonb_array_elements_text(m.meta->'to')) AS to,
427427
c.inbox_id,
428428
c.uuid as conversation_uuid,
429-
c.applied_sla_id as conversation_applied_sla_id,
430429
c.subject
431430
FROM conversation_messages m
432431
INNER JOIN conversations c ON c.id = m.conversation_id

internal/sla/queries.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ SET next_sla_deadline_at = CASE
7777

7878
-- If an external timestamp ($3) is provided (e.g. next_response), use the earliest of $3.
7979
WHEN $3 IS NOT NULL THEN LEAST(
80-
$3,
80+
$3::TIMESTAMPTZ,
8181
CASE
8282
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
8383
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

internal/sla/sla.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,22 @@ func (m *Manager) CreateNextResponseSLAEvent(conversationID, assignedTeamID int)
349349
}
350350

351351
// SetLatestSLAEventMetAt marks the latest SLA event as met for a given applied SLA.
352-
func (m *Manager) SetLatestSLAEventMetAt(appliedSLAID int, metric string) (time.Time, error) {
352+
func (m *Manager) SetLatestSLAEventMetAt(conversationID int, metric string) (time.Time, error) {
353+
var appliedSLA models.AppliedSLA
354+
if err := m.q.GetLatestAppliedSLAForConversation.Get(&appliedSLA, conversationID); err != nil {
355+
if err == sql.ErrNoRows {
356+
m.lo.Warn("no applied SLA found for conversation to update met at", "conversation_id", conversationID)
357+
return time.Time{}, fmt.Errorf("no applied SLA found for conversation: %d to update met at", conversationID)
358+
}
359+
m.lo.Error("error fetching latest applied SLA for conversation", "error", err)
360+
return time.Time{}, fmt.Errorf("fetching latest applied SLA for conversation: %w", err)
361+
}
362+
353363
var metAt time.Time
354-
if err := m.q.SetLatestSLAEventMetAt.QueryRow(appliedSLAID, metric).Scan(&metAt); err != nil {
364+
if err := m.q.SetLatestSLAEventMetAt.QueryRow(appliedSLA.ID, metric).Scan(&metAt); err != nil {
355365
if err == sql.ErrNoRows {
356-
m.lo.Warn("no SLA event found for applied SLA ID and metric to update met at", "applied_sla_id", appliedSLAID, "metric", metric)
357-
return metAt, fmt.Errorf("no SLA event found for applied SLA ID: %d and metric: %s to update met at", appliedSLAID, metric)
366+
m.lo.Warn("no SLA event found for applied SLA and metric to update met at", "applied_sla_id", appliedSLA.ID, "metric", metric)
367+
return metAt, fmt.Errorf("no SLA event found for applied SLA ID: %d and metric: %s to update met at", appliedSLA.ID, metric)
358368
}
359369
m.lo.Error("error marking SLA event as met", "error", err)
360370
return metAt, fmt.Errorf("marking SLA event as met: %w", err)

0 commit comments

Comments
 (0)