Skip to content

Commit 272d7ec

Browse files
committed
filter duplicate notifications, ensure updates are from end-users
1 parent 267bb81 commit 272d7ec

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

services/zendesk.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type SatisfactionRating struct {
5656
type User struct {
5757
ID int64 `json:"id"`
5858
Name string `json:"name"`
59+
Role string `json:"role"`
5960
}
6061

6162
// Organization represents a Zendesk organization.
@@ -147,8 +148,12 @@ func processTickets(ctx context.Context, db db.Database, tickets []zendesk.Ticke
147148

148149
userAlerts, err := models.GetAllTagAlerts(db)
149150
log.Printf("Processing %d tickets...\n", len(tickets))
150-
for _, ticket := range tickets {
151+
processedTickets := make(map[int64]bool)
151152

153+
for _, ticket := range tickets {
154+
if processedTickets[ticket.ID] {
155+
continue
156+
}
152157
if err != nil {
153158
fmt.Println("Error fetching user alerts:", err)
154159
continue
@@ -166,13 +171,14 @@ func processTickets(ctx context.Context, db db.Database, tickets []zendesk.Ticke
166171
sendAlert = isNewTicket(ticket)
167172
case AlertTypeTicketUpdate:
168173
if isUpdatedTicket(ticket) {
169-
lastPublicCommentTime, err := zc.GetLastPublicCommentTime(ticket.ID)
174+
lastPublicCommentTime, isEndUser, err := zc.GetLastPublicCommentTime(ticket.ID)
170175
if err != nil {
171176
log.Printf("Skipping ticket %d: %v", ticket.ID, err)
172177
continue
173178
}
174179

175-
if lastPublicCommentTime.After(lastPollTime) {
180+
// Only send alert if the last comment was after lastPollTime AND made by an end user
181+
if lastPublicCommentTime.After(lastPollTime) && isEndUser {
176182
sendAlert = true
177183
}
178184
}
@@ -234,6 +240,8 @@ func processTickets(ctx context.Context, db db.Database, tickets []zendesk.Ticke
234240
}
235241
}
236242
}
243+
processedTickets[ticket.ID] = true
244+
237245
}
238246
middlewares.AddGlobalNotification(sseServer, "Ticket processing complete", fmt.Sprintf("Processed %v tickets...", len(tickets)), "success")
239247
}
@@ -355,10 +363,12 @@ func (zc *ZendeskClient) GetOrganizationByID(organizationID int64) (*Organizatio
355363
return &result.Organization, nil
356364
}
357365

358-
// GetLastPublicCommentTime retrieves the timestamp of the last public comment on a ticket.
359-
// GetLastPublicCommentTime retrieves the timestamp of the last public comment on a ticket.
360-
func (zc *ZendeskClient) GetLastPublicCommentTime(ticketID int64) (time.Time, error) {
366+
// GetLastPublicCommentTime retrieves the timestamp of the last public comment on a ticket
367+
// and determines if the comment was made by an end user.
368+
func (zc *ZendeskClient) GetLastPublicCommentTime(ticketID int64) (time.Time, bool, error) {
361369
var lastPublicCommentTime time.Time
370+
var isEndUser bool // Tracks if the last comment was from an end user
371+
362372
ops := zendesk.NewPaginationOptions()
363373
ops.PageSize = 10
364374
ops.Id = ticketID
@@ -367,7 +377,7 @@ func (zc *ZendeskClient) GetLastPublicCommentTime(ticketID int64) (time.Time, er
367377
for it.HasMore() {
368378
comments, err := it.GetNext()
369379
if err != nil {
370-
return time.Time{}, fmt.Errorf("failed to retrieve comments for ticket %d: %v", ticketID, err)
380+
return time.Time{}, false, fmt.Errorf("failed to retrieve comments for ticket %d: %v", ticketID, err)
371381
}
372382

373383
// Ensure we have comments before accessing the last element
@@ -382,15 +392,25 @@ func (zc *ZendeskClient) GetLastPublicCommentTime(ticketID int64) (time.Time, er
382392
commentTime := lastComment.CreatedAt
383393
if commentTime.After(lastPublicCommentTime) {
384394
lastPublicCommentTime = commentTime
395+
396+
// Determine if the commenter is an end user
397+
requester, err := zc.GetRequesterByID(lastComment.AuthorID)
398+
if err != nil {
399+
return time.Time{}, false, fmt.Errorf("failed to retrieve author details for ticket %d: %v", ticketID, err)
400+
401+
}
402+
403+
isEndUser = (requester.Role == "end-user")
404+
385405
}
386406
}
387407
}
388408

389409
if lastPublicCommentTime.IsZero() {
390-
return time.Time{}, fmt.Errorf("no public comments found for ticket %d", ticketID)
410+
return time.Time{}, false, fmt.Errorf("no public comments found for ticket %d", ticketID)
391411
}
392412

393-
return lastPublicCommentTime, nil
413+
return lastPublicCommentTime, isEndUser, nil
394414
}
395415

396416
func getSLALabel(ticket zendesk.Ticket, slaData map[int64]SLAInfo) string {

0 commit comments

Comments
 (0)