Skip to content

Commit 99c0245

Browse files
authored
Merge pull request #936 from nktnet1/feat/sourceRecordsFilter
feat: sourceRecordsFilter option added
2 parents e5ec08b + 2158490 commit 99c0245

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

messages/resources.json

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
"writingToFile": "{%s} Creating the file %s ...",
192192
"nothingUpdated": "Nothing was updated.",
193193
"skippedUpdatesWarning": "{%s} %s target records remained untouched, since they do not differ from the corresponding source records.",
194+
"skippedSourceRecordsFilterWarning": "One 'sourceRecordsFilter' could not be applied: %s.",
194195
"skippedTargetRecordsFilterWarning": "One 'targetRecordsFilter' could not be applied: %s.",
195196
"missingParentLookupsPrompt": "{%s} %s missing parent lookup records were found. See %s file for the details.",
196197
"updatingSummary": "Data processing summary.",

src/addons/modules/sfdmu-run/custom-addons/package/ISfdmuRunCustomAddonScriptObject.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default interface ISfdmuRunCustomAddonScriptObject {
3030
hardDelete?: boolean;
3131
updateWithMockData?: boolean;
3232
//mockCSVData?: boolean;
33+
sourceRecordsFilter?: string;
3334
targetRecordsFilter?: string;
3435
excluded?: boolean;
3536
useCSVValuesMapping?: boolean;

src/modules/components/common_components/common.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1860,5 +1860,30 @@ export class Common {
18601860
return new Promise(resolve => setTimeout(resolve, time));
18611861
}
18621862

1863+
private static wrapWhereClauseInParenthesis(clause: WhereClause): { beginClause: WhereClause, endClause: WhereClause } {
1864+
const clone = JSON.parse(JSON.stringify(clause)) as WhereClause;
1865+
clone.left.openParen = (clone.left.openParen ?? 0) + 1
1866+
let current = clone;
1867+
while (current.right) {
1868+
current = current.right;
1869+
}
1870+
current.left.closeParen = (current.left.closeParen || 0) + 1
1871+
return { beginClause: clone, endClause: current };
1872+
}
1873+
1874+
static mergeWhereClauses(
1875+
where1?: WhereClause,
1876+
where2?: WhereClause,
1877+
operator: LogicalOperator = 'AND',
1878+
): WhereClause | undefined {
1879+
if (!where1 || !where2) return where1 || where2;
18631880

1881+
const { beginClause: wrappedWhere1, endClause: endClause1 } = Common.wrapWhereClauseInParenthesis(where1);
1882+
const { beginClause: wrappedWhere2 } = Common.wrapWhereClauseInParenthesis(where2);
1883+
1884+
endClause1.operator = operator;
1885+
endClause1.right = wrappedWhere2;
1886+
1887+
return wrappedWhere1;
1888+
}
18641889
}

src/modules/components/common_components/logger.ts

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export enum RESOURCES {
212212
writingToFile = "writingToFile",
213213
nothingUpdated = "nothingUpdated",
214214
skippedUpdatesWarning = "skippedUpdatesWarning",
215+
skippedSourceRecordsFilterWarning = "skippedSourceRecordsFilterWarning",
215216
skippedTargetRecordsFilterWarning = "skippedTargetRecordsFilterWarning",
216217
missingParentLookupsPrompt = "missingParentLookupsPrompt",
217218
updatingSummary = "updatingSummary",

src/modules/models/job_models/migrationJobTask.ts

+8
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,14 @@ export default class MigrationJobTask {
637637
if (isTargetQuery) {
638638
// Fix target query
639639
___filterTargetQuery(tempQuery);
640+
} else if (this.scriptObject.sourceRecordsFilter) {
641+
// Add any extra filter conditions to the source query
642+
try {
643+
const additionalWhereClause = parseQuery(`SELECT Id FROM ${this.sObjectName} WHERE ${this.scriptObject.sourceRecordsFilter}`).where;
644+
tempQuery.where = Common.mergeWhereClauses(tempQuery.where, additionalWhereClause);
645+
} catch (ex) {
646+
self.logger.warn(RESOURCES.skippedSourceRecordsFilterWarning, ex.message);
647+
}
640648
}
641649

642650
let query = composeQuery(tempQuery);

src/modules/models/script_models/scriptObject.ts

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default class ScriptObject implements ISfdmuRunScriptObject {
7878
hardDelete: boolean = false;
7979
updateWithMockData: boolean = false;
8080
//mockCSVData: boolean = false;
81+
sourceRecordsFilter: string = "";
8182
targetRecordsFilter: string = "";
8283
excluded: boolean = false;
8384
useCSVValuesMapping: boolean = false;

0 commit comments

Comments
 (0)