Skip to content

Commit

Permalink
Merge pull request rapidpro#260 from nyaruka/urns_endpoint_tweak
Browse files Browse the repository at this point in the history
Change format of URNs endpoint so it returns normalized form
  • Loading branch information
rowanseymour authored Jun 6, 2024
2 parents 92510a2 + f709be9 commit 742c9f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
33 changes: 27 additions & 6 deletions web/contact/testdata/urns.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,37 @@
"tel:+1 (605) 574 2222",
"tel:+1-6055747777",
"tel:[[[",
"xyz:2345"
"xyz:2345",
"abc"
]
},
"status": 200,
"response": {
"tel:+16055741111": 10000,
"tel:+1 (605) 574 2222": 10001,
"tel:+1-6055747777": null,
"tel:[[[": "invalid path component",
"xyz:2345": "unknown URN scheme"
"urns": [
{
"normalized": "tel:+16055741111",
"contact_id": 10000
},
{
"normalized": "tel:+16055742222",
"contact_id": 10001
},
{
"normalized": "tel:+16055747777"
},
{
"normalized": "tel:[[[",
"error": "invalid path component"
},
{
"normalized": "xyz:2345",
"error": "unknown URN scheme"
},
{
"normalized": ":abc",
"error": "scheme or path cannot be empty"
}
]
}
}
]
38 changes: 24 additions & 14 deletions web/contact/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,52 @@ func init() {
//
// {
// "org_id": 1,
// "urns": ["tel:+593979123456", "webchat:123456", "line:1234567890"]
// "urns": ["tel:+593 979 123456", "webchat:123456", "line:1234567890"]
// }
//
// {
// "tel:+593979123456": 35657,
// "webchat:123456": "invalid path component"
// "line:1234567890": null
// "urns": [
// {"normalized": "tel:+593979123456", "contact_id": 35657},
// {"normalized": "webchat:123456", "error": "invalid path component"}
// {"normalized": "line:1234567890"}
// ]
// }
type urnsRequest struct {
OrgID models.OrgID `json:"org_id" validate:"required"`
URNs []urns.URN `json:"urns" validate:"required"`
}

type urnResult struct {
Normalized urns.URN `json:"normalized"`
ContactID models.ContactID `json:"contact_id,omitempty"`
Error string `json:"error,omitempty"`
}

// handles a request to create the given contact
func handleURNs(ctx context.Context, rt *runtime.Runtime, r *urnsRequest) (any, int, error) {
urnsToLookup := make(map[urns.URN]urns.URN, len(r.URNs)) // normalized to original form of valid URNs
result := make(map[urns.URN]any, len(r.URNs))
urnsToLookup := make(map[urns.URN]int, len(r.URNs)) // normalized to index of valid URNs
results := make([]urnResult, len(r.URNs))

for _, urn := range r.URNs {
for i, urn := range r.URNs {
norm := urn.Normalize()

results[i].Normalized = norm

if err := norm.Validate(); err != nil {
result[urn] = err.Error()
results[i].Error = err.Error()
} else {
urnsToLookup[norm] = urn
urnsToLookup[norm] = i
}
}

owners, err := models.GetContactIDsFromURNs(ctx, rt.DB, r.OrgID, maps.Keys(urnsToLookup))
ownerIDs, err := models.GetContactIDsFromURNs(ctx, rt.DB, r.OrgID, maps.Keys(urnsToLookup))
if err != nil {
return nil, 0, fmt.Errorf("error getting URN owners: %w", err)
}

for nurn, owner := range owners {
orig := urnsToLookup[nurn]
result[orig] = owner
for nurn, ownerID := range ownerIDs {
results[urnsToLookup[nurn]].ContactID = ownerID
}

return result, http.StatusOK, nil
return map[string]any{"urns": results}, http.StatusOK, nil
}

0 comments on commit 742c9f3

Please sign in to comment.