Skip to content

Commit

Permalink
Use a more generic Question object for eSuite VragenService
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Oct 30, 2024
1 parent d7af03e commit 033e7bd
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 130 deletions.
86 changes: 45 additions & 41 deletions src/open_inwoner/accounts/views/contactmoments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from dataclasses import dataclass
from datetime import datetime
from typing import Iterable, Protocol, TypedDict
from typing import Iterable, Protocol

from django.contrib.auth.mixins import AccessMixin
from django.http import Http404, HttpResponseRedirect
Expand All @@ -13,6 +13,8 @@
from django.views.generic import TemplateView

from glom import glom
from pydantic import TypeAdapter
from typing_extensions import TypedDict
from view_breadcrumbs import BaseBreadcrumbMixin

from open_inwoner.accounts.models import User
Expand Down Expand Up @@ -80,33 +82,35 @@ class ZaakWithApiGroup:
api_group: ZGWApiGroupConfig


class KCMDict(TypedDict):
class Question(TypedDict):
identification: str
subject: str
registered_date: datetime
channel: str
text: str
url: str
identificatie: str
type: str
onderwerp: str
status: str
antwoord: str
question_text: str
answer_text: str | None

new_answer_available: bool
case_detail_url: str
channel: str


QuestionValidator = TypeAdapter(Question)


class VragenService(Protocol):
def list_questions(
self,
fetch_params: FetchParameters,
user: User,
) -> Iterable[KCMDict]: # noqa: E704
) -> Iterable[Question]: # noqa: E704
...

def retrieve_question(
self,
fetch_params: FetchParameters,
question_uuid: str,
user: User,
) -> tuple[KCMDict | None, ZaakWithApiGroup | None]: # noqa: E704
) -> tuple[Question | None, ZaakWithApiGroup | None]: # noqa: E704
...


Expand Down Expand Up @@ -162,33 +166,31 @@ def get_kcm_answer_mapping(
) -> dict[str, KlantContactMomentAnswer]:
return get_kcm_answer_mapping(contactmomenten, user)

def _get_kcm_data(
def _get_question_data(
self,
kcm: KlantContactMoment,
local_kcm_mapping: dict[str, KlantContactMomentAnswer] | None = None,
) -> KCMDict:
) -> Question:

if isinstance(kcm.contactmoment, str):
raise ValueError
raise ValueError("Received unresolved contactmoment")

data: KCMDict = {
"registered_date": kcm.contactmoment.registratiedatum,
"channel": kcm.contactmoment.kanaal.title(),
"text": kcm.contactmoment.tekst,
"url": reverse("cases:contactmoment_detail", kwargs={"kcm_uuid": kcm.uuid}),
# "case_url": None,
"onderwerp": self._get_kcm_subject(kcm) or "",
# eSuite extra
"identificatie": kcm.contactmoment.identificatie,
"type": kcm.contactmoment.type,
"status": Status.safe_label(kcm.contactmoment.status, _("Onbekend")),
"antwoord": kcm.contactmoment.antwoord,
return {
"answer_text": kcm.contactmoment.antwoord,
"identification": kcm.contactmoment.identificatie,
"question_text": kcm.contactmoment.tekst,
"new_answer_available": self.contactmoment_has_new_answer(
kcm.contactmoment, local_kcm_mapping=local_kcm_mapping
),
"subject": self._get_kcm_subject(kcm) or "",
"registered_date": kcm.contactmoment.registratiedatum,
"status": str(Status.safe_label(kcm.contactmoment.status, _("Onbekend"))),
"case_detail_url": reverse(
"cases:contactmoment_detail", kwargs={"kcm_uuid": kcm.uuid}
),
"channel": kcm.contactmoment.kanaal.title(),
}

return data

def fetch_klantcontactmomenten(
self,
user_bsn: str | None = None,
Expand All @@ -210,7 +212,7 @@ def fetch_klantcontactmoment(

def list_questions(
self, fetch_params: FetchParameters, user: User
) -> Iterable[KCMDict]:
) -> Iterable[Question]:
kcms = self.fetch_klantcontactmomenten(**fetch_params)

klant_config = OpenKlantConfig.get_solo()
Expand All @@ -222,7 +224,7 @@ def list_questions(
]

contactmomenten = [
self._get_kcm_data(
self._get_question_data(
kcm,
local_kcm_mapping=self.get_kcm_answer_mapping(
[kcm.contactmoment for kcm in kcms], user
Expand All @@ -234,7 +236,7 @@ def list_questions(

def retrieve_question(
self, fetch_params: FetchParameters, question_uuid: str, user: User
) -> tuple[KCMDict | None, ZaakWithApiGroup | None]:
) -> tuple[Question | None, ZaakWithApiGroup | None]:
if not (kcm := self.fetch_klantcontactmoment(question_uuid, **fetch_params)):
return None, None

Expand Down Expand Up @@ -270,9 +272,7 @@ def retrieve_question(
# We should at least receive a ping if this happens.
logger.error("Found one zaak in multiple backends")

data = self._get_kcm_data(kcm)

return data, zaak_with_api_group
return self._get_question_data(kcm), zaak_with_api_group


class KlantContactMomentBaseView(
Expand Down Expand Up @@ -323,11 +323,13 @@ def get_anchors(self) -> list:
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
service = self.get_service()
ctx["contactmomenten"] = service.list_questions(
self.fetch_params, user=self.request.user
)
questions = service.list_questions(self.fetch_params, user=self.request.user)
ctx["contactmomenten"] = [
QuestionValidator.validate_python(q) for q in questions
]
paginator_dict = self.paginate_with_context(ctx["contactmomenten"])
ctx.update(paginator_dict)

return ctx


Expand Down Expand Up @@ -357,14 +359,16 @@ def get_context_data(self, **kwargs):
if not kcm:
raise Http404()

QuestionValidator.validate_python(kcm)

local_kcm, created = KlantContactMomentAnswer.objects.get_or_create( # noqa
user=self.request.user, contactmoment_url=kcm["url"]
user=self.request.user, contactmoment_url=kcm["case_detail_url"]
)
if not local_kcm.is_seen:
local_kcm.is_seen = True
local_kcm.save()

contactmoment: KCMDict = kcm
contactmoment = kcm
ctx["contactmoment"] = contactmoment
ctx["zaak"] = zaak.zaak if zaak else None
case_url = (
Expand All @@ -389,7 +393,7 @@ def get_context_data(self, **kwargs):
},
{
"label": _("Vraag nummer: "),
"value": contactmoment["identificatie"],
"value": contactmoment["identification"],
},
{
"label": _("Contact gehad via: "),
Expand Down
Loading

0 comments on commit 033e7bd

Please sign in to comment.