Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMuriuki committed Jul 5, 2024
1 parent 8e9628d commit 1aada6d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 40 deletions.
3 changes: 2 additions & 1 deletion packages/fhir-flag/src/components/BaseComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useParams } from 'react-router';
import { FlagResourceType, PractitionerResourceType, thatiMinutes } from '../../constants';
import { FlagResourceType, PractitionerResourceType } from '@opensrp/fhir-helpers';
import { Col, Row, Spin } from 'antd';
import { useQuery } from 'react-query';
import { FHIRServiceClass, BrokenPage, getResourcesFromBundle } from '@opensrp/react-utils';
Expand All @@ -12,6 +12,7 @@ import { getExtraData } from '@onaio/session-reducer';
import { IBundle } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IBundle';
import { IPractitioner } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IPractitioner';
import { useTranslation } from '../../mls';
import { thatiMinutes } from '../../constants';

export interface CloseFlagProps {
fhirBaseURL: string;
Expand Down
3 changes: 1 addition & 2 deletions packages/fhir-flag/src/components/LocationFlag/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import { CloseFlagForm } from '../CloseFlagForm';
import { Alert, Button, Col, Row, Spin } from 'antd';
import { Spin } from 'antd';
import { useQuery } from 'react-query';
import { FHIRServiceClass, BrokenPage } from '@opensrp/react-utils';
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation';
import { IBundle } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IBundle';
import { locationResourceType } from '@opensrp/fhir-helpers';
import { putCloseFlagResources } from '../Utils/utils';
import { useTranslation } from '../../mls';
Expand Down
64 changes: 64 additions & 0 deletions packages/fhir-flag/src/components/LocationFlag/tests/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export const spCheckFlag = {
"resourceType": "Flag",
"id": "825b5491-9dad-4e28-ad73-521a31193de3",
"meta": {
"versionId": "2",
"lastUpdated": "2024-07-05T14:42:49.003+00:00",
"source": "#1dc83adf808b1de0",
"tag": [ {
"system": "https://smartregister.org/app-version",
"code": "1.1.0-eusm",
"display": "Application Version"
}, {
"system": "https://smartregister.org/organisation-tag-id",
"code": "d0882d3a-f35a-43cf-9d43-2ea9f9793e24",
"display": "Practitioner Organization"
}, {
"system": "https://smartregister.org/practitioner-tag-id",
"code": "ab929110-6918-4d0b-8961-13cce4d5c76b",
"display": "Practitioner"
}, {
"system": "https://smartregister.org/location-tag-id",
"code": "f15ff8ab-9475-4356-8363-7f518fdd66ce",
"display": "Practitioner Location"
}, {
"system": "https://smartregister.org/care-team-tag-id",
"code": "4ddd4157-921b-4c65-820c-161b6e845011",
"display": "Practitioner CareTeam"
} ]
},
"identifier": [ {
"use": "usual",
"value": "db6ccb63-3012-4143-be21-078345aaca79"
} ],
"status": "active",
"category": [ {
"coding": [ {
"system": "https://smartregister.org/",
"code": "SPCHECK",
"display": "Service Point Check"
} ],
"text": "Service Point Check"
} ],
"code": {
"coding": [ {
"system": "https://smartregister.org/",
"code": "65347579",
"display": "Vist Flag"
} ],
"text": "Vist Flag"
},
"subject": {
"reference": "Location/20bef46f-b5f2-490f-beca-d9fa6205be06"
},
"period": {
"start": "2024-04-26T10:48:12+03:00",
"end": "2024-04-26T10:48:12+03:00"
},
"encounter": {
"reference": "Encounter/a0c16e6e-b228-4bba-b707-53e484a993f6"
},
"author": {
"reference": "Practitioner/ab929110-6918-4d0b-8961-13cce4d5c76b"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import React from 'react';
12 changes: 7 additions & 5 deletions packages/fhir-flag/src/components/ProductFlag/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React from 'react';
import { CloseFlagForm } from '../CloseFlagForm';
import { GroupResourceType, ListResourceType, thatiMinutes } from '../../constants';
import { thatiMinutes } from '../../constants';
import { Alert, Button, Col, Row, Spin } from 'antd';
import { useQuery } from 'react-query';
import { FHIRServiceClass, BrokenPage } from '@opensrp/react-utils';
import { ILocation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/ILocation';
import { IGroup } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IGroup';
import {
listResourceType,
locationResourceType,
servicePointProfileInventoryListCoding,
groupResourceType
} from '@opensrp/fhir-helpers';
import { putCloseFlagResources } from '../Utils/utils';
import { useTranslation } from '../../mls';
Expand All @@ -26,7 +28,7 @@ export const ProductFlag = (props: ProductFlagProps) => {
const { t } = useTranslation();

const inventoryGroup = useQuery(
[GroupResourceType, inventoryGroupReference],
[groupResourceType, inventoryGroupReference],
() => new FHIRServiceClass<IGroup>(fhirBaseUrl, '').read(inventoryGroupReference as string),
{
enabled: !!inventoryGroupReference,
Expand All @@ -35,7 +37,7 @@ export const ProductFlag = (props: ProductFlagProps) => {
);

const product = useQuery(
[GroupResourceType, inventoryGroup.data?.member?.[0]?.entity?.reference],
[groupResourceType, inventoryGroup.data?.member?.[0]?.entity?.reference],
() =>
new FHIRServiceClass<IGroup>(fhirBaseUrl, '').read(
inventoryGroup.data?.member?.[0]?.entity?.reference as string
Expand All @@ -47,9 +49,9 @@ export const ProductFlag = (props: ProductFlagProps) => {
);

const list = useQuery(
[ListResourceType, inventoryGroupReference],
[listResourceType, inventoryGroupReference],
() =>
new FHIRServiceClass<any>(fhirBaseUrl, ListResourceType).list({
new FHIRServiceClass<any>(fhirBaseUrl, listResourceType).list({

Check warning on line 54 in packages/fhir-flag/src/components/ProductFlag/index.tsx

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-latest)

Unexpected any. Specify a different type
item: inventoryGroupReference?.split('/')[1],
code: `${servicePointProfileInventoryListCoding.system}|${servicePointProfileInventoryListCoding.code}`,
}),
Expand Down
File renamed without changes.
36 changes: 13 additions & 23 deletions packages/fhir-flag/src/components/Utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { IFlag } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IFlag';
import { IEncounter } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IEncounter';
import { IObservation } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IObservation';
import { v5 } from 'uuid';
import { encounter, observation } from '../../payloadConfigs';
import { encounter, observation } from './payloadConfigs';
import { FHIRServiceClass } from '@opensrp/react-utils';
import { EncounterResourceType, FlagResourceType, ObservationResourceType } from '../../constants';
import { EncounterResourceType, FlagResourceType, ObservationResourceType, PractitionerResourceType, conceptsHaveCodings, consultBeneficiaryCoding, servicePointCheckCoding } from '@opensrp/fhir-helpers';

export interface CloseFlagFormFields {
productName?: string;
Expand Down Expand Up @@ -60,14 +60,13 @@ export const generateEncounterPayload = (
encounter: IEncounter,
flag: IFlag,
practitionerId: string,
listSubjectReference: string
locationReference: string
) => {
const commonProperties = generateCommonProperties('Encounter', flag);
const reference =
flag.category?.[0]?.coding?.[0].code === 'SPCHECK' ||
flag.category?.[0]?.coding?.[0].code === 'CNBEN'
const commonProperties = generateCommonProperties(EncounterResourceType, flag);
const reference =
conceptsHaveCodings(flag.category, [servicePointCheckCoding, consultBeneficiaryCoding])
? flag.subject.reference
: listSubjectReference;
: locationReference;

return {
...encounter,
Expand All @@ -77,7 +76,7 @@ export const generateEncounterPayload = (
participant: [
{
individual: {
reference: `Practitioner/${practitionerId}`,
reference: `${PractitionerResourceType}/${practitionerId}`,
},
},
],
Expand All @@ -88,13 +87,11 @@ export const generateObservationPayload = (
observation: IObservation,
flag: IFlag,
practitionerId: string,
listSubjectReference: string,
locationReference: string,
values: CloseFlagFormFields
) => {
const commonProperties = generateCommonProperties('Encounter', flag);
const isSPCHECKOrCNBEN =
flag.category?.[0]?.coding?.[0].code === 'SPCHECK' ||
flag.category?.[0]?.coding?.[0].code === 'CNBEN';
const commonProperties = generateCommonProperties(ObservationResourceType, flag);
const isSPCHECKOrCNBEN =conceptsHaveCodings(flag.category, [servicePointCheckCoding, consultBeneficiaryCoding])

return {
...observation,
Expand All @@ -103,8 +100,8 @@ export const generateObservationPayload = (
encounter: { reference: `Encounter/${commonProperties.id}` },
focus: isSPCHECKOrCNBEN
? [{ reference: `Flag/${flag.id}` }]
: [{ reference: listSubjectReference }, { reference: `Flag/${flag.id}` }],
performer: isSPCHECKOrCNBEN ? [{ reference: `Practitioner/${practitionerId}` }] : undefined,
: [{ reference: locationReference }, { reference: `${FlagResourceType}/${flag.id}` }],
performer: isSPCHECKOrCNBEN ? [{ reference: `${PractitionerResourceType}/${practitionerId}` }] : undefined,
note: observation.note?.[0]?.text ? [{ text: values.comments }] : observation.note,
};
};
Expand All @@ -115,13 +112,6 @@ export const putCloseFlagResources = async (
activeFlag: any,
fhirBaseUrl: string
) => {
/**
* Check active flag type
* if category is spcheck on cnben call locationbasedflag builder
* get updated encounter/observation payload and updated flag
* do a post on encounter, observation and flag resources respectively
* Apply the same for product based flags */

const { listSubject, practitionerId } = initialValues;

const encounterPayload = generateEncounterPayload(
Expand Down
9 changes: 0 additions & 9 deletions packages/fhir-flag/src/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
// FHIR API strings
export const organizationAffiliationResourceType = 'OrganizationAffiliation';
export const FlagResourceType = 'Flag';
export const GroupResourceType = 'Group';
export const ListResourceType = 'List';
export const EncounterResourceType = 'Encounter';
export const ObservationResourceType = 'Observation';
export const PractitionerResourceType = 'Practitioner';

// urls
export const URL_ADMIN = '/admin';
export const URL_CLOSE_FLAGS = `${URL_ADMIN}/close-flags`;
Expand Down

0 comments on commit 1aada6d

Please sign in to comment.