Skip to content

Commit

Permalink
Merge pull request #61 from Ilhasoft/feature/ticketer-twilio-flex
Browse files Browse the repository at this point in the history
Feature/ticketer twilio flex
  • Loading branch information
rasoro authored Apr 4, 2022
2 parents 4b1398b + c40b752 commit 8f7d9f5
Show file tree
Hide file tree
Showing 17 changed files with 2,104 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/mailroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
_ "github.com/nyaruka/mailroom/services/tickets/intern"
_ "github.com/nyaruka/mailroom/services/tickets/mailgun"
_ "github.com/nyaruka/mailroom/services/tickets/rocketchat"
_ "github.com/nyaruka/mailroom/services/tickets/twilioflex"
_ "github.com/nyaruka/mailroom/services/tickets/zendesk"
_ "github.com/nyaruka/mailroom/web/contact"
_ "github.com/nyaruka/mailroom/web/docs"
Expand Down
53 changes: 53 additions & 0 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,59 @@ func LoadMessages(ctx context.Context, db Queryer, orgID OrgID, direction MsgDir
return msgs, nil
}

var selectContactMessagesSQL = `
SELECT
id,
broadcast_id,
uuid,
text,
created_on,
direction,
status,
visibility,
msg_count,
error_count,
next_attempt,
external_id,
attachments,
metadata,
channel_id,
connection_id,
contact_id,
contact_urn_id,
response_to_id,
org_id,
topup_id
FROM
msgs_msg
WHERE
contact_id = $1 AND
created_on >= $2
ORDER BY
id ASC`

// SelectContactMessages loads the given messages for the passed in contact, created after the passed in time
func SelectContactMessages(ctx context.Context, db Queryer, contactID int, after time.Time) ([]*Msg, error) {
rows, err := db.QueryxContext(ctx, selectContactMessagesSQL, contactID, after)
if err != nil {
return nil, errors.Wrapf(err, "error querying msgs for contact: %d", contactID)
}
defer rows.Close()

msgs := make([]*Msg, 0)
for rows.Next() {
msg := &Msg{}
err = rows.StructScan(&msg.m)
if err != nil {
return nil, errors.Wrapf(err, "error scanning msg row")
}

msgs = append(msgs, msg)
}

return msgs, nil
}

// NormalizeAttachment will turn any relative URL in the passed in attachment and normalize it to
// include the full host for attachment domains
func NormalizeAttachment(cfg *runtime.Config, attachment utils.Attachment) utils.Attachment {
Expand Down
16 changes: 16 additions & 0 deletions core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,19 @@ func TestNewOutgoingIVR(t *testing.T) {

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})
}

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

now := time.Now()
msgIn := testdata.InsertIncomingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "in 1", models.MsgStatusHandled)
msgOut := testdata.InsertOutgoingMsg(db, testdata.Org1, testdata.TwilioChannel, testdata.Cathy, "out 1", []utils.Attachment{"image/jpeg:hi.jpg"}, models.MsgStatusSent)

msgs, err := models.SelectContactMessages(ctx, db, int(testdata.Cathy.ID), now)

assert.NoError(t, err)
assert.Equal(t, 2, len(msgs))
assert.Equal(t, msgIn.Text(), msgs[0].Text())
assert.Equal(t, msgOut.Text(), msgs[1].Text())
assert.Equal(t, []utils.Attachment{"image/jpeg:hi.jpg"}, msgs[1].Attachments())
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
)

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/antlr/antlr4 v0.0.0-20200701161529-3d9351f61e0f // indirect
github.com/blevesearch/segment v0.9.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -43,6 +44,7 @@ require (
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/gofrs/uuid v3.3.0+incompatible // indirect
github.com/google/go-querystring v1.1.0
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -82,6 +84,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
Loading

0 comments on commit 8f7d9f5

Please sign in to comment.