Skip to content

Commit

Permalink
Actually save IVR messages with sent_on set
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jul 26, 2021
1 parent 1474dda commit 3dda6b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
5 changes: 1 addition & 4 deletions core/handlers/ivr_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func handleIVRCreated(ctx context.Context, tx *sqlx.Tx, rp *redis.Pool, oa *mode
return nil
}

msg, err := models.NewOutgoingIVR(oa.OrgID(), conn, event.Msg, event.CreatedOn())
if err != nil {
return errors.Wrapf(err, "error creating outgoing ivr say: %s", event.Msg.Text())
}
msg := models.NewOutgoingIVR(oa.OrgID(), conn, event.Msg, event.CreatedOn())

// register to have this message committed
scene.AppendToEventPreCommitHook(hooks.CommitIVRHook, msg)
Expand Down
16 changes: 8 additions & 8 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type Msg struct {
HighPriority bool `db:"high_priority" json:"high_priority"`
CreatedOn time.Time `db:"created_on" json:"created_on"`
ModifiedOn time.Time `db:"modified_on" json:"modified_on"`
SentOn time.Time `db:"sent_on" json:"sent_on"`
SentOn *time.Time `db:"sent_on" json:"sent_on"`
QueuedOn time.Time `db:"queued_on" json:"queued_on"`
Direction MsgDirection `db:"direction" json:"direction"`
Status MsgStatus `db:"status" json:"status"`
Expand Down Expand Up @@ -146,7 +146,7 @@ func (m *Msg) Text() string { return m.m.Text }
func (m *Msg) HighPriority() bool { return m.m.HighPriority }
func (m *Msg) CreatedOn() time.Time { return m.m.CreatedOn }
func (m *Msg) ModifiedOn() time.Time { return m.m.ModifiedOn }
func (m *Msg) SentOn() time.Time { return m.m.SentOn }
func (m *Msg) SentOn() *time.Time { return m.m.SentOn }
func (m *Msg) QueuedOn() time.Time { return m.m.QueuedOn }
func (m *Msg) Direction() MsgDirection { return m.m.Direction }
func (m *Msg) Status() MsgStatus { return m.m.Status }
Expand Down Expand Up @@ -250,7 +250,7 @@ func NewIncomingIVR(orgID OrgID, conn *ChannelConnection, in *flows.MsgIn, creat
}

// NewOutgoingIVR creates a new IVR message for the passed in text with the optional attachment
func NewOutgoingIVR(orgID OrgID, conn *ChannelConnection, out *flows.MsgOut, createdOn time.Time) (*Msg, error) {
func NewOutgoingIVR(orgID OrgID, conn *ChannelConnection, out *flows.MsgOut, createdOn time.Time) *Msg {
msg := &Msg{}
m := &msg.m

Expand All @@ -275,15 +275,15 @@ func NewOutgoingIVR(orgID OrgID, conn *ChannelConnection, out *flows.MsgOut, cre
m.OrgID = orgID
m.TopupID = NilTopupID
m.CreatedOn = createdOn
m.SentOn = createdOn
m.SentOn = &createdOn
msg.SetChannelID(conn.ChannelID())

// if we have attachments, add them
for _, a := range out.Attachments() {
m.Attachments = append(m.Attachments, string(NormalizeAttachment(config.Mailroom, a)))
}

return msg, nil
return msg
}

// NewOutgoingMsg creates an outgoing message for the passed in flow message.
Expand Down Expand Up @@ -482,10 +482,10 @@ func InsertMessages(ctx context.Context, tx Queryer, msgs []*Msg) error {

const insertMsgSQL = `
INSERT INTO
msgs_msg(uuid, text, high_priority, created_on, modified_on, queued_on, direction, status, attachments, metadata,
msgs_msg(uuid, text, high_priority, created_on, modified_on, queued_on, sent_on, direction, status, attachments, metadata,
visibility, msg_type, msg_count, error_count, next_attempt, channel_id, connection_id, response_to_id,
contact_id, contact_urn_id, org_id, topup_id, broadcast_id)
VALUES(:uuid, :text, :high_priority, :created_on, now(), now(), :direction, :status, :attachments, :metadata,
VALUES(:uuid, :text, :high_priority, :created_on, now(), now(), :sent_on, :direction, :status, :attachments, :metadata,
:visibility, :msg_type, :msg_count, :error_count, :next_attempt, :channel_id, :connection_id, :response_to_id,
:contact_id, :contact_urn_id, :org_id, :topup_id, :broadcast_id)
RETURNING
Expand Down Expand Up @@ -1078,7 +1078,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.Now()
msg.m.SentOn = dates.ZeroDateTime
msg.m.SentOn = nil
msg.m.ErrorCount = 0
msg.m.IsResend = true

Expand Down
27 changes: 27 additions & 0 deletions core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,30 @@ func TestNonPersistentBroadcasts(t *testing.T) {
// test ticket was updated
testsuite.AssertQuery(t, db, `SELECT count(*) FROM tickets_ticket WHERE id = $1 AND last_activity_on > $2`, ticket.ID, modelTicket.LastActivityOn()).Returns(1)
}

func TestNewOutgoingIVR(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

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

vonage := oa.ChannelByUUID(testdata.VonageChannel.UUID)
conn, err := models.InsertIVRConnection(ctx, db, testdata.Org1.ID, testdata.VonageChannel.ID, models.NilStartID, testdata.Cathy.ID, testdata.Cathy.URNID, models.ConnectionDirectionOut, models.ConnectionStatusInProgress, "")
require.NoError(t, err)

createdOn := time.Date(2021, 7, 26, 12, 6, 30, 0, time.UTC)

flowMsg := flows.NewMsgOut(testdata.Cathy.URN, vonage.ChannelReference(), "Hello", []utils.Attachment{"audio/mp3:http://example.com/hi.mp3"}, nil, nil, flows.NilMsgTopic)
dbMsg := models.NewOutgoingIVR(testdata.Org1.ID, conn, flowMsg, createdOn)

assert.Equal(t, flowMsg.UUID(), dbMsg.UUID())
assert.Equal(t, "Hello", dbMsg.Text())
assert.Equal(t, []utils.Attachment{"audio/mp3:http://example.com/hi.mp3"}, dbMsg.Attachments())
assert.Equal(t, createdOn, dbMsg.CreatedOn())
assert.Equal(t, &createdOn, dbMsg.SentOn())

err = models.InsertMessages(ctx, db, []*models.Msg{dbMsg})
require.NoError(t, err)

testsuite.AssertQuery(t, db, `SELECT text, created_on, sent_on FROM msgs_msg WHERE uuid = $1`, dbMsg.UUID()).Columns(map[string]interface{}{"text": "Hello", "created_on": createdOn, "sent_on": createdOn})
}

0 comments on commit 3dda6b7

Please sign in to comment.