Skip to content

Commit

Permalink
Merge pull request #181 from nyaruka/add_urn_tweak
Browse files Browse the repository at this point in the history
Don't add duplicate URNs to contacts
  • Loading branch information
rowanseymour authored Mar 13, 2018
2 parents 666ef45 + 323437e commit 4ed6557
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
6 changes: 4 additions & 2 deletions flows/actions/add_contact_urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ func (a *AddContactURNAction) Execute(run flows.FlowRun, step flows.Step, log fl
log.Add(events.NewErrorEvent(fmt.Errorf("unable to add URN '%s:%s': %s", a.Scheme, evaluatedPath, err.Error())))
return nil
}
urn = urn.Normalize("")

log.Add(events.NewURNAddedEvent(urn))
if !run.Contact().HasURN(urn) {
log.Add(events.NewURNAddedEvent(urn))
}

return nil
}
20 changes: 17 additions & 3 deletions flows/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ func (c *Contact) Name() string { return c.name }
func (c *Contact) URNs() URNList { return c.urns }

// AddURN adds a new URN to this contact
func (c *Contact) AddURN(urn urns.URN) {
// TODO normalize and check we're not adding duplicates
func (c *Contact) AddURN(urn urns.URN) bool {
if c.HasURN(urn) {
return false
}
c.urns = append(c.urns, &ContactURN{URN: urn.Normalize("")})
return true
}

// HasURN checks whether the contact has the given URN
func (c *Contact) HasURN(urn urns.URN) bool {
urn = urn.Normalize("")

c.urns = append(c.urns, &ContactURN{URN: urn})
for _, u := range c.urns {
if u.URN == urn {
return true
}
}
return false
}

// Groups returns the groups that this contact belongs to
Expand Down

0 comments on commit 4ed6557

Please sign in to comment.