Skip to content

Commit

Permalink
fix: include role resources for view record
Browse files Browse the repository at this point in the history
  • Loading branch information
Zangetsu101 committed Dec 31, 2024
1 parent 8672895 commit 935d5de
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
15 changes: 11 additions & 4 deletions packages/commons/src/fhir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
SavedCompositionSection
} from './composition'
import { SavedTask, Task, TaskHistory } from './task'
import { Practitioner, SavedPractitioner } from './practitioner'
import {
Practitioner,
PractitionerRole,
PractitionerRoleHistory,
SavedPractitioner
} from './practitioner'
import { Location, SavedLocation, Office, SavedOffice } from './location'

export * from './practitioner'
Expand Down Expand Up @@ -103,7 +108,7 @@ type SavedResource<T extends Resource> = T extends Encounter
? SavedRelatedPerson
: T extends Composition
? SavedComposition
: T extends SavedCompositionHistory
: T extends CompositionHistory
? SavedCompositionHistory
: T extends Reference
? SavedReference
Expand Down Expand Up @@ -202,7 +207,7 @@ type SavedQuestionnaireResponse = Omit<
status: 'completed'
}

export type CompositionHistory = Saved<Composition> & {
export type CompositionHistory = Omit<Saved<Composition>, 'resourceType'> & {
resourceType: 'CompositionHistory'
}

Expand Down Expand Up @@ -388,10 +393,12 @@ export function toHistoryResource<T extends Saved<Resource>>(resource: T) {
return {
...resource,
resourceType: `${resource.resourceType}History`
} as T extends Task
} as unknown as T extends Task
? TaskHistory
: T extends Composition
? CompositionHistory
: T extends PractitionerRole
? PractitionerRoleHistory
: never
}
export { updateFHIRBundle, buildFHIRBundle } from './transformers'
Expand Down
12 changes: 9 additions & 3 deletions packages/commons/src/fhir/practitioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
Resource,
WithStrictExtensions,
WithUUID,
SavedReference
SavedReference,
Saved
} from '.'

export type OpenCRVSPractitionerName = Omit<fhir3.HumanName, 'use'> & {
Expand All @@ -23,7 +24,12 @@ export type OpenCRVSPractitionerName = Omit<fhir3.HumanName, 'use'> & {
export type PractitionerRole = Omit<fhir3.PractitionerRole, 'location'> & {
location: [SavedReference]
}
export type PractitionerRoleHistory = PractitionerRole
export type PractitionerRoleHistory = Omit<
Saved<PractitionerRole>,
'resourceType'
> & {
resourceType: 'PractitionerRoleHistory'
}

export type Practitioner = WithStrictExtensions<
Omit<fhir3.Practitioner, 'name' | 'telecom'> & {
Expand Down Expand Up @@ -85,7 +91,7 @@ export function getPractitionerContactDetails(practitioner: Practitioner) {
}

export const getUserRoleFromHistory = (
practitionerRoleHistory: PractitionerRoleHistory[],
practitionerRoleHistory: (PractitionerRole | PractitionerRoleHistory)[],
lastModified: string
) => {
const practitionerRoleHistorySorted = practitionerRoleHistory.sort((a, b) => {
Expand Down
22 changes: 21 additions & 1 deletion packages/workflow/src/features/user/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import { getFromFhir } from '@workflow/features/registration/fhir/fhir-utils'
import {
Practitioner,
PractitionerRole,
PractitionerRoleHistory,
resourceIdentifierToUUID,
SavedPractitioner
Saved,
SavedBundle,
SavedPractitioner,
toHistoryResource
} from '@opencrvs/commons/types'
import { UUID } from '@opencrvs/commons'

Expand Down Expand Up @@ -130,6 +134,22 @@ export const getPractitionerOfficeId = async (practitionerId: string) => {
return resourceIdentifierToUUID(roleEntry.location[0].reference)
}

export const getPractitionerRoleWithHistory = async (
practitionerId: string
): Promise<(Saved<PractitionerRole> | Saved<PractitionerRoleHistory>)[]> => {
const roleBundle: SavedBundle<PractitionerRole> = await getFromFhir(
`/PractitionerRole?practitioner=${practitionerId}`
)
const role = roleBundle.entry[0].resource
const roleHistoryBundle: SavedBundle<PractitionerRole> = await getFromFhir(
`/PractitionerRole/${role.id}/_history`
)
const roleHistories = roleHistoryBundle.entry.map((e) =>
toHistoryResource(e.resource)
)
return [role, ...roleHistories]
}

export async function getLoggedInPractitionerResource(
token: string
): Promise<SavedPractitioner> {
Expand Down
38 changes: 30 additions & 8 deletions packages/workflow/src/records/fhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ import {
getResourceFromBundleById,
TransactionResponse,
TaskIdentifierSystem,
Location
Location,
Saved,
PractitionerRole,
PractitionerRoleHistory
} from '@opencrvs/commons/types'
import { FHIR_URL } from '@workflow/constants'
import fetch from 'node-fetch'
Expand All @@ -69,7 +72,8 @@ import { badRequest, internal } from '@hapi/boom'
import { getUserOrSystem, isSystem } from './user'
import {
getLoggedInPractitionerResource,
getPractitionerOfficeId
getPractitionerOfficeId,
getPractitionerRoleWithHistory
} from '@workflow/features/user/utils'
import { z } from 'zod'
import { fetchLocationHierarchy } from '@workflow/utils/location'
Expand All @@ -93,7 +97,12 @@ function getFHIRValueField(value: unknown) {
export async function mergeChangedResourcesIntoRecord(
record: ValidRecord,
unsavedChangedResources: Bundle,
practitionerResourcesBundle: SavedBundle<SavedLocation | SavedPractitioner>
practitionerResourcesBundle: SavedBundle<
| SavedLocation
| SavedPractitioner
| Saved<PractitionerRole>
| Saved<PractitionerRoleHistory>
>
) {
const responseBundle = await sendBundleToHearth(unsavedChangedResources)

Expand All @@ -110,7 +119,17 @@ export async function mergeChangedResourcesIntoRecord(
export async function withPractitionerDetails<T extends Task>(
task: T,
token: string
): Promise<[T, SavedBundle<SavedLocation | SavedPractitioner>]> {
): Promise<
[
T,
SavedBundle<
| SavedLocation
| SavedPractitioner
| Saved<PractitionerRole>
| Saved<PractitionerRoleHistory>
>
]
> {
const userOrSystem = await getUserOrSystem(token)
const newTask: T = {
...task,
Expand Down Expand Up @@ -143,6 +162,9 @@ export async function withPractitionerDetails<T extends Task>(
}
const user = userOrSystem
const practitioner = await getLoggedInPractitionerResource(token)
const practitionerRoleHistory = await getPractitionerRoleWithHistory(
practitioner.id
)
const practitionerOfficeId = await getPractitionerOfficeId(practitioner.id)
const hierarchy = await fetchLocationHierarchy(practitionerOfficeId)

Expand All @@ -167,7 +189,7 @@ export async function withPractitionerDetails<T extends Task>(
{
type: 'document',
resourceType: 'Bundle',
entry: [practitioner, ...hierarchy].map((r) =>
entry: [practitioner, ...hierarchy, ...practitionerRoleHistory].map((r) =>
resourceToSavedBundleEntry(r)
)
}
Expand Down Expand Up @@ -666,17 +688,17 @@ export function createCorrectedTask(
export async function createViewTask(
previousTask: SavedTask,
token: string
): Promise<SavedTask> {
): Promise<[SavedTask, Bundle]> {
const taskWithoutPracitionerExtensions = createNewTaskResource(previousTask, [
{ url: 'http://opencrvs.org/specs/extension/regViewed' }
])

const [viewedTask] = await withPractitionerDetails(
const [viewedTask, bundle] = await withPractitionerDetails(
taskWithoutPracitionerExtensions,
token
)

return viewedTask
return [viewedTask, bundle]
}

export function createDownloadTask(previousTask: SavedTask) {
Expand Down
7 changes: 5 additions & 2 deletions packages/workflow/src/records/state-transitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ export async function toViewed<T extends ValidRecord>(
token: string
): Promise<T> {
const previousTask: SavedTask = getTaskFromSavedBundle(record)
const viewedTask = await createViewTask(previousTask, token)
const [viewedTask, bundleWithRelatedResources] = await createViewTask(
previousTask,
token
)

const taskHistoryEntry = resourceToBundleEntry(
toHistoryResource(previousTask)
Expand All @@ -322,7 +325,7 @@ export async function toViewed<T extends ValidRecord>(
]
} as T

return viewedRecord
return mergeBundles(viewedRecord, bundleWithRelatedResources)
}

export function toIdentifierUpserted<T extends ValidRecord>(
Expand Down

0 comments on commit 935d5de

Please sign in to comment.