|
| 1 | +from django.core.exceptions import ValidationError |
| 2 | + |
| 3 | +from django_setup_configuration.configuration import BaseConfigurationStep |
| 4 | +from django_setup_configuration.exceptions import ConfigurationRunFailed |
| 5 | +from django_setup_configuration.fields import DjangoModelRef |
| 6 | +from django_setup_configuration.models import ConfigurationModel |
| 7 | +from zgw_consumers.constants import APITypes |
| 8 | +from zgw_consumers.models import Service |
| 9 | + |
| 10 | +from open_inwoner.configurations.bootstrap.utils import get_service |
| 11 | +from open_inwoner.openklant.models import OpenKlantConfig |
| 12 | + |
| 13 | + |
| 14 | +class KlantenApiConfigurationModel(ConfigurationModel): |
| 15 | + klanten_service_identifier: str |
| 16 | + contactmomenten_service_identifier: str |
| 17 | + exclude_contactmoment_kanalen: list[str] | None = DjangoModelRef( |
| 18 | + OpenKlantConfig, |
| 19 | + "exclude_contactmoment_kanalen", |
| 20 | + default=None, |
| 21 | + ) |
| 22 | + |
| 23 | + class Meta: |
| 24 | + django_model_refs = { |
| 25 | + OpenKlantConfig: ( |
| 26 | + "register_email", |
| 27 | + "register_contact_moment", |
| 28 | + "register_bronorganisatie_rsin", |
| 29 | + "register_channel", |
| 30 | + "register_type", |
| 31 | + "register_employee_id", |
| 32 | + "use_rsin_for_innNnpId_query_parameter", |
| 33 | + "send_email_confirmation", |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +class OpenKlantConfigurationStep(BaseConfigurationStep[KlantenApiConfigurationModel]): |
| 39 | + """ |
| 40 | + Configure the KIC settings and set any feature flags or other options if specified |
| 41 | + """ |
| 42 | + |
| 43 | + verbose_name = "Klantinteractie APIs configuration" |
| 44 | + enable_setting = "openklant_config_enable" |
| 45 | + namespace = "openklant_config" |
| 46 | + config_model = KlantenApiConfigurationModel |
| 47 | + |
| 48 | + def execute(self, model: KlantenApiConfigurationModel): |
| 49 | + config = OpenKlantConfig.get_solo() |
| 50 | + |
| 51 | + try: |
| 52 | + kc = get_service(model.klanten_service_identifier) |
| 53 | + if kc.api_type != APITypes.kc: |
| 54 | + raise ConfigurationRunFailed( |
| 55 | + f"Found klanten service with identifier `{kc.slug}`, but expected" |
| 56 | + f" `api_type` to equal `{APITypes.kc}` and got `{kc.api_type}`" |
| 57 | + ) |
| 58 | + config.klanten_service = kc |
| 59 | + |
| 60 | + cm = get_service(model.contactmomenten_service_identifier) |
| 61 | + if cm.api_type != APITypes.cmc: |
| 62 | + raise ConfigurationRunFailed( |
| 63 | + f"Found contactmomenten service with identifier `{cm.slug}`, but" |
| 64 | + f" expected `api_type` to equal `{APITypes.cmc}` and got " |
| 65 | + f"`{cm.api_type}`" |
| 66 | + ) |
| 67 | + |
| 68 | + config.contactmomenten_service = cm |
| 69 | + except Service.DoesNotExist as exc: |
| 70 | + raise ConfigurationRunFailed( |
| 71 | + "Unable to retrieve `klanten_service` and/or `contactmomenten_service`" |
| 72 | + ". Try first configuring the `zgw_consumers` configuration steps, and." |
| 73 | + " ensure that both the `identifier` and `api_type` fields match." |
| 74 | + ) from exc |
| 75 | + |
| 76 | + for key, val in model.model_dump( |
| 77 | + exclude={ |
| 78 | + "klanten_service_identifier", |
| 79 | + "contactmomenten_service_identifier", |
| 80 | + } |
| 81 | + ).items(): |
| 82 | + setattr(config, key, val) |
| 83 | + |
| 84 | + try: |
| 85 | + config.full_clean() |
| 86 | + config.save() |
| 87 | + except ValidationError as exc: |
| 88 | + raise ConfigurationRunFailed( |
| 89 | + "Unable to validate and save configuration" |
| 90 | + ) from exc |
0 commit comments