Skip to content

Commit

Permalink
fix post submission action error, improve post submission error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kajambiya committed Nov 14, 2023
1 parent 1bed5e3 commit e6145b7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 30 deletions.
34 changes: 16 additions & 18 deletions src/components/encounter/ohri-encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
}
flattenedFieldsTemp.push(question);
if (question.type == 'obsGroup') {
question.questions.forEach(groupedField => {
question.questions.forEach((groupedField) => {
if (groupedField.questionOptions.rendering == 'fixed-value' && !groupedField['fixedValue']) {
groupedField['fixedValue'] = groupedField.value;
}
Expand Down Expand Up @@ -419,9 +419,8 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
.filter((field) => !field['groupId']) // filter out grouped obs
.filter((field) => !field.questionOptions.isTransient && field.questionOptions.rendering !== 'file')
.forEach((field) => {

if (field.type == 'obsGroup') {
const obsGroupUuid = encounter?.obs?.find(m => m.concept.uuid == field.questionOptions.concept)?.uuid
const obsGroupUuid = encounter?.obs?.find((m) => m.concept.uuid == field.questionOptions.concept)?.uuid;
const obsGroup = {
person: patient?.id,
obsDatetime: encounterDate,
Expand Down Expand Up @@ -498,22 +497,21 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({

if (encounterForSubmission.obs?.length || encounterForSubmission.orders?.length) {
const ac = new AbortController();
return saveEncounter(ac, encounterForSubmission, encounter?.uuid)
.then((response) => response.data)
.then((encounter) => {
fields
?.filter((field) => field?.questionOptions.rendering === 'file')
.map((field) => {
return saveAttachment(
encounter?.patient.uuid,
field,
field?.questionOptions.concept,
new Date().toISOString(),
encounter?.uuid,
ac,
);
});
return saveEncounter(ac, encounterForSubmission, encounter?.uuid).then((response) => {
const encounter = response.data;
const fileFields = fields?.filter((field) => field?.questionOptions.rendering === 'file');
const saveAttachmentPromises = fileFields.map((field) => {
return saveAttachment(
encounter?.patient.uuid,
field,
field?.questionOptions.concept,
new Date().toISOString(),
encounter?.uuid,
ac,
);
});
return Promise.all(saveAttachmentPromises).then(() => response);
});
}
};

Expand Down
42 changes: 42 additions & 0 deletions src/hooks/usePostSubmissionAction.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { renderHook, act } from '@testing-library/react';
import { usePostSubmissionAction } from './usePostSubmissionAction';

// Mock the getRegisteredPostSubmissionAction function
jest.mock('../registry/registry', () => ({
getRegisteredPostSubmissionAction: jest.fn(),
}));

describe('usePostSubmissionAction', () => {
// Mock the actual post-submission action function
const mockPostAction = jest.fn();

// Sample action references
const actionRefs = [
{ actionId: 'action1', config: { param1: 'value1' } },
{ actionId: 'action2', config: { param2: 'value2' } },
];

// Set up the mock implementation for getRegisteredPostSubmissionAction
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(global.console, 'error').mockImplementation(() => {});
jest.requireMock('../registry/registry').getRegisteredPostSubmissionAction.mockImplementation((actionId) => {
if (actionId === 'action1') {
return Promise.resolve(mockPostAction);
}
return Promise.resolve(null);
});
});

it('should fetch post-submission actions and return them', async () => {
const { result } = renderHook(() => usePostSubmissionAction(actionRefs));

// Wait for the effect to complete
await act(async () => {});

expect(result.current).toEqual([
{ postAction: mockPostAction, config: { param1: 'value1' }, actionId: 'action1' },
{ postAction: null, config: { param2: 'value2' }, actionId: 'action2' },
]);
});
});
30 changes: 22 additions & 8 deletions src/ohri-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,28 @@ const OHRIForm: React.FC<OHRIFormProps> = ({
await Promise.all(
postSubmissionHandlers.map(async ({ postAction, config, actionId }) => {
try {
await postAction.applyAction(
{
patient,
sessionMode,
encounters: results.map((encounterResult) => encounterResult.data),
},
config,
);
const encounterData = [];
if (results) {
results.forEach((result) => {
if (result?.data) {
encounterData.push(result.data);
}
});
if (encounterData.length) {
await postAction.applyAction(
{
patient,
sessionMode,
encounters: encounterData,
},
config,
);
} else {
throw new Error('No encounter data to process post submission action');
}
} else {
throw new Error('No handlers available to process post submission action');
}
} catch (error) {
const errorMessages = extractErrorMessagesFromResponse(error);
showToast({
Expand Down
7 changes: 3 additions & 4 deletions src/utils/error-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ export function extractErrorMessagesFromResponse(errorObject) {
return [errorObject?.responseBody?.error?.message ?? errorObject?.message];
}

if(globalErrors){
if (globalErrors) {
return globalErrors.flatMap((error) => error.message);
}else{
return Object.values(fieldErrors).flatMap((errors: Array<Error>) => errors.map(error => error.message));
} else {
return Object.values(fieldErrors).flatMap((errors: Array<Error>) => errors.map((error) => error.message));
}

}

0 comments on commit e6145b7

Please sign in to comment.