diff --git a/backends/rapidpro/backend_test.go b/backends/rapidpro/backend_test.go index f58bb3186..74de96d55 100644 --- a/backends/rapidpro/backend_test.go +++ b/backends/rapidpro/backend_test.go @@ -481,6 +481,7 @@ func (ts *BackendTestSuite) TestMsgStatus() { ts.Equal(null.String("ext0"), m.ExternalID_) ts.True(m.ModifiedOn_.After(now)) ts.True(m.SentOn_.After(now)) + ts.Equal(null.NullString, m.FailedReason_) sentOn := *m.SentOn_ @@ -584,6 +585,7 @@ func (ts *BackendTestSuite) TestMsgStatus() { ts.Equal(m.ErrorCount_, 1) ts.True(m.ModifiedOn_.After(now)) ts.True(m.NextAttempt_.After(now)) + ts.Equal(null.NullString, m.FailedReason_) // second go status = ts.b.NewMsgStatusForExternalID(channel, "ext1", courier.MsgErrored) @@ -594,6 +596,7 @@ func (ts *BackendTestSuite) TestMsgStatus() { m = readMsgFromDB(ts.b, courier.NewMsgID(10000)) ts.Equal(m.Status_, courier.MsgErrored) ts.Equal(m.ErrorCount_, 2) + ts.Equal(null.NullString, m.FailedReason_) // third go status = ts.b.NewMsgStatusForExternalID(channel, "ext1", courier.MsgErrored) @@ -604,6 +607,7 @@ func (ts *BackendTestSuite) TestMsgStatus() { m = readMsgFromDB(ts.b, courier.NewMsgID(10000)) ts.Equal(m.Status_, courier.MsgFailed) ts.Equal(m.ErrorCount_, 3) + ts.Equal(null.String("E"), m.FailedReason_) // update URN when the new doesn't exist tx, _ := ts.b.db.BeginTxx(ctx, nil) diff --git a/backends/rapidpro/msg.go b/backends/rapidpro/msg.go index 020071f84..a194a9363 100644 --- a/backends/rapidpro/msg.go +++ b/backends/rapidpro/msg.go @@ -175,6 +175,7 @@ SELECT attachments, msg_count, error_count, + failed_reason, high_priority, status, visibility, @@ -497,8 +498,9 @@ type DBMsg struct { ContactID_ ContactID `json:"contact_id" db:"contact_id"` ContactURNID_ ContactURNID `json:"contact_urn_id" db:"contact_urn_id"` - MessageCount_ int `json:"msg_count" db:"msg_count"` - ErrorCount_ int `json:"error_count" db:"error_count"` + MessageCount_ int `json:"msg_count" db:"msg_count"` + ErrorCount_ int `json:"error_count" db:"error_count"` + FailedReason_ null.String `json:"failed_reason" db:"failed_reason"` ChannelUUID_ courier.ChannelUUID `json:"channel_uuid"` ContactName_ string `json:"contact_name"` diff --git a/backends/rapidpro/schema.sql b/backends/rapidpro/schema.sql index 191169480..e21cbb7c0 100644 --- a/backends/rapidpro/schema.sql +++ b/backends/rapidpro/schema.sql @@ -72,6 +72,7 @@ CREATE TABLE msgs_msg ( msg_count integer NOT NULL, error_count integer NOT NULL, next_attempt timestamp with time zone NOT NULL, + failed_reason character varying(1), external_id character varying(255), attachments character varying(255)[], channel_id integer references channels_channel(id) on delete cascade, @@ -79,7 +80,8 @@ CREATE TABLE msgs_msg ( contact_urn_id integer NOT NULL references contacts_contacturn(id) on delete cascade, org_id integer NOT NULL references orgs_org(id) on delete cascade, metadata text, - topup_id integer + topup_id integer, + delete_from_counts boolean ); DROP TABLE IF EXISTS channels_channellog CASCADE; diff --git a/backends/rapidpro/status.go b/backends/rapidpro/status.go index 6a81d6335..a707bb7a4 100644 --- a/backends/rapidpro/status.go +++ b/backends/rapidpro/status.go @@ -107,6 +107,14 @@ UPDATE msgs_msg SET ELSE next_attempt END, + failed_reason = CASE + WHEN + error_count >= 2 + THEN + 'E' + ELSE + failed_reason + END, sent_on = CASE WHEN :status = 'W' @@ -164,6 +172,14 @@ UPDATE msgs_msg SET ELSE next_attempt END, + failed_reason = CASE + WHEN + error_count >= 2 + THEN + 'E' + ELSE + failed_reason + END, sent_on = CASE WHEN :status IN ('W', 'S', 'D')