From 20aceaa2e682a8128df32653adce22a1fc3c26f7 Mon Sep 17 00:00:00 2001 From: delcroip Date: Thu, 7 Mar 2024 18:18:22 +0100 Subject: [PATCH 1/2] fix inquire product --- policy/services.py | 3 ++- policy/tests_gql.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/policy/services.py b/policy/services.py index 9f052f2..5c387e7 100644 --- a/policy/services.py +++ b/policy/services.py @@ -653,7 +653,7 @@ def get_eligibility(self, insuree, item_or_service, model, req, now): if min_date_qs["min_date_lte"] else min_date_qs["min_date_all"]) - if queryset_item_or_service.filter(min_date__lte=now).filter(left__isnull=True).first(): + if queryset_item_or_service.filter(min_date__lte=now).filter(left__isnull=True).order_by('-validity_from').first(): items_or_services_left = None else: items_or_services_left = queryset_item_or_service\ @@ -740,6 +740,7 @@ def get_total_filter(category): filter=get_total_filter(Service.CATEGORY_VISIT), distinct=True), 0)) \ .annotate(total_visits_left=F("policy__product__max_no_visits") - F("total_visits")) \ + .order_by('-expiry_date')\ .first() if result is None: diff --git a/policy/tests_gql.py b/policy/tests_gql.py index 01c06a0..cf28d3d 100644 --- a/policy/tests_gql.py +++ b/policy/tests_gql.py @@ -1,9 +1,11 @@ import base64 import json from dataclasses import dataclass +from core.utils import filter_validity from core.models import User from core.test_helpers import create_test_interactive_user from django.conf import settings +from medical.models import Service from graphene_django.utils.testing import GraphQLTestCase from graphql_jwt.shortcuts import get_token #credits https://docs.graphene-python.org/projects/django/en/latest/testing/ @@ -86,3 +88,25 @@ def test_query_with_variables(self): self.assertResponseNoErrors(response) + + def test_insuree_policy_query(self): + + response = self.query( + f''' +{{ + policyServiceEligibilityByInsuree(chfId:"070707070", serviceCode:"{Service.objects.filter(*filter_validity()).order_by('id').first().code}") + {{ + minDateService, serviceLeft, isServiceOk + }} +}} + ''', + headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}, + ) + + content = json.loads(response.content) + + # This validates the status code and if you get errors + self.assertResponseNoErrors(response) + + # Add some more asserts if you like + ... From 44ed271f44bd214f9ff50eae802e712dbe8a8408 Mon Sep 17 00:00:00 2001 From: delcroip Date: Thu, 7 Mar 2024 18:18:31 +0100 Subject: [PATCH 2/2] refactor --- policy/services.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/policy/services.py b/policy/services.py index 5c387e7..c9eccf0 100644 --- a/policy/services.py +++ b/policy/services.py @@ -319,7 +319,7 @@ def build_query(self, req): res.query.group_by = ['id'] if not req.show_history: - res = res.filter(*core.filter_validity()) + res = res.filter(*core.filter_validity(validity = req.target_date if req.target_date else None)) if req.active_or_last_expired_only: # sort on status, so that any active policy (status = 2) pops up... res = res.annotate(not_null_expiry_date=Coalesce('expiry_date', py_date.max)) \ @@ -333,25 +333,17 @@ def __init__(self, user): super(ByInsureeService, self).__init__(user) def request(self, by_insuree_request): - insurees = Insuree.objects.filter( - chf_id=by_insuree_request.chf_id, - *core.filter_validity() if not by_insuree_request.show_history else [] - ) res = self.build_query(by_insuree_request) - res = res.prefetch_related('insuree_policies') - res = res.filter(insuree_policies__insuree__in=insurees) - # .distinct('product__code') >> DISTINCT ON fields not supported by MS-SQL - if by_insuree_request.target_date: - res = get_queryset_valid_at_date(res, by_insuree_request.target_date) + res = res.filter(insuree_policies__insuree__chf_id=by_insuree_request.chf_id) if by_insuree_request.active_or_last_expired_only: products = {} - for r in res: - if r.product.code not in products.keys(): - products[r.product.code] = r + for policy in res: + if policy.status == Policy.STATUS_IDLE or policy.status == Policy.STATUS_READY: + products['policy.product.code-%s' % policy.uuid] = policy + elif policy.product.code not in products.keys(): + products[policy.product.code] = policy res = products.values() - items = tuple( - map(lambda x: FilteredPoliciesService._to_item(x), res) - ) + items = [FilteredPoliciesService._to_item(x) for x in res] # possible improvement: sort via the ORM # ... but beware of the active_or_last_expired_only filtering! order_attr = to_snake_case(by_insuree_request.order_by if by_insuree_request.order_by else "expiry_date") @@ -395,9 +387,8 @@ def __init__(self, user): super(ByFamilyService, self).__init__(user) def request(self, by_family_request): - family = Family.objects.get(uuid=by_family_request.family_uuid) res = self.build_query(by_family_request) - res = res.filter(family_id=family.id) + res = res.filter(family_uuid=by_family_request.family_uuid) # .distinct('product__code') >> DISTINCT ON fields not supported by MS-SQL if by_family_request.active_or_last_expired_only: products = {}