Skip to content

Commit

Permalink
Merge pull request #86 from beabee-communityrm/hotfix/segment-tag-rules
Browse files Browse the repository at this point in the history
Hotfix(feat/contact-tagging): Delete tag rules with with unused tags
  • Loading branch information
JumpLink authored Nov 19, 2024
2 parents ebecdbf + 3b28682 commit 16453b7
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@ export class MigrateSegmentTagRules1731670066414 implements MigrationInterface {
let migratedCount = 0;
let skippedCount = 0;

const filterDeletedRules = (
rules: (Rule | RuleGroup)[]
): (Rule | RuleGroup)[] => {
return rules.filter((rule) => {
if ("field" in rule) {
if (rule.value.includes("__DELETE_ME__")) {
return false;
}
} else {
rule.rules = filterDeletedRules(rule.rules);
}
return true;
});
};

for (const segment of segments) {
try {
let modified = false;
const ruleGroup = segment.ruleGroup;

const processRules = (rules: (Rule | RuleGroup)[]): void => {
const processRules = (
rules: (Rule | RuleGroup)[]
): (Rule | RuleGroup)[] => {
for (const rule of rules) {
if ("field" in rule) {
if (
Expand All @@ -52,17 +69,20 @@ export class MigrateSegmentTagRules1731670066414 implements MigrationInterface {
console.warn(
`Tag not found: ${tagName} in segment: ${segment.id}`
);
return value;
modified = true;
return "__DELETE_ME__";
});
}
}
} else {
processRules(rule.rules);
rule.rules = processRules(rule.rules);
}
}

return filterDeletedRules(rules);
};

processRules(ruleGroup.rules);
ruleGroup.rules = processRules(ruleGroup.rules);

if (modified) {
await queryRunner.manager.update(
Expand Down

0 comments on commit 16453b7

Please sign in to comment.