Skip to content

Commit

Permalink
[#2966] Refactor configuration for registering contactmomenten
Browse files Browse the repository at this point in the history
    - move relevant fields from ESuiteKlantConfig to global
      KlantenSysteemConfig so they can be used for OpenKlant2
      as well
  • Loading branch information
Paul Schilling committed Jan 15, 2025
1 parent 65b1058 commit 3928ecc
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 71 deletions.
71 changes: 38 additions & 33 deletions src/open_inwoner/cms/cases/tests/test_contactform.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
DigidUserFactory,
eHerkenningUserFactory,
)
from open_inwoner.openklant.constants import Status
from open_inwoner.openklant.models import ESuiteKlantConfig
from open_inwoner.openklant.constants import KlantenServiceType, Status
from open_inwoner.openklant.models import ESuiteKlantConfig, KlantenSysteemConfig
from open_inwoner.openklant.services import eSuiteVragenService
from open_inwoner.openklant.tests.data import CONTACTMOMENTEN_ROOT, KLANTEN_ROOT
from open_inwoner.openzaak.models import CatalogusConfig, OpenZaakConfig
Expand Down Expand Up @@ -82,21 +82,25 @@ def setUp(self):
)
self.oz_config.save()

# klant config
self.klant_config = ESuiteKlantConfig.get_solo()
# klant configurations
self.klant_config = KlantenSysteemConfig.get_solo()
self.klant_config.primary_backend = KlantenServiceType.ESUITE.value
self.klant_config.register_contact_via_api = True
self.klant_config.send_email_confirmation = True
self.klant_config.register_contact_moment = True
self.klant_config.register_bronorganisatie_rsin = "123456788"
self.klant_config.register_type = "Melding"
self.klant_config.register_employee_id = "FooVonBar"
self.klant_config.register_channel = "the-designated-channel"
self.klant_config.klanten_service = ServiceFactory(
self.klant_config.save()

self.esuite_config = ESuiteKlantConfig.get_solo()
self.esuite_config.register_bronorganisatie_rsin = "123456788"
self.esuite_config.register_type = "Melding"
self.esuite_config.register_employee_id = "FooVonBar"
self.esuite_config.register_channel = "the-designated-channel"
self.esuite_config.klanten_service = ServiceFactory(
api_root=KLANTEN_ROOT, api_type=APITypes.kc
)
self.klant_config.contactmomenten_service = ServiceFactory(
self.esuite_config.contactmomenten_service = ServiceFactory(
api_root=CONTACTMOMENTEN_ROOT, api_type=APITypes.cmc
)
self.klant_config.save()
self.esuite_config.save()

self.zaak = generate_oas_component_cached(
"zrc",
Expand Down Expand Up @@ -371,7 +375,7 @@ def test_form_is_shown_if_open_klant_api_configured(
self._setUpMocks(m)
self._setUpExtraMocks(m)

self.assertTrue(self.klant_config.has_api_configuration())
self.assertTrue(self.klant_config.has_api_configuration)

response = self.app.get(self.case_detail_url, user=self.user)
contact_form = response.pyquery("#contact-form")
Expand All @@ -387,12 +391,14 @@ def test_form_is_shown_if_open_klant_email_configured(
self._setUpMocks(m)
self._setUpExtraMocks(m)

self.klant_config.register_email = "example@example.com"
self.klant_config.register_contact_moment = False
self.esuite_config.klanten_service = None
self.esuite_config.save()

self.klant_config.register_contact_email = "example@example.com"
self.klant_config.save()

self.assertFalse(self.klant_config.has_api_configuration())
self.assertTrue(self.klant_config.has_register())
self.assertFalse(self.klant_config.has_api_configuration)
self.assertTrue(self.klant_config.contact_registration_enabled)

response = self.app.get(self.case_detail_url, user=self.user)
contact_form = response.pyquery("#contact-form")
Expand All @@ -411,8 +417,8 @@ def test_form_is_shown_if_open_klant_email_and_api_configured(
self.klant_config.register_email = "example@example.com"
self.klant_config.save()

self.assertTrue(self.klant_config.has_api_configuration())
self.assertTrue(self.klant_config.has_register())
self.assertTrue(self.klant_config.has_api_configuration)
self.assertTrue(self.klant_config.contact_registration_enabled)

response = self.app.get(self.case_detail_url, user=self.user)
contact_form = response.pyquery("#contact-form")
Expand All @@ -428,15 +434,14 @@ def test_no_form_shown_if_open_klant_not_configured(
self._setUpMocks(m)

# reset
self.klant_config.klanten_service = None
self.klant_config.contactmomenten_service = None
self.klant_config.register_email = ""
self.klant_config.register_contact_moment = False
self.klant_config.register_bronorganisatie_rsin = ""
self.klant_config.register_type = ""
self.klant_config.register_employee_id = ""
self.klant_config.save()
self.assertFalse(self.klant_config.has_api_configuration())
self.esuite_config.klanten_service = None
self.esuite_config.contactmomenten_service = None
self.esuite_config.register_bronorganisatie_rsin = ""
self.esuite_config.register_type = ""
self.esuite_config.register_employee_id = ""
self.esuite_config.save()

self.assertFalse(self.klant_config.has_api_configuration)

response = self.app.get(self.case_detail_url, user=self.user)
contact_form = response.pyquery("#contact-form")
Expand Down Expand Up @@ -665,8 +670,8 @@ def test_form_success_with_email(self, m, mock_contactmoment, mock_send_confirm)
self._setUpMocks(m)
self._setUpExtraMocks(m)

self.klant_config.register_email = "example@example.com"
self.klant_config.register_contact_moment = False
self.klant_config.register_contact_email = "example@example.com"
self.klant_config.register_contact_via_api = False
self.klant_config.save()

response = self.app.get(self.case_detail_url, user=self.user)
Expand Down Expand Up @@ -708,7 +713,7 @@ def test_form_success_with_both_email_and_api(
self._setUpMocks(m)
self._setUpExtraMocks(m)

self.klant_config.register_email = "example@example.com"
self.klant_config.register_contact_email = "example@example.com"
self.klant_config.save()

response = self.app.get(self.case_detail_url, user=self.user)
Expand Down Expand Up @@ -746,7 +751,7 @@ def test_send_email_confirmation_is_configurable__send_enabled(
self._setUpMocks(m)
self._setUpExtraMocks(m)

config = ESuiteKlantConfig.get_solo()
config = KlantenSysteemConfig.get_solo()
config.send_email_confirmation = True
config.save()

Expand All @@ -766,7 +771,7 @@ def test_send_email_confirmation_is_configurable__send_disabled(
self._setUpMocks(m)
self._setUpExtraMocks(m)

config = ESuiteKlantConfig.get_solo()
config = KlantenSysteemConfig.get_solo()
config.send_email_confirmation = False
config.save()

Expand Down
34 changes: 24 additions & 10 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
from open_inwoner.accounts.models import User
from open_inwoner.mail.service import send_contact_confirmation_mail
from open_inwoner.openklant.constants import KlantenServiceType
from open_inwoner.openklant.models import ESuiteKlantConfig
from open_inwoner.openklant.models import (
ESuiteKlantConfig,
KlantenSysteemConfig,
OpenKlant2Config,
)
from open_inwoner.openklant.services import (
OpenKlant2Service,
Question,
Expand Down Expand Up @@ -513,7 +517,7 @@ def get_upload_info_context(self, case: Zaak):
if not case:
return {}

open_klant_config = ESuiteKlantConfig.get_solo()
klanten_config = KlantenSysteemConfig.get_solo()

case_type_config_description = ""
case_type_document_upload_description = ""
Expand Down Expand Up @@ -558,7 +562,7 @@ def get_upload_info_context(self, case: Zaak):
and not getattr(self.case, "einddatum", None),
"external_upload_url": external_upload_url,
"contact_form_enabled": (
contact_form_enabled and open_klant_config.has_register()
contact_form_enabled and klanten_config.contact_registration_enabled
),
}

Expand Down Expand Up @@ -909,23 +913,25 @@ def post(self, request, *args, **kwargs):
form = self.get_form()

if form.is_valid():
config = ESuiteKlantConfig.get_solo()
klant_config = KlantenSysteemConfig.get_solo()

email_success = False
api_success = False
send_confirmation = False

if config.register_email:
if klant_config.register_contact_email:
form.cleaned_data[
"question"
] += f"\n\nCase number: {self.case.identificatie}"
email_success = self.register_by_email(form, config.register_email)
email_success = self.register_by_email(
form, klant_config.register_contact_email
)
send_confirmation = email_success

if config.register_contact_moment:
api_success = self.register_by_api(form, config)
if klant_config.register_contact_via_api:
api_success = self.register_by_api(form, config=klant_config)
if api_success:
send_confirmation = config.send_email_confirmation
send_confirmation = klant_config.send_email_confirmation
# else keep the send_confirmation if email set it

if send_confirmation:
Expand Down Expand Up @@ -990,7 +996,15 @@ def register_by_email(self, form, recipient_email):
)
return False

def register_by_api(self, form, config: ESuiteKlantConfig):
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())

def _register_via_openklant2(self, form, config: OpenKlant2Config):
pass

def _register_via_esuite(self, form, config: ESuiteKlantConfig):
assert config.has_api_configuration()

try:
Expand Down
26 changes: 23 additions & 3 deletions src/open_inwoner/configurations/bootstrap/openklant.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from zgw_consumers.models import Service

from open_inwoner.configurations.bootstrap.utils import get_service
from open_inwoner.openklant.models import ESuiteKlantConfig, OpenKlant2Config
from open_inwoner.openklant.models import (
ESuiteKlantConfig,
KlantenSysteemConfig,
OpenKlant2Config,
)


class OpenKlant2Configuration(ConfigurationModel):
Expand Down Expand Up @@ -41,13 +45,22 @@ class KlantenApiConfigurationModel(ConfigurationModel):
class Meta:
django_model_refs = {
ESuiteKlantConfig: (
"register_email",
"register_contact_moment",
"register_bronorganisatie_rsin",
"register_channel",
"register_type",
"register_employee_id",
"use_rsin_for_innNnpId_query_parameter",
)
}


class KlantSysteemConfigurationModel(ConfigurationModel):
class Meta:
django_model_refs = {
KlantenSysteemConfig: (
"primary_backend",
"register_contact_via_api",
"register_contact_email",
"send_email_confirmation",
)
}
Expand Down Expand Up @@ -139,3 +152,10 @@ def execute(self, model: OpenKlant2Configuration):

config.full_clean()
config.save()


# TODO: complete config step
class KlantSysteemConfigurationStep(
BaseConfigurationStep[KlantSysteemConfigurationModel]
):
pass
20 changes: 17 additions & 3 deletions src/open_inwoner/openklant/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,21 @@ class OpenKlant2ConfigAdmin(SingletonModelAdmin):

@admin.register(KlantenSysteemConfig)
class KlantenSysteemConfigAdmin(SingletonModelAdmin):
model = KlantenSysteemConfig
change_form_template = "admin/openklant/klantensysteemconfig/change_form.html"

class Meta:
model = KlantenSysteemConfig
fieldsets = [
(
_("API configuration globals"),
{"fields": ["primary_backend"]},
),
(
_("Vragen/contactmomenten"),
{
"fields": [
"register_contact_via_api",
"register_contact_email",
"send_email_confirmation",
]
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 4.2.16 on 2025-01-14 13:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("openklant", "0018_rename_openklantconfig_esuiteklantconfig_and_more"),
]

operations = [
migrations.AddField(
model_name="klantensysteemconfig",
name="register_contact_email",
field=models.EmailField(
blank=True,
help_text="Contacts initiated or questions submitted by a client (e.g. via a contact form) will be registered via email.",
max_length=254,
verbose_name="Registreer vragen/contactmomenten op email adres",
),
),
migrations.AddField(
model_name="klantensysteemconfig",
name="register_contact_via_api",
field=models.BooleanField(
default=False,
help_text="Contacts initiated or questions submitted by a client (e.g. via a contact form) will be registered in the appropriate API (eSuite or OpenKlant2).",
verbose_name="Registreer vragen/contactmomenten op API",
),
),
migrations.AddField(
model_name="klantensysteemconfig",
name="send_email_confirmation",
field=models.BooleanField(
help_text="If enabled the 'contactform_confirmation' email template will be sent. If disabled the external API will send a confirmation email.",
verbose_name="Stuur contactformulier e-mailbevestiging",
default=False,
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.16 on 2025-01-14 13:18

from django.db import migrations, models

import open_inwoner.openklant.models


class Migration(migrations.Migration):

dependencies = [
("openklant", "0019_klantensysteemconfig_register_contact_email_and_more"),
]

operations = [
migrations.AlterField(
model_name="klantensysteemconfig",
name="primary_backend",
field=models.CharField(
choices=[("ESUITE", "esuite"), ("OPENKLANT2", "openklant2")],
help_text="Choose the primary backend for retrieving klanten data. Changes to klanten data will be saved to both backends (if configured).",
max_length=10,
validators=[open_inwoner.openklant.models.validate_primary_backend],
),
),
]
Loading

0 comments on commit 3928ecc

Please sign in to comment.