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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 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 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 3x 5x 3x 3x 3x 5x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x | import { isNil, last } from 'lodash-es';
import { VariableType } from '@amalia/amalia-lang/tokens/types';
import {
OverwriteTypesEnum,
PaymentCategory,
type ComputedStatement,
type DatasetRow,
type Overwrite,
type PaymentAmountsByCategory,
type Statement,
} from '@amalia/core/types';
import { type RecordContent, type RecordContentPropertyType } from '@amalia/data-capture/connectors/types';
import { type FormatsEnum } from '@amalia/data-capture/fields/types';
import { type BaseCustomObjectDefinition } from '@amalia/data-capture/record-models/types';
import { type CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { replaceAt } from '@amalia/ext/lodash';
import { isCurrencyValue } from '@amalia/kernel/monetary/types';
import {
ComputedItemTypes,
type ComputedOverwrite,
type ComputedVariable,
type Dataset,
} from '@amalia/payout-calculation/types';
import { RuleType, type Relationship } from '@amalia/payout-definition/plans/types';
import { formatUserFullName } from '@amalia/tenants/users/types';
/**
* Get all fields that could be displayed in the current context.
*
* @param computedStatement
* @param dataset
*/
export const getAllFieldsInContext = (computedStatement: ComputedStatement, dataset: Dataset) => {
// Get the definition related to the selected dataset.
const definition = computedStatement.definitions.customObjects[dataset.customObjectDefinition.machineName] as
| BaseCustomObjectDefinition
| undefined;
const relationsIdToCustomObjectDefinition: Record<Relationship['id'], BaseCustomObjectDefinition> = {};
const relationsIdToRelationName: Record<Relationship['id'], Relationship['name']> = {};
const relationshipsLinked = dataset.relationMachineNames ?? [];
for (const relationshipLinked of relationshipsLinked) {
// Gautier: this is to fix a P1 but it didn't use to crash before, idk if something is broken in the backend.
const relationshipMachineName = last(relationshipLinked.split('.'))!;
// retrieve the custom object definition related to the relationship.
const relationshipFound = computedStatement.definitions.relationships[relationshipMachineName];
const relationCustomObjectDefinition =
computedStatement.definitions.customObjects[relationshipFound.toDefinitionMachineName];
const relationshipId = relationshipFound.id;
relationsIdToRelationName[relationshipId] = relationshipFound.name;
relationsIdToCustomObjectDefinition[relationshipId] = relationCustomObjectDefinition;
}
// Find labels and name for this definition.
const properties = definition?.properties
? Object.values(definition.properties)
.filter(Boolean)
.map((p) => ({ name: p.machineName, label: p.name }))
: [];
const computationItems = dataset.computedItems
// Find all computed object variables.
.filter((co) => co.variableType === VariableType.object)
.map((co) => ({
label: computedStatement.definitions.variables[co.variableMachineName].name,
name: co.variableMachineName,
}));
// Merge both.
return {
fieldsInContext: [...properties, ...computationItems],
relationsIdToCustomObjectDefinition,
relationsIdToRelationName,
};
};
export const applyOverwriteToStatementDataset = (rows: DatasetRow[], overwrite: Overwrite): DatasetRow[] => {
const index = rows.findIndex((row) => row.externalId === overwrite.appliesToExternalId);
// If we didn't find this row, return the untouched dataset.
if (index === -1) {
return rows;
}
if (overwrite.overwriteType === OverwriteTypesEnum.FILTER_ROW_REMOVE) {
return rows.filter((row) => row.externalId !== overwrite.appliesToExternalId);
}
const row = rows[index];
const newRow: DatasetRow = {
...row,
content: {
...row.content,
[overwrite.field]: (overwrite.overwriteValue as RecordContent)[overwrite.field],
},
overwrites: (row.overwrites ?? [])
// Remove existing overwrites on same field and same line
.filter((ov) => ov.field !== overwrite.field)
// And add the new one.
.concat(overwrite),
};
return replaceAt(rows, index, newRow);
};
export const clearOverwriteInStatementDataset = (rows: DatasetRow[], overwrite: Overwrite): DatasetRow[] => {
// If we clear a row added overwrite, we just remove the row.
if (overwrite.overwriteType === OverwriteTypesEnum.FILTER_ROW_ADD) {
return rows.filter((row) => row.externalId !== overwrite.appliesToExternalId);
}
const index = rows.findIndex((row) => row.externalId === overwrite.appliesToExternalId);
// If we didn't find this row, return the untouched dataset.
if (index === -1) {
return rows;
}
// Finding the overwrite to delete.
const row = rows[index];
const overwritesOnRow = row.overwrites;
const overwriteToRemoveIndex = overwritesOnRow?.findIndex((o) => o.id === overwrite.id) ?? -1;
if (overwriteToRemoveIndex === -1 || !overwritesOnRow) {
return rows;
}
const overwriteToRemove = overwritesOnRow[overwriteToRemoveIndex];
// Building the new row.
const newRow = {
...row,
// Put back the source value in the row.
content: {
...row.content,
[overwriteToRemove.field]: (overwriteToRemove.sourceValue as RecordContent | undefined)?.[
overwriteToRemove.field
],
},
// And delete the overwrite.
overwrites: overwritesOnRow.filter((_, index) => index !== overwriteToRemoveIndex),
};
return replaceAt(rows, index, newRow);
};
export const applyOverwriteToComputedStatement = (
computedStatement: ComputedStatement,
overwrite: Overwrite,
): ComputedStatement => {
const newStatement = { ...computedStatement };
// Replacing kpi.
// Build the computed overwrite from the overwrite.
const computedOverwrite: ComputedOverwrite = {
id: overwrite.id,
creator: formatUserFullName(overwrite.creator),
createdAt: overwrite.createdAt!, // createdAt should not be optional.
sourceValue: overwrite.sourceValue as RecordContentPropertyType,
overwriteValue: overwrite.overwriteValue as RecordContentPropertyType,
scope: overwrite.scope,
};
// Find the computed variable in the computed objects.
const index = computedStatement.computedObjects.findIndex(
(co) => co.type === ComputedItemTypes.VARIABLE && co.variableMachineName === overwrite.field,
);
// Immutable replace of the computedVariable in the statement.
newStatement.computedObjects = replaceAt(computedStatement.computedObjects, index, {
...computedStatement.computedObjects[index],
overwrite: computedOverwrite,
});
return newStatement;
};
export const removeOverwriteFromComputedStatement = (
computedStatement: ComputedStatement,
overwrite: ComputedOverwrite | Overwrite,
): ComputedStatement => ({
...computedStatement,
computedObjects: computedStatement.computedObjects.map((co) =>
co.overwrite?.id === overwrite.id
? {
...co,
currency: isCurrencyValue(overwrite.sourceValue) ? overwrite.sourceValue.symbol : co.currency,
// Put back the source value.
value: isNil(overwrite.sourceValue)
? undefined
: isCurrencyValue(overwrite.sourceValue)
? (overwrite.sourceValue.value ?? undefined)
: (overwrite.sourceValue as number),
// Delete the overwrite.
overwrite: undefined,
}
: co,
),
});
/**
* Check if statement contains a hold and release or split rule.
* @param statement
* @param paymentTotalByType
*/
export const showMultiPayouts = (
statement?: Statement | undefined,
paymentTotalByType?: PaymentAmountsByCategory,
): boolean => {
if (!paymentTotalByType || !statement) {
return false;
}
const containsHoldRules = statement.results.definitions.plan.rules.some((r) =>
[RuleType.HOLD_AND_RELEASE, RuleType.SPLIT].includes(r.type),
);
return (
(paymentTotalByType[PaymentCategory.hold] ?? 0) >= 0.5 ||
paymentTotalByType[PaymentCategory.achievement] !== paymentTotalByType[PaymentCategory.paid] ||
containsHoldRules
);
};
/**
* Get total result from a statement plan variable.
* @param statement
*/
export const getTotalFromStatementPlanVariable = (
statement: Statement,
):
| {
label: string;
value: number | null | undefined;
format: FormatsEnum;
currency: CurrencySymbolsEnum | undefined;
}
| undefined => {
const resultToTake = statement.resultSummary || statement.results;
const totalVariableId = resultToTake.definitions.plan?.totalVariableId;
if (!totalVariableId) {
return undefined;
}
if (!resultToTake.definitions.variables) return undefined;
const variables = resultToTake.definitions.variables;
const totalVariable = Object.values(variables).find((v) => v.id === totalVariableId);
if (!totalVariable) {
return undefined;
}
const totalComputedObject = resultToTake.computedObjects.find(
(co) => (co as ComputedVariable).variableMachineName === totalVariable.machineName,
);
return {
label: totalVariable.name,
value: totalComputedObject?.value as number,
format: totalVariable.format,
currency: totalComputedObject?.currency,
};
};
|