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 269 270 271 272 273 274 275 | 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 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 4x 3x 3x 4x 4x 4x 4x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x | import { HttpException, HttpStatus, Injectable, Logger, NotFoundException } from '@nestjs/common';
import { pick } from 'lodash-es';
import { DataSource, type QueryRunner } from 'typeorm';
import { type Company, type CustomObject, type CustomObjectDefinition, type Overwrite } from '@amalia/core/models';
import { OverwriteTypesEnum, type PatchCustomObjectResponse } from '@amalia/core/types';
import { FieldTransformer } from '@amalia/data-capture/connectors/library';
import { RecordContent } from '@amalia/data-capture/connectors/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { CustomObjectDefinitionsService } from '@amalia/data-capture/record-models/core';
import { OverwritesService } from '@amalia/data-correction/overwrites/core';
import { OverwriteScopeEnum } from '@amalia/data-correction/overwrites/types';
import { assert, toError } from '@amalia/ext/typescript';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { isCurrencyValue } from '@amalia/kernel/monetary/types';
import { CustomObjectsService } from './customObjects.service';
import { isCleanOperation, isDeleteOperation, isValueUnchanged } from './customObjectsOverwrites.utils';
import { type PatchCustomObjectDto } from './dto/patchCustomObject.dto';
@Injectable()
export class CustomObjectsOverwritesService {
private readonly logger = new Logger(CustomObjectsOverwritesService.name);
public constructor(
private readonly connection: DataSource,
private readonly customObjectDefinitionsService: CustomObjectDefinitionsService,
private readonly customObjectsService: CustomObjectsService,
private readonly overwritesService: OverwritesService,
) {}
/**
* Create new overwrite.
* @param company company of the new overwrite
* @param authenticatedContext
* @param definition
* @param externalId
* @param patchCustomObjectDto
*/
public async patchCustomObject(
company: Company,
authenticatedContext: AuthenticatedContext,
definition: string,
externalId: string,
patchCustomObjectDto: PatchCustomObjectDto,
): Promise<PatchCustomObjectResponse> {
const objectDefinition = await this.customObjectDefinitionsService.findByMachineName(company, definition);
assert(objectDefinition, new NotFoundException(`Definition not found: ${definition}`));
const customObject = await this.customObjectsService.findByDefinitionAndExternalId(
company,
objectDefinition.id,
externalId,
);
assert(customObject, new NotFoundException(`Object not found: ${definition} ${externalId}`));
return this.overwriteCustomObject(
company,
authenticatedContext,
objectDefinition,
customObject,
patchCustomObjectDto,
);
}
private async overwriteCustomObject(
company: Company,
authenticatedContext: AuthenticatedContext,
objectDefinition: CustomObjectDefinition,
customObject: CustomObject,
patchCustomObjectDto: PatchCustomObjectDto,
): Promise<PatchCustomObjectResponse> {
const customObjectDefinitionField = objectDefinition.properties[patchCustomObjectDto.field];
assert(customObjectDefinitionField, new NotFoundException(`Field not found: ${patchCustomObjectDto.field}`));
const overwriteToClear = await this.overwritesService.findOneByCustomObject(
company,
patchCustomObjectDto.field,
customObject,
);
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
if (isCleanOperation(overwriteToClear, patchCustomObjectDto.overwriteValue, patchCustomObjectDto.field)) {
// We should check if the overwrite exists before trying to clear it
if (overwriteToClear) {
await this.clearOverwrite(
company,
authenticatedContext,
customObject,
objectDefinition,
overwriteToClear,
queryRunner,
);
} else {
this.logger.warn({
message: `No overwrite to clear for custom object ${customObject.id} on field ${patchCustomObjectDto.field}`,
company: pick(company, ['id', 'name']),
});
}
await queryRunner.commitTransaction();
return null;
}
const sourceValue = {
[patchCustomObjectDto.field]: customObject.content[patchCustomObjectDto.field],
};
// If there's no overwrite and attempts to clean then interrupt the overwrite and do nothing
if (isValueUnchanged(overwriteToClear, sourceValue, patchCustomObjectDto.overwriteValue)) {
await queryRunner.commitTransaction();
return null;
}
// If there's already an overwrite on this value, clear it before creating the new one
if (overwriteToClear) {
await this.clearOverwrite(
company,
authenticatedContext,
customObject,
objectDefinition,
overwriteToClear,
queryRunner,
);
}
const overwriteValue = isDeleteOperation(patchCustomObjectDto as Overwrite, patchCustomObjectDto.field)
? ''
: FieldTransformer.transformField(
patchCustomObjectDto.overwriteValue,
patchCustomObjectDto.field,
objectDefinition.properties[patchCustomObjectDto.field]!.format,
);
const fieldContent = customObject.content[customObjectDefinitionField.machineName];
const currency =
customObjectDefinitionField.format === FormatsEnum.currency && isCurrencyValue(fieldContent)
? fieldContent.symbol
: undefined;
const dealName = objectDefinition.nameField
? (customObject.content[objectDefinition.nameField] as string)
: undefined;
const createdOverwrite = await this.overwritesService.create(
company,
authenticatedContext,
{
...patchCustomObjectDto,
company,
companyId: company.id,
creator: authenticatedContext.user,
overwriteValue,
sourceValue: overwriteToClear ? overwriteToClear.sourceValue : sourceValue,
currency,
appliesToDealName: dealName,
appliesToDefinition: objectDefinition,
appliesToExternalId: customObject.externalId,
appliesToDefinitionId: objectDefinition.id,
overwriteType: OverwriteTypesEnum.PROPERTY,
scope: OverwriteScopeEnum.GLOBAL,
statementId: undefined,
},
queryRunner,
);
// Update Custom Object content in place.
const newContent: RecordContent = {
...customObject.content,
[patchCustomObjectDto.field]: (createdOverwrite.overwriteValue as RecordContent)[patchCustomObjectDto.field],
};
const patchedObject = await this.customObjectsService.save(company, objectDefinition, newContent, false);
await queryRunner.commitTransaction();
return { ...patchedObject, createdOverwrite };
} catch (e) {
const error = toError(e);
this.logger.error({ message: 'Error on patching custom object', error, company: pick(company, ['id', 'name']) });
await queryRunner.rollbackTransaction();
throw new HttpException('overwrite creation failed, please contact admin', HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
await queryRunner.release();
}
}
public async clearCustomObject(
company: Company,
authenticatedContext: AuthenticatedContext,
externalId: string,
objectDefinitionName: string,
overwriteId: string,
) {
const objectDefinition = await this.customObjectDefinitionsService.findByMachineName(company, objectDefinitionName);
assert(objectDefinition, new NotFoundException(`Definition not found: ${objectDefinitionName}`));
const customObject = await this.customObjectsService.findByDefinitionAndExternalId(
company,
objectDefinition.id,
externalId,
);
assert(customObject, new NotFoundException(`Object not found: ${objectDefinitionName} ${externalId}`));
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const overwrite = await this.overwritesService.findOne(company, overwriteId, undefined, customObject);
await this.clearOverwrite(
company,
authenticatedContext,
customObject,
objectDefinition,
{
...overwrite,
appliesToDefinition: objectDefinition,
},
queryRunner,
);
await queryRunner.commitTransaction();
} catch (err) {
await queryRunner.rollbackTransaction();
throw err;
} finally {
await queryRunner.release();
}
}
public async clearOverwrite(
company: Company,
authenticatedContext: AuthenticatedContext | null,
customObject: CustomObject,
customObjectDefinition: CustomObjectDefinition,
overwrite: Overwrite,
queryRunner: QueryRunner,
): Promise<void> {
await this.overwritesService.clear(company, overwrite, authenticatedContext, queryRunner);
const fieldMachineName: string = overwrite.field;
assert(
customObjectDefinition.properties[fieldMachineName],
new NotFoundException(`Field not found: ${fieldMachineName}`),
);
const patchedValue = FieldTransformer.transformField(
overwrite.sourceValue as RecordContent,
fieldMachineName,
customObjectDefinition.properties[fieldMachineName].format,
)![fieldMachineName];
await this.customObjectsService.save(
company,
customObjectDefinition,
{
...customObject.content,
[fieldMachineName]: patchedValue,
},
false,
);
}
}
|