Skip to content

Commit

Permalink
Merge pull request #936 from nktnet1/feat/sourceRecordsFilter
Browse files Browse the repository at this point in the history
feat: sourceRecordsFilter option added
  • Loading branch information
hknokh authored Nov 28, 2024
2 parents e5ec08b + 2158490 commit 99c0245
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions messages/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
"writingToFile": "{%s} Creating the file %s ...",
"nothingUpdated": "Nothing was updated.",
"skippedUpdatesWarning": "{%s} %s target records remained untouched, since they do not differ from the corresponding source records.",
"skippedSourceRecordsFilterWarning": "One 'sourceRecordsFilter' could not be applied: %s.",
"skippedTargetRecordsFilterWarning": "One 'targetRecordsFilter' could not be applied: %s.",
"missingParentLookupsPrompt": "{%s} %s missing parent lookup records were found. See %s file for the details.",
"updatingSummary": "Data processing summary.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default interface ISfdmuRunCustomAddonScriptObject {
hardDelete?: boolean;
updateWithMockData?: boolean;
//mockCSVData?: boolean;
sourceRecordsFilter?: string;
targetRecordsFilter?: string;
excluded?: boolean;
useCSVValuesMapping?: boolean;
Expand Down
25 changes: 25 additions & 0 deletions src/modules/components/common_components/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1860,5 +1860,30 @@ export class Common {
return new Promise(resolve => setTimeout(resolve, time));
}

private static wrapWhereClauseInParenthesis(clause: WhereClause): { beginClause: WhereClause, endClause: WhereClause } {
const clone = JSON.parse(JSON.stringify(clause)) as WhereClause;
clone.left.openParen = (clone.left.openParen ?? 0) + 1
let current = clone;
while (current.right) {
current = current.right;
}
current.left.closeParen = (current.left.closeParen || 0) + 1
return { beginClause: clone, endClause: current };
}

static mergeWhereClauses(
where1?: WhereClause,
where2?: WhereClause,
operator: LogicalOperator = 'AND',
): WhereClause | undefined {
if (!where1 || !where2) return where1 || where2;

const { beginClause: wrappedWhere1, endClause: endClause1 } = Common.wrapWhereClauseInParenthesis(where1);
const { beginClause: wrappedWhere2 } = Common.wrapWhereClauseInParenthesis(where2);

endClause1.operator = operator;
endClause1.right = wrappedWhere2;

return wrappedWhere1;
}
}
1 change: 1 addition & 0 deletions src/modules/components/common_components/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export enum RESOURCES {
writingToFile = "writingToFile",
nothingUpdated = "nothingUpdated",
skippedUpdatesWarning = "skippedUpdatesWarning",
skippedSourceRecordsFilterWarning = "skippedSourceRecordsFilterWarning",
skippedTargetRecordsFilterWarning = "skippedTargetRecordsFilterWarning",
missingParentLookupsPrompt = "missingParentLookupsPrompt",
updatingSummary = "updatingSummary",
Expand Down
8 changes: 8 additions & 0 deletions src/modules/models/job_models/migrationJobTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ export default class MigrationJobTask {
if (isTargetQuery) {
// Fix target query
___filterTargetQuery(tempQuery);
} else if (this.scriptObject.sourceRecordsFilter) {
// Add any extra filter conditions to the source query
try {
const additionalWhereClause = parseQuery(`SELECT Id FROM ${this.sObjectName} WHERE ${this.scriptObject.sourceRecordsFilter}`).where;
tempQuery.where = Common.mergeWhereClauses(tempQuery.where, additionalWhereClause);
} catch (ex) {
self.logger.warn(RESOURCES.skippedSourceRecordsFilterWarning, ex.message);
}
}

let query = composeQuery(tempQuery);
Expand Down
1 change: 1 addition & 0 deletions src/modules/models/script_models/scriptObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class ScriptObject implements ISfdmuRunScriptObject {
hardDelete: boolean = false;
updateWithMockData: boolean = false;
//mockCSVData: boolean = false;
sourceRecordsFilter: string = "";
targetRecordsFilter: string = "";
excluded: boolean = false;
useCSVValuesMapping: boolean = false;
Expand Down

0 comments on commit 99c0245

Please sign in to comment.