Skip to content

Commit

Permalink
feat: implement the existing alteration list and alteration deletion …
Browse files Browse the repository at this point in the history
…for the applicant (HL-1154) (#2925)

* feat: add list of existing alterations to the decision summary

* feat: allow deletion of unopened applications for applicants in BE

* fix: adjust alteration page styling

* fix: format fields for handled alteration

* feat: add alteration deletion modal

* fix: use correct comparison for dates with existing alterations

* chore: update alteration backend translations

* chore: simplify alteration sorting
  • Loading branch information
EmiliaMakelaVincit committed Apr 11, 2024
1 parent ef38ce4 commit aea007f
Show file tree
Hide file tree
Showing 22 changed files with 794 additions and 147 deletions.
25 changes: 24 additions & 1 deletion backend/benefit/applications/api/v1/application_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from drf_spectacular.utils import extend_schema
from rest_framework import filters as drf_filters, status
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.parsers import MultiPartParser
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
Expand All @@ -34,6 +35,7 @@
)
from applications.api.v1.serializers.attachment import AttachmentSerializer
from applications.enums import (
ApplicationAlterationState,
ApplicationBatchStatus,
ApplicationOrigin,
ApplicationStatus,
Expand Down Expand Up @@ -373,7 +375,7 @@ def get_application_template(self, request, pk=None):
class ApplicationAlterationViewSet(AuditLoggingModelViewSet):
serializer_class = ApplicationAlterationSerializer
queryset = ApplicationAlteration.objects.all()
http_method_names = ["post", "patch", "head"]
http_method_names = ["post", "patch", "head", "delete"]

APPLICANT_UNEDITABLE_FIELDS = [
"state",
Expand Down Expand Up @@ -405,6 +407,27 @@ def create(self, request, *args, **kwargs):
def update(self, request, *args, **kwargs):
return super().update(self._prune_fields(request), *args, **kwargs)

def destroy(self, request, *args, **kwargs):
# Only the applicant can delete an alteration, and only if it hasn't yet been
# opened by a handler.

alteration = self.get_object()

if not settings.NEXT_PUBLIC_MOCK_FLAG:
company = get_company_from_request(request)
if company != alteration.application.company:
raise PermissionDenied(_("You are not allowed to do this action"))

if request.user.is_handler():
raise PermissionDenied(_("You are not allowed to do this action"))

if alteration.state != ApplicationAlterationState.RECEIVED:
raise PermissionDenied(
_("You cannot delete the change to employment in this state")
)

return super().destroy(request, *args, **kwargs)


@extend_schema(
description=(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,35 @@ def _validate_date_range_within_application_date_range(
errors = []
if start_date < application.start_date:
errors.append(
ValidationError(_("Alteration cannot start before first benefit day"))
ValidationError(
_(
"The change to employment cannot start before the start date of the benefit period"
)
)
)
if start_date > application.end_date:
errors.append(
ValidationError(_("Alteration cannot start after last benefit day"))
ValidationError(
_(
"The change to employment cannot start after the end date of the benefit period"
)
)
)
if end_date is not None:
if end_date > application.end_date:
errors.append(
ValidationError(_("Alteration cannot end after last benefit day"))
ValidationError(
_(
"The end date of the change period cannot be after the end date of the benefit period"
)
)
)
if start_date > end_date:
errors.append(
ValidationError(
_("Alteration end date cannot be before start date")
_(
"The end date of the change period cannot be before the start date of the same period"
)
)
)

Expand All @@ -108,7 +122,9 @@ def _validate_date_range_overlaps(self, application, start_date, end_date):

errors.append(
ValidationError(
_("Another alteration already overlaps the alteration period")
_(
"Another change to employment has already been reported for the same period"
)
)
)

Expand Down Expand Up @@ -139,7 +155,7 @@ def validate(self, data):

if current_state not in allowed_states:
raise PermissionDenied(
_("The alteration cannot be edited in this state")
_("You cannot edit the change to employment in this state")
)

# Verify that any fields that are required based on another field are filled
Expand Down
75 changes: 75 additions & 0 deletions backend/benefit/applications/tests/test_applications_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,81 @@ def test_application_alteration_patch_allowed_edit_states_applicant(
assert response.status_code == result


def test_application_alteration_patch_forbidden_another_company(
api_client, application
):
another_company = CompanyFactory()
application.company = another_company
application.save()

alteration = _create_application_alteration(application)

response = api_client.patch(
reverse("v1:application-alteration-detail", kwargs={"pk": alteration.pk}),
{
"end_date": application.start_date + relativedelta(days=12),
},
)
assert response.status_code == 403


@pytest.mark.parametrize(
"initial_state,result",
[
(ApplicationAlterationState.RECEIVED, 204),
(ApplicationAlterationState.OPENED, 403),
(ApplicationAlterationState.HANDLED, 403),
],
)
def test_application_alteration_allowed_delete_states_applicant(
api_client, application, initial_state, result
):
alteration = _create_application_alteration(application)
alteration.state = initial_state
alteration.save()

response = api_client.delete(
reverse("v1:application-alteration-detail", kwargs={"pk": alteration.pk})
)
assert response.status_code == result


@pytest.mark.parametrize(
"initial_state,result",
[
(ApplicationAlterationState.RECEIVED, 403),
(ApplicationAlterationState.OPENED, 403),
(ApplicationAlterationState.HANDLED, 403),
],
)
def test_application_alteration_delete_handler(
handler_api_client, application, initial_state, result
):
alteration = _create_application_alteration(application)
alteration.state = initial_state
alteration.save()

response = handler_api_client.delete(
reverse("v1:application-alteration-detail", kwargs={"pk": alteration.pk})
)
assert response.status_code == result


def test_application_alteration_delete_forbidden_another_company(
api_client, application
):
another_company = CompanyFactory()
application.company = another_company
application.save()

alteration = _create_application_alteration(application)

response = api_client.delete(
reverse("v1:application-alteration-detail", kwargs={"pk": alteration.pk})
)
assert response.status_code == 403


def _create_random_applications():
f = faker.Faker()
combos = [
Expand Down
32 changes: 22 additions & 10 deletions backend/benefit/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-09 11:28+0300\n"
"POT-Creation-Date: 2024-04-10 09:41+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -44,6 +44,12 @@ msgstr ""
msgid "File not found."
msgstr ""

msgid "You are not allowed to do this action"
msgstr ""

msgid "You cannot delete the change to employment in this state"
msgstr ""

#, python-brace-format
msgid "Helsinki-lisan hakemukset viety {date}"
msgstr ""
Expand Down Expand Up @@ -165,25 +171,31 @@ msgstr ""
msgid "{field} must be filled if using an e-invoice address"
msgstr ""

msgid "Alteration cannot start before first benefit day"
msgstr ""

msgid "Alteration cannot start after last benefit day"
msgid ""
"The change to employment cannot start before the start date of the benefit "
"period"
msgstr ""

msgid "Alteration cannot end after last benefit day"
msgid ""
"The change to employment cannot start after the end date of the benefit "
"period"
msgstr ""

msgid "Alteration end date cannot be before start date"
msgid ""
"The end date of the change period cannot be after the end date of the "
"benefit period"
msgstr ""

msgid "Another alteration already overlaps the alteration period"
msgid ""
"The end date of the change period cannot be before the start date of the "
"same period"
msgstr ""

msgid "You are not allowed to do this action"
msgid ""
"Another change to employment has already been reported for the same period"
msgstr ""

msgid "The alteration cannot be edited in this state"
msgid "You cannot edit the change to employment in this state"
msgstr ""

#, python-brace-format
Expand Down
39 changes: 25 additions & 14 deletions backend/benefit/locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-09 11:28+0300\n"
"POT-Creation-Date: 2024-04-10 09:41+0300\n"
"PO-Revision-Date: 2022-11-01 10:45+0200\n"
"Last-Translator: Kari Salminen <kari.salminen@anders.com>\n"
"Language-Team: \n"
Expand Down Expand Up @@ -44,6 +44,12 @@ msgstr "Toiminto ei ole sallittu tässä hakemuksen tilassa."
msgid "File not found."
msgstr "Tiedostoa ei löydy."

msgid "You are not allowed to do this action"
msgstr "Sinä et saa tehdä tätä toimenpidettä"

msgid "You cannot delete the change to employment in this state"
msgstr "Muutosta työsuhteessa ei voida poistaa tässä tilassa"

#, python-brace-format
msgid "Helsinki-lisan hakemukset viety {date}"
msgstr ""
Expand Down Expand Up @@ -172,27 +178,32 @@ msgstr "Verkkolaskuosoite"
msgid "{field} must be filled if using an e-invoice address"
msgstr "{field} on pakollinen tieto, mikäli lasku lähetetään verkkolaskuna"

msgid "Alteration cannot start before first benefit day"
msgid ""
"The change to employment cannot start before the start date of the benefit "
"period"
msgstr "Muutos työsuhteessa ei voi alkaa ennen tukijakson alkua"

msgid "Alteration cannot start after last benefit day"
msgid ""
"The change to employment cannot start after the end date of the benefit "
"period"
msgstr "Muutos työsuhteessa ei voi alkaa tukijakson päättymisen jälkeen"

msgid "Alteration cannot end after last benefit day"
msgstr ""
"Muutosjakson päättymispäivämäärä ei voi olla tukijakson päättymisen jälkeen"
msgid ""
"The end date of the change period cannot be after the end date of the "
"benefit period"
msgstr "Muutosjakson päättymispäivämäärä ei voi olla tukijakson päättymisen jälkeen"

msgid "Alteration end date cannot be before start date"
msgstr "Muutosjakson päättymispäivä ei voi olla aiemmin kuin aloituspäivä"
msgid ""
"The end date of the change period cannot be before the start date of the "
"same period"
msgstr "Muutosjakson päättymispäivä ei voi olla ennen sen aloituspäivää"

msgid "Another alteration already overlaps the alteration period"
msgid ""
"Another change to employment has already been reported for the same period"
msgstr "Valitulla aikavälillä on jo toinen muutos työsuhteessa"

msgid "You are not allowed to do this action"
msgstr "Sinä et saa tehdä tätä toimenpidettä"

msgid "The alteration cannot be edited in this state"
msgstr "Muutosta työsuhteessa ei voida muuttaa tässä tilassa"
msgid "You cannot edit the change to employment in this state"
msgstr "Muutosta työsuhteessa ei voida muokata tässä tilassa"

#, python-brace-format
msgid "Upload file size cannot be greater than {size} bytes"
Expand Down
Loading

0 comments on commit aea007f

Please sign in to comment.