Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: handle new broadcast_type field #190

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ type Broadcast struct {
OrgID OrgID `json:"org_id" db:"org_id"`
ParentID BroadcastID `json:"parent_id,omitempty" db:"parent_id"`
TicketID TicketID `json:"ticket_id,omitempty" db:"ticket_id"`
BroadcastType events.BroadcastType `json:"broadcast_type" db:"broadcast_type"`
}
}

Expand All @@ -1001,14 +1002,15 @@ func (b *Broadcast) BaseLanguage() envs.Language { ret
func (b *Broadcast) Translations() map[envs.Language]*BroadcastTranslation { return b.b.Translations }
func (b *Broadcast) TemplateState() TemplateState { return b.b.TemplateState }
func (b *Broadcast) TicketID() TicketID { return b.b.TicketID }
func (b *Broadcast) BroadcastType() events.BroadcastType { return b.b.BroadcastType }

func (b *Broadcast) MarshalJSON() ([]byte, error) { return json.Marshal(b.b) }
func (b *Broadcast) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &b.b) }

// NewBroadcast creates a new broadcast with the passed in parameters
func NewBroadcast(
orgID OrgID, id BroadcastID, translations map[envs.Language]*BroadcastTranslation,
state TemplateState, baseLanguage envs.Language, urns []urns.URN, contactIDs []ContactID, groupIDs []GroupID, ticketID TicketID) *Broadcast {
state TemplateState, baseLanguage envs.Language, urns []urns.URN, contactIDs []ContactID, groupIDs []GroupID, ticketID TicketID, broadcastType events.BroadcastType) *Broadcast {

bcast := &Broadcast{}
bcast.b.OrgID = orgID
Expand All @@ -1020,6 +1022,7 @@ func NewBroadcast(
bcast.b.ContactIDs = contactIDs
bcast.b.GroupIDs = groupIDs
bcast.b.TicketID = ticketID
bcast.b.BroadcastType = broadcastType

return bcast
}
Expand All @@ -1036,6 +1039,7 @@ func InsertChildBroadcast(ctx context.Context, db Queryer, parent *Broadcast) (*
parent.b.ContactIDs,
parent.b.GroupIDs,
parent.b.TicketID,
parent.b.BroadcastType,
)
// populate our parent id
child.b.ParentID = parent.ID()
Expand Down Expand Up @@ -1124,8 +1128,8 @@ type broadcastGroup struct {

const insertBroadcastSQL = `
INSERT INTO
msgs_broadcast( org_id, parent_id, ticket_id, created_on, modified_on, status, text, base_language, send_all)
VALUES(:org_id, :parent_id, :ticket_id, NOW() , NOW(), 'Q', :text, :base_language, FALSE)
msgs_broadcast( org_id, parent_id, ticket_id, created_on, modified_on, status, text, base_language, send_all, broadcast_type)
VALUES(:org_id, :parent_id, :ticket_id, NOW() , NOW(), 'Q', :text, :base_language, FALSE, :broadcast_type)
RETURNING
id
`
Expand Down Expand Up @@ -1175,7 +1179,7 @@ func NewBroadcastFromEvent(ctx context.Context, tx Queryer, org *OrgAssets, even
}
}

return NewBroadcast(org.OrgID(), NilBroadcastID, translations, TemplateStateEvaluated, event.BaseLanguage, event.URNs, contactIDs, groupIDs, NilTicketID), nil
return NewBroadcast(org.OrgID(), NilBroadcastID, translations, TemplateStateEvaluated, event.BaseLanguage, event.URNs, contactIDs, groupIDs, NilTicketID, event.BroadcastType), nil
}

func (b *Broadcast) CreateBatch(contactIDs []ContactID) *BroadcastBatch {
Expand Down
2 changes: 2 additions & 0 deletions core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/test"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
Expand Down Expand Up @@ -531,6 +532,7 @@ func TestNonPersistentBroadcasts(t *testing.T) {
[]models.ContactID{testdata.Alexandria.ID, testdata.Bob.ID, testdata.Cathy.ID},
[]models.GroupID{testdata.DoctorsGroup.ID},
ticket.ID,
events.BroadcastTypeDefault,
)

assert.Equal(t, models.NilBroadcastID, bcast.ID())
Expand Down
1 change: 1 addition & 0 deletions core/models/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ SELECT ROW_TO_JSON(s) FROM (SELECT
(SELECT JSON_OBJECT_AGG(ts.key, ts.value) FROM (SELECT key, JSON_BUILD_OBJECT('text', t.value) as value FROM each(b.text) t) ts) as translations,
'unevaluated' as template_state,
b.base_language as base_language,
b.broadcast_type as broadcast_type,
s.org_id as org_id,
(SELECT ARRAY_AGG(bc.contact_id) FROM (
SELECT
Expand Down
3 changes: 2 additions & 1 deletion core/models/schedules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
Expand All @@ -27,7 +28,7 @@ func TestGetExpired(t *testing.T) {
assert.NoError(t, err)

b1 := testdata.InsertBroadcast(db, testdata.Org1, "eng", map[envs.Language]string{"eng": "Test message", "fra": "Un Message"}, s1,
[]*testdata.Contact{testdata.Cathy, testdata.George}, []*testdata.Group{testdata.DoctorsGroup},
[]*testdata.Contact{testdata.Cathy, testdata.George}, []*testdata.Group{testdata.DoctorsGroup}, events.BroadcastTypeDefault,
)

// add a URN
Expand Down
45 changes: 25 additions & 20 deletions core/tasks/msgs/send_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,33 @@ func TestBroadcastEvents(t *testing.T) {
georgeOnly := []*flows.ContactReference{george}

tcs := []struct {
Translations map[envs.Language]*events.BroadcastTranslation
BaseLanguage envs.Language
Groups []*assets.GroupReference
Contacts []*flows.ContactReference
URNs []urns.URN
Queue string
BatchCount int
MsgCount int
MsgText string
Translations map[envs.Language]*events.BroadcastTranslation
BaseLanguage envs.Language
Groups []*assets.GroupReference
Contacts []*flows.ContactReference
URNs []urns.URN
Queue string
BatchCount int
MsgCount int
MsgText string
BroadcastType events.BroadcastType
}{
{basic, eng, doctorsOnly, nil, nil, queue.BatchQueue, 2, 121, "hello world"},
{basic, eng, doctorsOnly, georgeOnly, nil, queue.BatchQueue, 2, 121, "hello world"},
{basic, eng, nil, georgeOnly, nil, queue.HandlerQueue, 1, 0, "hello world"},
{basic, eng, doctorsOnly, cathyOnly, nil, queue.BatchQueue, 2, 121, "hello world"},
{basic, eng, nil, cathyOnly, nil, queue.HandlerQueue, 1, 1, "hello world"},
{basic, eng, nil, cathyOnly, []urns.URN{urns.URN("tel:+12065551212")}, queue.HandlerQueue, 1, 1, "hello world"},
{basic, eng, nil, cathyOnly, []urns.URN{urns.URN("tel:+250700000001")}, queue.HandlerQueue, 1, 2, "hello world"},
{basic, eng, nil, nil, []urns.URN{urns.URN("tel:+250700000001")}, queue.HandlerQueue, 1, 1, "hello world"},
{basic, eng, doctorsOnly, nil, nil, queue.BatchQueue, 2, 121, "hello world", events.BroadcastTypeDefault},
{basic, eng, doctorsOnly, georgeOnly, nil, queue.BatchQueue, 2, 121, "hello world", events.BroadcastTypeDefault},
{basic, eng, nil, georgeOnly, nil, queue.HandlerQueue, 1, 0, "hello world", events.BroadcastTypeDefault},
{basic, eng, doctorsOnly, cathyOnly, nil, queue.BatchQueue, 2, 121, "hello world", events.BroadcastTypeDefault},
{basic, eng, nil, cathyOnly, nil, queue.HandlerQueue, 1, 1, "hello world", events.BroadcastTypeDefault},
{basic, eng, nil, cathyOnly, []urns.URN{urns.URN("tel:+12065551212")}, queue.HandlerQueue, 1, 1, "hello world", events.BroadcastTypeDefault},
{basic, eng, nil, cathyOnly, []urns.URN{urns.URN("tel:+250700000001")}, queue.HandlerQueue, 1, 2, "hello world", events.BroadcastTypeDefault},
{basic, eng, nil, nil, []urns.URN{urns.URN("tel:+250700000001")}, queue.HandlerQueue, 1, 1, "hello world", events.BroadcastTypeDefault},
}

lastNow := time.Now()
time.Sleep(10 * time.Millisecond)

for i, tc := range tcs {
// handle our start task
event := events.NewBroadcastCreated(tc.Translations, tc.BaseLanguage, tc.Groups, tc.Contacts, tc.URNs)
event := events.NewBroadcastCreated(tc.Translations, tc.BaseLanguage, tc.Groups, tc.Contacts, tc.URNs, tc.BroadcastType)
bcast, err := models.NewBroadcastFromEvent(ctx, db, oa, event)
assert.NoError(t, err)

Expand Down Expand Up @@ -132,7 +133,7 @@ func TestBroadcastTask(t *testing.T) {
eng := envs.Language("eng")

// insert a broadcast so we can check it is being set to sent
legacyID := testdata.InsertBroadcast(db, testdata.Org1, "base", map[envs.Language]string{"base": "hi @(PROPER(contact.name)) legacy"}, models.NilScheduleID, nil, nil)
legacyID := testdata.InsertBroadcast(db, testdata.Org1, "base", map[envs.Language]string{"base": "hi @(PROPER(contact.name)) legacy"}, models.NilScheduleID, nil, nil, events.BroadcastTypeDefault)

ticket := testdata.InsertOpenTicket(db, testdata.Org1, testdata.Cathy, testdata.Mailgun, testdata.DefaultTopic, "", "", nil)
modelTicket := ticket.Load(db)
Expand Down Expand Up @@ -180,6 +181,7 @@ func TestBroadcastTask(t *testing.T) {
BatchCount int
MsgCount int
MsgText string
BroadcastType events.BroadcastType
}{
{
models.NilBroadcastID,
Expand All @@ -194,6 +196,7 @@ func TestBroadcastTask(t *testing.T) {
2,
121,
"hello world",
events.BroadcastTypeDefault,
},
{
legacyID,
Expand All @@ -208,6 +211,7 @@ func TestBroadcastTask(t *testing.T) {
1,
1,
"hi Cathy legacy URN: +12065551212 Gender: F",
events.BroadcastTypeDefault,
},
{
models.NilBroadcastID,
Expand All @@ -222,6 +226,7 @@ func TestBroadcastTask(t *testing.T) {
1,
1,
"hi Cathy from Nyaruka goflow URN: tel:+12065551212 Gender: F",
events.BroadcastTypeDefault,
},
}

Expand All @@ -230,7 +235,7 @@ func TestBroadcastTask(t *testing.T) {

for i, tc := range tcs {
// handle our start task
bcast := models.NewBroadcast(oa.OrgID(), tc.BroadcastID, tc.Translations, tc.TemplateState, tc.BaseLanguage, tc.URNs, tc.ContactIDs, tc.GroupIDs, tc.TicketID)
bcast := models.NewBroadcast(oa.OrgID(), tc.BroadcastID, tc.Translations, tc.TemplateState, tc.BaseLanguage, tc.URNs, tc.ContactIDs, tc.GroupIDs, tc.TicketID, tc.BroadcastType)
err = msgs.CreateBroadcastBatches(ctx, rt, bcast)
assert.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion core/tasks/msgs/send_wpp_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
_ "github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/core/models"
Expand All @@ -32,7 +33,7 @@ func TestWppBroadcastTask(t *testing.T) {
assert.NoError(t, err)

// insert a broadcast so we can check it is being set to sent
existingID := testdata.InsertBroadcast(db, testdata.Org1, "base", nil, models.NilScheduleID, nil, nil)
existingID := testdata.InsertBroadcast(db, testdata.Org1, "base", nil, models.NilScheduleID, nil, nil, events.BroadcastTypeDefault)

doctorsOnly := []models.GroupID{testdata.DoctorsGroup.ID}
cathyOnly := []models.ContactID{testdata.Cathy.ID}
Expand Down
3 changes: 2 additions & 1 deletion core/tasks/schedules/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/queue"
"github.com/nyaruka/mailroom/testsuite"
Expand All @@ -30,7 +31,7 @@ func TestCheckSchedules(t *testing.T) {
assert.NoError(t, err)

b1 := testdata.InsertBroadcast(db, testdata.Org1, "eng", map[envs.Language]string{"eng": "Test message", "fra": "Un Message"}, s1,
[]*testdata.Contact{testdata.Cathy, testdata.George}, []*testdata.Group{testdata.DoctorsGroup},
[]*testdata.Contact{testdata.Cathy, testdata.George}, []*testdata.Group{testdata.DoctorsGroup}, events.BroadcastTypeDefault,
)

// add a URN
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ go 1.17

replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni

replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v1.5.2
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v1.5.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4=
github.com/weni-ai/goflow v1.5.2 h1:icguYaPnYifapb0gA6++8P6yy9QrT2fz0pgyovmKpFs=
github.com/weni-ai/goflow v1.5.2/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v1.5.4 h1:8s4pwLcOv2mkceWLjcvQn52iOFvAvlssUVxVxezlzSY=
github.com/weni-ai/goflow v1.5.4/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
3 changes: 2 additions & 1 deletion services/tickets/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/msgio"
Expand Down Expand Up @@ -101,7 +102,7 @@ func SendReply(ctx context.Context, rt *runtime.Runtime, ticket *models.Ticket,
translations := map[envs.Language]*models.BroadcastTranslation{envs.Language("base"): base}

// we'll use a broadcast to send this message
bcast := models.NewBroadcast(oa.OrgID(), models.NilBroadcastID, translations, models.TemplateStateEvaluated, envs.Language("base"), nil, nil, nil, ticket.ID())
bcast := models.NewBroadcast(oa.OrgID(), models.NilBroadcastID, translations, models.TemplateStateEvaluated, envs.Language("base"), nil, nil, nil, ticket.ID(), events.BroadcastTypeDefault)
batch := bcast.CreateBatch([]models.ContactID{ticket.ContactID()})
msgs, err := models.CreateBroadcastMessages(ctx, rt, oa, batch)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions testsuite/testdata/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"

Expand Down Expand Up @@ -72,16 +73,16 @@ func insertOutgoingMsg(db *sqlx.DB, org *Org, channel *Channel, contact *Contact
return msg
}

func InsertBroadcast(db *sqlx.DB, org *Org, baseLanguage envs.Language, text map[envs.Language]string, schedID models.ScheduleID, contacts []*Contact, groups []*Group) models.BroadcastID {
func InsertBroadcast(db *sqlx.DB, org *Org, baseLanguage envs.Language, text map[envs.Language]string, schedID models.ScheduleID, contacts []*Contact, groups []*Group, broadcastType events.BroadcastType) models.BroadcastID {
textMap := make(map[string]sql.NullString, len(text))
for lang, t := range text {
textMap[string(lang)] = sql.NullString{String: t, Valid: true}
}

var id models.BroadcastID
must(db.Get(&id,
`INSERT INTO msgs_broadcast(org_id, base_language, text, schedule_id, status, send_all, created_on, modified_on, created_by_id, modified_by_id)
VALUES($1, $2, $3, $4, 'P', TRUE, NOW(), NOW(), 1, 1) RETURNING id`, org.ID, baseLanguage, hstore.Hstore{Map: textMap}, schedID,
`INSERT INTO msgs_broadcast(org_id, base_language, text, schedule_id, broadcast_type, status, send_all, created_on, modified_on, created_by_id, modified_by_id)
VALUES($1, $2, $3, $4, $5, 'P', TRUE, NOW(), NOW(), 1, 1) RETURNING id`, org.ID, baseLanguage, hstore.Hstore{Map: textMap}, schedID, broadcastType,
))

for _, contact := range contacts {
Expand Down
1 change: 1 addition & 0 deletions weni_dump.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
-- insert the SQL to be merged into the database using dump_merger.sh
-- this file should always be empty, and only be used locally to update the test database