From 0489f42a1e5c27266b5abb022a7283e23ba940c8 Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Fri, 22 Nov 2024 13:25:31 -0300 Subject: [PATCH] Fix: handle new broadcast_type field --- core/models/msgs.go | 12 ++++-- core/models/msgs_test.go | 2 + core/models/schedules.go | 1 + core/models/schedules_test.go | 3 +- core/tasks/msgs/send_broadcast_test.go | 45 ++++++++++++--------- core/tasks/msgs/send_wpp_broadcast_test.go | 3 +- core/tasks/schedules/cron_test.go | 3 +- go.mod | 2 +- go.sum | 4 +- mailroom_test.dump | Bin 1907630 -> 1907678 bytes services/tickets/utils.go | 3 +- testsuite/testdata/msgs.go | 7 ++-- weni_dump.sql | 1 + 13 files changed, 52 insertions(+), 34 deletions(-) diff --git a/core/models/msgs.go b/core/models/msgs.go index 503f7a5eb..bca0c8f39 100644 --- a/core/models/msgs.go +++ b/core/models/msgs.go @@ -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"` } } @@ -1001,6 +1002,7 @@ 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) } @@ -1008,7 +1010,7 @@ func (b *Broadcast) UnmarshalJSON(data []byte) error { return json.Unmarshal(dat // 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 @@ -1020,6 +1022,7 @@ func NewBroadcast( bcast.b.ContactIDs = contactIDs bcast.b.GroupIDs = groupIDs bcast.b.TicketID = ticketID + bcast.b.BroadcastType = broadcastType return bcast } @@ -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() @@ -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 ` @@ -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 { diff --git a/core/models/msgs_test.go b/core/models/msgs_test.go index 2afe66c11..7019eddae 100644 --- a/core/models/msgs_test.go +++ b/core/models/msgs_test.go @@ -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" @@ -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()) diff --git a/core/models/schedules.go b/core/models/schedules.go index b3dfe918a..6c7f563c8 100644 --- a/core/models/schedules.go +++ b/core/models/schedules.go @@ -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 diff --git a/core/models/schedules_test.go b/core/models/schedules_test.go index e01ecb0aa..3824d402d 100644 --- a/core/models/schedules_test.go +++ b/core/models/schedules_test.go @@ -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" @@ -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 diff --git a/core/tasks/msgs/send_broadcast_test.go b/core/tasks/msgs/send_broadcast_test.go index 36e42cf3a..1d8ab5ff0 100644 --- a/core/tasks/msgs/send_broadcast_test.go +++ b/core/tasks/msgs/send_broadcast_test.go @@ -56,24 +56,25 @@ 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() @@ -81,7 +82,7 @@ func TestBroadcastEvents(t *testing.T) { 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) @@ -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) @@ -180,6 +181,7 @@ func TestBroadcastTask(t *testing.T) { BatchCount int MsgCount int MsgText string + BroadcastType events.BroadcastType }{ { models.NilBroadcastID, @@ -194,6 +196,7 @@ func TestBroadcastTask(t *testing.T) { 2, 121, "hello world", + events.BroadcastTypeDefault, }, { legacyID, @@ -208,6 +211,7 @@ func TestBroadcastTask(t *testing.T) { 1, 1, "hi Cathy legacy URN: +12065551212 Gender: F", + events.BroadcastTypeDefault, }, { models.NilBroadcastID, @@ -222,6 +226,7 @@ func TestBroadcastTask(t *testing.T) { 1, 1, "hi Cathy from Nyaruka goflow URN: tel:+12065551212 Gender: F", + events.BroadcastTypeDefault, }, } @@ -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) diff --git a/core/tasks/msgs/send_wpp_broadcast_test.go b/core/tasks/msgs/send_wpp_broadcast_test.go index f7bb441c6..22a475db7 100644 --- a/core/tasks/msgs/send_wpp_broadcast_test.go +++ b/core/tasks/msgs/send_wpp_broadcast_test.go @@ -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" @@ -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} diff --git a/core/tasks/schedules/cron_test.go b/core/tasks/schedules/cron_test.go index 8be9d8855..1acf12933 100644 --- a/core/tasks/schedules/cron_test.go +++ b/core/tasks/schedules/cron_test.go @@ -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" @@ -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 diff --git a/go.mod b/go.mod index d2f41df6d..c7d7f1dbf 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 6c3ab06ef..46e2100fb 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/mailroom_test.dump b/mailroom_test.dump index 125d68f0862bd6ac04552fa157f138ea39ed230e..99d9a2579aef843ae7d5b169f47dfb9c07d4429f 100644 GIT binary patch delta 308 zcmZ3tz2x5Z62$;_7vBIzJ_Z&JMn*;kAg}}xN+5y{M2Kxv{3FO1GWjE$`1B2IOnj4_ zh1@64`^mWAQl5+aUhleVo4yD0%GayLAPaM F8~{7cZUO)R delta 266 zcmcb&y=2|?62$;_7vBIzJ_Z&JMn*;kAOI5rAeJzQ;M=J9N08BE@<%rD=@;0T_$E6G zxpOM``G+X@h5GnR?h?wLp3lk1KY7CE)X53L_ouIBW|W?6kij~Af&dfe^!)eiLem%6 zGipu#_e*Y)sL*7A-_p&BqV0;Jj6lq^T~UGrHSQLoG XfLI)eC4g8Gh^2s7dVAAtnHUEE)%9a) diff --git a/services/tickets/utils.go b/services/tickets/utils.go index 26b570e37..bb1970e1e 100644 --- a/services/tickets/utils.go +++ b/services/tickets/utils.go @@ -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" @@ -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 { diff --git a/testsuite/testdata/msgs.go b/testsuite/testdata/msgs.go index 1a3f1c8a0..bbb4a8f30 100644 --- a/testsuite/testdata/msgs.go +++ b/testsuite/testdata/msgs.go @@ -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" @@ -72,7 +73,7 @@ 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} @@ -80,8 +81,8 @@ func InsertBroadcast(db *sqlx.DB, org *Org, baseLanguage envs.Language, text map 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 { diff --git a/weni_dump.sql b/weni_dump.sql index 89f9c2d12..155a4ccf8 100644 --- a/weni_dump.sql +++ b/weni_dump.sql @@ -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 \ No newline at end of file