Skip to content

Commit

Permalink
Set queued_on to now() when resending messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 6, 2021
1 parent ecb4f52 commit a74599e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,13 +1014,13 @@ const updateMsgForResendingSQL = `
topup_id = r.topup_id::int,
status = 'P',
error_count = 0,
queued_on = NULL,
queued_on = r.queued_on::timestamp with time zone,
sent_on = NULL,
modified_on = NOW()
FROM (
VALUES(:id, :channel_id, :topup_id)
VALUES(:id, :channel_id, :topup_id, :queued_on)
) AS
r(id, channel_id, topup_id)
r(id, channel_id, topup_id, queued_on)
WHERE
m.id = r.id::bigint
`
Expand Down Expand Up @@ -1056,7 +1056,7 @@ func ResendMessages(ctx context.Context, db Queryer, rp *redis.Pool, oa *OrgAsse

// mark message as being a resend so it will be queued to courier as such
msg.m.Status = MsgStatusPending
msg.m.QueuedOn = dates.ZeroDateTime
msg.m.QueuedOn = dates.Now()
msg.m.SentOn = dates.ZeroDateTime
msg.m.ErrorCount = 0
msg.m.IsResend = true
Expand Down
7 changes: 6 additions & 1 deletion core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
Expand Down Expand Up @@ -180,6 +181,8 @@ func TestResendMessages(t *testing.T) {
db := testsuite.DB()
rp := testsuite.RP()

db.MustExec(`DELETE FROM msgs_msg`)

oa, err := models.GetOrgAssets(ctx, db, models.Org1)
require.NoError(t, err)

Expand All @@ -196,6 +199,8 @@ func TestResendMessages(t *testing.T) {
msgs, err := models.LoadMessages(ctx, db, models.Org1, models.DirectionOut, []models.MsgID{models.MsgID(msgOut1.ID()), models.MsgID(msgOut2.ID())})
require.NoError(t, err)

now := dates.Now()

// resend both msgs
err = models.ResendMessages(ctx, db, rp, oa, msgs)
require.NoError(t, err)
Expand All @@ -208,7 +213,7 @@ func TestResendMessages(t *testing.T) {
assert.Equal(t, models.VonageChannelID, msgs[1].ChannelID())
assert.Equal(t, models.TopupID(1), msgs[1].TopupID())

testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'P' AND sent_on IS NULL`, nil, 2)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM msgs_msg WHERE status = 'P' AND queued_on > $1 AND sent_on IS NULL`, []interface{}{now}, 2)
}

func TestNormalizeAttachment(t *testing.T) {
Expand Down

0 comments on commit a74599e

Please sign in to comment.