Skip to content

Commit

Permalink
Merge branch 'feature/fix-assert-in-broadcast-endpoint' of https://gi…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslinhares committed Jan 6, 2025
2 parents 3f1730d + d7d9d6f commit 93830e8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
4 changes: 4 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.20.1
----------
* Update weni-rp-apps version to 2.8.14

3.20.0
----------
* Endpoint to assignee user in ticket
Expand Down
56 changes: 33 additions & 23 deletions temba/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,34 +312,44 @@ def validate(self, data):

template_data = data.get("msg", {}).get("template", None)
if template_data is not None:
uuid = template_data.get("uuid", None)
name = template_data.get("name", None)
self._validate_and_assign_template(data, template_data, channel_data)

if not uuid and not name:
raise serializers.ValidationError("Template UUID or Name are required.")
if channel_data and "template" in data.get("msg", {}):
template = data["msg"]["template"]
if not channel.template_translations.filter(template__uuid=template["uuid"]).exists():
raise serializers.ValidationError(f"Template {template['uuid']} not found in channel {channel.uuid}")

if name and not channel_data:
raise serializers.ValidationError("Channel is required to use template name")
return data

try:
if uuid:
template = Template.objects.get(uuid=uuid)
elif name:
template = Template.objects.get(name=name, org=self.context["org"])

data["msg"]["template"] = {
"name": template.name,
"uuid": str(template.uuid),
"variables": data.get("msg", {}).get("template", {}).get("variables", []),
}
except Template.DoesNotExist:
raise serializers.ValidationError(f"Template with UUID {uuid} not found.")
def _validate_and_assign_template(self, data, template_data, channel_data):
uuid = template_data.get("uuid", None)
name = template_data.get("name", None)

if channel_data and template_data:
if not channel.template_translations.filter(template=template).exists():
raise serializers.ValidationError(f"Template {template.uuid} not found in channel {channel.uuid}")
if not uuid and not name:
raise serializers.ValidationError("Template UUID or Name are required.")

return data
if name and not channel_data:
raise serializers.ValidationError("Channel is required to use template name")

try:
template = self._get_template(uuid=uuid, name=name, org=self.context.get("org"))

data["msg"]["template"] = {
"name": template.name,
"uuid": str(template.uuid),
"variables": template_data.get("variables", []),
}

except Template.DoesNotExist:
if uuid:
raise serializers.ValidationError(f"Template with UUID {uuid} not found.")
raise serializers.ValidationError(f"Template with name {name} not found.")

def _get_template(self, uuid=None, name=None, org=None):
if uuid:
return Template.objects.get(uuid=uuid)
if name:
return Template.objects.get(name=name, org=org)

def save(self):
"""
Expand Down
28 changes: 28 additions & 0 deletions temba/api/v2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,34 @@ def test_whatsapp_broadcasts(self, mock_queue_broadcast):
self.assertEqual(expected_metadata, broadcast.metadata)
self.assertEqual(channel, broadcast.channel)

# Trying to get a not existing UUID
response = self.postJSON(
url,
None,
{
"urns": ["whatsapp:5561912345678"],
"contacts": [self.joe.uuid],
"msg": {"template": {"uuid": "1bb5d3de-6ddf-437b-9009-a04201bef143"}},
"channel": channel.uuid,
},
)
self.assertResponseError(
response, "non_field_errors", "Template with UUID 1bb5d3de-6ddf-437b-9009-a04201bef143 not found."
)

# Trying to get a not existing template name
response = self.postJSON(
url,
None,
{
"urns": ["whatsapp:5561912345678"],
"contacts": [self.joe.uuid],
"msg": {"template": {"name": "Away"}},
"channel": channel.uuid,
},
)
self.assertResponseError(response, "non_field_errors", "Template with name Away not found.")

def test_archives(self):
url = reverse("api.v2.archives")

Expand Down

0 comments on commit 93830e8

Please sign in to comment.