Skip to content

Commit

Permalink
Merge pull request #1301 from nyaruka/urn_limit
Browse files Browse the repository at this point in the history
Enforce a limit of 50 URNs per contact
  • Loading branch information
rowanseymour authored Jan 8, 2025
2 parents 6574946 + 4a91e28 commit 2331736
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 2 deletions.
3 changes: 3 additions & 0 deletions flows/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const (

// ContactStatusArchived is the contact status of archived
ContactStatusArchived ContactStatus = "archived"

// MaxContactURNs is maximum number of URNs a contact can have
MaxContactURNs = 50
)

// Contact represents a person who is interacting with the flow
Expand Down
189 changes: 189 additions & 0 deletions flows/modifiers/testdata/urns.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,194 @@
"text": "'xyz:12345' is not valid URN"
}
]
},
{
"description": "error event if too many URNs",
"contact_before": {
"uuid": "5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f",
"name": "Bob",
"status": "active",
"urns": [
"tel:+17010000000"
],
"created_on": "2018-06-20T11:40:30.123456789Z"
},
"modifier": {
"type": "urns",
"urns": [
"tel:+17020000000",
"tel:+17020000001",
"tel:+17020000002",
"tel:+17020000003",
"tel:+17020000004",
"tel:+17020000005",
"tel:+17020000006",
"tel:+17020000007",
"tel:+17020000008",
"tel:+17020000009",
"tel:+17020000010",
"tel:+17020000011",
"tel:+17020000012",
"tel:+17020000013",
"tel:+17020000014",
"tel:+17020000015",
"tel:+17020000016",
"tel:+17020000017",
"tel:+17020000018",
"tel:+17020000019",
"tel:+17020000020",
"tel:+17020000021",
"tel:+17020000022",
"tel:+17020000023",
"tel:+17020000024",
"tel:+17020000025",
"tel:+17020000026",
"tel:+17020000027",
"tel:+17020000028",
"tel:+17020000029",
"tel:+17020000030",
"tel:+17020000031",
"tel:+17020000032",
"tel:+17020000033",
"tel:+17020000034",
"tel:+17020000035",
"tel:+17020000036",
"tel:+17020000037",
"tel:+17020000038",
"tel:+17020000039",
"tel:+17020000040",
"tel:+17020000041",
"tel:+17020000042",
"tel:+17020000043",
"tel:+17020000044",
"tel:+17020000045",
"tel:+17020000046",
"tel:+17020000047",
"tel:+17020000048",
"tel:+17020000049"
],
"modification": "append"
},
"contact_after": {
"uuid": "5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f",
"name": "Bob",
"status": "active",
"created_on": "2018-06-20T11:40:30.123456789Z",
"urns": [
"tel:+17010000000",
"tel:+17020000000",
"tel:+17020000001",
"tel:+17020000002",
"tel:+17020000003",
"tel:+17020000004",
"tel:+17020000005",
"tel:+17020000006",
"tel:+17020000007",
"tel:+17020000008",
"tel:+17020000009",
"tel:+17020000010",
"tel:+17020000011",
"tel:+17020000012",
"tel:+17020000013",
"tel:+17020000014",
"tel:+17020000015",
"tel:+17020000016",
"tel:+17020000017",
"tel:+17020000018",
"tel:+17020000019",
"tel:+17020000020",
"tel:+17020000021",
"tel:+17020000022",
"tel:+17020000023",
"tel:+17020000024",
"tel:+17020000025",
"tel:+17020000026",
"tel:+17020000027",
"tel:+17020000028",
"tel:+17020000029",
"tel:+17020000030",
"tel:+17020000031",
"tel:+17020000032",
"tel:+17020000033",
"tel:+17020000034",
"tel:+17020000035",
"tel:+17020000036",
"tel:+17020000037",
"tel:+17020000038",
"tel:+17020000039",
"tel:+17020000040",
"tel:+17020000041",
"tel:+17020000042",
"tel:+17020000043",
"tel:+17020000044",
"tel:+17020000045",
"tel:+17020000046",
"tel:+17020000047",
"tel:+17020000048"
]
},
"events": [
{
"type": "error",
"created_on": "2018-10-18T14:20:30.000123456Z",
"text": "contact has too many URNs, limit is 50"
},
{
"type": "contact_urns_changed",
"created_on": "2018-10-18T14:20:30.000123456Z",
"urns": [
"tel:+17010000000",
"tel:+17020000000",
"tel:+17020000001",
"tel:+17020000002",
"tel:+17020000003",
"tel:+17020000004",
"tel:+17020000005",
"tel:+17020000006",
"tel:+17020000007",
"tel:+17020000008",
"tel:+17020000009",
"tel:+17020000010",
"tel:+17020000011",
"tel:+17020000012",
"tel:+17020000013",
"tel:+17020000014",
"tel:+17020000015",
"tel:+17020000016",
"tel:+17020000017",
"tel:+17020000018",
"tel:+17020000019",
"tel:+17020000020",
"tel:+17020000021",
"tel:+17020000022",
"tel:+17020000023",
"tel:+17020000024",
"tel:+17020000025",
"tel:+17020000026",
"tel:+17020000027",
"tel:+17020000028",
"tel:+17020000029",
"tel:+17020000030",
"tel:+17020000031",
"tel:+17020000032",
"tel:+17020000033",
"tel:+17020000034",
"tel:+17020000035",
"tel:+17020000036",
"tel:+17020000037",
"tel:+17020000038",
"tel:+17020000039",
"tel:+17020000040",
"tel:+17020000041",
"tel:+17020000042",
"tel:+17020000043",
"tel:+17020000044",
"tel:+17020000045",
"tel:+17020000046",
"tel:+17020000047",
"tel:+17020000048"
]
}
]
}
]
11 changes: 9 additions & 2 deletions flows/modifiers/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ func (m *URNsModifier) Apply(eng flows.Engine, env envs.Environment, sa flows.Se
log(events.NewErrorf("'%s' is not valid URN", urn))
} else {
if m.Modification == URNsAppend || m.Modification == URNsSet {
modified = contact.AddURN(urn, nil)
if len(contact.URNs()) >= flows.MaxContactURNs {
log(events.NewErrorf("contact has too many URNs, limit is %d", flows.MaxContactURNs))
break
} else if contact.AddURN(urn, nil) {
modified = true
}
} else {
modified = contact.RemoveURN(urn)
if contact.RemoveURN(urn) {
modified = true
}
}
}
}
Expand Down

0 comments on commit 2331736

Please sign in to comment.