Skip to content

refactor(jubilee): enforce one-JSR-per-encounter in create_preauthorization_doc#1193

Merged
av-dev2 merged 1 commit intoAakvatech-Limited:nurse-ot-modulefrom
av-dev2:feat_nurse_ot_module
Apr 28, 2026
Merged

refactor(jubilee): enforce one-JSR-per-encounter in create_preauthorization_doc#1193
av-dev2 merged 1 commit intoAakvatech-Limited:nurse-ot-modulefrom
av-dev2:feat_nurse_ot_module

Conversation

@av-dev2
Copy link
Copy Markdown
Collaborator

@av-dev2 av-dev2 commented Apr 28, 2026

Prevent duplicate Jubilee Service Request documents from being created when a doctor clicks "Request Pre-Authorization" more than once for the same Patient Encounter. Previously, each call unconditionally inserted a new JSR; now the function checks for an existing one first.

create_preauthorization_doc() logic:

  • Resolves benefit_name and benefit_balance upfront (before the existence check) so the values are available regardless of which branch is taken
  • Queries Jubilee Service Request filtered by patient_encounter to find an existing document (latest by creation date), returning name + docstatus
  • Three-branch execution:
    1. Submitted JSR exists (docstatus == 1): Return the stored preauth_status, submission_id, and preauth_description immediately without re-calling the Jubilee API
    2. Draft JSR exists (docstatus == 0): Update benefit_code, benefit_name, benefit_balance on the existing draft and call save(); the before_save hook (set_missing_values) re-populates all encounter-derived fields with the latest encounter data before submit
    3. No JSR found: Create via frappe.new_doc(), set patient_encounter and benefit fields, then call save() to trigger before_save population before submit
  • Removed redundant jsr.reload() after submit; the before_submit hook already writes preauth_status/submission_id/preauth_description onto self before Frappe final document save, so the in-memory jsr object is already current
  • Switched new-document path from frappe.get_doc + insert to frappe.new_doc + attribute assignment + save for consistency with the draft-reuse path and to ensure before_save fires in both cases

…zation_doc

Prevent duplicate Jubilee Service Request documents from being created
when a doctor clicks "Request Pre-Authorization" more than once for the
same Patient Encounter. Previously, each call unconditionally inserted a
new JSR; now the function checks for an existing one first.

create_preauthorization_doc() logic:
- Resolves benefit_name and benefit_balance upfront (before the existence
  check) so the values are available regardless of which branch is taken
- Queries Jubilee Service Request filtered by patient_encounter to find
  an existing document (latest by creation date), returning name + docstatus
- Three-branch execution:
  1. Submitted JSR exists (docstatus == 1):
     Return the stored preauth_status, submission_id, and preauth_description
     immediately without re-calling the Jubilee API
  2. Draft JSR exists (docstatus == 0):
     Update benefit_code, benefit_name, benefit_balance on the existing draft
     and call save(); the before_save hook (set_missing_values) re-populates
     all encounter-derived fields with the latest encounter data before submit
  3. No JSR found:
     Create via frappe.new_doc(), set patient_encounter and benefit fields,
     then call save() to trigger before_save population before submit
- Removed redundant jsr.reload() after submit; the before_submit hook already
  writes preauth_status/submission_id/preauth_description onto self before
  Frappe final document save, so the in-memory jsr object is already current
- Switched new-document path from frappe.get_doc + insert to
  frappe.new_doc + attribute assignment + save for consistency with the
  draft-reuse path and to ensure before_save fires in both cases
@av-dev2 av-dev2 merged commit 59b5af0 into Aakvatech-Limited:nurse-ot-module Apr 28, 2026
1 check failed
@codacy-production
Copy link
Copy Markdown

Not up to standards ⛔

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant