import { FlatConvectorModel } from '@worldsibu/convector-core' ; import { Currencies , CodingTypes } from './enums' ; import { ClaimResponseItemAdjudication , CodeableConcept , ClaimResponseTotal , ClaimItem , InvoiceLineItem , ChargeItem , Identifier , Narrative , Coding , Money , Reference } from '../models/financial.model' ; import { ChaincodeTx } from '@worldsibu/convector-core-chaincode' ; import { PrivateCollectionsRoutes } from '../models/privateCollectionsRoutes.model' ; import { Iterators } from 'fabric-shim' ; param type param val export function buildAdjudicationItem ( type , val ) { let adjudication = new ClaimResponseItemAdjudication ( ) ; adjudication . category = new CodeableConcept ( ) ; adjudication . category . coding = [ buildCoding ( type ) ] ; adjudication . amount = buildMoney ( val ) ; return adjudication ; } export function buildTotalCosts ( ) { const totalCost = new ClaimResponseTotal ( ) ; totalCost . amount = buildMoney ( 0 ) ; totalCost . category = new CodeableConcept ( ) ; const totalCostCategory = buildCoding ( CodingTypes . SUBMITTED , 'Submitted Amount' , 'http://terminology.hl7.org/CodeSystem/adjudication' ) ; totalCost . category . coding = [ totalCostCategory ] ; return { totalCost , totalCostCategory } ; } export function buildTotalBenefits ( ) { const totalBenefit = new ClaimResponseTotal ( ) ; totalBenefit . amount = buildMoney ( 0 ) ; totalBenefit . category = new CodeableConcept ( ) ; const totalBenefitCategory = buildCoding ( CodingTypes . BENEFIT , 'Benefit Amount' , 'http://terminology.hl7.org/CodeSystem/adjudication' ) ; totalBenefit . category . coding = [ totalBenefitCategory ] ; return { totalBenefit , totalBenefitCategory } ; } async function iteratorToList ( iterator ) { const allResults = [ ] ; let realIterator = ( iterator as any ) . iterator ? ( iterator as any ) . iterator : iterator ; let res ; while ( res == null || ! res . done ) { res = await realIterator . next ( ) ; if ( res . value && res . value . value . toString ( ) ) { let parsedItem ; try { parsedItem = JSON . parse ( res . value . value . toString ( 'utf8' ) ) ; } catch ( err ) { parsedItem = res . value . value . toString ( 'utf8' ) ; } allResults . push ( parsedItem ) ; } } await realIterator . close ( ) ; return allResults ; } export async function buildInvoiceLineItems ( items , privateCollection , tx ) { let invoiceLineItems = [ ] ; for ( let item of items ) { let encounterId = item . encounter [ 0 ] . identifier . value ; let chargeItems = await iteratorToList ( await tx . stub . getStub ( ) . getPrivateDataQueryResult ( privateCollection . chargeItem , JSON . stringify ( { 'selector' : { 'context.identifier.value' : encounterId } } ) ) ) ; let key = 0 ; for ( let chargeItem of chargeItems ) { key ++ ; let newInvoiceLine = new InvoiceLineItem ( ) ; newInvoiceLine . sequence = key ; let reference = buildReference ( chargeItem . identifier [ 0 ] ) ; newInvoiceLine . chargeItemReference = reference ; newInvoiceLine . chargeItemCodeableConcept = chargeItem . code ; invoiceLineItems . push ( newInvoiceLine ) ; } } return invoiceLineItems ; } param value param use param system export function buildIdentifier ( value , use ? , system ? ) { let identifier = new Identifier ( ) ; identifier . use = use ; identifier . system = system ; identifier . value = value ; return identifier ; } param status param div export function buildNarrative ( status ? , div ? ) { const narrative = new Narrative ( ) ; narrative . status = status ; narrative . div = div ; return narrative ; } param code param display param system export function buildCoding ( code , display ? , system ? ) { const coding = new Coding ( ) ; coding . code = code ; coding . display = display || '' ; coding . system = system || '' ; return coding ; } param identifier export function buildReference ( identifier ) { let reference = new Reference ( ) ; reference . identifier = identifier ; return reference ; } param value param currency export function buildMoney ( value , currency ? ) { let amount = new Money ( ) ; amount . value = value ; amount . currency = currency || Currencies . USD ; return amount ; }