diff --git a/core/models/msgs.go b/core/models/msgs.go index f4d1fd706..b94e7f49a 100644 --- a/core/models/msgs.go +++ b/core/models/msgs.go @@ -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 ` @@ -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 diff --git a/core/models/msgs_test.go b/core/models/msgs_test.go index d01a1ddec..ab32a1396 100644 --- a/core/models/msgs_test.go +++ b/core/models/msgs_test.go @@ -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" @@ -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) @@ -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) @@ -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) {