Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/weni-ai/mailroom into feat/…
Browse files Browse the repository at this point in the history
…wpp-buttons
  • Loading branch information
paulobernardoaf committed Nov 27, 2024
2 parents b7c56eb + d970766 commit 1a4bad9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 11 additions & 1 deletion core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ type WppBroadcast struct {
OrgID OrgID `json:"org_id" db:"org_id"`
ParentID BroadcastID `json:"parent_id,omitempty" db:"parent_id"`
Msg WppBroadcastMessage `json:"msg"`
ChannelID ChannelID `json:"channel_id,omitempty"`
}
}

Expand All @@ -1483,18 +1484,20 @@ func (b *WppBroadcast) ContactIDs() []ContactID { return b.b.ContactIDs }
func (b *WppBroadcast) GroupIDs() []GroupID { return b.b.GroupIDs }
func (b *WppBroadcast) URNs() []urns.URN { return b.b.URNs }
func (b *WppBroadcast) Msg() WppBroadcastMessage { return b.b.Msg }
func (b *WppBroadcast) ChannelID() ChannelID { return b.b.ChannelID }

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

func NewWppBroadcast(orgID OrgID, id BroadcastID, msg WppBroadcastMessage, urns []urns.URN, contactIDs []ContactID, groupIDs []GroupID) *WppBroadcast {
func NewWppBroadcast(orgID OrgID, id BroadcastID, msg WppBroadcastMessage, urns []urns.URN, contactIDs []ContactID, groupIDs []GroupID, channelID ChannelID) *WppBroadcast {
bcast := &WppBroadcast{}
bcast.b.OrgID = orgID
bcast.b.BroadcastID = id
bcast.b.Msg = msg
bcast.b.URNs = urns
bcast.b.ContactIDs = contactIDs
bcast.b.GroupIDs = groupIDs
bcast.b.ChannelID = channelID

return bcast
}
Expand All @@ -1504,6 +1507,7 @@ func (b *WppBroadcast) CreateBatch(contactIDs []ContactID) *WppBroadcastBatch {
batch.b.BroadcastID = b.b.BroadcastID
batch.b.Msg = b.b.Msg
batch.b.OrgID = b.b.OrgID
batch.b.ChannelID = b.b.ChannelID
batch.b.ContactIDs = contactIDs
return batch
}
Expand All @@ -1516,6 +1520,7 @@ type WppBroadcastBatch struct {
ContactIDs []ContactID `json:"contact_ids,omitempty"`
IsLast bool `json:"is_last"`
OrgID OrgID `json:"org_id"`
ChannelID ChannelID `json:"channel_id,omitempty"`
}
}

Expand All @@ -1525,6 +1530,7 @@ func (b *WppBroadcastBatch) URNs() map[ContactID]urns.URN { return b.b.UR
func (b *WppBroadcastBatch) SetURNs(urns map[ContactID]urns.URN) { b.b.URNs = urns }
func (b *WppBroadcastBatch) OrgID() OrgID { return b.b.OrgID }
func (b *WppBroadcastBatch) Msg() WppBroadcastMessage { return b.b.Msg }
func (b *WppBroadcastBatch) ChannelID() ChannelID { return b.b.ChannelID }

func (b *WppBroadcastBatch) IsLast() bool { return b.b.IsLast }
func (b *WppBroadcastBatch) SetIsLast(last bool) { b.b.IsLast = last }
Expand Down Expand Up @@ -1606,6 +1612,10 @@ func CreateWppBroadcastMessages(ctx context.Context, rt *runtime.Runtime, oa *Or
}
}

if bcast.ChannelID() != NilChannelID {
channel = oa.ChannelByID(bcast.ChannelID())
}

// no urn and channel? move on
if channel == nil {
return nil, nil
Expand Down
32 changes: 30 additions & 2 deletions core/tasks/msgs/send_wpp_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestWppBroadcastTask(t *testing.T) {
// add an extra URN for cathy
testdata.InsertContactURN(db, testdata.Org1, testdata.Cathy, urns.URN("tel:+12065551212"), 1001)

// change alexandrias URN to a twitter URN and set her language to eng so that a template gets used for her
// change alexandrias URN to a whatsapp URN and set her language to eng so that a template gets used for her
db.MustExec(`UPDATE contacts_contacturn SET identity = 'whatsapp:559899999999', path='559899999999', scheme='whatsapp' WHERE contact_id = $1`, testdata.Alexandria.ID)
db.MustExec(`UPDATE contacts_contact SET language='eng' WHERE id = $1`, testdata.Alexandria.ID)

Expand Down Expand Up @@ -164,6 +164,7 @@ func TestWppBroadcastTask(t *testing.T) {
MsgCount int
Msg models.WppBroadcastMessage
MsgText string
ChannelID models.ChannelID
}{
{
models.NilBroadcastID,
Expand All @@ -175,6 +176,7 @@ func TestWppBroadcastTask(t *testing.T) {
121,
baseMsg,
"hello world",
models.NilChannelID,
},
{
existingID,
Expand All @@ -186,6 +188,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
evaluationMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -197,6 +200,19 @@ func TestWppBroadcastTask(t *testing.T) {
1,
evaluationMsg,
"hello Cathy",
testdata.WhatsAppCloudChannel.ID,
},
{
models.NilBroadcastID,
nil,
cathyOnly,
nil,
queue.HandlerQueue,
1,
1,
evaluationMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -208,6 +224,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
replyMsg,
"hello Cathy, how are you doing today?",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -219,6 +236,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
listMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -230,6 +248,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
ctaMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -241,6 +260,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
flowMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -252,6 +272,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
orderDetailsMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -265,6 +286,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
templateMsg,
"Welcome Alexandia!",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -284,7 +306,7 @@ func TestWppBroadcastTask(t *testing.T) {

for i, tc := range tcs {
// handle our start task
bcast := models.NewWppBroadcast(oa.OrgID(), tc.BroadcastID, tc.Msg, tc.URNs, tc.ContactIDs, tc.GroupIDs)
bcast := models.NewWppBroadcast(oa.OrgID(), tc.BroadcastID, tc.Msg, tc.URNs, tc.ContactIDs, tc.GroupIDs, tc.ChannelID)
err = msgs.CreateWppBroadcastBatches(ctx, rt, bcast)
assert.NoError(t, err)

Expand Down Expand Up @@ -378,6 +400,12 @@ func TestWppBroadcastTask(t *testing.T) {
).Returns(1, "%d: unexpected template count", i)
}

// assert our channel is being set
if tc.ChannelID != models.NilChannelID {
testsuite.AssertQuery(t, db, `SELECT count(*) FROM msgs_msg WHERE org_id = 1 AND created_on > $1 AND text = $2 AND channel_id = $3`, lastNow, tc.MsgText, tc.ChannelID).
Returns(1, "%d: unexpected channel count", i)
}

// make sure our broadcast is marked as sent
if tc.BroadcastID != models.NilBroadcastID {
testsuite.AssertQuery(t, db, `SELECT count(*) FROM msgs_broadcast WHERE id = $1 AND status = 'S'`, tc.BroadcastID).
Expand Down

0 comments on commit 1a4bad9

Please sign in to comment.