Skip to content

Commit

Permalink
Merge branch 'fix/urn-teams' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Oct 18, 2024
2 parents 7d0f75c + ff74bf7 commit 503cba2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.17.0
----------
* Feat: Email Channel Handler

1.16.0
----------
* Upload documents to telegram
Expand Down
50 changes: 50 additions & 0 deletions backends/rapidpro/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"strconv"
"strings"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -96,6 +97,47 @@ WHERE
c.is_active = TRUE
`

const lookupContactFromTeamsURNSQL = `
SELECT
c.org_id,
c.id,
c.uuid,
c.modified_on,
c.created_on,
c.name,
u.id as "urn_id",
c.status
FROM
contacts_contact AS c,
contacts_contacturn AS u
WHERE
u.identity ~ $1 AND
u.contact_id = c.id AND
u.org_id = $2 AND
c.is_active = TRUE
ORDER BY c.modified_on ASC
LIMIT 1;
`

func contactForURNTeams(ctx context.Context, b *backend, urn urns.URN, org OrgID) (*DBContact, error) {
contact := &DBContact{}

urnIdentity := strings.Split(urn.Identity().String(), ":serviceURL:")
err := b.db.GetContext(ctx, contact, lookupContactFromTeamsURNSQL, urnIdentity[0], org)
if err != nil && err != sql.ErrNoRows {
logrus.WithError(err).WithField("urn", urn.Identity()).WithField("org_id", org).Error("error looking up contact")
return nil, err
}

err = updateContactTeamsURN(b.db, contact.URNID_, urn.Identity().String())
if err != nil {
logrus.WithError(err).WithField("urn", urn.Identity()).WithField("org_id", org).Error("error updating contact urn")
return contact, err
}

return contact, nil
}

// contactForURN first tries to look up a contact for the passed in URN, if not finding one then creating one
func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChannel, urn urns.URN, auth string, name string) (*DBContact, error) {
// try to look up our contact by URN
Expand All @@ -106,6 +148,14 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne
return nil, err
}

if urn.Scheme() == "teams" && err == sql.ErrNoRows {
contact, err = contactForURNTeams(ctx, b, urn, org)
if err != nil {
logrus.WithError(err).WithField("urn", urn.Identity()).WithField("org_id", org).Error("error looking up contact")
return nil, err
}
}

// we found it, return it
if err != sql.ErrNoRows {
// insert it
Expand Down
20 changes: 20 additions & 0 deletions backends/rapidpro/urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ WHERE
id = :id
`

const updateTeamsURN = `
UPDATE
contacts_contacturn
SET
identity = $1
WHERE
id = $2;
`

// UpdateContactURN updates the Channel and Contact on an existing URN
func updateContactURN(db *sqlx.Tx, urn *DBContactURN) error {
rows, err := db.NamedQuery(updateURN, urn)
Expand Down Expand Up @@ -323,6 +333,16 @@ func fullyUpdateContactURN(db *sqlx.Tx, urn *DBContactURN) error {
return err
}

func updateContactTeamsURN(db *sqlx.DB, urnID ContactURNID, newURN string) error {
_, err := db.Exec(updateTeamsURN, newURN, urnID)
if err != nil {
logrus.WithError(err).WithField("urn_id", urnID).Error("error updating contact urn")
return err
}

return err
}

// DBContactURN is our struct to map to database level URNs
type DBContactURN struct {
OrgID OrgID `db:"org_id"`
Expand Down

0 comments on commit 503cba2

Please sign in to comment.