diff --git a/src/open_inwoner/cms/cases/tests/test_contactform.py b/src/open_inwoner/cms/cases/tests/test_contactform.py index 9c2db28601..bba63ea24c 100644 --- a/src/open_inwoner/cms/cases/tests/test_contactform.py +++ b/src/open_inwoner/cms/cases/tests/test_contactform.py @@ -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 @@ -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", @@ -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") @@ -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") @@ -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") @@ -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") @@ -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) @@ -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) @@ -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() @@ -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() diff --git a/src/open_inwoner/cms/cases/tests/test_htmx.py b/src/open_inwoner/cms/cases/tests/test_htmx.py index 876b6a0fb4..4fca4e4018 100644 --- a/src/open_inwoner/cms/cases/tests/test_htmx.py +++ b/src/open_inwoner/cms/cases/tests/test_htmx.py @@ -17,8 +17,8 @@ from open_inwoner.accounts.tests.factories import DigidUserFactory from open_inwoner.cms.tests import cms_tools from open_inwoner.configurations.models import SiteConfiguration -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 OpenZaakConfig @@ -89,18 +89,23 @@ def setUp(self) -> None: self.oz_config.save() # klant config - self.klant_config = ESuiteKlantConfig.get_solo() - 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.klanten_service = ServiceFactory( + 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.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.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", diff --git a/src/open_inwoner/cms/cases/views/status.py b/src/open_inwoner/cms/cases/views/status.py index c3fcb75911..d8a1e5ff9e 100644 --- a/src/open_inwoner/cms/cases/views/status.py +++ b/src/open_inwoner/cms/cases/views/status.py @@ -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, @@ -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 = "" @@ -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 ), } @@ -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: @@ -990,7 +996,16 @@ 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()) + + # TODO + def _register_via_openklant2(self, form, config: OpenKlant2Config): + raise NotImplementedError + + def _register_via_esuite(self, form, config: ESuiteKlantConfig): assert config.has_api_configuration() try: diff --git a/src/open_inwoner/cms/footer/cms_plugins.py b/src/open_inwoner/cms/footer/cms_plugins.py index a03bd78609..8d7f3db3fc 100644 --- a/src/open_inwoner/cms/footer/cms_plugins.py +++ b/src/open_inwoner/cms/footer/cms_plugins.py @@ -4,7 +4,7 @@ from cms.plugin_pool import plugin_pool from open_inwoner.configurations.models import SiteConfiguration -from open_inwoner.openklant.models import ESuiteKlantConfig +from open_inwoner.openklant.models import KlantenSysteemConfig @plugin_pool.register_plugin @@ -17,7 +17,7 @@ class FooterPagesPlugin(CMSPluginBase): def render(self, context, instance, placeholder): # TODO move options to plugin model config = SiteConfiguration.get_solo() - ok_config = ESuiteKlantConfig.get_solo() + klant_config = KlantenSysteemConfig.get_solo() context["flatpages"] = config.get_ordered_flatpages - context["has_form_configuration"] = ok_config.has_form_configuration() + context["has_form_configuration"] = klant_config.has_contactform_configuration return context diff --git a/src/open_inwoner/cms/footer/tests/test_plugin.py b/src/open_inwoner/cms/footer/tests/test_plugin.py index 021defbfe0..5475368f8a 100644 --- a/src/open_inwoner/cms/footer/tests/test_plugin.py +++ b/src/open_inwoner/cms/footer/tests/test_plugin.py @@ -3,7 +3,8 @@ from open_inwoner.cms.footer.cms_plugins import FooterPagesPlugin from open_inwoner.cms.tests import cms_tools -from open_inwoner.openklant.models import ESuiteKlantConfig +from open_inwoner.openklant.constants import KlantenServiceType +from open_inwoner.openklant.models import ESuiteKlantConfig, KlantenSysteemConfig from open_inwoner.openklant.tests.factories import ContactFormSubjectFactory from open_inwoner.utils.test import ClearCachesMixin @@ -11,37 +12,39 @@ class ContactFormTestCase(ClearCachesMixin, TestCase): def setUp(self): super().setUp() - # clear config - config = ESuiteKlantConfig.get_solo() - config.klanten_service = None - config.contactmomenten_service = None - config.register_email = "" - config.register_contact_moment = False - config.register_bronorganisatie_rsin = "" - config.register_type = "" - config.register_employee_id = "" - config.send_email_confirmation = True - config.save() + # clear esuite_config + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.klanten_service = None + esuite_config.contactmomenten_service = None + esuite_config.register_bronorganisatie_rsin = "" + esuite_config.register_type = "" + esuite_config.register_employee_id = "" + esuite_config.send_email_confirmation = True + esuite_config.save() def test_no_form_link_shown_in_footer_if_not_has_configuration(self): # set nothing - config = ESuiteKlantConfig.get_solo() - self.assertFalse(config.has_form_configuration()) + klant_config = KlantenSysteemConfig.get_solo() + self.assertFalse(klant_config.has_contactform_configuration) html, context = cms_tools.render_plugin(FooterPagesPlugin) self.assertNotIn(_("Contact formulier"), html) def test_form_link_is_shown_in_footer_when_has_configuration(self): - ok_config = ESuiteKlantConfig.get_solo() - self.assertFalse(ok_config.has_form_configuration()) + klant_config = KlantenSysteemConfig.get_solo() + klant_config.primary_backend = KlantenServiceType.ESUITE.value + klant_config.save() - ContactFormSubjectFactory(config=ok_config) + self.assertFalse(klant_config.has_contactform_configuration) - ok_config.register_email = "example@example.com" - ok_config.save() + esuite_config = ESuiteKlantConfig.get_solo() + ContactFormSubjectFactory(config=esuite_config) - self.assertTrue(ok_config.has_form_configuration()) + klant_config.register_contact_email = "example@example.com" + klant_config.save() + + self.assertTrue(klant_config.has_contactform_configuration) html, context = cms_tools.render_plugin(FooterPagesPlugin) diff --git a/src/open_inwoner/configurations/bootstrap/openklant.py b/src/open_inwoner/configurations/bootstrap/openklant.py index ea8eb8f9ab..e2c8c70125 100644 --- a/src/open_inwoner/configurations/bootstrap/openklant.py +++ b/src/open_inwoner/configurations/bootstrap/openklant.py @@ -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): @@ -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 KlantenSysteemConfigurationModel(ConfigurationModel): + class Meta: + django_model_refs = { + KlantenSysteemConfig: ( + "primary_backend", + "register_contact_via_api", + "register_contact_email", "send_email_confirmation", ) } @@ -58,9 +71,9 @@ class ESuiteKlantConfigurationStep(BaseConfigurationStep[KlantenApiConfiguration Configure the KIC settings and set any feature flags or other options if specified """ - verbose_name = "Klantinteractie APIs configuration" - enable_setting = "openklant_config_enable" - namespace = "openklant_config" + verbose_name = "eSuite Klant APIs configuration" + enable_setting = "esuiteklant_config_enable" + namespace = "esuiteklant_config" config_model = KlantenApiConfigurationModel def execute(self, model: KlantenApiConfigurationModel): @@ -139,3 +152,27 @@ def execute(self, model: OpenKlant2Configuration): config.full_clean() config.save() + + +class KlantenSysteemConfigurationStep( + BaseConfigurationStep[KlantenSysteemConfigurationModel] +): + """ + Configure the KlantenSysteem settings + """ + + verbose_name = "KlantenSysteem configuration" + enable_setting = "klantensysteem_config_enable" + namespace = "klantensysteem_config" + config_model = KlantenSysteemConfigurationModel + + def execute(self, model: KlantenApiConfigurationModel): + create_or_update_kwargs = model.model_dump() + + config = KlantenSysteemConfig.get_solo() + + for key, val in create_or_update_kwargs.items(): + setattr(config, key, val) + + config.full_clean() + config.save() diff --git a/src/open_inwoner/configurations/tests/bootstrap/files/esuiteklant_config_step_full.yaml b/src/open_inwoner/configurations/tests/bootstrap/files/esuiteklant_config_step_full.yaml index b2dd801ee4..bae7088b1a 100644 --- a/src/open_inwoner/configurations/tests/bootstrap/files/esuiteklant_config_step_full.yaml +++ b/src/open_inwoner/configurations/tests/bootstrap/files/esuiteklant_config_step_full.yaml @@ -1,13 +1,10 @@ -openklant_config_enable: true -openklant_config: +esuiteklant_config_enable: true +esuiteklant_config: klanten_service_identifier: klanten-service contactmomenten_service_identifier: contactmomenten-service exclude_contactmoment_kanalen: [] - register_email: admin@oip.org - register_contact_moment: true register_bronorganisatie_rsin: '837194569' register_channel: email register_type: bericht register_employee_id: '1234' use_rsin_for_innNnpId_query_parameter: true - send_email_confirmation: false diff --git a/src/open_inwoner/configurations/tests/bootstrap/files/klantensysteem_config_step_full.yaml b/src/open_inwoner/configurations/tests/bootstrap/files/klantensysteem_config_step_full.yaml new file mode 100644 index 0000000000..69bf3be6ed --- /dev/null +++ b/src/open_inwoner/configurations/tests/bootstrap/files/klantensysteem_config_step_full.yaml @@ -0,0 +1,6 @@ +klantensysteem_config_enable: true +klantensysteem_config: + primary_backend: esuite + register_contact_via_api: true + register_contact_email: "oip-test@test.nl" + send_email_confirmation: true diff --git a/src/open_inwoner/configurations/tests/bootstrap/test_setup_openklant_config.py b/src/open_inwoner/configurations/tests/bootstrap/test_setup_openklant_config.py index 5cf64b619e..d9b3636215 100644 --- a/src/open_inwoner/configurations/tests/bootstrap/test_setup_openklant_config.py +++ b/src/open_inwoner/configurations/tests/bootstrap/test_setup_openklant_config.py @@ -1,16 +1,23 @@ from pathlib import Path +from django.core.exceptions import ValidationError from django.test import TestCase from django_setup_configuration.exceptions import ConfigurationRunFailed from django_setup_configuration.test_utils import execute_single_step from zgw_consumers.constants import APITypes -from open_inwoner.openklant.models import ESuiteKlantConfig, OpenKlant2Config +from open_inwoner.openklant.constants import KlantenServiceType +from open_inwoner.openklant.models import ( + ESuiteKlantConfig, + KlantenSysteemConfig, + OpenKlant2Config, +) from open_inwoner.openzaak.tests.factories import ServiceFactory from ...bootstrap.openklant import ( ESuiteKlantConfigurationStep, + KlantenSysteemConfigurationStep, OpenKlant2ConfigurationStep, ) @@ -19,6 +26,9 @@ BASE_DIR = Path(__file__).parent / "files" ESUITEKLANT_CONFIG_STEP_FULL_YAML = str(BASE_DIR / "esuiteklant_config_step_full.yaml") +KLANTENSYSTEEM_CONFIG_STEP_FULL_YAML = str( + BASE_DIR / "klantensysteem_config_step_full.yaml" +) OPENKLANT2_CONFIG_STEP_FULL_YAML = str(BASE_DIR / "openklant2_config_step_full.yaml") @@ -43,8 +53,6 @@ def test_configure(self): self.assertEqual(config.klanten_service, kc) self.assertEqual(config.contactmomenten_service, cmc) - self.assertEqual(config.register_email, "admin@oip.org") - self.assertEqual(config.register_contact_moment, True) self.assertEqual(config.register_bronorganisatie_rsin, "837194569") self.assertEqual(config.register_channel, "email") self.assertEqual(config.register_type, "bericht") @@ -149,8 +157,6 @@ def assert_values(): self.assertEqual(config.klanten_service, kc) self.assertEqual(config.contactmomenten_service, cmc) - self.assertEqual(config.register_email, "admin@oip.org") - self.assertEqual(config.register_contact_moment, True) self.assertEqual(config.register_bronorganisatie_rsin, "837194569") self.assertEqual(config.register_channel, "email") self.assertEqual(config.register_type, "bericht") @@ -164,8 +170,6 @@ def assert_values(): assert_values() config = ESuiteKlantConfig.get_solo() - config.register_email = "not-admin@oip.org" - config.register_contact_moment = False config.register_bronorganisatie_rsin = "800000009" config.register_channel = "not-email" config.register_type = "not-bericht" @@ -205,3 +209,39 @@ def test_configure(self): self.assertEqual( config.interne_taak_toelichting, "Vraag via OIP, graag beantwoorden" ) + + +class KlantenSysteemConfigurationStepTest(TestCase): + def test_configure_success(self): + kc = ServiceFactory( + slug="klanten-service", + api_root=KLANTEN_SERVICE_API_ROOT, + api_type=APITypes.kc, + ) + cmc = ServiceFactory( + slug="contactmomenten-service", + api_root=CONTACTMOMENTEN_SERVICE_API_ROOT, + api_type=APITypes.cmc, + ) + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.klanten_service = kc + esuite_config.contactmomenten_service = cmc + esuite_config.save() + + execute_single_step( + KlantenSysteemConfigurationStep, + yaml_source=KLANTENSYSTEEM_CONFIG_STEP_FULL_YAML, + ) + + config = KlantenSysteemConfig.get_solo() + self.assertEqual(config.primary_backend, KlantenServiceType.ESUITE.value) + self.assertTrue(config.register_contact_via_api) + self.assertEqual(config.register_contact_email, "oip-test@test.nl") + self.assertTrue(config.send_email_confirmation) + + def test_configure_missing_api_services(self): + with self.assertRaises(ValidationError): + execute_single_step( + KlantenSysteemConfigurationStep, + yaml_source=KLANTENSYSTEEM_CONFIG_STEP_FULL_YAML, + ) diff --git a/src/open_inwoner/openklant/admin.py b/src/open_inwoner/openklant/admin.py index 7fcfd2c918..36d8dcfcb1 100644 --- a/src/open_inwoner/openklant/admin.py +++ b/src/open_inwoner/openklant/admin.py @@ -43,17 +43,6 @@ class Meta: model = ESuiteKlantConfig fields = "__all__" - def clean(self, *args, **kwargs): - cleaned_data = super().clean(*args, **kwargs) - - if cleaned_data["register_contact_moment"]: - msg = _( - "Voor registratie in de Klanten en Contactmomenten API is dit veld vereist." - ) - for field_name in ESuiteKlantConfig.register_api_required_fields: - if not cleaned_data[field_name]: - self.add_error(field_name, msg) - @admin.register(ESuiteKlantConfig) class ESuiteKlantConfigAdmin(OrderedInlineModelAdminMixin, SingletonModelAdmin): @@ -62,25 +51,15 @@ class ESuiteKlantConfigAdmin(OrderedInlineModelAdminMixin, SingletonModelAdmin): ContactFormSubjectInlineAdmin, ] fieldsets = [ - ( - _("Email registratie"), - { - "fields": [ - "register_email", - ], - }, - ), ( _("Klanten en Contacten API registratie"), { "fields": [ - "register_contact_moment", "register_bronorganisatie_rsin", "register_type", "register_channel", "register_employee_id", "use_rsin_for_innNnpId_query_parameter", - "send_email_confirmation", ], }, ), @@ -157,7 +136,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", + ] + }, + ), + ] diff --git a/src/open_inwoner/openklant/forms.py b/src/open_inwoner/openklant/forms.py index f04cb47f30..3a6d32bd36 100644 --- a/src/open_inwoner/openklant/forms.py +++ b/src/open_inwoner/openklant/forms.py @@ -57,6 +57,7 @@ def __init__(self, user, request_session, *args, **kwargs): self.user = user self.request_session = request_session + # TODO: check for primary_backend? What in case of OK2? config = ESuiteKlantConfig.get_solo() self.fields["subject"].queryset = config.contactformsubject_set.all() diff --git a/src/open_inwoner/openklant/migrations/0021_klantensysteemconfig_register_contact_email_and_more.py b/src/open_inwoner/openklant/migrations/0021_klantensysteemconfig_register_contact_email_and_more.py new file mode 100644 index 0000000000..fc9dfbe058 --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0021_klantensysteemconfig_register_contact_email_and_more.py @@ -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", "0020_add_more_help_texts_to_openklant2_config"), + ] + + 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, + ), + ), + ] diff --git a/src/open_inwoner/openklant/migrations/0022_contactmoment_register_config.py b/src/open_inwoner/openklant/migrations/0022_contactmoment_register_config.py new file mode 100644 index 0000000000..d4f289725b --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0022_contactmoment_register_config.py @@ -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", "0021_klantensysteemconfig_register_contact_email_and_more"), + ] + + operations = [ + migrations.RunPython( + code=migrate_registration_defaults, + reverse_code=migrations.RunPython.noop, + ) + ] diff --git a/src/open_inwoner/openklant/migrations/0023_remove_esuiteklantconfig_register_contact_moment_and_more.py b/src/open_inwoner/openklant/migrations/0023_remove_esuiteklantconfig_register_contact_moment_and_more.py new file mode 100644 index 0000000000..7fdd65ad60 --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0023_remove_esuiteklantconfig_register_contact_moment_and_more.py @@ -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", "0022_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", + ), + ), + ] diff --git a/src/open_inwoner/openklant/models.py b/src/open_inwoner/openklant/models.py index a34973d85d..94956f16d2 100644 --- a/src/open_inwoner/openklant/models.py +++ b/src/open_inwoner/openklant/models.py @@ -42,14 +42,6 @@ class ESuiteKlantConfig(SingletonModel): blank=True, ) - register_email = models.EmailField( - verbose_name=_("Registreer op email adres"), - blank=True, - ) - register_contact_moment = models.BooleanField( - verbose_name=_("Registreer in Contactmomenten API"), - default=False, - ) register_bronorganisatie_rsin = models.CharField( verbose_name=_("Organisatie RSIN"), max_length=9, @@ -89,14 +81,6 @@ class ESuiteKlantConfig(SingletonModel): ), default=False, ) - send_email_confirmation = models.BooleanField( - verbose_name=_("Stuur contactformulier e-mailbevestiging"), - help_text=_( - "If enabled the 'contactform_confirmation' email template will be sent. " - "If disabled the external API will send a confirmation email." - ), - default=False, - ) exclude_contactmoment_kanalen = ArrayField( base_field=models.CharField( blank=True, @@ -113,7 +97,6 @@ class ESuiteKlantConfig(SingletonModel): ) register_api_required_fields = ( - "register_contact_moment", "contactmomenten_service", "klanten_service", "register_bronorganisatie_rsin", @@ -125,12 +108,6 @@ class ESuiteKlantConfig(SingletonModel): class Meta: verbose_name = _("eSuite Klant configuration") - def has_register(self) -> bool: - return self.register_email or self.has_api_configuration() - - def has_form_configuration(self) -> bool: - return self.has_register() and self.contactformsubject_set.exists() - def has_api_configuration(self): return all(getattr(self, f, "") for f in self.register_api_required_fields) @@ -274,9 +251,58 @@ class KlantenSysteemConfig(SingletonModel): ), validators=[validate_primary_backend], ) + register_contact_via_api = models.BooleanField( + verbose_name=_("Registreer op API"), + 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)." + ), + ) + register_contact_email = models.EmailField( + verbose_name=_("Registreer op email adres"), + blank=True, + help_text=_( + "Contacts initiated or questions submitted by a client (e.g. via a contact form) will be " + "registered via email." + ), + ) + send_email_confirmation = models.BooleanField( + verbose_name=_("Stuur contactformulier e-mailbevestiging"), + help_text=_( + "If enabled the 'contactform_confirmation' email template will be sent. " + "If disabled the external API will send a confirmation email." + ), + default=False, + ) class Meta: verbose_name = _("Configuratie Klanten Systeem") def __str__(self): return "Configuratie Klanten Systeem" + + @property + def has_api_configuration(self) -> bool: + if self.primary_backend == KlantenServiceType.ESUITE.value: + esuite_config = ESuiteKlantConfig.get_solo() + return esuite_config.has_api_configuration() + + # TODO: support `has_api_configuration` check for OK2? + return False + + @property + def contact_registration_enabled(self) -> bool: + return bool(self.register_contact_email or self.has_api_configuration) + + @property + def has_contactform_configuration(self) -> bool: + if not self.contact_registration_enabled: + return False + + if self.primary_backend == KlantenServiceType.ESUITE.value: + esuite_config = ESuiteKlantConfig.get_solo() + return esuite_config.contactformsubject_set.exists() + + # TODO: check conditions for OK2 + return False diff --git a/src/open_inwoner/openklant/tests/test_contactform.py b/src/open_inwoner/openklant/tests/test_contactform.py index 68a4d8b74e..0bf5f63697 100644 --- a/src/open_inwoner/openklant/tests/test_contactform.py +++ b/src/open_inwoner/openklant/tests/test_contactform.py @@ -11,7 +11,8 @@ from open_inwoner.accounts.tests.factories import UserFactory from open_inwoner.openklant.api_models import KlantContactRol -from open_inwoner.openklant.models import ESuiteKlantConfig +from open_inwoner.openklant.constants import KlantenServiceType +from open_inwoner.openklant.models import ESuiteKlantConfig, KlantenSysteemConfig from open_inwoner.openklant.tests.data import MockAPICreateData from open_inwoner.openklant.tests.factories import ContactFormSubjectFactory from open_inwoner.openklant.views.contactform import ContactFormView @@ -50,17 +51,20 @@ def setUpTestData(cls): def setUp(self): super().setUp() - # clear config - config = ESuiteKlantConfig.get_solo() - config.klanten_service = None - config.contactmomenten_service = None - config.register_email = "" - config.register_contact_moment = False - config.register_bronorganisatie_rsin = "" - config.register_type = "" - config.register_employee_id = "" - config.send_email_confirmation = True - config.save() + # clear esuite_config + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.klanten_service = None + esuite_config.contactmomenten_service = None + esuite_config.register_bronorganisatie_rsin = "" + esuite_config.register_type = "" + esuite_config.register_employee_id = "" + esuite_config.send_email_confirmation = True + esuite_config.save() + + klant_config = KlantenSysteemConfig.get_solo() + klant_config.primary_backend = KlantenServiceType.ESUITE.value + klant_config.register_contact_email = "" + klant_config.save() # bypass CMS for rendering form template directly via ContactFormView ContactFormView.template_name = "pages/contactform/form.html" @@ -69,36 +73,40 @@ def test_singleton_has_configuration_method( self, m, mock_captcha, mock_send_confirm ): # use cleared (from setUp() - config = ESuiteKlantConfig.get_solo() - self.assertFalse(config.has_form_configuration()) + klant_config = KlantenSysteemConfig.get_solo() + esuite_config = ESuiteKlantConfig.get_solo() + + self.assertFalse(klant_config.has_contactform_configuration) - ContactFormSubjectFactory(config=config) + # set email on glogal config and create subject for eSuite backend + klant_config.register_contact_email = "example@example.com" + ContactFormSubjectFactory(config=esuite_config) - # email and subject - config.register_email = "example@example.com" - self.assertTrue(config.has_form_configuration()) + self.assertTrue(klant_config.has_contactform_configuration) # subject but nothing else - config.register_email = "" - self.assertFalse(config.has_form_configuration()) + klant_config.register_contact_email = "" + self.assertFalse(klant_config.has_contactform_configuration) # API and subject - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "0123456789" - config.klanten_service = ServiceFactory() - config.contactmomenten_service = ServiceFactory() - config.register_type = "Melding" - config.register_employee_id = "FooVonBar" - self.assertTrue(config.has_form_configuration()) + esuite_config.register_contact_moment = True + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.klanten_service = ServiceFactory() + esuite_config.contactmomenten_service = ServiceFactory() + esuite_config.register_type = "Melding" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() + + self.assertTrue(klant_config.has_contactform_configuration) mock_send_confirm.assert_not_called() - def test_no_form_shown_if_not_has_configuration( + def test_no_form_shown_if_no_contactform_configuration( self, m, mock_captcha, mock_send_confirm ): # set nothing - config = ESuiteKlantConfig.get_solo() - self.assertFalse(config.has_form_configuration()) + klant_config = KlantenSysteemConfig.get_solo() + self.assertFalse(klant_config.has_contactform_configuration) response = self.app.get(self.url) self.assertContains(response, _("Contact formulier niet geconfigureerd.")) @@ -107,9 +115,11 @@ def test_no_form_shown_if_not_has_configuration( def test_anon_form_requires_either_email_or_phonenumber( self, m, mock_captcha, mock_send_confirm ): - config = ESuiteKlantConfig.get_solo() - config.register_email = "example@example.com" + config = KlantenSysteemConfig.get_solo() + config.register_contact_email = "example@example.com" config.save() + + config = ESuiteKlantConfig.get_solo() subject = ContactFormSubjectFactory(config=config) response = self.app.get(self.url) @@ -145,9 +155,12 @@ def test_anon_form_requires_either_email_or_phonenumber( def test_regular_auth_form_fills_email_and_phonenumber( self, m, mock_captcha, mock_send_confirm ): - config = ESuiteKlantConfig.get_solo() - config.register_email = "example@example.com" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_email = "example@example.com" config.save() + + config = ESuiteKlantConfig.get_solo() subject = ContactFormSubjectFactory(config=config) user = UserFactory() @@ -170,9 +183,12 @@ def test_regular_auth_form_fills_email_and_phonenumber( def test_expected_ordered_subjects_are_shown( self, m, mock_captcha, mock_send_confirm ): - config = ESuiteKlantConfig.get_solo() - config.register_email = "example@example.com" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_email = "example@example.com" config.save() + + config = ESuiteKlantConfig.get_solo() subject_1 = ContactFormSubjectFactory(config=config) subject_2 = ContactFormSubjectFactory(config=config) @@ -207,11 +223,13 @@ def test_expected_ordered_subjects_are_shown( mock_send_confirm.assert_not_called() def test_register_contactmoment_via_email(self, m, mock_captcha, mock_send_confirm): - config = ESuiteKlantConfig.get_solo() - config.register_email = "example@example.com" - config.has_form_configuration = True + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_email = "example@example.com" config.save() - subject = ContactFormSubjectFactory(config=config) + + esuite_config = ESuiteKlantConfig.get_solo() + subject = ContactFormSubjectFactory(config=esuite_config) response = self.app.get(self.url) form = response.forms["contactmoment-form"] @@ -252,18 +270,23 @@ def test_register_contactmoment_for_anon_user_via_api( ): MockAPICreateData.setUpServices() - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - config.register_employee_id = "FooVonBar" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True + config.send_email_confirmation = True config.save() + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() + data = MockAPICreateData() data.install_mocks_anon(m) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) @@ -319,20 +342,24 @@ def test_register_contactmoment_for_anon_user_via_api_does_not_send_empty_email_ # we need to patch the captcha Q&A twice because they are re-generated by the form mock_captcha2.return_value = ("", 42) - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - config.register_channel = "contactformulier" - config.register_employee_id = "FooVonBar" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True config.save() + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + esuite_config.register_channel = "contactformulier" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() + MockAPICreateData.setUpServices() data = MockAPICreateData() data.install_mocks_anon_without_klant(m) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) @@ -379,18 +406,23 @@ def test_register_contactmoment_for_bsn_user_via_api( ): MockAPICreateData.setUpServices() - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - config.register_employee_id = "FooVonBar" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True + config.send_email_confirmation = True config.save() + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() + data = MockAPICreateData() data.install_mocks_digid(m) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) @@ -455,19 +487,23 @@ def test_register_contactmoment_for_bsn_user_via_api_without_id( ): MockAPICreateData.setUpServices() - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - # empty id should be excluded from contactmoment_create_data - config.register_employee_id = "" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True config.save() + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + # empty id should be excluded from contactmoment_create_data + esuite_config.register_employee_id = "" + esuite_config.save() + data = MockAPICreateData() data.install_mocks_digid(m) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) @@ -508,13 +544,18 @@ def test_register_contactmoment_for_kvk_or_rsin_user_via_api( ): MockAPICreateData.setUpServices() - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - config.register_employee_id = "FooVonBar" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True + config.send_email_confirmation = True config.save() + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() + for use_rsin_for_innNnpId_query_parameter in [True, False]: with self.subTest( use_rsin_for_innNnpId_query_parameter=use_rsin_for_innNnpId_query_parameter @@ -523,10 +564,10 @@ def test_register_contactmoment_for_kvk_or_rsin_user_via_api( # `m` is overridden somewhere, which causes issues when `MockAPIData.setUpOASMocks` # is run for the second time with requests_mock.Mocker() as m: - config.use_rsin_for_innNnpId_query_parameter = ( + esuite_config.use_rsin_for_innNnpId_query_parameter = ( use_rsin_for_innNnpId_query_parameter ) - config.save() + esuite_config.save() data = MockAPICreateData() data.install_mocks_eherkenning( @@ -534,7 +575,7 @@ def test_register_contactmoment_for_kvk_or_rsin_user_via_api( ) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) @@ -613,18 +654,23 @@ def test_register_contactmoment_for_bsn_user_via_api_and_update_klant( ): MockAPICreateData.setUpServices() - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - config.register_employee_id = "FooVonBar" + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True + config.send_email_confirmation = True config.save() + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() + data = MockAPICreateData() data.install_mocks_digid_missing_contact_info(m) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) @@ -702,8 +748,13 @@ def test_register_contactmoment_for_kvk_or_rsin_user_via_api_and_update_klant( # we need to patch the captcha Q&A twice because they are re-generated by the form mock_captcha2.return_value = ("", 42) + config = KlantenSysteemConfig.get_solo() + config.primary_backend = KlantenServiceType.ESUITE.value + config.register_contact_via_api = True + config.send_email_confirmation = True + config.save() + config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True config.register_bronorganisatie_rsin = "123456789" config.register_type = "Melding" config.register_employee_id = "FooVonBar" @@ -809,25 +860,30 @@ def test_send_email_confirmation_is_configurable( # we need to patch the captcha Q&A twice because they are re-generated by the form mock_captcha2.return_value = ("", 42) - config = ESuiteKlantConfig.get_solo() - config.register_contact_moment = True - config.register_bronorganisatie_rsin = "123456789" - config.register_type = "Melding" - config.register_employee_id = "FooVonBar" - config.save() + klant_config = KlantenSysteemConfig.get_solo() + klant_config.primary_backend = KlantenServiceType.ESUITE.value + klant_config.register_contact_via_api = True + klant_config.save() + + esuite_config = ESuiteKlantConfig.get_solo() + esuite_config.register_contact_moment = True + esuite_config.register_bronorganisatie_rsin = "123456789" + esuite_config.register_type = "Melding" + esuite_config.register_employee_id = "FooVonBar" + esuite_config.save() data = MockAPICreateData() data.install_mocks_anon(m) subject = ContactFormSubjectFactory( - config=config, + config=esuite_config, subject="Aanvraag document", subject_code="afdeling-xyz", ) for send in [True, False]: with self.subTest(send=send): - config.send_email_confirmation = send - config.save() + klant_config.send_email_confirmation = send + klant_config.save() response = self.app.get(self.url) form = response.forms["contactmoment-form"] diff --git a/src/open_inwoner/openklant/views/contactform.py b/src/open_inwoner/openklant/views/contactform.py index 27d9ffbd45..97a89dca22 100644 --- a/src/open_inwoner/openklant/views/contactform.py +++ b/src/open_inwoner/openklant/views/contactform.py @@ -21,8 +21,13 @@ build_contactmomenten_client, build_klanten_client, ) +from open_inwoner.openklant.constants import KlantenServiceType from open_inwoner.openklant.forms import ContactForm -from open_inwoner.openklant.models import ESuiteKlantConfig +from open_inwoner.openklant.models import ( + ESuiteKlantConfig, + KlantenSysteemConfig, + OpenKlant2Config, +) from open_inwoner.openklant.views.utils import generate_question_answer_pair from open_inwoner.openklant.wrap import get_fetch_parameters from open_inwoner.utils.views import CommonPageMixin, LogMixin @@ -87,8 +92,8 @@ def get_initial(self): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - config = ESuiteKlantConfig.get_solo() - context["has_form_configuration"] = config.has_form_configuration() + config = KlantenSysteemConfig.get_solo() + context["has_form_configuration"] = config.has_contactform_configuration return context def set_result_message(self, success: bool): @@ -102,23 +107,19 @@ def set_result_message(self, success: bool): ) def form_valid(self, form: ContactForm): - config = ESuiteKlantConfig.get_solo() + config = KlantenSysteemConfig.get_solo() email_success = False api_success = False send_confirmation = False api_user_email = None - if config.register_email: - email_success = self.register_contactmoment_by_email( - form, config.register_email - ) + if config.register_contact_email: + email_success = self.register_by_email(form, config.register_contact_email) send_confirmation = email_success - if config.register_contact_moment: - api_success, api_user_email = self.register_contactmoment_by_api( - form, config - ) + if config.register_contact_via_api: + api_success, api_user_email = self.register_by_api(form, config) if api_success: send_confirmation = config.send_email_confirmation # else keep the send_confirmation if email set it @@ -138,7 +139,7 @@ def form_valid(self, form: ContactForm): return super().form_valid(form) - def register_contactmoment_by_email(self, form: ContactForm, recipient_email: str): + def register_by_email(self, form: ContactForm, recipient_email: str): template = find_template("contactform_registration") context = { @@ -168,19 +169,30 @@ def register_contactmoment_by_email(self, form: ContactForm, recipient_email: st ) return False - def register_contactmoment_by_api( + def register_by_api(self, form, config: KlantenSysteemConfig): + if config.primary_backend == KlantenServiceType.ESUITE.value: + return self._register_via_esuite( + form, esuite_config=ESuiteKlantConfig.get_solo() + ) + return self._register_via_openklant2(form, config=OpenKlant2Config.get_solo()) + + # TODO + def _register_via_openklant2(self, form: ContactForm, config: OpenKlant2Config): + raise NotImplementedError + + def _register_via_esuite( self, form: ContactForm, - klanten_config: ESuiteKlantConfig, + esuite_config: ESuiteKlantConfig, ) -> tuple[bool, str]: - assert klanten_config.has_api_configuration() + assert esuite_config.has_api_configuration() klant = self._fetch_klant() if klant: self._update_klant_with_form_data(klant, form.cleaned_data) contactmoment = self._create_contactmoment( - form.cleaned_data, klanten_config, klant + form.cleaned_data, esuite_config, klant ) if not contactmoment: