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 14, 2025
1 parent 65b1058 commit c6d86d2
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 53 deletions.
37 changes: 22 additions & 15 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,26 @@ def setUp(self):
)
self.oz_config.save()

# klant config
self.klant_config = ESuiteKlantConfig.get_solo()
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(
# 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.save()

self.esuite_config = ESuiteKlantConfig.get_solo()
self.esuite_config.send_email_confirmation = True
self.esuite_config.register_contact_moment = True
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 @@ -381,6 +386,7 @@ def test_form_is_shown_if_open_klant_api_configured(

mock_send_confirm.assert_not_called()

# TODO: refactor
def test_form_is_shown_if_open_klant_email_configured(
self, m, mock_contactmoment, mock_send_confirm
):
Expand All @@ -402,6 +408,7 @@ def test_form_is_shown_if_open_klant_email_configured(

mock_send_confirm.assert_not_called()

# TODO: refactor
def test_form_is_shown_if_open_klant_email_and_api_configured(
self, m, mock_contactmoment, mock_send_confirm
):
Expand Down Expand Up @@ -746,7 +753,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 +773,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],
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.16 on 2025-01-14 13:42

from django.db import migrations


def migrate_registration_defaults(apps, _):
ESuiteKlantConfig = apps.get_model("openklant", "ESuiteKlantConfig")
KlantenSysteemConfig = apps.get_model("openklant", "KlantenSysteemConfig")

es_config = ESuiteKlantConfig.objects.first()
klanten_config = KlantenSysteemConfig.objects.first()

if es_config and klanten_config:
klanten_config.register_contact_via_api = es_config.register_contact_moment
klanten_config.register_contact_email = es_config.register_email
klanten_config.send_email_confirmation = es_config.send_email_confirmation
klanten_config.save()


class Migration(migrations.Migration):

dependencies = [
("openklant", "0020_alter_klantensysteemconfig_primary_backend"),
]

operations = [
migrations.RunPython(
code=migrate_registration_defaults,
reverse_code=migrations.RunPython.noop,
)
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.2.16 on 2025-01-14 13:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("openklant", "0021_contactmoment_register_config"),
]

operations = [
migrations.RemoveField(
model_name="esuiteklantconfig",
name="register_contact_moment",
),
migrations.RemoveField(
model_name="esuiteklantconfig",
name="register_email",
),
migrations.RemoveField(
model_name="esuiteklantconfig",
name="send_email_confirmation",
),
migrations.AlterField(
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 op email adres",
),
),
migrations.AlterField(
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 op API",
),
),
]
Loading

0 comments on commit c6d86d2

Please sign in to comment.