Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
feat: Post search (#70)
Browse files Browse the repository at this point in the history
* feat: add post search 

Co-authored-by: zheyanyu <zheyanyu@amazon.com>
Co-authored-by: Nestor Carvantes <carvantes@gmail.com>
  • Loading branch information
3 people committed Apr 14, 2021
1 parent 61932e0 commit 0c29a2d
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions src/router/routes/genericResourceRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import express, { Router } from 'express';
import { Authorization, TypeOperation } from 'fhir-works-on-aws-interface';
import createError from 'http-errors';
import { isEmpty, mergeWith } from 'lodash';
import CrudHandlerInterface from '../handlers/CrudHandlerInterface';
import RouteHelper from './routeHelper';

Expand Down Expand Up @@ -127,31 +128,59 @@ export default class GenericResourceRoute {
}

if (this.operations.includes('search-type')) {
const handleSearch = async (res: express.Response, resourceType: string, searchParamQuery: any) => {
const allowedResourceTypes = await this.authService.getAllowedResourceTypesForOperation({
operation: 'search-type',
userIdentity: res.locals.userIdentity,
});

const response = await this.handler.typeSearch(
resourceType,
searchParamQuery,
allowedResourceTypes,
res.locals.userIdentity,
);
const updatedSearchResponse = await this.authService.authorizeAndFilterReadResponse({
operation: 'search-type',
userIdentity: res.locals.userIdentity,
readResponse: response,
});
return updatedSearchResponse;
};
// SEARCH
this.router.get(
'/',
RouteHelper.wrapAsync(async (req: express.Request, res: express.Response) => {
// Get the ResourceType looks like '/Patient'
const resourceType = req.baseUrl.substr(1);
const searchParamQuery = req.query;
const updatedSearchResponse = await handleSearch(res, resourceType, searchParamQuery);
res.send(updatedSearchResponse);
}),
);
this.router.post(
'/_search',
RouteHelper.wrapAsync(async (req: express.Request, res: express.Response) => {
// Get the ResourceType looks like '/Patient'
const resourceType = req.baseUrl.substr(1);
const searchParamQuery = req.query;
const { body } = req;

const allowedResourceTypes = await this.authService.getAllowedResourceTypesForOperation({
operation: 'search-type',
userIdentity: res.locals.userIdentity,
});
if (!isEmpty(body)) {
mergeWith(searchParamQuery, body, (valueOne, valueTwo) => {
if (
!isEmpty(valueOne) &&
!isEmpty(valueTwo) &&
(Array.isArray(valueOne) || Array.isArray(valueTwo) || valueOne !== valueTwo)
) {
return [...new Set([].concat(valueOne, valueTwo))];
}
return undefined; // Merging is handled by lodash mergeWith if undefined is returned
});
}

const response = await this.handler.typeSearch(
resourceType,
searchParamQuery,
allowedResourceTypes,
res.locals.userIdentity,
);
const updatedReadResponse = await this.authService.authorizeAndFilterReadResponse({
operation: 'search-type',
userIdentity: res.locals.userIdentity,
readResponse: response,
});
res.send(updatedReadResponse);
const updatedSearchResponse = await handleSearch(res, resourceType, searchParamQuery);
res.send(updatedSearchResponse);
}),
);
}
Expand Down

0 comments on commit 0c29a2d

Please sign in to comment.