diff --git a/backends/rapidpro/backend_test.go b/backends/rapidpro/backend_test.go index d18f99395..b55a44e43 100644 --- a/backends/rapidpro/backend_test.go +++ b/backends/rapidpro/backend_test.go @@ -2,6 +2,7 @@ package rapidpro import ( "context" + "database/sql" "encoding/base64" "encoding/json" "fmt" @@ -1526,3 +1527,26 @@ func readMsgFromDB(b *backend, id courier.MsgID) *DBMsg { m.channel = ch return m } + +const selectMsgIDForID = ` +SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')` + +const selectMsgIDForExternalID = ` +SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."external_id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')` + +func checkMsgExists(b *backend, status courier.MsgStatus) (err error) { + var id int64 + + if status.ID() != courier.NilMsgID { + err = b.db.QueryRow(selectMsgIDForID, status.ID(), status.ChannelUUID()).Scan(&id) + } else if status.ExternalID() != "" { + err = b.db.QueryRow(selectMsgIDForExternalID, status.ExternalID(), status.ChannelUUID()).Scan(&id) + } else { + return fmt.Errorf("no id or external id for status update") + } + + if err == sql.ErrNoRows { + return courier.ErrMsgNotFound + } + return err +} diff --git a/backends/rapidpro/status.go b/backends/rapidpro/status.go index c8478e7f3..0b4c5259a 100644 --- a/backends/rapidpro/status.go +++ b/backends/rapidpro/status.go @@ -2,7 +2,6 @@ package rapidpro import ( "context" - "database/sql" "encoding/json" "errors" "fmt" @@ -56,29 +55,6 @@ func writeMsgStatus(ctx context.Context, b *backend, status courier.MsgStatus) e return err } -const selectMsgIDForID = ` -SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')` - -const selectMsgIDForExternalID = ` -SELECT m."id" FROM "msgs_msg" m INNER JOIN "channels_channel" c ON (m."channel_id" = c."id") WHERE (m."external_id" = $1 AND c."uuid" = $2 AND m."direction" = 'O')` - -func checkMsgExists(b *backend, status courier.MsgStatus) (err error) { - var id int64 - - if status.ID() != courier.NilMsgID { - err = b.db.QueryRow(selectMsgIDForID, status.ID(), status.ChannelUUID()).Scan(&id) - } else if status.ExternalID() != "" { - err = b.db.QueryRow(selectMsgIDForExternalID, status.ExternalID(), status.ChannelUUID()).Scan(&id) - } else { - return fmt.Errorf("no id or external id for status update") - } - - if err == sql.ErrNoRows { - return courier.ErrMsgNotFound - } - return err -} - // the craziness below lets us update our status to 'F' and schedule retries without knowing anything about the message const sqlUpdateMsgByID = ` UPDATE msgs_msg SET