Skip to content

Commit

Permalink
Merge pull request #2311 from uktrade/LTD-5673-goods-ratings-endpoint
Browse files Browse the repository at this point in the history
LTD-5673: Productionise goods ratings endpoint
  • Loading branch information
saruniitr authored Nov 29, 2024
2 parents 63c3b5a + 666e139 commit 6a2824f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 78 deletions.
12 changes: 12 additions & 0 deletions api/data_workspace/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from api.cases.enums import LicenceDecisionType
from api.cases.models import LicenceDecision
from api.licences.models import GoodOnLicence
from api.staticdata.control_list_entries.models import ControlListEntry
from api.staticdata.countries.models import Country
from api.staticdata.report_summaries.models import ReportSummary

Expand Down Expand Up @@ -139,3 +140,14 @@ def get_first_closed_at(self, application) -> typing.Optional[datetime.datetime]
return application.baseapplication_ptr.case_ptr.closed_status_updates[0].created_at

return None


class AssessmentSerializer(serializers.ModelSerializer):
good_id = serializers.UUIDField()

class Meta:
model = ControlListEntry
fields = (
"good_id",
"rating",
)
70 changes: 70 additions & 0 deletions api/data_workspace/v2/tests/bdd/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
from api.parties.tests.factories import PartyDocumentFactory
from api.organisations.tests.factories import OrganisationFactory
from api.staticdata.letter_layouts.models import LetterLayout
from api.staticdata.report_summaries.models import (
ReportSummaryPrefix,
ReportSummarySubject,
)
from api.staticdata.statuses.enums import CaseStatusEnum
from api.staticdata.statuses.models import CaseStatus
from api.staticdata.units.enums import Units
Expand Down Expand Up @@ -368,9 +372,75 @@ def check_rows(client, parse_table, unpage_data, table_name, rows):
for row in parsed_rows[1:]:
expected_data.append({key: value for key, value in zip(keys, row)})
expected_data = cast_to_types(expected_data, table_metadata["fields"])

actual_data = sorted(actual_data, key=lambda d, key=keys[0]: d[key])
expected_data = sorted(expected_data, key=lambda d, key=keys[0]: d[key])
assert actual_data == expected_data


@given(parsers.parse("the application has the following goods:{goods}"))
def given_the_application_has_the_following_goods(parse_table, draft_standard_application, goods):
draft_standard_application.goods.all().delete()
good_attributes = parse_table(goods)[1:]
for id, name in good_attributes:
GoodOnApplicationFactory(
application=draft_standard_application,
id=id,
good__name=name,
)


@when(parsers.parse("the goods are assessed by TAU as:{assessments}"))
def when_the_goods_are_assessed_by_tau(
parse_table,
submitted_standard_application,
assessments,
api_client,
lu_case_officer,
gov_headers,
):
assessments = parse_table(assessments)[1:]
url = reverse("assessments:make_assessments", kwargs={"case_pk": submitted_standard_application.pk})

assessment_payload = []
for good_on_application_id, control_list_entry, report_summary_prefix, report_summary_subject in assessments:
data = {
"id": good_on_application_id,
"comment": "Some comment",
}

if control_list_entry == "NLR":
data.update(
{
"control_list_entries": [],
"is_good_controlled": False,
}
)
else:
if report_summary_prefix:
prefix = ReportSummaryPrefix.objects.get(name=report_summary_prefix)
else:
prefix = None
subject = ReportSummarySubject.objects.get(name=report_summary_subject)
data.update(
{
"control_list_entries": [control_list_entry],
"report_summary_prefix": prefix.pk if prefix else None,
"report_summary_subject": subject.pk,
"is_good_controlled": True,
"regime_entries": [],
}
)
assessment_payload.append(data)

response = api_client.put(
url,
assessment_payload,
**gov_headers,
)
assert response.status_code == 200, response.content


@pytest.fixture()
def parse_attributes(parse_table):
def _parse_attributes(attributes):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ Scenario: Assess application
| 4dad5dc6-38ef-4bf7-99fd-0c6bc5d86048 | NLR | | |
Then the `goods_descriptions` table has the following rows:
| good_id | description |
| 118a003c-7191-4a2c-97e9-be243722cbb2 | composite laminates |
| 8fa8dc3c-c103-42f5-ba94-2d9098b8821d | accessories for composite laminates |
| 118a003c-7191-4a2c-97e9-be243722cbb2 | composite laminates |
33 changes: 33 additions & 0 deletions api/data_workspace/v2/tests/bdd/scenarios/goods_ratings.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@db
Feature: goods_ratings Table

Scenario: Draft application
Given a draft standard application
Then the `goods_ratings` table is empty

Scenario: Submitted application
Given a draft standard application
And the application has the following goods:
| id | name |
| 8fa8dc3c-c103-42f5-ba94-2d9098b8821d | A controlled good |
| 4dad5dc6-38ef-4bf7-99fd-0c6bc5d86048 | An NLR good |
When the application is submitted
Then the `goods_ratings` table is empty

Scenario: Assess application
Given a draft standard application
And the application has the following goods:
| id | name |
| 8fa8dc3c-c103-42f5-ba94-2d9098b8821d | A controlled good |
| 118a003c-7191-4a2c-97e9-be243722cbb2 | Another controlled good |
| 4dad5dc6-38ef-4bf7-99fd-0c6bc5d86048 | An NLR good |
When the application is submitted
And the goods are assessed by TAU as:
| id | Control list entry | Report summary prefix | Report summary subject |
| 8fa8dc3c-c103-42f5-ba94-2d9098b8821d | ML22a | accessories for | composite laminates |
| 118a003c-7191-4a2c-97e9-be243722cbb2 | PL9010 | | composite laminates |
| 4dad5dc6-38ef-4bf7-99fd-0c6bc5d86048 | NLR | | |
Then the `goods_ratings` table has the following rows:
| good_id | rating |
| 8fa8dc3c-c103-42f5-ba94-2d9098b8821d | ML22a |
| 118a003c-7191-4a2c-97e9-be243722cbb2 | PL9010 |
78 changes: 1 addition & 77 deletions api/data_workspace/v2/tests/bdd/test_goods_descriptions.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,4 @@
from pytest_bdd import (
given,
parsers,
scenarios,
when,
)

from django.urls import reverse

from api.applications.tests.factories import GoodOnApplicationFactory
from api.staticdata.report_summaries.models import (
ReportSummaryPrefix,
ReportSummarySubject,
)
from pytest_bdd import scenarios


scenarios("./scenarios/goods_descriptions.feature")


@given(parsers.parse("the application has the following goods:{goods}"))
def given_the_application_has_the_following_goods(parse_table, draft_standard_application, goods):
draft_standard_application.goods.all().delete()
good_attributes = parse_table(goods)[1:]
for id, name in good_attributes:
GoodOnApplicationFactory(
application=draft_standard_application,
id=id,
good__name=name,
)


@when(parsers.parse("the goods are assessed by TAU as:{assessments}"))
def when_the_goods_are_assessed_by_tau(
parse_table,
submitted_standard_application,
assessments,
api_client,
lu_case_officer,
gov_headers,
):
assessments = parse_table(assessments)[1:]
url = reverse("assessments:make_assessments", kwargs={"case_pk": submitted_standard_application.pk})

assessment_payload = []
for good_on_application_id, control_list_entry, report_summary_prefix, report_summary_subject in assessments:
data = {
"id": good_on_application_id,
"comment": "Some comment",
}

if control_list_entry == "NLR":
data.update(
{
"control_list_entries": [],
"is_good_controlled": False,
}
)
else:
if report_summary_prefix:
prefix = ReportSummaryPrefix.objects.get(name=report_summary_prefix)
else:
prefix = None
subject = ReportSummarySubject.objects.get(name=report_summary_subject)
data.update(
{
"control_list_entries": [control_list_entry],
"report_summary_prefix": prefix.pk if prefix else None,
"report_summary_subject": subject.pk,
"is_good_controlled": True,
"regime_entries": [],
}
)
assessment_payload.append(data)

response = api_client.put(
url,
assessment_payload,
**gov_headers,
)
assert response.status_code == 200, response.content
4 changes: 4 additions & 0 deletions api/data_workspace/v2/tests/bdd/test_goods_ratings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pytest_bdd import scenarios


scenarios("./scenarios/goods_ratings.feature")
1 change: 1 addition & 0 deletions api/data_workspace/v2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
router_v2.register(views.GoodDescriptionViewSet)
router_v2.register(views.GoodOnLicenceViewSet)
router_v2.register(views.ApplicationViewSet)
router_v2.register(views.AssessmentViewSet)
18 changes: 18 additions & 0 deletions api/data_workspace/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from api.core.helpers import str_to_bool
from api.data_workspace.v2.serializers import (
ApplicationSerializer,
AssessmentSerializer,
CountrySerializer,
DestinationSerializer,
GoodDescriptionSerializer,
Expand All @@ -38,6 +39,7 @@
)
from api.licences.enums import LicenceStatus
from api.licences.models import GoodOnLicence
from api.staticdata.control_list_entries.models import ControlListEntry
from api.staticdata.countries.models import Country
from api.staticdata.report_summaries.models import ReportSummary
from api.staticdata.statuses.enums import CaseStatusEnum
Expand Down Expand Up @@ -155,3 +157,19 @@ class ApplicationViewSet(BaseViewSet):

class DataWorkspace:
table_name = "applications"


class AssessmentViewSet(BaseViewSet):
serializer_class = AssessmentSerializer

def get_queryset(self):
return (
ControlListEntry.objects.annotate(
good_id=F("goodonapplication__id"),
)
.exclude(good_id__isnull=True)
.order_by("rating")
)

class DataWorkspace:
table_name = "goods_ratings"

0 comments on commit 6a2824f

Please sign in to comment.