diff --git a/src/routes/savedFilters.ts b/src/routes/savedFilters.ts index a58e4ab..6051561 100644 --- a/src/routes/savedFilters.ts +++ b/src/routes/savedFilters.ts @@ -12,6 +12,7 @@ import { updateAsDefault, } from '../db/dal/savedFilter'; import { + formatByTag, getFilterIDs, handleUniqueName, removeQueryFromFilters, @@ -46,7 +47,7 @@ savedFiltersRouter.get('/', async (req, res, next) => { } res.status(StatusCodes.OK).send(updatedResults); } else { - res.status(StatusCodes.OK).send(results); + res.status(StatusCodes.OK).send(formatByTag(results)); } } catch (e) { next(e); diff --git a/src/utils/savedFilters.ts b/src/utils/savedFilters.ts index 59f6ac0..dc11d7c 100644 --- a/src/utils/savedFilters.ts +++ b/src/utils/savedFilters.ts @@ -97,9 +97,12 @@ const getCount = (filter) => { return sequelizeConnection .query( - `SELECT count(*) from saved_filters where keycloak_id = :keycloak_id and title = :title and type = :type and tag = :tag ${ - filter.id ? 'and id <> :id' : '' - }`, + `SELECT count(*) + from saved_filters + where keycloak_id = :keycloak_id + and title = :title + and type = :type + and tag = :tag ${filter.id ? 'and id <> :id' : ''}`, { replacements, type: QueryTypes.SELECT, @@ -133,3 +136,15 @@ export const handleUniqueName = async (filter) => { }, }; }; + +export const formatByTag = (queries) => + queries.reduce((acc, query) => { + const { tag, ...rest } = query; + + if (!acc[tag]) { + acc[tag] = []; + } + + acc[tag].push(rest.dataValues); + return acc; + }, {});