From f709be9c5d85d885f2fb86f39e44a448007c888d Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 6 Jun 2024 14:53:10 -0500 Subject: [PATCH] Change format of URNs endpoint so it returns normalized form --- web/contact/testdata/urns.json | 33 +++++++++++++++++++++++------ web/contact/urns.go | 38 +++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/web/contact/testdata/urns.json b/web/contact/testdata/urns.json index 6abe572ae..a599c798a 100644 --- a/web/contact/testdata/urns.json +++ b/web/contact/testdata/urns.json @@ -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" + } + ] } } ] \ No newline at end of file diff --git a/web/contact/urns.go b/web/contact/urns.go index c488a5910..d618f064c 100644 --- a/web/contact/urns.go +++ b/web/contact/urns.go @@ -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 }