Skip to content

Commit

Permalink
fix: an array of fixes for HL-1053 (#2726)
Browse files Browse the repository at this point in the history
* refactor: rename private function

* fix: issue with setting inspection data with rejected applications

* feat: add possibility to reorder batches differently on each tab

* fix: swap column order for title and name field

* fix: bunch of translation issues

* feat: conditionally load table headers; improve typing; use helper

* feat: useState to update batch count on heading for each tab

* fix: use smaller margin for listing batches
  • Loading branch information
sirtawast authored Jan 16, 2024
1 parent 58376c5 commit 6800c39
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 136 deletions.
114 changes: 65 additions & 49 deletions backend/benefit/applications/api/v1/application_batch_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class ApplicationBatchViewSet(AuditLoggingModelViewSet):
"applications__company_contact_person_email",
]

def get_queryset(self):
order_by = self.request.query_params.get("order_by") or None

if order_by:
self.queryset = self.queryset.order_by(order_by)

return self.queryset

def get_serializer_class(self):
"""
ApplicationBatchSerializer for default behaviour on mutation functions,
Expand All @@ -80,7 +88,7 @@ def get_serializer_class(self):

return ApplicationBatchListSerializer

def get_batch(self, id: str) -> ApplicationBatch:
def _get_batch(self, id: str) -> ApplicationBatch:
"""
Just a wrapper for Django's get_object_or_404 function
"""
Expand All @@ -91,7 +99,7 @@ def destroy(self, request, pk=None):
"""
Override default destroy(), batch can only be deleted if it's status is "draft"
"""
batch = self.get_batch(pk)
batch = self._get_batch(pk)
if batch.status == ApplicationBatchStatus.DRAFT:
batch.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
Expand Down Expand Up @@ -272,7 +280,7 @@ def deassign_applications(self, request, pk=None):
Remove one or more applications from a specific batch
"""
application_ids = request.data.get("application_ids")
batch = self.get_batch(pk)
batch = self._get_batch(pk)

deassign_apps = Application.objects.filter(
batch=batch,
Expand Down Expand Up @@ -302,57 +310,22 @@ def status(self, request, pk=None):
Assign a new status for batch: as pending for Ahjo proposal or switch back to draft
"""
new_status = request.data["status"]
batch = self.get_batch(pk)
if new_status not in [
ApplicationBatchStatus.DRAFT,
ApplicationBatchStatus.AHJO_REPORT_CREATED,
ApplicationBatchStatus.AWAITING_AHJO_DECISION,
ApplicationBatchStatus.DECIDED_ACCEPTED,
ApplicationBatchStatus.DECIDED_REJECTED,
ApplicationBatchStatus.SENT_TO_TALPA,
ApplicationBatchStatus.COMPLETED,
]:

if _unallowed_status_change(new_status):
return Response(status=status.HTTP_400_BAD_REQUEST)

# Patch all required fields after batch inspection
if new_status in [
ApplicationBatchStatus.DECIDED_ACCEPTED,
ApplicationBatchStatus.DECIDED_REJECTED,
]:
for key in request.data:
setattr(batch, key, request.data.get(key))
batch = self._get_batch(pk)
previous_status = batch.status

# Archive all applications if this batch will be completed
if new_status in [
ApplicationBatchStatus.COMPLETED,
]:
Application.objects.filter(batch=batch).update(archived=True)
_save_batch_or_raise(batch, new_status, request.data)

batch.status = new_status
# Decided on rejection, can set status to completed
if new_status == ApplicationBatchStatus.DECIDED_REJECTED:
_save_batch_or_raise(batch, ApplicationBatchStatus.COMPLETED, request.data)

try:
batch.save()
except BatchCompletionDecisionDateError:
return Response(
{"errorKey": "batchInvalidDecisionDate"},
status=status.HTTP_400_BAD_REQUEST,
)
except BatchTooManyDraftsError:
return Response(
{"errorKey": "batchInvalidDraftAlreadyExists"},
status=status.HTTP_400_BAD_REQUEST,
)
except BatchCompletionRequiredFieldsError:
return Response(
{"errorKey": "batchInvalidCompletionRequiredFieldsMissing"},
status=status.HTTP_400_BAD_REQUEST,
)
except ValidationError:
return Response(
status=status.HTTP_400_BAD_REQUEST,
)

previous_status = batch.status
# Archive all applications if this batch will be completed
if batch.status == ApplicationBatchStatus.COMPLETED:
Application.objects.filter(batch=batch).update(archived=True)

return Response(
{
Expand All @@ -363,3 +336,46 @@ def status(self, request, pk=None):
},
status=status.HTTP_200_OK,
)


def _save_batch_or_raise(batch, new_status, data):
# Patch all required fields after batch inspection
if new_status in [
ApplicationBatchStatus.DECIDED_ACCEPTED,
ApplicationBatchStatus.DECIDED_REJECTED,
]:
for key in data:
setattr(batch, key, data.get(key))
try:
batch.status = new_status
batch.save()
except BatchCompletionDecisionDateError:
return Response(
{"errorKey": "batchInvalidDecisionDate"},
status=status.HTTP_400_BAD_REQUEST,
)
except BatchTooManyDraftsError:
return Response(
{"errorKey": "batchInvalidDraftAlreadyExists"},
status=status.HTTP_400_BAD_REQUEST,
)
except BatchCompletionRequiredFieldsError:
return Response(
{"errorKey": "batchInvalidCompletionRequiredFieldsMissing"},
status=status.HTTP_400_BAD_REQUEST,
)
except ValidationError:
return Response(
status=status.HTTP_400_BAD_REQUEST,
)


def _unallowed_status_change(new_status):
return new_status not in [
ApplicationBatchStatus.DRAFT,
ApplicationBatchStatus.AHJO_REPORT_CREATED,
ApplicationBatchStatus.AWAITING_AHJO_DECISION,
ApplicationBatchStatus.DECIDED_ACCEPTED,
ApplicationBatchStatus.DECIDED_REJECTED,
ApplicationBatchStatus.COMPLETED,
]
24 changes: 15 additions & 9 deletions backend/benefit/applications/tests/test_application_batch_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def get_valid_batch_completion_data():
}


def get_valid_rejected_batch_completion_data():
return {
"decision_maker_title": get_faker().job(),
"decision_maker_name": get_faker().name(),
"section_of_the_law": "$1234",
"decision_date": date.today(),
}


def get_valid_p2p_batch_completion_data():
return {
"decision_maker_title": get_faker().job(),
Expand Down Expand Up @@ -270,7 +279,7 @@ def test_deassign_applications_from_batch_all(handler_api_client, application_ba
"batch_status,status_code,changed_status",
[
(ApplicationBatchStatus.COMPLETED, 200, None),
(ApplicationBatchStatus.SENT_TO_TALPA, 200, None),
(ApplicationBatchStatus.SENT_TO_TALPA, 400, None),
(ApplicationBatchStatus.RETURNED, 400, None),
(ApplicationBatchStatus.DECIDED_ACCEPTED, 200, None),
(ApplicationBatchStatus.DECIDED_REJECTED, 200, None),
Expand Down Expand Up @@ -347,15 +356,12 @@ def remove_inspection_data_from_batch(batch):
batch.save()

url = get_batch_detail_url(application_batch, "status/")
payload = get_valid_batch_completion_data()
payload["status"] = batch_status

# With months and a day over the range
payload["decision_date"] = date.today() + relativedelta(
days=delta_days, months=delta_months
payload = (
get_valid_batch_completion_data()
if batch_status == ApplicationBatchStatus.DECIDED_ACCEPTED
else get_valid_rejected_batch_completion_data()
)
response = handler_api_client.patch(url, payload)
assert response.status_code == 400
payload["status"] = batch_status

# With exact months
payload["decision_date"] = date.today() + relativedelta(months=(delta_months))
Expand Down
39 changes: 27 additions & 12 deletions frontend/benefit/handler/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,27 @@
}
},
"statusChange": {
"awaiting_ahjo_decision": "Koonti merkitty Ahjoon viedyksi",
"exported_ahjo_report": "Ahjo-valmistelu aloitettu, koonti lukittu",
"completed": "Koonti käsitelty ja arkistoitu",
"draft": "Koonti palautettu takaisin odottamaan päätösvalmistelua",
"accepted": "Tarkastustiedot tallennettu",
"rejected": "Koonti arkistoitu"
"awaiting_ahjo_decision": {
"heading": "Koonti merkitty Ahjoon viedyksi",
"text": ""
},
"exported_ahjo_report": {
"heading": "Ahjo-valmistelu aloitettu, koonti lukittu",
"text": ""
},
"completed": {
"heading": "Koonti käsitelty ja arkistoitu",
"text": ""
},
"draft": {
"heading": "Koonti palautettu takaisin odottamaan päätösvalmistelua",
"text": ""
},
"accepted": {
"heading": "Hakemuksia siirretty maksuun",
"text": "Olet lähettänyt {{ count }} myönteistä hakemusta maksuun. Hakemukset siirtyvät automaattisesti maksuun tänään klo 23.00."
},
"rejected": { "heading": "Koonti arkistoitu", "text": "" }
},
"errors": {
"batchInvalidDraftAlreadyExists": {
Expand All @@ -1120,10 +1135,10 @@
"markAsReadyForAhjo": "Aloita Ahjo-valmistelu",
"markAsRegisteredToAhjo": "Merkitse Ahjoon viedyksi",
"markedAsRegisteredToAhjo": "Viety Ahjoon",
"markToTalpa": "Hyväksy tarkastustiedot",
"markToTalpa": "Siirrä maksuun",
"markToArchive": "Arkistoi",
"markAsWaitingForAhjo": "Palauta odottamaan Ahjoon vientiä",
"returnToInspection": "Muokkaa tarkastustietoja",
"returnToInspection": "Palauta odottamaan maksuun vientiä",
"downloadFiles": "Lataa liitteet",
"downloadP2PFile": "Lataa Talpa-tiedosto",
"deleteBatch": "Tyhjennä koonti",
Expand Down Expand Up @@ -1151,11 +1166,11 @@
"text": "Oletko varma, että haluat palauttaa koonnin odottamaan Ahjoon vientiä?"
},
"fromInspectionToCompletion": {
"heading": "Hyväksy tarkastustiedot",
"text": "Haluatko merkitä koonnin tarkastetuksi?"
"heading": "Siirrä koonti maksuun",
"text": "Haluatko siirtää koonnin maksuun?"
},
"fromCompletionToInspection": {
"heading": "Palauta koonti aiempaan vaiheeseen",
"heading": "Palauta koonti odottamaan maksuun vientiä",
"text": "Haluatko palata aiempaan vaiheeseen ja tehdä muutoksia asiantarkastajien nimiin?"
},
"fromCompletionToArchive": {
Expand All @@ -1168,7 +1183,7 @@
"decisionMakerName": "Päättäjän nimi",
"decisionMakerTitle": "Päättäjän titteli",
"sectionOfTheLaw": "Pykälä",
"decisionDate": "Päätöksen päivämäärä",
"decisionDate": "Päätöspäivä",
"expertInspectorName": "Asiantarkistajan nimi, Ahjo",
"expertInspectorTitle": "Asiantarkistajan titteli, Ahjo",
"p2pInspectorName": "Tarkastajan nimi, P2P",
Expand Down
39 changes: 27 additions & 12 deletions frontend/benefit/handler/public/locales/fi/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,27 @@
}
},
"statusChange": {
"awaiting_ahjo_decision": "Koonti merkitty Ahjoon viedyksi",
"exported_ahjo_report": "Ahjo-valmistelu aloitettu, koonti lukittu",
"completed": "Koonti käsitelty ja arkistoitu",
"draft": "Koonti palautettu takaisin odottamaan päätösvalmistelua",
"accepted": "Tarkastustiedot tallennettu",
"rejected": "Koonti arkistoitu"
"awaiting_ahjo_decision": {
"heading": "Koonti merkitty odottamaan maksuun vientiä",
"text": ""
},
"exported_ahjo_report": {
"heading": "Ahjo-valmistelu aloitettu, koonti lukittu",
"text": ""
},
"completed": {
"heading": "Koonti käsitelty ja arkistoitu",
"text": ""
},
"draft": {
"heading": "Koonti palautettu takaisin odottamaan päätösvalmistelua",
"text": ""
},
"accepted": {
"heading": "Hakemuksia siirretty maksuun",
"text": "Olet lähettänyt {{ count }} myönteistä hakemusta maksuun. Hakemukset siirtyvät automaattisesti maksuun tänään klo 23.00."
},
"rejected": { "heading": "Koonti arkistoitu", "text": "" }
},
"errors": {
"batchInvalidDraftAlreadyExists": {
Expand All @@ -1120,10 +1135,10 @@
"markAsReadyForAhjo": "Aloita Ahjo-valmistelu",
"markAsRegisteredToAhjo": "Merkitse Ahjoon viedyksi",
"markedAsRegisteredToAhjo": "Viety Ahjoon",
"markToTalpa": "Hyväksy tarkastustiedot",
"markToTalpa": "Siirrä maksuun",
"markToArchive": "Arkistoi",
"markAsWaitingForAhjo": "Palauta odottamaan Ahjoon vientiä",
"returnToInspection": "Muokkaa tarkastustietoja",
"returnToInspection": "Palauta odottamaan maksuun vientiä",
"downloadFiles": "Lataa liitteet",
"downloadP2PFile": "Lataa Talpa-tiedosto",
"deleteBatch": "Tyhjennä koonti",
Expand Down Expand Up @@ -1151,11 +1166,11 @@
"text": "Oletko varma, että haluat palauttaa koonnin odottamaan Ahjoon vientiä?"
},
"fromInspectionToCompletion": {
"heading": "Hyväksy tarkastustiedot",
"text": "Haluatko merkitä koonnin tarkastetuksi?"
"heading": "Siirrä koonti maksuun",
"text": "Haluatko siirtää koonnin maksuun?"
},
"fromCompletionToInspection": {
"heading": "Palauta koonti aiempaan vaiheeseen",
"heading": "Palauta koonti odottamaan maksuun vientiä",
"text": "Haluatko palata aiempaan vaiheeseen ja tehdä muutoksia asiantarkastajien nimiin?"
},
"fromCompletionToArchive": {
Expand All @@ -1168,7 +1183,7 @@
"decisionMakerName": "Päättäjän nimi",
"decisionMakerTitle": "Päättäjän titteli",
"sectionOfTheLaw": "Pykälä",
"decisionDate": "Päätöksen päivämäärä",
"decisionDate": "Päätöspäivä",
"expertInspectorName": "Asiantarkistajan nimi, Ahjo",
"expertInspectorTitle": "Asiantarkistajan titteli, Ahjo",
"p2pInspectorName": "Tarkastajan nimi, P2P",
Expand Down
Loading

0 comments on commit 6800c39

Please sign in to comment.