Skip to content

Commit

Permalink
Merge pull request #141 from Lightning-Flow-Scanner/fix/dynamic-rule-…
Browse files Browse the repository at this point in the history
…invocation

feat(RuleEngine): make rule imports for non default rule store rules
  • Loading branch information
junners authored Nov 16, 2024
2 parents e287716 + 865489f commit e81f85a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/main/libs/DynamicRule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { DefaultRuleStore } from "../store/DefaultRuleStore";

import { RuleLoader } from "./RuleLoader";
import * as p from "path-browserify";
export class DynamicRule {
constructor(className: string) {
if (DefaultRuleStore[className] === undefined || DefaultRuleStore[className] === null) {
const customRule = RuleLoader.loadCustomRule(
className,
p.join(__dirname, `../rules/${className}`)
);
if (customRule) {
return customRule;
}
throw new Error(`Rule \'${className}\' does not exist in the store.`);
}
return new DefaultRuleStore[className]();
Expand Down
39 changes: 38 additions & 1 deletion tests/SameRecordFieldUpdates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import "mocha";

import { ParsedFlow } from "../src/main/models/ParsedFlow";
import { SameRecordFieldUpdates } from "../src/main/rules/SameRecordFieldUpdates";
import { RuleResult, Flow } from "../src";
import { RuleResult, Flow, parse, scan, ScanResult } from "../src";
import * as path from "path-browserify";

describe("SameRecordFieldUpdates", () => {
let expect;
Expand Down Expand Up @@ -133,4 +134,40 @@ describe("SameRecordFieldUpdates", () => {

expect(ruleResult.occurs).to.be.false;
});

it("should not trigger from default configuration on store", async () => {
let example_uri1 = path.join(__dirname, "./xmlfiles/Same_Record_Field_Updates.flow-meta.xml");
let flows = await parse([example_uri1]);
const ruleConfig = {
rules: {},
exceptions: {},
};
const results: ScanResult[] = scan(flows, ruleConfig);
const scanResults = results.pop();

scanResults?.ruleResults.forEach((rule) => {
expect(rule.occurs).to.be.false;
});
});

it("should trigger when opt-in configuration", async () => {
let example_uri1 = path.join(__dirname, "./xmlfiles/Same_Record_Field_Updates.flow-meta.xml");
let flows = await parse([example_uri1]);
const ruleConfig = {
rules: {
SameRecordFieldUpdates: {
severity: "error",
},
},
exceptions: {},
};
const results: ScanResult[] = scan(flows, ruleConfig);
const scanResults = results.pop();

const expectedRule = scanResults?.ruleResults.find(
(rule) => rule.ruleName === "SameRecordFieldUpdates"
);
expect(expectedRule).to.be.ok;
expect(expectedRule?.occurs).to.be.true;
});
});
52 changes: 52 additions & 0 deletions tests/xmlfiles/Same_Record_Field_Updates.flow-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<description>test</description>
<environments>Default</environments>
<interviewLabel>Test Flow {!$Flow.CurrentDateTime}</interviewLabel>
<label>Test Flow</label>
<processMetadataValues>
<name>BuilderType</name>
<value>
<stringValue>LightningFlowBuilder</stringValue>
</value>
</processMetadataValues>
<processMetadataValues>
<name>CanvasMode</name>
<value>
<stringValue>AUTO_LAYOUT_CANVAS</stringValue>
</value>
</processMetadataValues>
<processMetadataValues>
<name>OriginBuilderType</name>
<value>
<stringValue>LightningFlowBuilder</stringValue>
</value>
</processMetadataValues>
<processType>AutoLaunchedFlow</processType>
<recordUpdates>
<description>test</description>
<name>Update_triggering_records</name>
<label>Update triggering records</label>
<locationX>176</locationX>
<locationY>287</locationY>
<inputAssignments>
<field>Active__c</field>
<value>
<stringValue>Yes</stringValue>
</value>
</inputAssignments>
<inputReference>$Record</inputReference>
</recordUpdates>
<start>
<locationX>50</locationX>
<locationY>0</locationY>
<connector>
<targetReference>Update_triggering_records</targetReference>
</connector>
<object>Account</object>
<recordTriggerType>Create</recordTriggerType>
<triggerType>RecordBeforeSave</triggerType>
</start>
<status>Active</status>
</Flow>

0 comments on commit e81f85a

Please sign in to comment.