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 | 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 4x 4x 4x 4x 4x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 3x 3x 3x 3x 3x 3x 13x 6x 6x 6x 6x 6x 6x 13x 4x 4x 4x 4x 4x 4x 4x 4x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 2x 2x 4x 3x 3x 3x 3x 3x 3x 3x 3x 1x 5x 5x 5x 5x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 13x 13x 13x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 13x 13x 13x 4x 4x 4x 4x 4x 4x 4x 4x 9x 11x 9x 11x 2x 2x 7x 14x 7x 7x 7x 1x | import {
ForbiddenException,
Injectable,
InternalServerErrorException,
Logger,
NotFoundException,
} from '@nestjs/common';
import { pick } from 'lodash-es';
import { DataSource } from 'typeorm';
import { type Overwrite, type Statement, type Variable } from '@amalia/core/models';
import { isBeingReviewed, OverwriteTypesEnum } from '@amalia/core/types';
import { RecordContent, RecordContentPropertyType } from '@amalia/data-capture/connectors/types';
import { OverwritesService } from '@amalia/data-correction/overwrites/core';
import { OverwriteScopeEnum } from '@amalia/data-correction/overwrites/types';
import { assert, toError } from '@amalia/ext/typescript';
import {
canOverwriteOnAllStatements,
canOverwriteThisStatement,
canSimulateThisForecastedStatement,
canViewThisStatement,
defineAbilityFor,
} from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { isCurrencyValue } from '@amalia/kernel/monetary/types';
import { type ComputedItem, type ComputedVariable } from '@amalia/payout-calculation/types';
import { VariablesService } from '@amalia/payout-definition/designer/core';
import { type Company } from '@amalia/tenants/companies/types';
import { StatementsService } from '../statements/statements.service';
import { type CreateStatementOverwriteDto } from './dto/createStatementOverwrite.dto';
@Injectable()
export class StatementOverwritesService {
private readonly logger = new Logger(StatementOverwritesService.name);
public constructor(
private readonly connection: DataSource,
private readonly overwriteService: OverwritesService,
private readonly variablesService: VariablesService,
private readonly statementsService: StatementsService,
) {}
public async createStatementOverwrite(
company: Company,
authenticatedContext: AuthenticatedContext,
statement: Statement,
createStatementOverwriteDto: CreateStatementOverwriteDto,
) {
const ability = defineAbilityFor(authenticatedContext);
const canApplyGlobalOverwrite = canOverwriteOnAllStatements(ability);
assert(
createStatementOverwriteDto.scope !== OverwriteScopeEnum.GLOBAL || canApplyGlobalOverwrite,
new ForbiddenException('You cannot apply overwrite on all statements.'),
);
let sourceValue: RecordContent | RecordContentPropertyType;
const kpiComputedItem = this.getKpiComputedItem(
statement,
createStatementOverwriteDto.variableId,
createStatementOverwriteDto.scope === OverwriteScopeEnum.FORECAST,
);
sourceValue =
kpiComputedItem.currency && !isCurrencyValue(kpiComputedItem.value)
? { symbol: kpiComputedItem.currency, value: kpiComputedItem.value as number }
: (kpiComputedItem.value as RecordContentPropertyType);
const currency = kpiComputedItem.currency;
const variable = await this.variablesService.findById(company, createStatementOverwriteDto.variableId);
assert(variable, 'Variable for statement overwrite not found');
const { overwritesToClear, sourceValue: overwriteSourceValue } = await this.getOverwritesToClearAndSourceValue(
company,
createStatementOverwriteDto,
statement,
variable,
);
sourceValue = overwriteSourceValue || sourceValue;
return this.overwriteService.applyOverwrite(
company,
authenticatedContext,
{
...createStatementOverwriteDto,
company,
companyId: company.id,
creator: authenticatedContext.user,
sourceValue: sourceValue as RecordContentPropertyType,
currency,
statement: [OverwriteScopeEnum.FORECAST, OverwriteScopeEnum.STATEMENT].includes(
createStatementOverwriteDto.scope,
)
? statement
: undefined,
createdOnStatementId: statement.id,
overwriteType: OverwriteTypesEnum.KPI,
variable,
scope: createStatementOverwriteDto.scope,
},
overwritesToClear,
);
}
private async getOverwritesToClearAndSourceValue(
company: Company,
createStatementOverwriteDto: CreateStatementOverwriteDto,
statement: Statement,
variable: Variable,
) {
const overwritesRelatedToVariable = await this.overwriteService.findByCustomObjectAndStatementAndVariableAndFilter(
company,
null,
statement,
variable,
true,
);
let overwritesToClear: Overwrite[] = [];
let sourceValue: RecordContent | RecordContentPropertyType | null | undefined = null;
switch (createStatementOverwriteDto.scope) {
case OverwriteScopeEnum.FORECAST: {
overwritesToClear = overwritesRelatedToVariable.filter(({ scope }) => scope === OverwriteScopeEnum.FORECAST);
sourceValue = overwritesRelatedToVariable.find(
(overwrite) => overwrite.scope === OverwriteScopeEnum.FORECAST,
)?.sourceValue;
break;
}
case OverwriteScopeEnum.STATEMENT: {
overwritesToClear = overwritesRelatedToVariable.filter(({ scope }) => scope === OverwriteScopeEnum.STATEMENT);
sourceValue =
overwritesRelatedToVariable.find(({ scope }) => scope === OverwriteScopeEnum.GLOBAL)?.sourceValue ??
overwritesRelatedToVariable.find(({ scope }) => scope === OverwriteScopeEnum.STATEMENT)?.sourceValue;
break;
}
case OverwriteScopeEnum.GLOBAL: {
overwritesToClear = overwritesRelatedToVariable.filter(
({ scope }) => scope === OverwriteScopeEnum.GLOBAL || scope === OverwriteScopeEnum.STATEMENT,
);
sourceValue =
overwritesRelatedToVariable.find(({ scope }) => scope === OverwriteScopeEnum.STATEMENT)?.sourceValue ??
overwritesRelatedToVariable.find(({ scope }) => scope === OverwriteScopeEnum.GLOBAL)?.sourceValue;
break;
}
default:
break;
}
return {
overwritesToClear,
sourceValue,
};
}
public async clearStatementOverwrite(
company: Company,
overwriteId: string,
authenticatedContext: AuthenticatedContext,
statementId: string,
) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const overwrite = await this.overwriteService.findById(company, overwriteId);
// Check to abilities
await this.findStatementToOverwrite(company, authenticatedContext, statementId, overwrite.scope);
await this.overwriteService.clear(company, overwrite, authenticatedContext, queryRunner);
await queryRunner.commitTransaction();
} catch (err) {
const error = toError(err);
this.logger.error({
message: `Error on clearing overwrite - ${error.message}`,
error,
company: pick(company, ['id', 'name']),
});
await queryRunner.rollbackTransaction();
if ('status' in error && [403, 404].includes(error.status as number)) throw err;
throw new InternalServerErrorException('Error on updating kpi, please contact admin');
} finally {
await queryRunner.release();
}
}
private getKpiComputedItem(statement: Statement, variableId: string, isForecast: boolean = false): ComputedItem {
const statementResults = isForecast && statement.forecast ? statement.forecast.results : statement.results;
const variable = Object.values(statementResults.definitions.variables).find((v) => v.id === variableId);
if (!variable) {
throw new NotFoundException();
}
const computedItem = statementResults.computedObjects.find(
(co) => (co as ComputedVariable).variableMachineName === variable.machineName,
);
if (!computedItem) {
throw new NotFoundException();
}
return computedItem;
}
public async findStatementToOverwrite(
company: Company,
authenticatedContext: AuthenticatedContext,
id: string,
overwriteScope: OverwriteScopeEnum,
): Promise<Statement> {
const statement = await this.statementsService.findById(
company,
id,
true,
overwriteScope === OverwriteScopeEnum.FORECAST,
);
const ability = defineAbilityFor(authenticatedContext);
if (overwriteScope === OverwriteScopeEnum.FORECAST) {
assert(statement.plan?.isForecasted, new ForbiddenException("This plan isn't forecasted"));
assert(statement.plan.isSimulationEnabled, new ForbiddenException("This plan doesn't have simulation enabled"));
if (!canSimulateThisForecastedStatement(ability, statement)) {
throw new ForbiddenException("You don't have access to this statement");
}
return statement;
}
if (!canViewThisStatement(ability, statement)) {
throw new ForbiddenException("You don't have access to this statement");
}
if (!canOverwriteThisStatement(ability, statement)) {
throw new ForbiddenException("You don't have access to overwrite this statement");
}
if (overwriteScope !== OverwriteScopeEnum.GLOBAL && isBeingReviewed(statement)) {
throw new ForbiddenException("You cannot overwrite a statement while it's being reviewed");
}
return statement;
}
}
|