Fix custom field sending in new ticket webhook

This commit is contained in:
Timothy Hobbs 2023-12-27 19:34:56 +00:00
parent 82653806c4
commit 7aa7f0eca2
3 changed files with 51 additions and 16 deletions

View File

@ -11,6 +11,7 @@ models.py - Model (and hence database) definitions. This is the core of the
from .lib import convert_value from .lib import convert_value
from .templated_email import send_templated_mail from .templated_email import send_templated_mail
from .validators import validate_file_extension from .validators import validate_file_extension
from .webhooks import send_new_ticket_webhook
import datetime import datetime
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
@ -664,6 +665,8 @@ class Ticket(models.Model):
if self.queue.enable_notifications_on_email_events: if self.queue.enable_notifications_on_email_events:
for cc in self.ticketcc_set.all(): for cc in self.ticketcc_set.all():
send('ticket_cc', cc.email_address) send('ticket_cc', cc.email_address)
if "new_ticket_cc" in roles:
send_new_ticket_webhook(self)
return recipients return recipients
def _get_assigned_to(self): def _get_assigned_to(self):

View File

@ -1,5 +1,6 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from helpdesk.models import Queue from helpdesk.models import Queue, CustomField, TicketCustomFieldValue, Ticket
from helpdesk.serializers import TicketSerializer
from rest_framework.status import ( from rest_framework.status import (
HTTP_201_CREATED HTTP_201_CREATED
) )
@ -82,12 +83,16 @@ class WebhookTest(APITestCase):
def setUp(self): def setUp(self):
staff_user = User.objects.create_user(username='test', is_staff=True) staff_user = User.objects.create_user(username='test', is_staff=True)
CustomField(
name="my_custom_field",
data_type="varchar",
required=False,
).save()
self.client.force_authenticate(staff_user) self.client.force_authenticate(staff_user)
def test_test_server(self): def test_test_server(self):
server = WebhookServer(('localhost', 8123), WebhookRequestHandler) server = WebhookServer(('localhost', 8123), WebhookRequestHandler)
server.start() server.start()
requests.post('http://localhost:8123/new-ticket', json={ requests.post('http://localhost:8123/new-ticket', json={
"foo": "bar"}) "foo": "bar"})
handled_webhook_requests = requests.get('http://localhost:8123/get-past-requests').json() handled_webhook_requests = requests.get('http://localhost:8123/get-past-requests').json()
@ -105,8 +110,11 @@ class WebhookTest(APITestCase):
'title': 'Test title', 'title': 'Test title',
'description': 'Test description\nMulti lines', 'description': 'Test description\nMulti lines',
'submitter_email': 'test@mail.com', 'submitter_email': 'test@mail.com',
'priority': 4 'priority': 4,
'custom_my_custom_field': 'custom value',
}) })
self.assertEqual(CustomField.objects.all().first().name, "my_custom_field")
self.assertEqual(TicketCustomFieldValue.objects.get(ticket=response.data['id']).value, 'custom value')
self.assertEqual(response.status_code, HTTP_201_CREATED) self.assertEqual(response.status_code, HTTP_201_CREATED)
handled_webhook_requests = requests.get('http://localhost:8124/get-past-requests') handled_webhook_requests = requests.get('http://localhost:8124/get-past-requests')
handled_webhook_requests = handled_webhook_requests.json() handled_webhook_requests = handled_webhook_requests.json()
@ -116,6 +124,35 @@ class WebhookTest(APITestCase):
self.assertEqual(handled_webhook_requests['new_ticket_requests'][-1]["ticket"]["title"], "Test title") self.assertEqual(handled_webhook_requests['new_ticket_requests'][-1]["ticket"]["title"], "Test title")
self.assertEqual(handled_webhook_requests['new_ticket_requests_1'][-1]["ticket"]["title"], "Test title") self.assertEqual(handled_webhook_requests['new_ticket_requests_1'][-1]["ticket"]["title"], "Test title")
self.assertEqual(handled_webhook_requests['new_ticket_requests'][-1]["ticket"]["description"], "Test description\nMulti lines") self.assertEqual(handled_webhook_requests['new_ticket_requests'][-1]["ticket"]["description"], "Test description\nMulti lines")
ticket = Ticket.objects.get(id=handled_webhook_requests["new_ticket_requests"][-1]["ticket"]["id"])
ticket.set_custom_field_values()
serializer = TicketSerializer(ticket)
self.assertEqual(
list(sorted(serializer.fields.keys())),
['assigned_to',
'attachment',
'custom_my_custom_field',
'description',
'due_date',
'followup_set',
'id',
'merged_to',
'on_hold',
'priority',
'queue',
'resolution',
'status',
'submitter_email',
'title']
)
with open("/tmp/request", "w") as fd:
import json
json.dump(handled_webhook_requests['new_ticket_requests_1'][-1]["ticket"], fd, indent=4, default=str)
with open("/tmp/serilaizer_data", "w") as fd:
import json
json.dump(serializer.data, fd, indent=4, default=str)
self.assertEqual(serializer.data, handled_webhook_requests["new_ticket_requests"][-1]["ticket"])
response = self.client.post('/api/followups/', { response = self.client.post('/api/followups/', {
'ticket': handled_webhook_requests['new_ticket_requests'][-1]["ticket"]["id"], 'ticket': handled_webhook_requests['new_ticket_requests'][-1]["ticket"]["id"],
"comment": "Test comment", "comment": "Test comment",
@ -127,6 +164,7 @@ class WebhookTest(APITestCase):
self.assertEqual(len(handled_webhook_requests['follow_up_requests_1']), 1) self.assertEqual(len(handled_webhook_requests['follow_up_requests_1']), 1)
self.assertEqual(handled_webhook_requests['follow_up_requests'][-1]["ticket"]["followup_set"][-1]["comment"], "Test comment") self.assertEqual(handled_webhook_requests['follow_up_requests'][-1]["ticket"]["followup_set"][-1]["comment"], "Test comment")
self.assertEqual(handled_webhook_requests['follow_up_requests_1'][-1]["ticket"]["followup_set"][-1]["comment"], "Test comment") self.assertEqual(handled_webhook_requests['follow_up_requests_1'][-1]["ticket"]["followup_set"][-1]["comment"], "Test comment")
server.stop() server.stop()
def test_create_ticket_and_followup_via_email(self): def test_create_ticket_and_followup_via_email(self):
@ -206,5 +244,3 @@ class WebhookTest(APITestCase):
self.assertEqual(handled_webhook_requests['follow_up_requests'][-1]["ticket"]["id"], ticket_id) self.assertEqual(handled_webhook_requests['follow_up_requests'][-1]["ticket"]["id"], ticket_id)
server.stop() server.stop()

View File

@ -1,14 +1,9 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from . import settings from . import settings
import requests import requests
import requests.exceptions import requests.exceptions
import logging import logging
from .models import Ticket
from .serializers import TicketSerializer
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def notify_followup_webhooks(followup): def notify_followup_webhooks(followup):
@ -16,7 +11,9 @@ def notify_followup_webhooks(followup):
if not urls: if not urls:
return return
# Serialize the ticket associated with the followup # Serialize the ticket associated with the followup
from .serializers import TicketSerializer
ticket = followup.ticket ticket = followup.ticket
ticket.set_custom_field_values()
serialized_ticket = TicketSerializer(ticket).data serialized_ticket = TicketSerializer(ticket).data
# Prepare the data to send # Prepare the data to send
@ -33,20 +30,19 @@ def notify_followup_webhooks(followup):
logger.error('Timeout while sending followup webhook to %s', url) logger.error('Timeout while sending followup webhook to %s', url)
@receiver(post_save, sender=Ticket) def send_new_ticket_webhook(ticket):
def ticket_post_save(sender, instance, created, **kwargs):
if not created:
return
urls = settings.HELPDESK_GET_NEW_TICKET_WEBHOOK_URLS() urls = settings.HELPDESK_GET_NEW_TICKET_WEBHOOK_URLS()
if not urls: if not urls:
return return
# Serialize the ticket # Serialize the ticket
serialized_ticket = TicketSerializer(instance).data from .serializers import TicketSerializer
ticket.set_custom_field_values()
serialized_ticket = TicketSerializer(ticket).data
# Prepare the data to send # Prepare the data to send
data = { data = {
'ticket': serialized_ticket, 'ticket': serialized_ticket,
'queue_slug': instance.queue.slug 'queue_slug': ticket.queue.slug
} }
for url in urls: for url in urls: