Skip to content

Commit 0e9f342

Browse files
BN-173 | Refactor.Models to NewVi
sitData and Tests
1 parent f0a9913 commit 0e9f342

File tree

7 files changed

+44
-45
lines changed

7 files changed

+44
-45
lines changed

apps/registration/src/pages/createPatientPage/__tests__/visitTypeSelector.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ jest.mock('@bahmni-frontend/bahmni-services', () => ({
4040
dispatchAuditEvent: jest.fn(),
4141
}));
4242

43-
const mockVisitTypes = [
44-
{ name: 'EMERGENCY', uuid: '493ebb53-b2bd-4ced-b444-e0965804d771' },
45-
{ name: 'OPD', uuid: '54f43754-c6ce-4472-890e-0f28acaeaea6' },
46-
{ name: 'IPD', uuid: 'b7494a80-fdf9-49bb-bb40-396c47b40343' },
47-
{ name: 'Special OPD', uuid: 'f3185c75-2c8c-476a-bf95-c77e8bb42edd' },
48-
{ name: 'Follow Up', uuid: '9772f68d-9fc5-4470-9b87-2b6139011cad' },
49-
];
43+
const mockVisitTypes = {
44+
visitTypes: {
45+
EMERGENCY: '493ebb53-b2bd-4ced-b444-e0965804d771',
46+
OPD: '54f43754-c6ce-4472-890e-0f28acaeaea6',
47+
IPD: 'b7494a80-fdf9-49bb-bb40-396c47b40343',
48+
},
49+
};
5050

5151
const mockLoginLocation = {
5252
name: 'Support',
@@ -117,7 +117,7 @@ describe('VisitTypeSelector', () => {
117117

118118
await waitFor(() => {
119119
const options = screen.getAllByRole('option');
120-
expect(options).toHaveLength(4);
120+
expect(options).toHaveLength(2);
121121
});
122122
});
123123

apps/registration/src/pages/createPatientPage/visitTypeSelector.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
dispatchAuditEvent,
1111
AUDIT_LOG_EVENT_DETAILS,
1212
getRegistrationConfig,
13-
type CreateVisitRequest,
13+
type NewVisitData,
1414
type AuditEventType,
1515
} from '@bahmni-frontend/bahmni-services';
1616
import { useQuery } from '@tanstack/react-query';
@@ -27,7 +27,7 @@ export const VisitTypeSelector = ({
2727
patientUuid,
2828
}: VisitTypeSelectorProps) => {
2929
const { t } = useTranslation();
30-
const [visitPayload, setVisitPayload] = useState<CreateVisitRequest>();
30+
const [visitPayload, setVisitPayload] = useState<NewVisitData>();
3131
const [patientUUID, setPatientUUID] = useState<string | null>(null);
3232

3333
useEffect(() => {
@@ -36,7 +36,7 @@ export const VisitTypeSelector = ({
3636
}
3737
}, [patientUuid, patientUUID]);
3838
const {
39-
data: visitTypesFromApi = [],
39+
data: visitTypes,
4040
isLoading: isLoadingVisitTypes,
4141
error: visitTypesError,
4242
} = useQuery({
@@ -60,16 +60,23 @@ export const VisitTypeSelector = ({
6060
gcTime: 10 * 60 * 1000,
6161
});
6262

63+
const visitTypesArray = visitTypes?.visitTypes
64+
? Object.entries(visitTypes.visitTypes).map(([name, uuid]) => ({
65+
name,
66+
uuid,
67+
}))
68+
: [];
69+
6370
const defaultVisitType =
64-
visitTypesFromApi.find(
71+
visitTypesArray.find(
6572
(vt) => vt.name === registrationConfig?.defaultVisitType,
66-
) ?? visitTypesFromApi[0];
73+
) ?? visitTypesArray[0];
6774

6875
const createVisitAndLogAudit = async () => {
6976
const result = await createVisit(visitPayload!);
7077

7178
if (visitPayload) {
72-
const visitType = visitTypesFromApi.find(
79+
const visitType = visitTypesArray.find(
7380
(vt) => vt.uuid === visitPayload.visitType,
7481
);
7582
if (visitType) {
@@ -94,7 +101,7 @@ export const VisitTypeSelector = ({
94101
gcTime: 10 * 60 * 1000,
95102
});
96103

97-
const { data: visitStarted, error: getVisitError } = useQuery({
104+
const { data: activeVisit, error: getVisitError } = useQuery({
98105
queryKey: [
99106
'getActiveVisitByPatient',
100107
visitPayload?.patient,
@@ -138,15 +145,14 @@ export const VisitTypeSelector = ({
138145
}
139146
};
140147

141-
const hasActiveVisit =
142-
visitStarted?.results && visitStarted.results.length > 0;
148+
const hasActiveVisit = activeVisit?.results && activeVisit.results.length > 0;
143149

144150
return (
145151
<div className={styles.opdVisitGroup}>
146152
<Button
147153
id="visit-button"
148154
kind="primary"
149-
disabled={isLoadingVisitTypes || visitTypesFromApi.length === 0}
155+
disabled={isLoadingVisitTypes || visitTypesArray.length === 0}
150156
onClick={() => handleVisitTypeChange(defaultVisitType)}
151157
>
152158
{!isLoadingVisitTypes && defaultVisitType
@@ -158,7 +164,7 @@ export const VisitTypeSelector = ({
158164
{!hasActiveVisit && (
159165
<Dropdown
160166
id="visit-dropdown"
161-
items={visitTypesFromApi.filter(
167+
items={visitTypesArray.filter(
162168
(vt) => vt.uuid !== defaultVisitType?.uuid,
163169
)}
164170
itemToString={(item) =>
@@ -169,7 +175,7 @@ export const VisitTypeSelector = ({
169175
type="inline"
170176
disabled={
171177
isLoadingVisitTypes ||
172-
visitTypesFromApi.length === 0 ||
178+
visitTypesArray.length === 0 ||
173179
hasActiveVisit
174180
}
175181
titleText=""

packages/bahmni-services/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export {
2222
type IdentifierTypesResponse,
2323
type CreatePatientRequest,
2424
type CreatePatientResponse,
25-
type CreateVisitRequest,
25+
type NewVisitData,
2626
type PatientName,
2727
type PatientAddress,
2828
type PatientIdentifier,

packages/bahmni-services/src/patientService/__tests__/patientService.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ describe('Patient Service', () => {
16721672
});
16731673

16741674
describe('getVisitTypes', () => {
1675-
it('should fetch and transform visit types correctly', async () => {
1675+
it('should fetch visit types correctly', async () => {
16761676
const mockResponse = {
16771677
visitTypes: {
16781678
OPD: 'c22a5000-3f10-11e4-adec-0800271c1b75',
@@ -1684,10 +1684,12 @@ describe('Patient Service', () => {
16841684
const result = await getVisitTypes();
16851685

16861686
expect(mockedGet).toHaveBeenCalledWith(VISIT_TYPES_URL());
1687-
expect(result).toEqual([
1688-
{ name: 'OPD', uuid: 'c22a5000-3f10-11e4-adec-0800271c1b75' },
1689-
{ name: 'IPD', uuid: 'd22a5000-3f10-11e4-adec-0800271c1b76' },
1690-
]);
1687+
expect(result).toEqual({
1688+
visitTypes: {
1689+
OPD: 'c22a5000-3f10-11e4-adec-0800271c1b75',
1690+
IPD: 'd22a5000-3f10-11e4-adec-0800271c1b76',
1691+
},
1692+
});
16911693
});
16921694
});
16931695

packages/bahmni-services/src/patientService/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export {
2222
type IdentifierTypesResponse,
2323
type CreatePatientRequest,
2424
type CreatePatientResponse,
25-
type CreateVisitRequest,
25+
type NewVisitData,
2626
type PatientName,
2727
type PatientAddress,
2828
type PatientIdentifier,

packages/bahmni-services/src/patientService/models.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ export interface AddressHierarchyResponse {
145145
}
146146

147147
export interface VisitType {
148-
name: string;
149-
uuid: string;
148+
visitTypes: Record<string, string>;
150149
}
151150

152-
export interface CreateVisitRequest {
151+
export interface NewVisitData {
153152
patient: string;
154153
visitType: string;
155154
location: string;
@@ -160,5 +159,5 @@ export interface VisitLocationResponse {
160159
}
161160

162161
export interface ActiveVisit {
163-
results: unknown[];
162+
results: string[];
164163
}

packages/bahmni-services/src/patientService/patientService.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
CreatePatientResponse,
3131
AddressHierarchyEntry,
3232
VisitType,
33-
CreateVisitRequest,
33+
NewVisitData,
3434
VisitLocationResponse,
3535
ActiveVisit,
3636
} from './models';
@@ -348,27 +348,19 @@ export const getAddressHierarchyEntries = async (
348348

349349
/**
350350
* Fetches visit types from Bahmni configuration
351-
* @returns Promise<VisitType[]> - Array of visit types
351+
* @returns Promise<VisitType> - Visit types response
352352
*/
353-
export const getVisitTypes = async (): Promise<VisitType[]> => {
354-
const response = await get<{ visitTypes: Record<string, string> }>(
355-
VISIT_TYPES_URL(),
356-
);
357-
return Object.entries(response.visitTypes).map(([name, uuid]) => ({
358-
name,
359-
uuid,
360-
}));
353+
export const getVisitTypes = async (): Promise<VisitType> => {
354+
return get<VisitType>(VISIT_TYPES_URL());
361355
};
362356

363357
/**
364358
* Create a new visit for a patient
365359
* @param visitData - The visit data including patient UUID, visit type, and location
366360
* @returns Promise<unknown> - The created visit object
367361
*/
368-
export const createVisit = async (
369-
visitData: CreateVisitRequest,
370-
): Promise<unknown> => {
371-
return post<unknown>(CREATE_VISIT_URL, visitData);
362+
export const createVisit = async (visitData: NewVisitData): Promise<string> => {
363+
return post<string>(CREATE_VISIT_URL, visitData);
372364
};
373365

374366
/**

0 commit comments

Comments
 (0)