Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Use common filter definition and clean up filter transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
wpf500 committed Apr 25, 2024
1 parent ed6e26e commit a5cc102
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test": "jest --setupFiles dotenv/config"
},
"dependencies": {
"@beabee/beabee-common": "^0.20.3",
"@beabee/beabee-common": "^0.20.5",
"@captchafox/node": "^1.2.0",
"@inquirer/prompts": "^3.3.0",
"@sendgrid/mail": "^8.1.0",
Expand Down
25 changes: 14 additions & 11 deletions src/api/transformers/BaseContactTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ import {
PaginatedQuery,
Rule,
RuleGroup,
contactCalloutFilters,
contactFilters,
getCalloutFilters,
isRuleGroup
} from "@beabee/beabee-common";
import { isUUID } from "class-validator";
import { Brackets } from "typeorm";

import { createQueryBuilder, getRepository } from "@core/database";

import { individualAnswerFilterHandler } from "@api/transformers/BaseCalloutResponseTransformer";
import { BaseTransformer } from "@api/transformers/BaseTransformer";
import { prefixKeys } from "@api/utils";

import Callout from "@models/Callout";
import CalloutResponse from "@models/CalloutResponse";
import Contact from "@models/Contact";
import ContactProfile from "@models/ContactProfile";
import ContactRole from "@models/ContactRole";
import ContactContribution from "@models/ContactContribution";

import { FilterHandler, FilterHandlers } from "@type/filter-handlers";
import Callout from "@models/Callout";
import { isUUID } from "class-validator";
import { individualAnswerFilterHandler } from "./BaseCalloutResponseTransformer";
import CalloutResponse from "@models/CalloutResponse";

function flattenRules(rules: RuleGroup): Rule[] {
return rules.rules.flatMap((rule) =>
Expand Down Expand Up @@ -73,13 +75,14 @@ export abstract class BaseContactTransformer<
for (const calloutId of calloutIds) {
const callout = await getRepository(Callout).findOneBy({ id: calloutId });
if (callout) {
filters[`callouts.${calloutId}.hasAnswered`] = { type: "boolean" };

const calloutFilters = getCalloutFilters(callout.formSchema);
for (const key in calloutFilters) {
filters[`callouts.${calloutId}.responses.${key}`] =
calloutFilters[key];
}
Object.assign(
filters,
prefixKeys(`callouts.${calloutId}.`, contactCalloutFilters),
prefixKeys(
`callouts.${calloutId}.responses.`,
getCalloutFilters(callout.formSchema)
)
);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/api/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ export function groupBy<T>(
}
return result;
}

export function prefixKeys(
prefix: string,
obj: Record<string, unknown>
): Record<string, unknown> {
const newObj: any = {};
for (const key in obj) {
newObj[`${prefix}${key}`] = obj[key];
}
return newObj;
}

0 comments on commit a5cc102

Please sign in to comment.