Skip to content

Commit 86125a9

Browse files
authored
Merge pull request #668 from nyaruka/remove_topups
Remove topups
2 parents 7883a1b + 221fcbb commit 86125a9

12 files changed

+26
-352
lines changed

core/hooks/commit_ivr.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,8 @@ func (h *commitIVRHook) Apply(ctx context.Context, rt *runtime.Runtime, tx *sqlx
2424
}
2525
}
2626

27-
// find the topup we will assign
28-
topup, err := models.AllocateTopups(ctx, tx, rt.RP, oa.Org(), len(msgs))
29-
if err != nil {
30-
return errors.Wrapf(err, "error allocating topup for outgoing IVR message")
31-
}
32-
33-
// if we have an active topup, assign it to our messages
34-
if topup != models.NilTopupID {
35-
for _, m := range msgs {
36-
m.SetTopup(topup)
37-
}
38-
}
39-
4027
// insert all our messages
41-
err = models.InsertMessages(ctx, tx, msgs)
42-
if err != nil {
28+
if err := models.InsertMessages(ctx, tx, msgs); err != nil {
4329
return errors.Wrapf(err, "error writing messages")
4430
}
4531

core/hooks/commit_messages.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,8 @@ func (h *commitMessagesHook) Apply(ctx context.Context, rt *runtime.Runtime, tx
2424
}
2525
}
2626

27-
// allocate a topup for this message if org uses topups
28-
topup, err := models.AllocateTopups(ctx, tx, rt.RP, oa.Org(), len(msgs))
29-
if err != nil {
30-
return errors.Wrapf(err, "error allocating topup for outgoing message")
31-
}
32-
33-
// if we have an active topup, assign it to our messages
34-
if topup != models.NilTopupID {
35-
for _, m := range msgs {
36-
m.SetTopup(topup)
37-
}
38-
}
39-
4027
// insert all our messages
41-
err = models.InsertMessages(ctx, tx, msgs)
42-
if err != nil {
28+
if err := models.InsertMessages(ctx, tx, msgs); err != nil {
4329
return errors.Wrapf(err, "error writing messages")
4430
}
4531

core/ivr/ivr.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -580,17 +580,8 @@ func buildMsgResume(
580580
// create an incoming message
581581
msg := models.NewIncomingIVR(rt.Config, oa.OrgID(), call, msgIn, time.Now())
582582

583-
// allocate a topup for this message if org uses topups)
584-
topupID, err := models.AllocateTopups(ctx, rt.DB, rt.RP, oa.Org(), 1)
585-
if err != nil {
586-
return nil, nil, errors.Wrapf(err, "error allocating topup for incoming IVR message")
587-
}
588-
589-
msg.SetTopup(topupID)
590-
591583
// commit it
592-
err = models.InsertMessages(ctx, rt.DB, []*models.Msg{msg})
593-
if err != nil {
584+
if err := models.InsertMessages(ctx, rt.DB, []*models.Msg{msg}); err != nil {
594585
return nil, nil, errors.Wrapf(err, "error committing new message")
595586
}
596587

core/models/msgs.go

+8-39
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ type Msg struct {
141141
URN urns.URN `db:"urn_urn" json:"urn"`
142142
URNAuth null.String `db:"urn_auth" json:"urn_auth,omitempty"`
143143
OrgID OrgID `db:"org_id" json:"org_id"`
144-
TopupID TopupID `db:"topup_id" json:"-"`
145144
FlowID FlowID `db:"flow_id" json:"-"`
146145

147146
// extra data from handling added to the courier payload
@@ -184,14 +183,11 @@ func (m *Msg) ChannelUUID() assets.ChannelUUID { return m.m.ChannelUUID }
184183
func (m *Msg) URN() urns.URN { return m.m.URN }
185184
func (m *Msg) URNAuth() null.String { return m.m.URNAuth }
186185
func (m *Msg) OrgID() OrgID { return m.m.OrgID }
187-
func (m *Msg) TopupID() TopupID { return m.m.TopupID }
188186
func (m *Msg) FlowID() FlowID { return m.m.FlowID }
189187
func (m *Msg) ContactID() ContactID { return m.m.ContactID }
190188
func (m *Msg) ContactURNID() *URNID { return m.m.ContactURNID }
191189
func (m *Msg) IsResend() bool { return m.m.IsResend }
192190

193-
func (m *Msg) SetTopup(topupID TopupID) { m.m.TopupID = topupID }
194-
195191
func (m *Msg) SetChannel(channel *Channel) {
196192
m.channel = channel
197193
if channel != nil {
@@ -255,7 +251,6 @@ func NewIncomingIVR(cfg *runtime.Config, orgID OrgID, call *Call, in *flows.MsgI
255251
m.ChannelID = call.ChannelID()
256252

257253
m.OrgID = orgID
258-
m.TopupID = NilTopupID
259254
m.CreatedOn = createdOn
260255

261256
// add any attachments
@@ -288,7 +283,6 @@ func NewOutgoingIVR(cfg *runtime.Config, orgID OrgID, call *Call, out *flows.Msg
288283
m.URN = out.URN()
289284

290285
m.OrgID = orgID
291-
m.TopupID = NilTopupID
292286
m.CreatedOn = createdOn
293287
m.SentOn = &createdOn
294288

@@ -354,7 +348,6 @@ func newOutgoingMsg(rt *runtime.Runtime, org *Org, channel *Channel, contact *fl
354348
m.OrgID = org.ID()
355349
m.ContactID = ContactID(contact.ID())
356350
m.BroadcastID = broadcastID
357-
m.TopupID = NilTopupID
358351
m.Text = out.Text()
359352
m.HighPriority = false
360353
m.Direction = DirectionOut
@@ -451,7 +444,6 @@ func NewIncomingMsg(cfg *runtime.Config, orgID OrgID, channel *Channel, contactI
451444
m.MsgType = MsgTypeFlow
452445
m.ContactID = contactID
453446
m.OrgID = orgID
454-
m.TopupID = NilTopupID
455447
m.CreatedOn = createdOn
456448

457449
// add any attachments
@@ -483,8 +475,7 @@ SELECT
483475
channel_id,
484476
contact_id,
485477
contact_urn_id,
486-
org_id,
487-
topup_id
478+
org_id
488479
FROM
489480
msgs_msg
490481
WHERE
@@ -521,7 +512,6 @@ SELECT
521512
m.contact_id,
522513
m.contact_urn_id,
523514
m.org_id,
524-
m.topup_id,
525515
u.identity AS "urn_urn",
526516
u.auth AS "urn_auth"
527517
FROM
@@ -626,18 +616,18 @@ const insertMsgSQL = `
626616
INSERT INTO
627617
msgs_msg(uuid, text, high_priority, created_on, modified_on, queued_on, sent_on, direction, status, attachments, metadata,
628618
visibility, msg_type, msg_count, error_count, next_attempt, failed_reason, channel_id,
629-
contact_id, contact_urn_id, org_id, topup_id, flow_id, broadcast_id)
619+
contact_id, contact_urn_id, org_id, flow_id, broadcast_id)
630620
VALUES(:uuid, :text, :high_priority, :created_on, now(), now(), :sent_on, :direction, :status, :attachments, :metadata,
631621
:visibility, :msg_type, :msg_count, :error_count, :next_attempt, :failed_reason, :channel_id,
632-
:contact_id, :contact_urn_id, :org_id, :topup_id, :flow_id, :broadcast_id)
622+
:contact_id, :contact_urn_id, :org_id, :flow_id, :broadcast_id)
633623
RETURNING
634624
id as id,
635625
now() as modified_on,
636626
now() as queued_on
637627
`
638628

639629
// UpdateMessage updates a message after handling
640-
func UpdateMessage(ctx context.Context, tx Queryer, msgID flows.MsgID, status MsgStatus, visibility MsgVisibility, msgType MsgType, flow FlowID, topup TopupID, attachments []utils.Attachment, logUUIDs []ChannelLogUUID) error {
630+
func UpdateMessage(ctx context.Context, tx Queryer, msgID flows.MsgID, status MsgStatus, visibility MsgVisibility, msgType MsgType, flow FlowID, attachments []utils.Attachment, logUUIDs []ChannelLogUUID) error {
641631
_, err := tx.ExecContext(ctx,
642632
`UPDATE
643633
msgs_msg
@@ -646,12 +636,11 @@ func UpdateMessage(ctx context.Context, tx Queryer, msgID flows.MsgID, status Ms
646636
visibility = $3,
647637
msg_type = $4,
648638
flow_id = $5,
649-
topup_id = $6,
650-
attachments = $7,
651-
log_uuids = array_cat(log_uuids, $8)
639+
attachments = $6,
640+
log_uuids = array_cat(log_uuids, $7)
652641
WHERE
653642
id = $1`,
654-
msgID, status, visibility, msgType, flow, topup, pq.Array(attachments), pq.Array(logUUIDs))
643+
msgID, status, visibility, msgType, flow, pq.Array(attachments), pq.Array(logUUIDs))
655644

656645
if err != nil {
657646
return errors.Wrapf(err, "error updating msg: %d", msgID)
@@ -1121,19 +1110,6 @@ func (b *BroadcastBatch) CreateMessages(ctx context.Context, rt *runtime.Runtime
11211110
}
11221111
}
11231112

1124-
// allocate a topup for these message if org uses topups
1125-
topup, err := AllocateTopups(ctx, rt.DB, rt.RP, oa.Org(), len(msgs))
1126-
if err != nil {
1127-
return nil, errors.Wrapf(err, "error allocating topup for broadcast messages")
1128-
}
1129-
1130-
// if we have an active topup, assign it to our messages
1131-
if topup != NilTopupID {
1132-
for _, m := range msgs {
1133-
m.SetTopup(topup)
1134-
}
1135-
}
1136-
11371113
// insert them in a single request
11381114
err = InsertMessages(ctx, rt.DB, msgs)
11391115
if err != nil {
@@ -1184,14 +1160,13 @@ func (b *BroadcastBatch) updateTicket(ctx context.Context, db Queryer, oa *OrgAs
11841160
const sqlUpdateMsgForResending = `
11851161
UPDATE msgs_msg m
11861162
SET channel_id = r.channel_id::int,
1187-
topup_id = r.topup_id::int,
11881163
status = 'P',
11891164
error_count = 0,
11901165
failed_reason = NULL,
11911166
queued_on = r.queued_on::timestamp with time zone,
11921167
sent_on = NULL,
11931168
modified_on = NOW()
1194-
FROM (VALUES(:id, :channel_id, :topup_id, :queued_on)) AS r(id, channel_id, topup_id, queued_on)
1169+
FROM (VALUES(:id, :channel_id, :queued_on)) AS r(id, channel_id, queued_on)
11951170
WHERE m.id = r.id::bigint`
11961171

11971172
const sqlUpdateMsgResendFailed = `
@@ -1211,7 +1186,6 @@ func ResendMessages(ctx context.Context, db Queryer, rp *redis.Pool, oa *OrgAsse
12111186

12121187
for _, msg := range msgs {
12131188
var ch *flows.Channel
1214-
var err error
12151189
urnID := msg.ContactURNID()
12161190

12171191
if urnID != nil {
@@ -1243,11 +1217,6 @@ func ResendMessages(ctx context.Context, db Queryer, rp *redis.Pool, oa *OrgAsse
12431217
msg.m.FailedReason = ""
12441218
msg.m.IsResend = true // mark message as being a resend so it will be queued to courier as such
12451219

1246-
// allocate a new topup for this message if org uses topups
1247-
msg.m.TopupID, err = AllocateTopups(ctx, db, rp, oa.Org(), 1)
1248-
if err != nil {
1249-
return nil, errors.Wrapf(err, "error allocating topup for message resending")
1250-
}
12511220
resends = append(resends, msg.m)
12521221
resent = append(resent, msg)
12531222
} else {

core/models/msgs_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,11 @@ func TestResendMessages(t *testing.T) {
472472

473473
assert.Len(t, resent, 3) // only #1, #2 and #3 can be resent
474474

475-
// both messages should now have a channel, a topup and be marked for resending
475+
// both messages should now have a channel and be marked for resending
476476
assert.True(t, resent[0].IsResend())
477477
assert.Equal(t, testdata.TwilioChannel.ID, resent[0].ChannelID())
478-
assert.Equal(t, models.TopupID(1), resent[0].TopupID())
479478
assert.True(t, resent[1].IsResend())
480479
assert.Equal(t, testdata.VonageChannel.ID, resent[1].ChannelID()) // channel changed
481-
assert.Equal(t, models.TopupID(1), resent[1].TopupID())
482480
assert.True(t, resent[2].IsResend())
483481
assert.Equal(t, testdata.TwilioChannel.ID, resent[2].ChannelID()) // channel added
484482

core/models/orgs.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ const (
6868
// Org is mailroom's type for RapidPro orgs. It also implements the envs.Environment interface for GoFlow
6969
type Org struct {
7070
o struct {
71-
ID OrgID `json:"id"`
72-
Suspended bool `json:"is_suspended"`
73-
UsesTopups bool `json:"uses_topups"`
74-
Config null.Map `json:"config"`
71+
ID OrgID `json:"id"`
72+
Suspended bool `json:"is_suspended"`
73+
Config null.Map `json:"config"`
7574
}
7675
env envs.Environment
7776
}
@@ -82,9 +81,6 @@ func (o *Org) ID() OrgID { return o.o.ID }
8281
// Suspended returns whether the org has been suspended
8382
func (o *Org) Suspended() bool { return o.o.Suspended }
8483

85-
// UsesTopups returns whether the org uses topups
86-
func (o *Org) UsesTopups() bool { return o.o.UsesTopups }
87-
8884
// DateFormat returns the date format for this org
8985
func (o *Org) DateFormat() envs.DateFormat { return o.env.DateFormat() }
9086

@@ -252,7 +248,6 @@ const selectOrgByID = `
252248
SELECT ROW_TO_JSON(o) FROM (SELECT
253249
id,
254250
is_suspended,
255-
uses_topups,
256251
COALESCE(o.config::json,'{}'::json) AS config,
257252
(SELECT CASE date_format WHEN 'D' THEN 'DD-MM-YYYY' WHEN 'M' THEN 'MM-DD-YYYY' END) AS date_format,
258253
'tt:mm' AS time_format,

core/models/orgs_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func TestOrgs(t *testing.T) {
3636

3737
assert.Equal(t, models.OrgID(1), org.ID())
3838
assert.False(t, org.Suspended())
39-
assert.True(t, org.UsesTopups())
4039
assert.Equal(t, envs.DateFormatDayMonthYear, org.DateFormat())
4140
assert.Equal(t, envs.TimeFormatHourMinute, org.TimeFormat())
4241
assert.Equal(t, envs.RedactionPolicyNone, org.RedactionPolicy())

0 commit comments

Comments
 (0)