|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from django.http import HttpResponse |
| 5 | +from graphene.test import Client |
| 6 | +from graphene import Schema |
| 7 | +# from graphqlclient import GraphQLClient |
| 8 | +from insuree.schema import Query, Mutation |
| 9 | +from .insureequery import * |
| 10 | +from core.schema import Query as core_query, Mutation as core_mutation |
| 11 | +from insuree.models import Insuree |
| 12 | + |
| 13 | + |
| 14 | +def get_client(schema, query, mutation): |
| 15 | + return Client(schema=schema(query=query, mutation=mutation)) |
| 16 | + |
| 17 | +def get_context(request): |
| 18 | + context = QueryTest.BaseTestContext() |
| 19 | + context.user = None |
| 20 | + print('Request user: ', request.user) |
| 21 | + if request.user.is_authenticated: |
| 22 | + context.user = request.user |
| 23 | + else: |
| 24 | + context = QueryTest.AnonymousUserContext() |
| 25 | + print('Context user: ', context.user) |
| 26 | + return context |
| 27 | + |
| 28 | + |
| 29 | +def get_update_registry_query(uuid="", chf_id="", update_fields="") -> str: |
| 30 | + query = f''' |
| 31 | + mutation {{ |
| 32 | + updateInsuree( |
| 33 | + input: {{ |
| 34 | + clientMutationId: "552f8e55-ed5a-4e1e-a159-ea8f8cec0560" |
| 35 | + clientMutationLabel: "Update insuree" |
| 36 | + uuid: "{uuid}" |
| 37 | + chfId: "{chf_id}" |
| 38 | + {update_fields} |
| 39 | + genderId: "F" |
| 40 | + head: true |
| 41 | + dob: "1974-06-11" |
| 42 | + cardIssued:false |
| 43 | + familyId: 1 |
| 44 | + relationshipId: 4 |
| 45 | + }} |
| 46 | + ) {{ |
| 47 | + clientMutationId |
| 48 | + internalId |
| 49 | + }} |
| 50 | + }} |
| 51 | + ''' |
| 52 | + return query |
| 53 | + |
| 54 | + |
| 55 | +def get_insurees_query(variable_values: str = "", fetched_fields: str = "") -> str: |
| 56 | + return f''' |
| 57 | + query GetInsurees {{ |
| 58 | + insurees({variable_values}) {{ |
| 59 | + edges{{ |
| 60 | + node{{ |
| 61 | + {fetched_fields} |
| 62 | + }} |
| 63 | + }} |
| 64 | + }} |
| 65 | + }} |
| 66 | + ''' |
| 67 | + |
| 68 | + |
| 69 | +def create_insurees_query(variables: dict) -> str: |
| 70 | + return f''' |
| 71 | + mutation {{ |
| 72 | + createInsuree( |
| 73 | + input: {{ |
| 74 | + clientMutationLabel: "{variables['clientMutationLabel']}" |
| 75 | + chfId: "{variables['chfId']}" |
| 76 | + lastName: "{variables['lastName']}" |
| 77 | + otherNames: "{variables['otherNames']}" |
| 78 | + genderId: "{variables['genderId']}" |
| 79 | + dob: "{variables['dob']}" |
| 80 | + head: {str(variables['head']).lower()} |
| 81 | + cardIssued: {str(variables['cardIssued']).lower()} |
| 82 | + jsonExt: "{variables['jsonExt']}" |
| 83 | + familyId: 1 |
| 84 | + }} |
| 85 | + ) {{ |
| 86 | + clientMutationId |
| 87 | + internalId |
| 88 | + }} |
| 89 | + }} |
| 90 | + ''' |
| 91 | + |
| 92 | +def delete_insuree_query(uuid): |
| 93 | + return f'''mutation |
| 94 | + {{ |
| 95 | + deleteInsurees( |
| 96 | + input: {{ |
| 97 | + clientMutationId: "c164412c-45a6-4f3f-8a2b-4290739751e2" |
| 98 | + clientMutationLabel: "Delete insuree" |
| 99 | +
|
| 100 | + uuid: "{uuid}", uuids: ["{uuid}"] |
| 101 | + }} |
| 102 | + ) {{ |
| 103 | + clientMutationId |
| 104 | + internalId |
| 105 | + }} |
| 106 | + }} |
| 107 | + ''' |
| 108 | + |
| 109 | + |
| 110 | +def get_query_content_values(query_content: dict) -> dict: |
| 111 | + content_values = {} |
| 112 | + if not query_content: |
| 113 | + return {} |
| 114 | + content_values['chfId'] = query_content.get('ID', "") |
| 115 | + content_values['FirstName'] = query_content.get('FirstName', "") |
| 116 | + content_values['LastName'] = query_content.get('LastName', "") |
| 117 | + content_values['BirthCertificateID'] = query_content.get('BirthCertificateID', "") |
| 118 | + return content_values |
| 119 | + |
| 120 | + |
| 121 | +def get_query_write_values(query_write: dict) -> dict: |
| 122 | + write_values = {} |
| 123 | + if not query_write: |
| 124 | + return {} |
| 125 | + write_values['chfId'] = query_write.get('ID', "") |
| 126 | + write_values['FirstName'] = query_write.get('FirstName', "") |
| 127 | + write_values['LastName'] = query_write.get('LastName', "") |
| 128 | + write_values['BirthCertificateID'] = query_write.get('BirthCertificateID', "") |
| 129 | + return write_values |
| 130 | + |
| 131 | + |
| 132 | +def get_values_for_insurees(content_values: dict) -> dict: |
| 133 | + return { |
| 134 | + 'clientMutationLabel': f"Create insuree - {content_values['chfId']}", |
| 135 | + 'chfId': f"{content_values['chfId']}", |
| 136 | + 'lastName': f"{content_values['LastName']}", |
| 137 | + 'otherNames': f"{content_values['FirstName']}", |
| 138 | + 'genderId': 'M', |
| 139 | + 'dob': '2000-06-20', |
| 140 | + 'head': True, |
| 141 | + 'cardIssued': False, |
| 142 | + 'jsonExt': '{}', |
| 143 | + } |
| 144 | + |
| 145 | +def get_search_insurees_arguments(query_content: dict) -> str: |
| 146 | + insurees_arguments = "" |
| 147 | + if 'ID' in query_content: |
| 148 | + insurees_arguments += f'chfId: "{query_content["ID"]}",' |
| 149 | + elif 'chfId' in query_content: |
| 150 | + insurees_arguments += f'chfId: "{query_content["chfId"]}",' |
| 151 | + if 'FirstName' in query_content: |
| 152 | + insurees_arguments += f'otherNames: "{query_content["FirstName"]}",' |
| 153 | + if 'LastName' in query_content: |
| 154 | + insurees_arguments += f'lastName: "{query_content["LastName"]}",' |
| 155 | + # if 'BirthCertificateID' in query_content: |
| 156 | + # variable_values += ": $" |
| 157 | + |
| 158 | + if insurees_arguments.endswith(','): |
| 159 | + return insurees_arguments[:-1] |
| 160 | + return insurees_arguments |
| 161 | + |
| 162 | +def get_update_fields(write_values) -> str: |
| 163 | + field_mapping = { |
| 164 | + "LastName": "lastName", |
| 165 | + "FirstName": "otherNames" |
| 166 | + } |
| 167 | + return "".join(f'{field_mapping[key]}: "{value}" ' |
| 168 | + for key, value in write_values.items() |
| 169 | + if value and key in field_mapping) |
| 170 | + |
| 171 | + |
| 172 | +# def login(request): |
| 173 | +# client = GraphQLClient('http://localhost:8000/graphql') # Zastąp to swoim adresem URL |
| 174 | +# mutation = ''' |
| 175 | +# mutation { |
| 176 | +# tokenAuth(username: "Admin", password: "admin123") { |
| 177 | +# token |
| 178 | +# refreshExpiresIn |
| 179 | +# } |
| 180 | +# } |
| 181 | +# ''' |
| 182 | +# result = client.execute(mutation) |
| 183 | +# result_data = json.loads(result) |
| 184 | +# if 'errors' in result_data: |
| 185 | +# # Obsłuż błędy logowania tutaj |
| 186 | +# pass |
| 187 | +# jwt_token = result_data['data']['tokenAuth']['token'] |
| 188 | +# request.session['jwt_token'] = jwt_token # Zapisz token JWT w sesji |
| 189 | +# return HttpResponse('Logged in.') |
| 190 | + |
| 191 | + |
| 192 | +def login_with_env_variables(request): |
| 193 | + client = get_client(Schema, core_query, core_mutation) |
| 194 | + username = os.getenv('login_openIMIS') |
| 195 | + password = os.getenv('password_openIMIS') |
| 196 | + |
| 197 | + mutation = f''' |
| 198 | + mutation {{ |
| 199 | + tokenAuth(username: "{username}", password: "{password}") {{ |
| 200 | + token |
| 201 | + refreshExpiresIn |
| 202 | + }} |
| 203 | + }} |
| 204 | + ''' |
| 205 | + context = get_context(request) |
| 206 | + client.execute(mutation, context=context) |
| 207 | + return context |
0 commit comments