Skip to content

Commit

Permalink
fix: Show ABHA card of existing patient on creation and linking
Browse files Browse the repository at this point in the history
  • Loading branch information
akashkrishna619 authored and akashkrishna619 committed Aug 25, 2022
1 parent 9f8f5c6 commit 832bfe1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 39 deletions.
88 changes: 61 additions & 27 deletions healthcare/regional/india/abdm/js/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 <b>${d.get_value('healthid')}</b> already exists {0}`,
['<a href="/app/patient/'+r.message.name+'">' + r.message.name + '</a>']),
title: __("Patient already exist")
});
if (r.message.abha_card) {
frappe.throw({
message: __(`{0}`,
["<img src='"+ r.message.abha_card + "'>"]),
title: __("Patient already exist")
});
} else {
frappe.throw({
message: __(`{0}`,
['<a href="/app/patient/'+r.message.name+'">' + r.message.name + '</a>']),
title: __("Patient already exist")
});
}
}
}),
() => {show_message(d, 'Sending Auth OTP...', 'black', '')
Expand Down Expand Up @@ -260,20 +273,30 @@ let verify_auth_otp = function(r, d) {
freeze_message: __(`<br><br>Verifying OTP... <br>
<small>Please note, this may take a while</small>`),
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("<img src='"+ data.message[1] + "'>")
d.set_values({
'abha_card': data.message[1]
});
}
frappe.show_alert({
message:__('Failed to fetch health Data, Please try again later'),
indicator:'red'
}, 10);
}
}
});
Expand Down Expand Up @@ -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 = $(`<table class="table table-bordered" style="cursor:pointer; margin:0px;">
Expand Down Expand Up @@ -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'))
}
}
}

Expand Down Expand Up @@ -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
<b>${data.message['healthIdNumber']}</b> already exists {0}`,
if (r.message.abha_card) {
frappe.throw({
message: __(`{0}`,
["<img src='"+ r.message.abha_card + "'>"]),
title: __("Patient already exist")
});
} else {
frappe.throw({
message: __(`{0}`,
['<a href="/app/patient/'+r.message.name+'">' + r.message.name + '</a>']),
title: __("Patient already exist")
});
title: __("Patient already exist")
});
}
}
}),
() => {
Expand Down
54 changes: 42 additions & 12 deletions healthcare/regional/india/abdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
})
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')

0 comments on commit 832bfe1

Please sign in to comment.