11import Oystehr , { BatchInputJSONPatchRequest , BatchInputPatchRequest , User } from '@oystehr/sdk' ;
22import { APIGatewayProxyResult } from 'aws-lambda' ;
33import { Operation } from 'fast-json-patch' ;
4- import { Appointment , Coding , Encounter , Extension , Patient } from 'fhir/r4b' ;
4+ import { Account , Appointment , Coding , Encounter , Extension , Patient } from 'fhir/r4b' ;
55import { DateTime } from 'luxon' ;
66import {
77 BOOKING_CONFIG ,
88 BookingDetails ,
99 cleanUpStaffHistoryTag ,
1010 FHIR_EXTENSION ,
1111 FHIR_RESOURCE_NOT_FOUND ,
12+ FHIR_RESOURCE_NOT_FOUND_CUSTOM ,
1213 getCriticalUpdateTagOp ,
1314 getReasonForVisitAndAdditionalDetailsFromAppointment ,
1415 getSecret ,
@@ -18,11 +19,13 @@ import {
1819 isValidUUID ,
1920 MISSING_REQUEST_BODY ,
2021 MISSING_REQUIRED_PARAMETERS ,
22+ OCCUPATIONAL_MEDICINE_ACCOUNT_TYPE ,
2123 REASON_ADDITIONAL_MAX_CHAR ,
2224 Secrets ,
2325 SecretsKeys ,
2426 userMe ,
2527 VALUE_SETS ,
28+ WORKERS_COMP_ACCOUNT_TYPE ,
2629} from 'utils' ;
2730import {
2831 checkOrCreateM2MClientToken ,
@@ -31,6 +34,7 @@ import {
3134 wrapHandler ,
3235 ZambdaInput ,
3336} from '../../../shared' ;
37+ import { accountMatchesType } from '../../shared/harvest' ;
3438
3539const ZAMBDA_NAME = 'update-visit-details' ;
3640
@@ -66,10 +70,13 @@ interface EffectInput extends Input {
6670 user : User ;
6771 appointment : Appointment ;
6872 encounter : Encounter ;
73+ workersCompAccount ?: Account ;
74+ occupationalMedicineAccount ?: Account ;
6975}
7076
7177const performEffect = async ( input : EffectInput , oystehr : Oystehr ) : Promise < void > => {
72- const { patient, appointment, bookingDetails, user, encounter } = input ;
78+ const { patient, appointment, bookingDetails, user, encounter, workersCompAccount, occupationalMedicineAccount } =
79+ input ;
7380
7481 const patchRequests : BatchInputJSONPatchRequest [ ] = [ ] ;
7582 if ( bookingDetails . confirmedDob ) {
@@ -307,6 +314,41 @@ const performEffect = async (input: EffectInput, oystehr: Oystehr): Promise<void
307314 }
308315
309316 if ( bookingDetails . serviceCategory ) {
317+ const newEncounterAccounts = ( encounter . account ?? [ ] )
318+ . filter ( ( reference ) => reference . reference === 'Account/' + occupationalMedicineAccount ?. id )
319+ . filter ( ( reference ) => reference . reference === 'Account/' + workersCompAccount ?. id ) ;
320+
321+ if ( bookingDetails . serviceCategory . code === 'workers-comp' ) {
322+ if ( ! workersCompAccount ) {
323+ throw FHIR_RESOURCE_NOT_FOUND_CUSTOM ( 'Workmans Comp Account missing' ) ;
324+ }
325+ newEncounterAccounts . push ( {
326+ reference : 'Account/' + workersCompAccount . id ,
327+ } ) ;
328+ }
329+
330+ if ( bookingDetails . serviceCategory . code === 'occupational-medicine' ) {
331+ if ( ! occupationalMedicineAccount ) {
332+ throw FHIR_RESOURCE_NOT_FOUND_CUSTOM ( 'Occupational Medicine Account missing' ) ;
333+ }
334+ newEncounterAccounts . push ( {
335+ reference : 'Account/' + occupationalMedicineAccount . id ,
336+ } ) ;
337+ }
338+
339+ const encounterPatch : BatchInputPatchRequest < Encounter > = {
340+ method : 'PATCH' ,
341+ url : `/Encounter/${ encounter . id } ` ,
342+ operations : [
343+ {
344+ op : encounter ?. account ? 'replace' : 'add' ,
345+ path : '/account' ,
346+ value : newEncounterAccounts ,
347+ } ,
348+ ] ,
349+ } ;
350+ patchRequests . push ( encounterPatch ) ;
351+
310352 const appointmentPatch : BatchInputJSONPatchRequest = {
311353 url : '/Appointment/' + appointment . id ,
312354 method : 'PATCH' ,
@@ -365,6 +407,13 @@ const complexValidation = async (input: Input, oystehr: Oystehr): Promise<Effect
365407 throw FHIR_RESOURCE_NOT_FOUND ( 'Encounter' ) ;
366408 }
367409
410+ const accounts = (
411+ await oystehr . fhir . search < Account > ( {
412+ resourceType : 'Account' ,
413+ params : [ { name : 'patient' , value : patientResource . id } ] ,
414+ } )
415+ ) . unbundle ( ) ;
416+
368417 // const selfPay = getPaymentVariantFromEncounter(encounterResource) === PaymentVariant.selfPay;
369418
370419 return {
@@ -373,6 +422,10 @@ const complexValidation = async (input: Input, oystehr: Oystehr): Promise<Effect
373422 appointment,
374423 encounter : encounterResource ,
375424 user,
425+ workersCompAccount : accounts . find ( ( account ) => accountMatchesType ( account , WORKERS_COMP_ACCOUNT_TYPE ) ) ,
426+ occupationalMedicineAccount : accounts . find ( ( account ) =>
427+ accountMatchesType ( account , OCCUPATIONAL_MEDICINE_ACCOUNT_TYPE )
428+ ) ,
376429 } ;
377430} ;
378431
0 commit comments