-
Notifications
You must be signed in to change notification settings - Fork 103
(feat) O3-5420: Add PersonAttribute adapter support to Form Engine #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RajPrakash681
wants to merge
3
commits into
openmrs:main
Choose a base branch
from
RajPrakash681:feat/add-person-attribute-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { PersonAttributeAdapter } from './person-attribute-adapter'; | ||
| import { type FormField, type FormProcessorContextProps } from '../types'; | ||
| import { type FormContextProps } from '../provider/form-provider'; | ||
|
|
||
| describe('PersonAttributeAdapter', () => { | ||
| const mockField: FormField = { | ||
| id: 'test-person-attribute', | ||
| type: 'personAttribute', | ||
| questionOptions: { | ||
| attributeType: '7ef225db-94db-4e40-9dd8-fb121d9dc370', | ||
| rendering: 'text', | ||
| }, | ||
| meta: { | ||
| submission: {}, | ||
| initialValue: {}, | ||
| }, | ||
| } as any; | ||
|
|
||
| const mockContext: FormContextProps = { | ||
| patient: { | ||
| id: 'test-patient-uuid', | ||
| } as fhir.Patient, | ||
| } as any; | ||
|
|
||
| describe('transformFieldValue', () => { | ||
| it('should return null for empty value', () => { | ||
| const result = PersonAttributeAdapter.transformFieldValue(mockField, '', mockContext); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null when value equals initial value', () => { | ||
| const field = { | ||
| ...mockField, | ||
| meta: { | ||
| submission: {}, | ||
| initialValue: { | ||
| omrsObject: null, | ||
| refinedValue: 'test-value', | ||
| }, | ||
| }, | ||
| } as any; | ||
| const result = PersonAttributeAdapter.transformFieldValue(field, 'test-value', mockContext); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should transform field value correctly for new attribute', () => { | ||
| const field = { ...mockField }; | ||
| const result = PersonAttributeAdapter.transformFieldValue(field, 'new-attribute-value', mockContext); | ||
|
|
||
| expect(result).toEqual({ | ||
| value: 'new-attribute-value', | ||
| attributeType: '7ef225db-94db-4e40-9dd8-fb121d9dc370', | ||
| uuid: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it('should include uuid when updating existing attribute', () => { | ||
| const field = { | ||
| ...mockField, | ||
| meta: { | ||
| submission: {}, | ||
| initialValue: { | ||
| omrsObject: { uuid: 'existing-attr-uuid' }, | ||
| refinedValue: 'old-value', | ||
| }, | ||
| }, | ||
| } as any; | ||
| const result = PersonAttributeAdapter.transformFieldValue(field, 'updated-value', mockContext); | ||
|
|
||
| expect(result).toEqual({ | ||
| value: 'updated-value', | ||
| attributeType: '7ef225db-94db-4e40-9dd8-fb121d9dc370', | ||
| uuid: 'existing-attr-uuid', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getInitialValue', () => { | ||
| it('should return undefined when no person attribute exists', () => { | ||
| const mockProcessorContext: FormProcessorContextProps = { | ||
| patient: { | ||
| id: 'test-patient', | ||
| extension: [], | ||
| } as fhir.Patient, | ||
| } as any; | ||
|
|
||
| const result = PersonAttributeAdapter.getInitialValue(mockField, null, mockProcessorContext); | ||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return valueString when person attribute exists', () => { | ||
| const mockProcessorContext: FormProcessorContextProps = { | ||
| patient: { | ||
| id: 'test-patient', | ||
| extension: [ | ||
| { | ||
| url: 'http://fhir.openmrs.org/ext/person-attribute/7ef225db-94db-4e40-9dd8-fb121d9dc370', | ||
| valueString: 'test-attribute-value', | ||
| }, | ||
| ], | ||
| } as fhir.Patient, | ||
| } as any; | ||
|
|
||
| const field = { ...mockField }; | ||
| const result = PersonAttributeAdapter.getInitialValue(field, null, mockProcessorContext); | ||
|
|
||
| expect(result).toBe('test-attribute-value'); | ||
| expect(field.meta.initialValue.refinedValue).toBe('test-attribute-value'); | ||
| }); | ||
|
|
||
| it('should return valueReference when person attribute has reference', () => { | ||
| const mockProcessorContext: FormProcessorContextProps = { | ||
| patient: { | ||
| id: 'test-patient', | ||
| extension: [ | ||
| { | ||
| url: 'http://fhir.openmrs.org/ext/person-attribute/7ef225db-94db-4e40-9dd8-fb121d9dc370', | ||
| valueReference: { | ||
| reference: 'Location/test-location-uuid', | ||
| }, | ||
| }, | ||
| ], | ||
| } as fhir.Patient, | ||
| } as any; | ||
|
|
||
| const field = { ...mockField }; | ||
| const result = PersonAttributeAdapter.getInitialValue(field, null, mockProcessorContext); | ||
|
|
||
| expect(result).toBe('Location/test-location-uuid'); | ||
| expect(field.meta.initialValue.refinedValue).toBe('Location/test-location-uuid'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getPreviousValue', () => { | ||
| it('should return null', () => { | ||
| const result = PersonAttributeAdapter.getPreviousValue(mockField, null, {} as any); | ||
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getDisplayValue', () => { | ||
| it('should return display property if present', () => { | ||
| const value = { display: 'Test Display', value: 'test-value' }; | ||
| const result = PersonAttributeAdapter.getDisplayValue(mockField, value); | ||
| expect(result).toBe('Test Display'); | ||
| }); | ||
|
|
||
| it('should return value as-is if no display property', () => { | ||
| const value = 'simple-value'; | ||
| const result = PersonAttributeAdapter.getDisplayValue(mockField, value); | ||
| expect(result).toBe('simple-value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('tearDown', () => { | ||
| it('should execute without errors', () => { | ||
| expect(() => PersonAttributeAdapter.tearDown()).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { type OpenmrsResource } from '@openmrs/esm-framework'; | ||
| import { type FormContextProps } from '../provider/form-provider'; | ||
| import { type FormField, type FormFieldValueAdapter, type FormProcessorContextProps } from '../types'; | ||
| import { clearSubmission } from '../utils/common-utils'; | ||
| import { isEmpty } from '../validators/form-validator'; | ||
|
|
||
| export const PersonAttributeAdapter: FormFieldValueAdapter = { | ||
| transformFieldValue: function (field: FormField, value: any, context: FormContextProps) { | ||
| clearSubmission(field); | ||
| if (field.meta.initialValue?.refinedValue === value || isEmpty(value)) { | ||
| return null; | ||
| } | ||
| field.meta.submission.newValue = { | ||
| value: value, | ||
| attributeType: field.questionOptions.attributeType, | ||
| uuid: (field.meta.initialValue?.omrsObject as OpenmrsResource)?.uuid, | ||
| }; | ||
| return field.meta.submission.newValue; | ||
| }, | ||
| getInitialValue: function (field: FormField, sourceObject: OpenmrsResource, context: FormProcessorContextProps) { | ||
| const latestAttribute = context.patient?.extension?.find( | ||
| (ext) => ext.url === `http://fhir.openmrs.org/ext/person-attribute/${field.questionOptions.attributeType}`, | ||
| ); | ||
| field.meta = { | ||
| ...(field.meta || {}), | ||
| initialValue: { | ||
| omrsObject: latestAttribute as any, | ||
| refinedValue: latestAttribute?.valueString || latestAttribute?.valueReference?.reference, | ||
| }, | ||
| }; | ||
| return field.meta.initialValue.refinedValue; | ||
| }, | ||
| getPreviousValue: function (field: FormField, sourceObject: OpenmrsResource, context: FormProcessorContextProps) { | ||
| return null; | ||
| }, | ||
| getDisplayValue: function (field: FormField, value: any) { | ||
| if (value?.display) { | ||
| return value.display; | ||
| } | ||
| return value; | ||
| }, | ||
| tearDown: function (): void { | ||
| return; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import type { | |||||
| PatientDeathPayload, | ||||||
| PatientIdentifier, | ||||||
| PatientProgramPayload, | ||||||
| PersonAttribute, | ||||||
| } from '../types'; | ||||||
| import { isUuid } from '../utils/boolean-utils'; | ||||||
|
|
||||||
|
|
@@ -179,6 +180,24 @@ export function savePatientIdentifier(patientIdentifier: PatientIdentifier, pati | |||||
| }); | ||||||
| } | ||||||
|
|
||||||
| export function savePersonAttribute(personAttribute: PersonAttribute, patientUuid: string) { | ||||||
| let url: string; | ||||||
|
|
||||||
| if (personAttribute.uuid) { | ||||||
| url = `${restBaseUrl}/person/${patientUuid}/attribute/${personAttribute.uuid}`; | ||||||
| } else { | ||||||
| url = `${restBaseUrl}/person/${patientUuid}/attribute`; | ||||||
| } | ||||||
|
|
||||||
| return openmrsFetch(url, { | ||||||
| headers: { | ||||||
| 'Content-Type': 'application/json', | ||||||
| }, | ||||||
| method: 'POST', | ||||||
| body: JSON.stringify(personAttribute), | ||||||
|
||||||
| body: JSON.stringify(personAttribute), | |
| body: personAttribute, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try to avoid
as anywherever possible. Most of these use-cases, it's better to use an explicit cast. This one can be done (satisfies FormField) with very little effort.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok!