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 cdc794a commit 0fb6313
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 85 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 = "[email protected]"
self.klant_config.register_contact_moment = False
self.esuite_config.klanten_service = None
self.esuite_config.save()

self.klant_config.register_contact_email = "[email protected]"
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 = "[email protected]"
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 = "[email protected]"
self.klant_config.register_contact_moment = False
self.klant_config.register_contact_email = "[email protected]"
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 = "[email protected]"
self.klant_config.register_contact_email = "[email protected]"
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
32 changes: 26 additions & 6 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 All @@ -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):
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
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def test_configure(self):
self.assertEqual(config.klanten_service, kc)
self.assertEqual(config.contactmomenten_service, cmc)

self.assertEqual(config.register_email, "[email protected]")
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")
Expand Down Expand Up @@ -149,8 +147,8 @@ def assert_values():
self.assertEqual(config.klanten_service, kc)
self.assertEqual(config.contactmomenten_service, cmc)

self.assertEqual(config.register_email, "[email protected]")
self.assertEqual(config.register_contact_moment, True)
# self.assertEqual(config.register_email, "[email protected]")
# 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")
Expand All @@ -164,8 +162,8 @@ def assert_values():
assert_values()

config = ESuiteKlantConfig.get_solo()
config.register_email = "[email protected]"
config.register_contact_moment = False
# config.register_email = "[email protected]"
# config.register_contact_moment = False
config.register_bronorganisatie_rsin = "800000009"
config.register_channel = "not-email"
config.register_type = "not-bericht"
Expand Down
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",
]
},
),
]
Loading

0 comments on commit 0fb6313

Please sign in to comment.