Skip to content

Commit adb96e9

Browse files
committed
add more queries
1 parent 0251099 commit adb96e9

File tree

3 files changed

+222
-20
lines changed

3 files changed

+222
-20
lines changed

src/lib/AnalysisApps/GWAS/Utils/cohortApi.test.tsx

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ import {
1616
GEN3_COHORT_MIDDLEWARE_API,
1717
useGetCohortDefinitionsQuery,
1818
useGetSourcesQuery,
19+
useGetSourceIdQuery,
1920
} from './cohortApi';
2021

2122
const server = setupServer();
2223

24+
const sourcesData = {
25+
sources: [{ source_id: 123, source_name: 'MVP-batch19000101' }],
26+
};
27+
2328
const cohortDefinitionAndStatsData = {
2429
cohort_definitions_and_stats: [
2530
{
@@ -132,12 +137,9 @@ describe('cohortApi', () => {
132137
});
133138

134139
it('test for successful useGetSources ', async () => {
135-
const data = {
136-
sources: [{ source_id: 123, source_name: 'MVP-batch19000101' }],
137-
};
138140
server.use(
139141
http.get(`${GEN3_COHORT_MIDDLEWARE_API}/sources`, () => {
140-
return HttpResponse.json(data);
142+
return HttpResponse.json(sourcesData);
141143
}),
142144
);
143145

@@ -151,7 +153,72 @@ describe('cohortApi', () => {
151153
isFetching: false,
152154
isSuccess: true,
153155
isLoading: false,
154-
data: data,
156+
data: sourcesData,
157+
});
158+
});
159+
160+
it('test for useGetSources handling error', async () => {
161+
server.use(
162+
http.get(`${GEN3_COHORT_MIDDLEWARE_API}/sources`, () => {
163+
return new HttpResponse(null, {
164+
status: 500,
165+
});
166+
}),
167+
);
168+
169+
const { result } = renderHook(() => useGetSourcesQuery());
170+
171+
expect(result.current.isFetching).toBe(true);
172+
173+
await waitFor(() => expect(result.current.isError).toBeTruthy());
174+
expect(result.current).toMatchObject({
175+
isError: true,
176+
isFetching: false,
177+
isSuccess: false,
178+
isLoading: false,
179+
error: { status: 500 },
180+
});
181+
});
182+
183+
it('test for successful useGetSourceIdQuery ', async () => {
184+
server.use(
185+
http.get(`${GEN3_COHORT_MIDDLEWARE_API}/sources`, () => {
186+
return HttpResponse.json(sourcesData);
187+
}),
188+
);
189+
190+
const { result } = renderHook(() => useGetSourceIdQuery());
191+
192+
expect(result.current.isFetching).toBe(true);
193+
194+
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
195+
expect(result.current).toMatchObject({
196+
isError: false,
197+
isFetching: false,
198+
isSuccess: true,
199+
isLoading: false,
200+
data: 123,
201+
});
202+
});
203+
204+
it('test for useGetSourceIdQuery error', async () => {
205+
server.use(
206+
http.get(`${GEN3_COHORT_MIDDLEWARE_API}/sources`, () => {
207+
return HttpResponse.json({ } );
208+
}),
209+
);
210+
211+
const { result } = renderHook(() => useGetSourceIdQuery());
212+
213+
expect(result.current.isFetching).toBe(true);
214+
215+
await waitFor(() => expect(result.current.isError).toBeTruthy());
216+
expect(result.current).toMatchObject({
217+
isError: true,
218+
isFetching: false,
219+
isSuccess: false,
220+
isLoading: false,
221+
data: undefined,
155222
});
156223
});
157224
});

src/lib/AnalysisApps/GWAS/Utils/cohortApi.tsx

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { gen3Api, GEN3_API} from '@gen3/core';
1+
import { gen3Api, GEN3_API } from '@gen3/core';
22

33
const TAGS = 'GWASApp';
44
export const GEN3_COHORT_MIDDLEWARE_API = `${GEN3_API}/cohort-middleware`;
@@ -16,6 +16,19 @@ interface CohortDefinitionQueryParams {
1616
selectedTeamProject: string;
1717
}
1818

19+
interface CovariateQueryParams {
20+
sourceId: number;
21+
cohortDefinitionId: string;
22+
selectedCovariateIds: Array<string>;
23+
}
24+
25+
interface ConceptStatsByHareSubset {
26+
sourceId: number;
27+
cohortDefinitionId: string;
28+
subsetCovariates: string;
29+
outcome: Array<string>;
30+
}
31+
1932
export interface GWASCohortDefinition {
2033
cohort_definition_id: number;
2134
cohort_name: string;
@@ -27,18 +40,29 @@ export interface GWASCohortDefinitionResponse {
2740
}
2841

2942
interface SourcesResponse {
30-
sources: Array<{source_id: string, source_name: string}>
43+
sources: Array<{ source_id: string; source_name: string }>;
3144
}
3245

33-
export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
34-
endpoints: (builder) => ({
46+
interface CovariateInformation {
47+
concept_id: number;
48+
prefixed_concept_id: string;
49+
concept_code: string;
50+
concept_name: string;
51+
concept_type: string;
52+
}
3553

54+
interface CovariateResponse {
55+
covariates: Array<CovariateInformation>;
56+
}
3657

37-
getCohortDefinitions: builder.query<GWASCohortDefinitionResponse, CohortDefinitionQueryParams>({
38-
query: ({
39-
sourceId,
40-
selectedTeamProject
41-
}) => `${GEN3_COHORT_MIDDLEWARE_API}/cohortdefinition-stats/by-source-id/${sourceId}/by-team-project?team-project=${selectedTeamProject}`,
58+
export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
59+
endpoints: (builder) => ({
60+
getCohortDefinitions: builder.query<
61+
GWASCohortDefinitionResponse,
62+
CohortDefinitionQueryParams
63+
>({
64+
query: ({ sourceId, selectedTeamProject }) =>
65+
`${GEN3_COHORT_MIDDLEWARE_API}/cohortdefinition-stats/by-source-id/${sourceId}/by-team-project?team-project=${selectedTeamProject}`,
4266
transformResponse: (response: Record<string, never>) => {
4367
// confirm data is valid
4468
if (!response || typeof response !== 'object') {
@@ -47,13 +71,15 @@ export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
4771
if (!('cohort_definitions_and_stats' in response)) {
4872
throw new Error('Missing field cohort_definitions_and_stats');
4973
}
50-
return { cohort_definitions_and_stats: response.cohort_definitions_and_stats };
74+
return {
75+
cohort_definitions_and_stats: response.cohort_definitions_and_stats,
76+
};
5177
},
5278
}),
53-
getSources: builder.query< SourcesResponse, void> ({
79+
getSources: builder.query<SourcesResponse, void>({
5480
query: () => `${GEN3_COHORT_MIDDLEWARE_API}/sources`,
5581
}),
56-
getSourceId: builder.query<string, void> ({
82+
getSourceId: builder.query<string, void>({
5783
query: () => `${GEN3_COHORT_MIDDLEWARE_API}/sources`,
5884
transformResponse: (response: SourcesResponse) => {
5985
if (Array.isArray(response?.sources) && response.sources.length === 1) {
@@ -63,14 +89,48 @@ export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
6389
${JSON.stringify(response?.sources)}`;
6490
throw new Error(message);
6591
}
66-
}
67-
})
92+
},
93+
}),
94+
getCovariates: builder.query<CovariateResponse, string>({
95+
query: (sourceId: string) => ({
96+
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept/by-source-id/${sourceId}/by-type`,
97+
method: 'POST',
98+
body: JSON.stringify({
99+
ConceptTypes: ['MVP Continuous'],
100+
}),
101+
}),
102+
}),
103+
getCovariateStats: builder.query<string, CovariateQueryParams>({
104+
query: (params: CovariateQueryParams) => ({
105+
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${params.sourceId}/by-cohort-definition-id/${params.cohortDefinitionId}`,
106+
method: 'POST',
107+
body: JSON.stringify({
108+
ConceptIds: params.selectedCovariateIds,
109+
}),
110+
}),
111+
}),
112+
getConceptStatsByHareSubset: builder.query<
113+
string,
114+
ConceptStatsByHareSubset
115+
>({
116+
query: (params: ConceptStatsByHareSubset) => {
117+
const variablesPayload = {
118+
variables: [params.outcome, ...params.subsetCovariates],
119+
};
120+
return {
121+
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${params.sourceId}/by-cohort-definition-id/${params.cohortDefinitionId}/breakdown-by-concept-id/${hareConceptId}`,
122+
method: 'POST',
123+
body: JSON.stringify(variablesPayload),
124+
};
125+
},
126+
}),
68127
}),
69128
});
70129

71-
72130
export const {
73131
useGetCohortDefinitionsQuery,
74132
useGetSourcesQuery,
75133
useGetSourceIdQuery,
134+
useGetCovariatesQuery,
135+
useGetCovariateStatsQuery,
76136
} = gwasCohortApi;

src/pages/api/cohorts.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,81 @@ export default function handler(req, res) {
2525
},
2626
];
2727

28+
const convariteResponce = {
29+
concepts: [
30+
{
31+
concept_id: 3000009001,
32+
prefixed_concept_id: 'ID_3000009001',
33+
concept_name: 'Body Mass Index (BMI) [MVP Demographics]',
34+
concept_code: '',
35+
concept_type: 'MVP Continuous',
36+
},
37+
{
38+
concept_id: 3000009002,
39+
prefixed_concept_id: 'ID_3000009002',
40+
concept_name: 'Body Mass Index squared [MVP Demographics]',
41+
concept_code: '',
42+
concept_type: 'MVP Continuous',
43+
},
44+
{
45+
concept_id: 3000010001,
46+
prefixed_concept_id: 'ID_3000010001',
47+
concept_name: 'Framingham risk score v1',
48+
concept_code: '',
49+
concept_type: 'MVP Continuous',
50+
},
51+
{
52+
concept_id: 3000010002,
53+
prefixed_concept_id: 'ID_3000010002',
54+
concept_name: 'Framingham risk score v2',
55+
concept_code: '',
56+
concept_type: 'MVP Continuous',
57+
},
58+
{
59+
concept_id: 3000010003,
60+
prefixed_concept_id: 'ID_3000010003',
61+
concept_name: 'Framingham risk score v3',
62+
concept_code: '',
63+
concept_type: 'MVP Continuous',
64+
},
65+
{
66+
concept_id: 3000010004,
67+
prefixed_concept_id: 'ID_3000010004',
68+
concept_name: 'Framingham risk score v4',
69+
concept_code: '',
70+
concept_type: 'MVP Continuous',
71+
},
72+
{
73+
concept_id: 3000010005,
74+
prefixed_concept_id: 'ID_3000010005',
75+
concept_name: 'Framingham risk score v5',
76+
concept_code: '',
77+
concept_type: 'MVP Continuous',
78+
},
79+
{
80+
concept_id: 3000011001,
81+
prefixed_concept_id: 'ID_3000011001',
82+
concept_name: 'Systolic blood pressure average [MVP Vitals]',
83+
concept_code: '',
84+
concept_type: 'MVP Continuous',
85+
},
86+
{
87+
concept_id: 3000011002,
88+
prefixed_concept_id: 'ID_3000011002',
89+
concept_name: 'Diastolic blood pressure average [MVP Vitals]',
90+
concept_code: '',
91+
concept_type: 'MVP Continuous',
92+
},
93+
{
94+
concept_id: 3000011003,
95+
prefixed_concept_id: 'ID_3000011003',
96+
concept_name: 'Resting heart rate [MVP Vitals]',
97+
concept_code: '',
98+
concept_type: 'MVP Continuous',
99+
},
100+
],
101+
};
102+
28103
// Send JSON response
29104
res.status(200).json(data);
30105
}

0 commit comments

Comments
 (0)