From 832bfe1cc24e343a04676dd7f065cab977c33b17 Mon Sep 17 00:00:00 2001 From: akash Date: Wed, 20 Apr 2022 19:08:12 +0530 Subject: [PATCH] fix: Show ABHA card of existing patient on creation and linking --- healthcare/regional/india/abdm/js/patient.js | 88 ++++++++++++++------ healthcare/regional/india/abdm/utils.py | 54 +++++++++--- 2 files changed, 103 insertions(+), 39 deletions(-) diff --git a/healthcare/regional/india/abdm/js/patient.js b/healthcare/regional/india/abdm/js/patient.js index 751aa4606b..4e27b42a22 100644 --- a/healthcare/regional/india/abdm/js/patient.js +++ b/healthcare/regional/india/abdm/js/patient.js @@ -148,6 +148,11 @@ let verify_health_id = function (frm, recieved_abha_number = '') { fieldtype: 'Small Text', hidden: 1 }, + { + fieldname: 'abha_card', + fieldtype: 'Attach', + hidden: 1 + }, { fieldname: 'response_message', fieldtype: 'HTML', @@ -159,15 +164,23 @@ let verify_health_id = function (frm, recieved_abha_number = '') { d.get_primary_btn().attr('disabled', true); $(d.fields_dict['response_message'].wrapper).empty(); frappe.run_serially([ - () =>frappe.db.get_value('Patient', {abha_number: d.get_value('healthid'), name: ['!=', frm.doc.name] }, 'name') + () =>frappe.db.get_value('Patient', {abha_number: d.get_value('healthid'), name: ['!=', frm.doc.name] }, ['name', 'abha_card']) .then(r =>{ if (r.message.name) { frappe.set_route("Form", "Patient", r.message.name); - frappe.throw({ - message: __(`Patient with ABHA number ${d.get_value('healthid')} already exists {0}`, - ['' + r.message.name + '']), - title: __("Patient already exist") - }); + if (r.message.abha_card) { + frappe.throw({ + message: __(`{0}`, + [""]), + title: __("Patient already exist") + }); + } else { + frappe.throw({ + message: __(`{0}`, + ['' + r.message.name + '']), + title: __("Patient already exist") + }); + } } }), () => {show_message(d, 'Sending Auth OTP...', 'black', '') @@ -260,20 +273,30 @@ let verify_auth_otp = function(r, d) { freeze_message: __(`

Verifying OTP...
Please note, this may take a while`), callback: function (data) { - if (data.message && data.message['healthIdNumber']) { - d.get_primary_btn().attr('hidden', true); - set_qr_scanned_data(d, data.message) - d.set_values({ - 'scanned_data': JSON.stringify(data.message) - }); - } else { - if (data.message && data.message.details[0]['message']) { - show_message(d, data.message.message, 'red', data.message.details[0]['message']) + if (data.message) { + if (data.message[0] && data.message[0]['healthIdNumber']) { + d.get_primary_btn().attr('hidden', true); + if (!data.message[1] && !data.message[1]['file_url']) { + set_qr_scanned_data(d, data.message[0]) + } + d.set_values({ + 'scanned_data': JSON.stringify(data.message[0]) + }); + } else { + if (data.message[0].details[0]['message']) { + show_message(d, data.message[0].message, 'red', data.message[0].details[0]['message']) + } + frappe.show_alert({ + message:__('Failed to fetch health Data, Please try again later'), + indicator:'red' + }, 10); + } + if (data.message[1]) { + $(d.fields_dict.qr_data.$wrapper).html("") + d.set_values({ + 'abha_card': data.message[1] + }); } - frappe.show_alert({ - message:__('Failed to fetch health Data, Please try again later'), - indicator:'red' - }, 10); } } }); @@ -375,8 +398,9 @@ let set_qr_scanned_data = function(d, scanned_data) { if (scanned_data['dob']) { dob = scanned_data['dob'] } else { - dob = scanned_data['dayOfBirth'] || '-' +'/' + scanned_data['monthOfBirth'] || '-'+'/'+ - scanned_data['yearOfBirth'] || '-' + dob = `${scanned_data['dayOfBirth'] ? scanned_data['dayOfBirth'] : '-'} - + ${scanned_data['monthOfBirth'] ? scanned_data['monthOfBirth'] : '-'} - + ${scanned_data['yearOfBirth']}`; } let qr_table = $(` @@ -443,6 +467,9 @@ let set_data_to_form = function(frm, scanned_data, dialog, d) { frm.set_value('consent_for_aadhaar_use', d.get_value('patient_consent_attach')) } } + if (dialog.get_value('abha_card')) { + frm.set_value('abha_card', dialog.get_value('abha_card')) + } } } @@ -630,16 +657,23 @@ let create_abha_with_aadhaar = function(frm, d) { dialog.hide() frappe.run_serially([ () =>frappe.db.get_value('Patient', {abha_number: data.message['healthIdNumber'], - name: ['!=', frm.doc.name] }, 'name') + name: ['!=', frm.doc.name] }, ['name', 'abha_card']) .then(r =>{ if (r.message.name) { frappe.set_route("Form", "Patient", r.message.name); - frappe.throw({ - message: __(`Patient with ABHA number - ${data.message['healthIdNumber']} already exists {0}`, + if (r.message.abha_card) { + frappe.throw({ + message: __(`{0}`, + [""]), + title: __("Patient already exist") + }); + } else { + frappe.throw({ + message: __(`{0}`, ['' + r.message.name + '']), - title: __("Patient already exist") - }); + title: __("Patient already exist") + }); + } } }), () => { diff --git a/healthcare/regional/india/abdm/utils.py b/healthcare/regional/india/abdm/utils.py index 5e346e3e86..60d8c084b1 100644 --- a/healthcare/regional/india/abdm/utils.py +++ b/healthcare/regional/india/abdm/utils.py @@ -182,7 +182,7 @@ def get_encrypted_message(message): pub_key = pub_key.replace('\n', '').replace( '-----BEGIN PUBLIC KEY-----', '').replace('-----END PUBLIC KEY-----', '') if pub_key: - encrypted_msg = rsa_encryption(message, pub_key) + encrypted_msg = get_rsa_encrypted_message(message, pub_key) req.response = encrypted_msg req.status = 'Granted' req.insert(ignore_permissions=True) @@ -202,7 +202,7 @@ def get_encrypted_message(message): return None -def rsa_encryption(message, pub_key): +def get_rsa_encrypted_message(message, pub_key): # TODO:- Use cryptography from Crypto.Cipher import PKCS1_v1_5 from Crypto.PublicKey import RSA @@ -230,21 +230,51 @@ def get_health_data(otp, txnId, auth_method): url_key = 'confirm_w_mobile_otp' # returns X-Token response = abdm_request(confirm_w_otp_payload, url_key, 'Health ID', '', 'otp') + abha_url = '' if response and response.get('token'): + abha_url = get_abha_card(response['token']) header = { "X-Token": 'Bearer ' + response['token'] } response = abdm_request('', 'get_acc_info', 'Health ID', header, '') - return response + return response, abha_url +# patient after_insert def set_consent_attachment_details(doc, method=None): - if doc.consent_for_aadhaar_use: - file_name = frappe.db.get_value('File', { - 'file_url': doc.consent_for_aadhaar_use - }, 'name') - frappe.db.set_value('File', file_name, { - 'attached_to_doctype': 'Patient', - 'attached_to_name': doc.name, - 'attached_to_field': doc.consent_for_aadhaar_use - }) \ No newline at end of file + if frappe.db.exists( + "ABDM Integration", + {"company": frappe.defaults.get_user_default("Company"), "default": 1}, + ): + if doc.consent_for_aadhaar_use: + file_name = frappe.db.get_value( + "File", {"file_url": doc.consent_for_aadhaar_use}, "name" + ) + if file_name: + frappe.db.set_value("File", file_name, + { + "attached_to_doctype": "Patient", + "attached_to_name": doc.name, + "attached_to_field": doc.consent_for_aadhaar_use, + }, + ) + if doc.abha_card: + abha_file_name = frappe.db.get_value( + "File", {"file_url": doc.abha_card, "attached_to_name": None}, "name" + ) + if abha_file_name: + frappe.db.set_value("File", abha_file_name, + { + "attached_to_doctype": "Patient", + "attached_to_name": doc.name, + "attached_to_field": doc.abha_card, + }, + ) + + +def get_abha_card(token): + header = { + "X-Token": 'Bearer ' + token + } + response = abdm_request('', 'get_card', 'Health ID', header, '') + return response.get('file_url')