|
1 | 1 | import logging
|
| 2 | +from datetime import timedelta |
2 | 3 | from typing import List, Optional
|
3 | 4 |
|
| 5 | +from django.conf import settings |
| 6 | + |
4 | 7 | from open_inwoner.accounts.models import User
|
5 | 8 | from open_inwoner.kvk.branches import get_kvk_branch_number
|
6 |
| -from open_inwoner.openklant.api_models import KlantContactMoment |
| 9 | +from open_inwoner.openklant.api_models import ContactMoment, KlantContactMoment |
7 | 10 | from open_inwoner.openklant.clients import build_client
|
8 | 11 | from open_inwoner.openklant.models import KlantContactMomentLocal, OpenKlantConfig
|
| 12 | +from open_inwoner.utils.time import instance_is_new |
9 | 13 |
|
10 | 14 | logger = logging.getLogger(__name__)
|
11 | 15 |
|
@@ -99,28 +103,40 @@ def get_fetch_parameters(request, use_vestigingsnummer: bool = False) -> dict:
|
99 | 103 |
|
100 | 104 |
|
101 | 105 | def get_local_kcm_mapping(
|
102 |
| - kcms: list[KlantContactMoment], user: User |
| 106 | + contactmomenten: list[ContactMoment], |
| 107 | + user: User, |
103 | 108 | ) -> dict[str, KlantContactMomentLocal]:
|
| 109 | + to_create = [] |
104 | 110 | existing_kcms = set(
|
105 | 111 | KlantContactMomentLocal.objects.filter(user=user).values_list(
|
106 |
| - "klantcontactmoment_url", flat=True |
| 112 | + "contactmoment_url", flat=True |
107 | 113 | )
|
108 | 114 | )
|
109 |
| - |
110 |
| - to_create = [] |
111 |
| - for kcm in kcms: |
112 |
| - if kcm.url in existing_kcms: |
| 115 | + for contactmoment in contactmomenten: |
| 116 | + if contactmoment.url in existing_kcms: |
113 | 117 | continue
|
114 | 118 |
|
115 | 119 | to_create.append(
|
116 |
| - KlantContactMomentLocal(user=user, klantcontactmoment_url=kcm.url) |
| 120 | + KlantContactMomentLocal(user=user, contactmoment_url=contactmoment.url) |
117 | 121 | )
|
118 | 122 |
|
119 | 123 | KlantContactMomentLocal.objects.bulk_create(to_create)
|
120 | 124 |
|
121 | 125 | kcm_answer_mapping = {
|
122 |
| - kcm_answer.klantcontactmoment_url: kcm_answer |
| 126 | + kcm_answer.contactmoment_url: kcm_answer |
123 | 127 | for kcm_answer in KlantContactMomentLocal.objects.filter(user=user)
|
124 | 128 | }
|
125 | 129 |
|
126 | 130 | return kcm_answer_mapping
|
| 131 | + |
| 132 | + |
| 133 | +def contactmoment_has_new_answer( |
| 134 | + contactmoment: ContactMoment, local_kcm_mapping: dict[str, KlantContactMomentLocal] |
| 135 | +) -> bool: |
| 136 | + is_new = instance_is_new( |
| 137 | + contactmoment, |
| 138 | + "registratiedatum", |
| 139 | + timedelta(days=settings.CONTACTMOMENT_NEW_DAYS), |
| 140 | + ) |
| 141 | + is_seen = getattr(local_kcm_mapping.get(contactmoment.url), "is_seen", False) |
| 142 | + return bool(contactmoment.antwoord) and is_new and not is_seen |
0 commit comments