diff --git a/src/app.ts b/src/app.ts index 8e69864..e1e4bc8 100644 --- a/src/app.ts +++ b/src/app.ts @@ -207,16 +207,27 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP res.status(StatusCodes.UNPROCESSABLE_ENTITY).send('Bad Inputs'); return; } + + if (!req.body?.queryPillSqons || req.body.queryPillSqons.length !== req.body.sqons.length) { + res.status(StatusCodes.UNPROCESSABLE_ENTITY).send('Bad Inputs'); + return; + } + // Convert sqon(s) with set_id if exists to intelligible sqon for ES query translation. const { resolvedSqons: sqons, m: mSetItToIds } = await resolveSetsInAllSqonsWithMapper( req.body.sqons, null, req.headers.authorization, ); - const data1 = await venn(sqons); + + const index = ['participant', 'file', 'biospecimen'].includes(req.body?.index) + ? req.body.index + : 'participant'; + + const data1 = await venn(sqons, index); const data2 = data1.map(x => ({ ...x, sqon: replaceIdsWithSetId(x.sqon, mSetItToIds) })); res.send({ - data: reformatVenn(data2), + data: reformatVenn(data2, req.body.queryPillSqons), }); } catch (e) { next(e); diff --git a/src/endpoints/venn/venn.ts b/src/endpoints/venn/venn.ts index dd85d0b..5e867b8 100644 --- a/src/endpoints/venn/venn.ts +++ b/src/endpoints/venn/venn.ts @@ -16,7 +16,7 @@ type OutputReformattedElement = Output & { }; type OutputReformatted = { - summary: OutputReformattedElement[]; + summary: (OutputReformattedElement & { queryPillSqon?: Sqon })[]; operations: OutputReformattedElement[]; }; @@ -86,21 +86,20 @@ const setFormulasTrio = (s1: Sqon, s2: Sqon, s3: Sqon) => [ }, ]; -let nestedFields: string[] = null; +//let nestedFields: string[] = null; -export const venn = async (sqons: Sqon[]): Promise => { +export const venn = async (sqons: Sqon[], index: string): Promise => { const setFormulas = sqons.length === 2 ? setFormulasDuo(sqons[0], sqons[1]) : setFormulasTrio(sqons[0], sqons[1], sqons[2]); const client = EsInstance.getInstance(); - const needToFetchMapping = !nestedFields || nestedFields.length === 0; - if (needToFetchMapping) { - nestedFields = await getNestedFieldsForIndex(client, 'participant_centric'); - } + const indexName = `${index}_centric`; + //const needToFetchMapping = !nestedFields || nestedFields.length === 0; + const nestedFields = await getNestedFieldsForIndex(client, indexName); const mSearchBody = setFormulas .map(x => [ - {}, + { index: indexName }, { track_total_hits: true, size: 0, @@ -124,11 +123,17 @@ export const venn = async (sqons: Sqon[]): Promise => { })); }; -export const reformatVenn = (data: Output[]): OutputReformatted => { +export const reformatVenn = (data: Output[], queryPillSqons: Sqon[]): OutputReformatted => { const tables = data.reduce( (xs: OutputReformatted, x: OutputReformattedElement) => { + const queryPillSqon = { + ['Q₁']: queryPillSqons[0], + ['Q₂']: queryPillSqons[1], + ['Q₃']: queryPillSqons[2], + }; + if (['Q₁', 'Q₂', 'Q₃'].some(y => y === x.operation)) { - return { ...xs, summary: [...xs.summary, x] }; + return { ...xs, summary: [...xs.summary, { ...x, queryPillSqon: queryPillSqon[x.operation] }] }; } return { ...xs, operations: [...xs.operations, x] }; },