From 816ba7627e792a579bc84cd168314c50e0fbadeb Mon Sep 17 00:00:00 2001 From: Dan Jacob Date: Tue, 6 Feb 2024 08:57:29 +0200 Subject: [PATCH] Add resolver to return the extended price list Refs: PV-779 --- parking_permits/models/parking_permit.py | 11 +++++-- parking_permits/resolvers.py | 17 ++++++++++ parking_permits/schema/parking_permit.graphql | 10 ++++++ parking_permits/tests/test_resolvers.py | 31 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/parking_permits/models/parking_permit.py b/parking_permits/models/parking_permit.py index 3a994626..5837632f 100644 --- a/parking_permits/models/parking_permit.py +++ b/parking_permits/models/parking_permit.py @@ -19,6 +19,7 @@ from ..constants import ParkingPermitEndType from ..exceptions import ParkkihubiPermitError, PermitCanNotBeEnded from ..utils import ( + calc_net_price, calc_vat_price, diff_months_ceil, end_date_to_datetime, @@ -451,10 +452,13 @@ def get_price_list_for_extended_permit(self, month_count): Each item consists of: - "product": product - "price": gross price + "product": product instance "start_date": start of period "end_date": end of period + "vat": VAT rate + "price": gross price + "net_price": net price + "vat_price": VAT price """ # these are unchanged @@ -485,7 +489,10 @@ def get_price_list_for_extended_permit(self, month_count): price_list.append( { "product": product, + "vat": product.vat, "price": price, + "net_price": calc_net_price(price, product.vat), + "vat_price": calc_vat_price(price, product.vat), "start_date": month_start_date, "end_date": month_start_date + relativedelta(months=1, days=-1), } diff --git a/parking_permits/resolvers.py b/parking_permits/resolvers.py index 361db5fc..4c08e075 100644 --- a/parking_permits/resolvers.py +++ b/parking_permits/resolvers.py @@ -256,6 +256,23 @@ def resolve_get_update_address_price_changes(_obj, info, address_id): return permit_price_changes +@query.field("getExtendedPriceList") +@is_authenticated +@convert_kwargs_to_snake_case +def resolve_get_extended_permit_price_list(_obj, info, permit_id, month_count): + """Returns the updated price list for additional months on a fixed period permit.""" + + customer = info.context["request"].user.customer + try: + permit = ( + ParkingPermit.objects.active().filter(customer=customer).get(pk=permit_id) + ) + except ParkingPermit.DoesNotExist: + raise ObjectNotFound(_("Permit not found")) + + return permit.get_price_list_for_extended_permit(month_count) + + @mutation.field("deleteParkingPermit") @is_authenticated @convert_kwargs_to_snake_case diff --git a/parking_permits/schema/parking_permit.graphql b/parking_permits/schema/parking_permit.graphql index b35bc766..e39474ba 100644 --- a/parking_permits/schema/parking_permit.graphql +++ b/parking_permits/schema/parking_permit.graphql @@ -119,10 +119,20 @@ type PermitPriceChangeResult { priceChanges: [PermitPriceChangeItem]! } +type PermitExtendedPriceResult { + startDate: String! + endDate: String! + vat: Float! + price: Float! + netPrice: Float! + vatPrice: Float! +} + type Query { profile: CustomerNode! getPermits: [PermitNode] getUpdateAddressPriceChanges(addressId: ID!): [PermitPriceChangeResult] + getExtendedPriceList(permitId: ID!, monthCount: Int): [PermitExtendedPriceResult] } enum PermitEndType { diff --git a/parking_permits/tests/test_resolvers.py b/parking_permits/tests/test_resolvers.py index 0b46c573..aa4f24a6 100644 --- a/parking_permits/tests/test_resolvers.py +++ b/parking_permits/tests/test_resolvers.py @@ -14,6 +14,7 @@ from parking_permits.resolvers import ( resolve_change_address, resolve_extend_parking_permit, + resolve_get_extended_permit_price_list, resolve_update_permit_vehicle, ) from parking_permits.tests.factories.address import AddressFactory @@ -384,6 +385,36 @@ def test_resolve_change_address_no_change_to_parking_zone(rf): assert Order.objects.count() == 0 +@pytest.mark.django_db() +def test_resolve_get_extended_permit_price_list(rf): + request = rf.post("/") + customer = CustomerFactory() + + now = timezone.now() + permit = ParkingPermitFactory( + customer=customer, + status=ParkingPermitStatus.VALID, + contract_type=ContractType.FIXED_PERIOD, + start_time=now, + end_time=now + timedelta(days=10), + ) + + ProductFactory( + zone=permit.parking_zone, + type=ProductType.RESIDENT, + start_date=(now - timedelta(days=360)).date(), + end_date=(now + timedelta(days=360)).date(), + ) + request.user = customer.user + + info = Info(context={"request": request}) + + with _mock_jwt(request.user): + response = resolve_get_extended_permit_price_list(None, info, str(permit.pk), 3) + + assert len(response) == 3 + + @pytest.mark.django_db() def test_resolve_extend_parking_permit_ok(rf): request = rf.post("/")