Skip to content

Commit

Permalink
Update ticket last_activity_on for incoming messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jun 17, 2021
1 parent 3b01c81 commit 439d8f4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
33 changes: 13 additions & 20 deletions core/models/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,11 @@ func InsertTickets(ctx context.Context, tx Queryer, tickets []*Ticket) error {
return BulkQuery(ctx, "inserted tickets", tx, insertTicketSQL, ts)
}

const updateTicketExternalIDSQL = `
UPDATE
tickets_ticket
SET
external_id = $2
WHERE
id = $1
`

// UpdateTicketExternalID updates the external ID of the given ticket
func UpdateTicketExternalID(ctx context.Context, db Queryer, ticket *Ticket, externalID string) error {
t := &ticket.t
t.ExternalID = null.String(externalID)
return Exec(ctx, "update ticket external ID", db, updateTicketExternalIDSQL, t.ID, t.ExternalID)
return Exec(ctx, "update ticket external ID", db, `UPDATE tickets_ticket SET external_id = $2 WHERE id = $1`, t.ID, t.ExternalID)
}

// UpdateTicketConfig updates the passed in ticket's config with any passed in values
Expand All @@ -320,6 +311,17 @@ func UpdateTicketConfig(ctx context.Context, db Queryer, ticket *Ticket, config
return Exec(ctx, "update ticket config", db, `UPDATE tickets_ticket SET config = $2 WHERE id = $1`, t.ID, t.Config)
}

// UpdateTicketLastActivity updates the last_activity_on of the given tickets to be now
func UpdateTicketLastActivity(ctx context.Context, db Queryer, tickets []*Ticket) error {
now := dates.Now()
ids := make([]TicketID, len(tickets))
for i, t := range tickets {
t.t.LastActivityOn = now
ids[i] = t.ID()
}
return Exec(ctx, "update ticket last activity", db, `UPDATE tickets_ticket SET last_activity_on = $2 WHERE id = ANY($1)`, pq.Array(ids), now)
}

const closeTicketSQL = `
UPDATE
tickets_ticket
Expand Down Expand Up @@ -501,15 +503,6 @@ func (t *Ticketer) AsService(ticketer *flows.Ticketer) (TicketService, error) {
return nil, errors.Errorf("unrecognized ticket service type '%s'", t.Type())
}

const updateTicketerConfigSQL = `
UPDATE
tickets_ticketer
SET
config = $2
WHERE
id = $1
`

// UpdateConfig updates the configuration of this ticketer with the given values
func (t *Ticketer) UpdateConfig(ctx context.Context, db Queryer, add map[string]string, remove map[string]bool) error {
for key, value := range add {
Expand All @@ -525,7 +518,7 @@ func (t *Ticketer) UpdateConfig(ctx context.Context, db Queryer, add map[string]
dbMap[key] = value
}

return Exec(ctx, "update ticketer config", db, updateTicketerConfigSQL, t.t.ID, null.NewMap(dbMap))
return Exec(ctx, "update ticketer config", db, `UPDATE tickets_ticketer SET config = $2 WHERE id = $1`, t.t.ID, null.NewMap(dbMap))
}

// TicketService extends the engine's ticket service and adds support for forwarding new incoming messages
Expand Down
12 changes: 12 additions & 0 deletions core/tasks/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestMsgEvents(t *testing.T) {
testdata.InsertKeywordTrigger(t, db, testdata.Org2, testdata.Org2Favorites, "start", models.MatchOnly, nil, nil)
testdata.InsertCatchallTrigger(t, db, testdata.Org2, testdata.Org2SingleMessage, nil, nil)

// give Cathy an open ticket
cathyTicketID := testdata.InsertOpenTicket(t, db, testdata.Org1, testdata.Cathy, testdata.Mailgun, "aa906c81-7766-427c-9ffd-9a86e49bd657", "Hi there", "Ok", "")

// give Bob a closed ticket
bobTicketID := testdata.InsertClosedTicket(t, db, testdata.Org1, testdata.Bob, testdata.Mailgun, "46faf0f3-5558-4865-bd8e-b3c83cdb5770", "Hi there", "Ok", "")

db.MustExec(`UPDATE tickets_ticket SET last_activity_on = '2021-01-01T00:00:00Z' WHERE id IN ($1, $2)`, cathyTicketID, bobTicketID)

// clear all of Alexandria's URNs
db.MustExec(`UPDATE contacts_contacturn SET contact_id = NULL WHERE contact_id = $1`, testdata.Alexandria.ID)

Expand Down Expand Up @@ -149,6 +157,10 @@ func TestMsgEvents(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 9, count)

// Cathy's open ticket will have been updated but not Bob's closed ticket
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM tickets_ticket WHERE id = $1 AND last_activity_on > '2021-01-01T00:00:00Z'`, []interface{}{cathyTicketID}, 1)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM tickets_ticket WHERE id = $1 AND last_activity_on = '2021-01-01T00:00:00Z'`, []interface{}{bobTicketID}, 1)

// Fred's sessions should not have a timeout because courier will set them
testsuite.AssertQueryCount(t, db,
`SELECT count(*) from flows_flowsession where contact_id = $1 and timeout_on IS NULL AND wait_started_on IS NOT NULL`,
Expand Down
26 changes: 23 additions & 3 deletions core/tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
if err != nil {
return errors.Wrapf(err, "error marking message as handled")
}

if len(tickets) > 0 {
err = models.UpdateTicketLastActivity(ctx, tx, tickets)
if err != nil {
return errors.Wrapf(err, "error updating last activity for open tickets")
}
}

return nil
}

Expand All @@ -613,9 +621,21 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
if flow != nil {
// if this is an IVR flow, we need to trigger that start (which happens in a different queue)
if flow.FlowType() == models.FlowTypeVoice {
err = runner.TriggerIVRFlow(ctx, rt, oa.OrgID(), flow.ID(), []models.ContactID{modelContact.ID()}, func(ctx context.Context, tx *sqlx.Tx) error {
return models.UpdateMessage(ctx, tx, event.MsgID, models.MsgStatusHandled, models.VisibilityVisible, models.TypeFlow, topupID)
})
ivrHook := func(ctx context.Context, tx *sqlx.Tx) error {
err := models.UpdateMessage(ctx, tx, event.MsgID, models.MsgStatusHandled, models.VisibilityVisible, models.TypeFlow, topupID)
if err != nil {
return errors.Wrapf(err, "error marking message as handled")
}

if len(tickets) > 0 {
err = models.UpdateTicketLastActivity(ctx, tx, tickets)
if err != nil {
return errors.Wrapf(err, "error updating last activity for open tickets")
}
}
return nil
}
err = runner.TriggerIVRFlow(ctx, rt, oa.OrgID(), flow.ID(), []models.ContactID{modelContact.ID()}, ivrHook)
if err != nil {
return errors.Wrapf(err, "error while triggering ivr flow")
}
Expand Down

0 comments on commit 439d8f4

Please sign in to comment.