Skip to content

Commit

Permalink
[#2996] Create questions linked to a zaak with OpenKlant
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schilling committed Jan 30, 2025
1 parent 74d489b commit ed0569b
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 10 deletions.
60 changes: 58 additions & 2 deletions src/open_inwoner/cms/cases/tests/test_contactform.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from unittest.mock import ANY, patch

from django.conf import settings
from django.contrib.auth import signals
from django.core import mail
from django.test import override_settings
from django.urls import reverse
from django.utils.translation import gettext as _

import factory
import requests_mock
from django_webtest import WebTest
from zgw_consumers.api_models.constants import (
Expand All @@ -20,9 +22,15 @@
eHerkenningUserFactory,
)
from open_inwoner.openklant.constants import KlantenServiceType, Status
from open_inwoner.openklant.models import ESuiteKlantConfig, KlantenSysteemConfig
from open_inwoner.openklant.models import (
ContactFormSubject,
ESuiteKlantConfig,
KlantenSysteemConfig,
)
from open_inwoner.openklant.services import eSuiteVragenService
from open_inwoner.openklant.tests.data import CONTACTMOMENTEN_ROOT, KLANTEN_ROOT
from open_inwoner.openklant.tests.factories import OpenKlant2ConfigFactory
from open_inwoner.openklant.tests.mocks import MockOpenKlant2Service
from open_inwoner.openzaak.models import CatalogusConfig, OpenZaakConfig
from open_inwoner.openzaak.tests.factories import (
ServiceFactory,
Expand Down Expand Up @@ -89,6 +97,8 @@ def setUp(self):
self.klant_config.send_email_confirmation = True
self.klant_config.save()

self.openklant_config = OpenKlant2ConfigFactory()

self.esuite_config = ESuiteKlantConfig.get_solo()
self.esuite_config.register_bronorganisatie_rsin = "123456788"
self.esuite_config.register_type = "Melding"
Expand Down Expand Up @@ -473,7 +483,9 @@ def test_no_form_shown_if_contact_form_disabled(

mock_send_confirm.assert_not_called()

def test_form_success_with_api(self, m, mock_contactmoment, mock_send_confirm):
def test_form_success_with_api_esuite(
self, m, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpExtraMocks(m)

Expand Down Expand Up @@ -528,6 +540,50 @@ def test_form_success_with_api(self, m, mock_contactmoment, mock_send_confirm):

mock_send_confirm.assert_called_once_with("foo@example.com", ANY)

@factory.django.mute_signals(signals.user_logged_in)
@patch(
"open_inwoner.cms.cases.views.status.OpenKlant2Service",
return_value=MockOpenKlant2Service(),
)
def test_form_success_with_api_openklant(
self, m, mock_openklant_service, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpExtraMocks(m)

self.contactformsubject = ContactFormSubject.objects.create(
subject="oip_subject",
)

self.klant_config.primary_backend = KlantenServiceType.OPENKLANT2.value
self.klant_config.save()

response = self.app.get(self.case_detail_url, user=self.user)
form = response.forms["contact-form"]
form.action = reverse(
"cases:case_detail_contact_form",
kwargs={"object_id": self.zaak["uuid"], "api_group_id": self.api_group.id},
)
form["question"] = "What?"
response = form.submit()

self.assertEqual(
response.headers["HX-Redirect"],
reverse(
"cases:case_detail",
kwargs={
"object_id": str(self.zaak["uuid"]),
"api_group_id": self.api_group.id,
},
),
)

redirect = self.app.get(response.headers["HX-Redirect"])
redirect_messages = list(redirect.context["messages"])

self.assertEqual(redirect_messages[0].message, _("Vraag verstuurd!"))
mock_send_confirm.assert_called_once_with("foo@example.com", ANY)

def test_form_success_missing_medewerker(
self, m, mock_contactmoment, mock_send_confirm
):
Expand Down
25 changes: 21 additions & 4 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,28 @@ def register_by_email(self, form, recipient_email):
def register_by_api(self, form, config: KlantenSysteemConfig):
if config.primary_backend == KlantenServiceType.ESUITE.value:
return self._register_via_esuite(form, config=ESuiteKlantConfig.get_solo())
return self._register_via_openklant2(form, config=OpenKlant2Config.get_solo())
return self._register_via_openklant(form, config=OpenKlant2Config.get_solo())

# TODO
def _register_via_openklant2(self, form, config: OpenKlant2Config):
raise NotImplementedError
def _register_via_openklant(self, form, config: OpenKlant2Config):
user = self.request.user
service = OpenKlant2Service(config=config)

partij, created = service.get_or_create_partij_for_user(
fetch_params=service.get_fetch_parameters(user=user),
user=user,
)

cleaned_data = form.cleaned_data
question = cleaned_data["question"]

question = service.create_question_for_zaak(
partij_uuid=partij["uuid"],
question=question,
subject=self.case.omschrijving,
zaak=self.case,
)

return True if question else False

def _register_via_esuite(self, form, config: ESuiteKlantConfig):
assert config.has_api_configuration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Migration(migrations.Migration):

dependencies = [
("openklant", "0023_remove_esuiteklantconfig_register_contact_moment_and_more"),
("openklant", "0024_alter_klantensysteemconfig_primary_backend"),
]

operations = [
Expand Down
38 changes: 37 additions & 1 deletion src/open_inwoner/openklant/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from open_inwoner.utils.logentry import system_action
from open_inwoner.utils.time import instance_is_new
from open_inwoner.utils.url import uuid_from_url
from open_inwoner.utils.views import LogMixin
from openklant2.client import OpenKlant2Client
from openklant2.types.resources.digitaal_adres import DigitaalAdres
from openklant2.types.resources.klant_contact import (
Expand Down Expand Up @@ -842,7 +843,7 @@ def from_klantcontact_and_answer(
)


class OpenKlant2Service(KlantenService):
class OpenKlant2Service(LogMixin, KlantenService):
config: OpenKlant2Config
client: OpenKlant2Client

Expand Down Expand Up @@ -1197,6 +1198,41 @@ def create_question(

return OpenKlant2Question.from_klantcontact_and_answer(klantcontact)

def create_question_for_zaak(
self,
partij_uuid: str,
question: str,
subject: str,
zaak: str,
) -> OpenKlant2Question:
question = self.create_question(
partij_uuid=partij_uuid, question=question, subject=subject
)

# self.log_system_action(
# "registered question {question_uuid} for partij {partij} via OpenKlant".format(
# question_uuid=question.question_kcm_uuid, partij=partij_uuid
# )
# )

# No convention has (yet) been established for the use of
# `onderwerpobjectidentificator`; the implementation is provisianal
onderwerp_object = self.client.onderwerp_object.create(
data={
"klantcontact": {"uuid": question.question_kcm_uuid},
"wasKlantcontact": None,
"onderwerpobjectidentificator": {
"objectId": zaak.identificatie,
"codeObjecttype": "zaak",
"codeRegister": "openzaak",
"codeSoortObjectId": "identificatie",
},
}
)
# self.log_system_action("Created onderwerp_object %s", onderwerp_object["uuid"])

return question

def create_answer(
self, partij_uuid: str, question_klantcontact_uuid: str, answer: str
) -> OpenKlant2Answer:
Expand Down
Loading

0 comments on commit ed0569b

Please sign in to comment.