1
1
import logging
2
- from datetime import datetime
3
- from typing import TypedDict
2
+ from datetime import datetime , timedelta
3
+ from typing import Optional , TypedDict
4
+ from uuid import UUID
4
5
6
+ from django .conf import settings
5
7
from django .contrib .auth .mixins import AccessMixin
6
8
from django .http import Http404
7
9
from django .shortcuts import redirect
12
14
13
15
from view_breadcrumbs import BaseBreadcrumbMixin
14
16
17
+ from open_inwoner .accounts .models import User
15
18
from open_inwoner .openklant .api_models import KlantContactMoment
16
19
from open_inwoner .openklant .clients import build_client
17
20
from open_inwoner .openklant .constants import Status
18
- from open_inwoner .openklant .models import ContactFormSubject
21
+ from open_inwoner .openklant .models import ContactFormSubject , KlantContactMomentLocal
19
22
from open_inwoner .openklant .wrap import (
20
23
fetch_klantcontactmoment ,
21
24
fetch_klantcontactmomenten ,
22
25
get_fetch_parameters ,
23
26
)
24
27
from open_inwoner .openzaak .clients import build_client as build_client_openzaak
25
28
from open_inwoner .utils .mixins import PaginationMixin
29
+ from open_inwoner .utils .time import is_new
26
30
from open_inwoner .utils .views import CommonPageMixin
27
31
28
32
logger = logging .getLogger (__name__ )
29
33
30
34
35
+ # TODO rename to local kcm data?
36
+ def get_kcm_answer_mapping (
37
+ kcms : list [KlantContactMoment ], user : User
38
+ ) -> dict [str , KlantContactMomentLocal ]:
39
+ existing_kcms = KlantContactMomentLocal .objects .filter (user = user ).values_list (
40
+ "kcm_url" , flat = True
41
+ )
42
+ to_create = []
43
+ for kcm in kcms :
44
+ if kcm .url in existing_kcms :
45
+ continue
46
+
47
+ to_create .append (KlantContactMomentLocal (user = user , kcm_url = kcm .url ))
48
+
49
+ KlantContactMomentLocal .objects .bulk_create (to_create )
50
+
51
+ kcm_answer_mapping = {
52
+ kcm_answer .kcm_url : kcm_answer
53
+ for kcm_answer in KlantContactMomentLocal .objects .filter (user = user )
54
+ }
55
+
56
+ return kcm_answer_mapping
57
+
58
+
31
59
class KlantContactMomentAccessMixin (AccessMixin ):
32
60
"""
33
61
Shared authorisation check
@@ -69,12 +97,26 @@ class KCMDict(TypedDict):
69
97
onderwerp : str
70
98
status : str
71
99
antwoord : str
100
+ new_answer_available : bool
72
101
73
102
74
103
class KlantContactMomentBaseView (
75
104
CommonPageMixin , BaseBreadcrumbMixin , KlantContactMomentAccessMixin , TemplateView
76
105
):
77
- def get_kcm_data (self , kcm : KlantContactMoment ) -> KCMDict :
106
+ def get_kcm_data (
107
+ self ,
108
+ kcm : KlantContactMoment ,
109
+ kcm_answer_mapping : Optional [dict [UUID , KlantContactMomentLocal ]] = None ,
110
+ ) -> KCMDict :
111
+ _is_new = is_new (
112
+ kcm .contactmoment ,
113
+ "registratiedatum" ,
114
+ timedelta (days = settings .CONTACTMOMENT_NEW_DAYS ),
115
+ )
116
+ if kcm_answer_mapping :
117
+ is_seen = getattr (kcm_answer_mapping .get (kcm .url ), "is_seen" , False )
118
+ else :
119
+ is_seen = True
78
120
data = {
79
121
"registered_date" : kcm .contactmoment .registratiedatum ,
80
122
"channel" : kcm .contactmoment .kanaal .title (),
@@ -85,6 +127,9 @@ def get_kcm_data(self, kcm: KlantContactMoment) -> KCMDict:
85
127
"type" : kcm .contactmoment .type ,
86
128
"status" : Status .safe_label (kcm .contactmoment .status , _ ("Onbekend" )),
87
129
"antwoord" : kcm .contactmoment .antwoord ,
130
+ "new_answer_available" : bool (kcm .contactmoment .antwoord )
131
+ and _is_new
132
+ and not is_seen ,
88
133
}
89
134
90
135
# replace e_suite_subject_code with OIP configured subject, if applicable
@@ -139,7 +184,12 @@ def get_context_data(self, **kwargs):
139
184
kcms = fetch_klantcontactmomenten (
140
185
** get_fetch_parameters (self .request , use_vestigingsnummer = True )
141
186
)
142
- ctx ["contactmomenten" ] = [self .get_kcm_data (kcm ) for kcm in kcms ]
187
+ ctx ["contactmomenten" ] = [
188
+ self .get_kcm_data (
189
+ kcm , kcm_answer_mapping = get_kcm_answer_mapping (kcms , self .request .user )
190
+ )
191
+ for kcm in kcms
192
+ ]
143
193
paginator_dict = self .paginate_with_context (ctx ["contactmomenten" ])
144
194
ctx .update (paginator_dict )
145
195
return ctx
@@ -172,6 +222,13 @@ def get_context_data(self, **kwargs):
172
222
if not kcm :
173
223
raise Http404 ()
174
224
225
+ local_kcm , is_created = KlantContactMomentLocal .objects .get_or_create ( # noqa
226
+ user = self .request .user , kcm_url = kcm .url
227
+ )
228
+ if not local_kcm .is_seen :
229
+ local_kcm .is_seen = True
230
+ local_kcm .save ()
231
+
175
232
if client := build_client ("contactmomenten" ):
176
233
zaken_client = build_client_openzaak ("zaak" )
177
234
ocm = client .retrieve_objectcontactmoment (
0 commit comments