Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 1x 1x 56x 56x 1x 1x 1x 1x 1x 1x 7x 7x 7x 4x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 84x 84x 84x 64x 84x 84x 84x 51x 51x 33x 33x 33x 84x 33x 33x 33x 1x 1x 1x 1x 1x 1x 54x 54x 1x 1x 1x 1x 1x 1x 128x 128x 128x 1x | import { isEqual } from 'lodash-es';
import { v4 as uuid } from 'uuid';
import {
FormulaBuilderBlockType,
FormulaBuilderLogicalOperatorType,
type AmaliaFormula,
} from '@amalia/amalia-lang/formula/types';
import { type Variable } from '@amalia/amalia-lang/tokens/types';
import { CalculationType } from '@amalia/core/types';
import { type BaseCustomObjectDefinition } from '@amalia/data-capture/record-models/types';
import { assert } from '@amalia/ext/typescript';
import { type Filter, type Plan, type Relationship, type Rule } from '@amalia/payout-definition/plans/types';
export class DefinitionsContainer<
TPlan extends Pick<Plan, 'forecast' | 'id' | 'isForecasted'> = Plan,
TRelationship extends Pick<Relationship, 'id' | 'name'> = Relationship,
TFilter extends Pick<Filter, 'id' | 'machineName' | 'planId'> = Filter,
TVariable extends Pick<Variable, 'id' | 'machineName' | 'planId'> = Variable,
> {
private readonly calculationType?: CalculationType;
private readonly plan: TPlan | null = null;
private readonly filters: TFilter[] = [];
private readonly rules: Rule[] = [];
private readonly variables: TVariable[] = [];
private readonly customObjectDefinitions: BaseCustomObjectDefinition[] = [];
private readonly relationships: TRelationship[] = [];
public constructor(options: {
calculationType?: CalculationType;
plan: TPlan | null;
filters: TFilter[];
rules: Rule[];
variables: TVariable[];
customObjectDefinitions: BaseCustomObjectDefinition[];
relationships: TRelationship[];
}) {
this.calculationType = options.calculationType;
this.plan = options.plan ?? null;
this.filters = options.filters;
this.rules = options.rules;
this.variables = options.variables;
this.customObjectDefinitions = options.customObjectDefinitions;
this.relationships = options.relationships;
}
public getPlan(): TPlan | null {
return this.plan;
}
public getRuleByMachineName(machineName: string, plan: TPlan | null): Rule | undefined {
// Either get global rule or plan scoped rule.
return (
this.rules.find((r) => r.machineName === machineName && r.planId === plan?.id) ||
this.rules.find((r) => r.machineName === machineName && r.planId === null)
);
}
public getRuleById(id: string): Rule | undefined {
return this.rules.find((r) => r.id === id);
}
public getFilter(machineName: string, plan: TPlan | null): TFilter | undefined {
// First try to find a scoped filter.
let filter =
this.filters.find((f) => f.machineName === machineName && f.planId === plan?.id) ||
// Then try a global filter.
this.filters.find((f) => f.machineName === machineName && f.planId === null);
// If filter is forecasted, we replace its condition by the forecasted one.
if (this.calculationType === CalculationType.FORECAST && plan?.isForecasted) {
assert(filter, `Filter ${machineName} not found in StatementCalculationCache`);
const forecastedCondition = plan.forecast?.forecastConfig?.datasets?.[filter.id];
if (forecastedCondition) {
filter = {
...filter,
condition: forecastedCondition,
formulaBuilder: {
root: {
id: uuid(),
type: FormulaBuilderBlockType.LOGICAL_OPERATOR,
logicalOperator: FormulaBuilderLogicalOperatorType.AND,
operands: [
{
id: uuid(),
type: FormulaBuilderBlockType.LOGICAL_OPERATOR,
logicalOperator: FormulaBuilderLogicalOperatorType.AND,
operands: [
{
id: uuid(),
type: FormulaBuilderBlockType.FORMULA,
label: '',
formula: forecastedCondition,
},
],
},
],
},
},
};
}
}
return filter;
}
public getFilterById(id: string): TFilter | undefined {
return this.filters.find((f) => f.id === id);
}
public getVariables(): TVariable[] {
return this.variables;
}
public getVariable(machineName: string, plan: TPlan | null): TVariable | null {
// First try to find a scoped variable.
let variable =
this.variables.find((v) => v.machineName === machineName && v.planId === plan?.id) ||
// Then try a global variable.
this.variables.find((v) => v.machineName === machineName && v.planId === null);
if (!variable) {
return null;
}
// If variable is forecasted, we replace its formula by the forecasted one.
let forecastedFormula: AmaliaFormula | undefined;
if (this.calculationType === CalculationType.FORECAST && plan?.isForecasted) {
forecastedFormula =
plan.forecast?.forecastConfig?.fields?.[variable.id] ?? plan.forecast?.forecastConfig?.kpis?.[variable.id];
if (forecastedFormula) {
variable = {
...variable,
formula: forecastedFormula,
};
}
}
return variable;
}
public getVariableById(id: string): TVariable | undefined {
return this.variables.find((v) => v.id === id);
}
public getCustomObjectDefinitions() {
return this.customObjectDefinitions;
}
public getCustomObjectDefinitionByMachineName(machineName: string): BaseCustomObjectDefinition | undefined {
return this.customObjectDefinitions.find((c) => c.machineName === machineName);
}
public getCustomObjectDefinitionById(id: string): BaseCustomObjectDefinition | undefined {
return this.customObjectDefinitions.find((c) => c.id === id);
}
public getRelationships(): TRelationship[] {
return this.relationships;
}
public getRelationshipFromName(name?: string): TRelationship | undefined {
if (!name) {
return undefined;
}
return this.relationships.find((r) => isEqual(r.name, name));
}
}
|