Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1437 #1457] Enhanced contact-form to create/update Klant, or fallback to appending contact info #664

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/open_inwoner/openklant/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.forms import Form
from django.utils.translation import gettext_lazy as _

from open_inwoner.accounts.models import User
from open_inwoner.openklant.models import ContactFormSubject, OpenKlantConfig
from open_inwoner.utils.validators import validate_phone_number

Expand Down Expand Up @@ -44,21 +45,47 @@ class ContactForm(Form):
required=True,
)

def __init__(self, *args, **kwargs):
user: User

def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user

config = OpenKlantConfig.get_solo()
self.fields["subject"].queryset = config.contactformsubject_set.order_by(
"subject"
)

if self.user.is_authenticated:
del self.fields["first_name"]
del self.fields["last_name"]
del self.fields["infix"]
if self.user.email:
del self.fields["email"]
if self.user.phonenumber:
del self.fields["phonenumber"]

def clean(self, *args, **kwargs):
cleaned_data = super().clean(*args, **kwargs)

email = cleaned_data["email"]
phonenumber = cleaned_data["phonenumber"]
email = cleaned_data.get("email", "")
phonenumber = cleaned_data.get("phonenumber", "")

if not email and not phonenumber:
if ("email" in self.fields and not email) and (
"phonenumber" in self.fields and not phonenumber
):
msg = _("Vul een e-mailadres of telefoonnummer in.")
self.add_error("email", msg)
self.add_error("phonenumber", msg)

if self.user.is_authenticated:
if not email and self.user.email:
cleaned_data["email"] = self.user.email
if not phonenumber and self.user.phonenumber:
cleaned_data["phonenumber"] = self.user.phonenumber

cleaned_data["first_name"] = self.user.first_name
cleaned_data["infix"] = self.user.infix
cleaned_data["last_name"] = self.user.last_name

return cleaned_data
8 changes: 8 additions & 0 deletions src/open_inwoner/openklant/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from zgw_consumers.constants import APITypes


class OpenKlantConfigManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
return qs.select_related("klanten_service", "contactmomenten_service")


class OpenKlantConfig(SingletonModel):
"""
Global configuration and defaults for Klant & Contactmomenten APIs
Expand Down Expand Up @@ -67,6 +73,8 @@ class OpenKlantConfig(SingletonModel):
"register_employee_id",
)

objects = OpenKlantConfigManager()

class Meta:
verbose_name = _("Open Klant configuration")

Expand Down
51 changes: 50 additions & 1 deletion src/open_inwoner/openklant/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ def __init__(self):
emailadres="[email protected]",
telefoonnummer="0612345678",
)
self.klant_no_contact_info = generate_oas_component(
"kc",
"schemas/Klant",
uuid="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
url=f"{KLANTEN_ROOT}klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
bronorganisatie="123456789",
voornaam="Foo",
achternaam="Bar",
emailadres="",
telefoonnummer="",
)
self.contactmoment = generate_oas_component(
"cmc",
"schemas/ContactMoment",
Expand All @@ -123,7 +134,7 @@ def __init__(self):

self.matchers = []

def install_mocks_anon(self, m) -> "MockAPICreateData":
def install_mocks_anon_with_klant(self, m) -> "MockAPICreateData":
self.setUpOASMocks(m)

self.matchers = [
Expand All @@ -141,6 +152,19 @@ def install_mocks_anon(self, m) -> "MockAPICreateData":
]
return self

def install_mocks_anon_without_klant(self, m) -> "MockAPICreateData":
self.setUpOASMocks(m)

self.matchers = [
m.post(f"{KLANTEN_ROOT}klanten", json=self.klant, status_code=500),
m.post(
f"{CONTACTMOMENTEN_ROOT}contactmomenten",
json=self.contactmoment,
status_code=201,
),
]
return self

def install_mocks_digid(self, m) -> "MockAPICreateData":
self.setUpOASMocks(m)

Expand All @@ -161,3 +185,28 @@ def install_mocks_digid(self, m) -> "MockAPICreateData":
),
]
return self

def install_mocks_digid_missing_contact_info(self, m) -> "MockAPICreateData":
self.setUpOASMocks(m)
self.matchers = [
m.get(
f"{KLANTEN_ROOT}klanten?subjectNatuurlijkPersoon__inpBsn={self.user.bsn}",
json=paginated_response([self.klant_no_contact_info]),
),
m.patch(
f"{KLANTEN_ROOT}klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
json=self.klant,
status_code=200,
),
m.post(
f"{CONTACTMOMENTEN_ROOT}contactmomenten",
json=self.contactmoment,
status_code=201,
),
m.post(
f"{CONTACTMOMENTEN_ROOT}klantcontactmomenten",
json=self.klant_contactmoment,
status_code=201,
),
]
return self
Loading