-
Notifications
You must be signed in to change notification settings - Fork 32
fix: accept v3 fields in filters when using v3 jobs #2321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1040,6 +1040,9 @@ export const datasetsFullQueryDescriptionFields = | |
| export const jobsFullQueryExampleFields = | ||
| '{"ownerGroup": "group1", "statusCode": "jobCreated"}'; | ||
|
|
||
| export const jobsFullQueryExampleFieldsV3 = | ||
| '{"emailJobInitiator": "[email protected]", "jobStatusMessage": "jobCreated"}'; | ||
|
|
||
| export const jobsFullQueryDescriptionFields = | ||
| '<pre>\n \ | ||
| {\n \ | ||
|
|
@@ -1059,6 +1062,20 @@ export const jobsFullQueryDescriptionFields = | |
| }\n \ | ||
| </pre>'; | ||
|
|
||
| export const jobsFullQueryDescriptionFieldsV3 = | ||
| '<pre>\n \ | ||
| {\n \ | ||
| "creationTime": { <optional>\n \ | ||
| "begin": string,\n \ | ||
| "end": string,\n \ | ||
| },\n \ | ||
| "type": string, <optional>\n \ | ||
| "id": string, <optional>\n \ | ||
| "jobStatusMessage": string, <optional>\n \ | ||
| ... <optional>\n \ | ||
| }\n \ | ||
| </pre>'; | ||
|
|
||
| export const proposalsFullQueryExampleFields = | ||
| '{"text": "some text", "proposalId": "proposal_id"}'; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { PipeTransform, Injectable, ArgumentMetadata } from "@nestjs/common"; | ||
| import { jobV3toV4FieldMap } from "../types/jobs-filter-content"; | ||
| import _ from "lodash"; | ||
|
|
||
| type KeyMap = Record<string, string>; | ||
|
|
||
| type Func = (value: unknown) => unknown; | ||
|
|
||
| type FuncMap = Record<string, Func>; | ||
|
|
||
| interface TransformDeepOptions { | ||
| keyMap?: KeyMap; | ||
| funcMap?: FuncMap; | ||
| arrayFn?: Func; | ||
| valueFn?: Func; | ||
| } | ||
|
|
||
| const transformDeep = ( | ||
| obj: unknown, | ||
| opts: TransformDeepOptions = {}, | ||
| ): unknown => { | ||
| const { keyMap = {}, funcMap = {}, arrayFn, valueFn } = opts; | ||
|
|
||
| if (Array.isArray(obj)) { | ||
| return obj.map((item) => | ||
| arrayFn ? arrayFn(transformDeep(item, opts)) : transformDeep(item, opts), | ||
| ); | ||
| } | ||
|
|
||
| if (obj && typeof obj === "object") { | ||
| const newObj: Record<string, unknown> = {}; | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| const mappedKey = keyMap[key] ?? key; | ||
| let transformed: unknown; | ||
| if (funcMap[key]) { | ||
| transformed = funcMap[key](value); | ||
| } else { | ||
| transformed = transformDeep(value, opts); | ||
| } | ||
| newObj[mappedKey] = valueFn ? valueFn(transformed) : transformed; | ||
| } | ||
| return newObj; | ||
| } | ||
|
|
||
| return obj; | ||
| }; | ||
|
|
||
| class ParseJsonPipe implements PipeTransform<string, string> { | ||
| transform(value: string): string { | ||
| if (!value || typeof value !== "string") return value; | ||
| try { | ||
| return JSON.parse(value); | ||
| } catch { | ||
| return value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class ParseDeepJsonPipe implements PipeTransform<string, string | object> { | ||
| private jsonPipe = new ParseJsonPipe(); | ||
|
|
||
| transform(value: string): string | object { | ||
| const parsed = this.jsonPipe.transform(value); | ||
| return transformDeep(parsed, { | ||
| valueFn: (value) => this.jsonPipe.transform(value as string), | ||
| }) as object; | ||
| } | ||
| } | ||
|
|
||
| class ReplaceObjKeysPipe implements PipeTransform<unknown, unknown> { | ||
| constructor(private keyMap: KeyMap) {} | ||
|
|
||
| transform(value: unknown): unknown { | ||
| return transformDeep(value, { keyMap: this.keyMap }); | ||
| } | ||
| } | ||
|
|
||
| class TransformObjValuesPipe implements PipeTransform<unknown, unknown> { | ||
| constructor(private funcMap: FuncMap) {} | ||
|
|
||
| transform(value: unknown): unknown { | ||
| return transformDeep(value, { funcMap: this.funcMap }); | ||
| } | ||
| } | ||
|
|
||
| class TransformArrayValuesPipe implements PipeTransform<unknown, unknown> { | ||
| constructor(private arrayFn: (item: unknown) => unknown) {} | ||
|
|
||
| transform(value: unknown): unknown { | ||
| return transformDeep(value, { arrayFn: this.arrayFn }); | ||
| } | ||
| } | ||
|
|
||
| class ComposePipe<T = unknown> implements PipeTransform<T, T> { | ||
| private readonly pipes: PipeTransform[]; | ||
| private readonly jsonToString = new JsonToStringPipe(); | ||
| private readonly parseDeepJson = new ParseDeepJsonPipe(); | ||
|
|
||
| constructor( | ||
| pipes: PipeTransform[], | ||
| private readonly jsonTransform = true, | ||
| ) { | ||
| this.pipes = [...pipes]; | ||
| if (this.jsonTransform) { | ||
| this.pipes.unshift(this.parseDeepJson); | ||
| this.pipes.push(this.jsonToString); | ||
| } | ||
| } | ||
|
|
||
| transform(value: T, metadata: ArgumentMetadata = {} as ArgumentMetadata): T { | ||
| return this.pipes.reduce( | ||
| (val, pipe) => pipe.transform(val, metadata), | ||
| value, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class JsonToStringPipe implements PipeTransform<object, string | object> { | ||
| transform(value: object): string | object { | ||
| try { | ||
| return JSON.stringify(value); | ||
| } catch { | ||
| return value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class V3ConditionToV4Pipe extends ComposePipe<object> { | ||
| // it replaces object keys following the keyMappings object | ||
| // for example, it replaces keys from the v3 DTO (user-facing) | ||
| // to database fields later used in the aggregation pipeline | ||
| // for example from {where: {user-facing-1: 'abc'} to {where: {db-field1: 'abc'} | ||
|
|
||
| constructor(keyMappings = jobV3toV4FieldMap, jsonTransform = true) { | ||
| super([new ReplaceObjKeysPipe(keyMappings)], jsonTransform); | ||
| } | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class V3LimitsToV4Pipe extends ComposePipe<object> { | ||
| // it replaces list elements following the <keyMappings object>:asc|desc | ||
| // for example, it replaces {order: ['user-facing1:asc', 'user-facing2:asc']} | ||
| // with {order: ['db-field1:asc', 'db-field2:asc']} | ||
|
|
||
| constructor(keyMappings = jobV3toV4FieldMap, jsonTransform = true) { | ||
| const sortToOrderPipe = new TransformObjValuesPipe({ | ||
| order: (value: unknown) => { | ||
| const isArray = _.isArray(value); | ||
| const order = (isArray ? value : [value]).reduce((acc, orderValue) => { | ||
| const [field, direction] = (orderValue as string).split(":"); | ||
| return acc.concat(`${keyMappings[field]}:${direction ?? "asc"}`); | ||
| }, [] as string[]); | ||
| return isArray ? order : order[0]; | ||
| }, | ||
| }); | ||
| super([sortToOrderPipe], jsonTransform); | ||
| } | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class V3FieldsToV4Pipe extends ComposePipe<object> { | ||
| // it replaces list elements following the keyMappings object | ||
| // for example, it replaces the fields: [user-facing1, user-facing2] | ||
| // with [db-field1, db-field2] | ||
|
|
||
| constructor(keyMappings = jobV3toV4FieldMap, jsonTransform = true) { | ||
| super( | ||
| [ | ||
| new TransformArrayValuesPipe((item) => { | ||
| if (_.isString(item) && keyMappings[item]) return keyMappings[item]; | ||
| return item; | ||
| }), | ||
| ], | ||
| jsonTransform, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class V3FilterToV4Pipe extends ComposePipe<string> { | ||
| // it combines the 3 pipes together | ||
| // for example | ||
| // from {where: {user-facing1: 'abc'}, limits: {order: ['user-facing1:asc']}, fields: ['user-facing1']} | ||
| // to {where: {db-field1: 'abc'}, limits: {order: ['db-field1:asc']}, fields: ['db-field1']} | ||
|
|
||
| constructor(keyMappings = jobV3toV4FieldMap, jsonTransform = true) { | ||
| super( | ||
| [ | ||
| new V3LimitsToV4Pipe(keyMappings, false), | ||
| new V3ConditionToV4Pipe(keyMappings, false), | ||
| new V3FieldsToV4Pipe(keyMappings, false), | ||
| ], | ||
| jsonTransform, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.