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 | 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 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 27x 27x 27x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x | import { Body, Controller, Delete, Get, Inject, Logger, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { type DeleteResult } from 'typeorm';
import { Company, type CustomObject } from '@amalia/core/models';
import { type PatchCustomObjectResponse } from '@amalia/core/types';
import { type PaginatedResponse } from '@amalia/core-http-types';
import { DataOverwriteValidateResponse } from '@amalia/data-capture/records/types';
import { ClearOverwriteDto } from '@amalia/data-correction/overwrites/core';
import { BooleanPipe, FeatureFlag, FeatureFlagGuard, NumberPipe, StringPipe, UuidPipe } from '@amalia/kernel/api';
import {
AmaliaAuthGuard,
CheckPolicies,
CurrentAuthenticatedContext,
CurrentUserCompany,
PoliciesGuard,
} from '@amalia/kernel/auth/core';
import { canModifyData, canOverwriteData, canViewData } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { CompanyFeatureFlags } from '@amalia/tenants/companies/types';
import { CustomObjectsService } from './customObjects.service';
import { CustomObjectsOverwritesService } from './customObjectsOverwrites.service';
import { BulkImportDataOverwritesDto } from './dto/BulkImportDataOverwrites.dto';
import { FilterPipe } from './dto/filterPipe.pipe';
import { PatchCustomObjectDto } from './dto/patchCustomObject.dto';
import { type QueryFilterGroupDto } from './dto/queryFilterGroup.dto';
import { DataOverwriteBulkImportUseCase } from './use-cases/data-overwrite-bulk-import.use-case';
/**
* Controller to manage object definitions.
*/
@UseGuards(AmaliaAuthGuard, PoliciesGuard, FeatureFlagGuard)
@ApiBearerAuth()
@ApiTags('objects')
@Controller('objects')
export class CustomObjectsController {
private readonly logger = new Logger(CustomObjectsController.name);
public constructor(
@Inject(CustomObjectsService)
private readonly customObjectsService: CustomObjectsService,
private readonly customObjectsOverwritesService: CustomObjectsOverwritesService,
private readonly dataOverwriteBulkImportUseCase: DataOverwriteBulkImportUseCase,
) {}
/**
* Paginate custom objects.
* @param company
* @param definition
* @param search
* @param page
* @param limit
* @param sortColumn
* @param descParam
* @param overwrite
* @param filterGroups
* @param externalIds
*/
@CheckPolicies((ability) => canViewData(ability))
@Get(':definition')
public async paginate(
@CurrentUserCompany() company: Company,
@Param('definition', new StringPipe()) definition: string,
@Query('page', new NumberPipe({ optional: true })) page: number = 1,
@Query('limit', new NumberPipe({ optional: true })) limit: number = 10,
@Query('sort', new StringPipe({ optional: true })) sortColumn?: string,
@Query('desc', new BooleanPipe({ optional: true })) descParam?: boolean,
@Query('q', new StringPipe({ optional: true })) search?: string,
@Query('overwrite', new BooleanPipe({ optional: true })) overwrite: boolean = false,
@Query('externalIds') externalIds?: string,
@Query('filter', new FilterPipe()) filterGroups?: QueryFilterGroupDto[],
): Promise<PaginatedResponse<CustomObject>> {
return this.customObjectsService.paginate(
definition,
company,
{ page: page || 1, limit },
search,
sortColumn,
!!descParam,
filterGroups,
overwrite,
externalIds,
);
}
/**
* Search record by external id or name.
* There is no rbac check here because it is used by all users that have access to forecast/simulation.
* @param company
* @param definition
* @param search
*/
@FeatureFlag(CompanyFeatureFlags.FORECAST)
@Get(':definition/quick-search')
public async search(
@CurrentUserCompany() company: Company,
@Param('definition', new StringPipe()) definition: string,
@Query('q', new StringPipe({ optional: true })) search?: string,
): Promise<CustomObject[]> {
return this.customObjectsService.quickSearch(company, definition, search);
}
/**
* Purge objects from a definition for a company.
* @param company
* @param authenticatedContext
* @param definitionId
*/
@Delete(':definitionId')
@CheckPolicies((ability) => canModifyData(ability))
public async purge(
@CurrentUserCompany() company: Company,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@Param('definitionId', new UuidPipe()) definitionId: string,
): Promise<DeleteResult> {
this.logger.log(`Purge custom objects of definition ${definitionId}.`);
return this.customObjectsService.bulkDelete(company, definitionId, authenticatedContext);
}
@Patch(':definition/records/:externalId')
@CheckPolicies((ability) => canOverwriteData(ability))
public async patchCustomObject(
@Param('definition', new StringPipe()) definition: string,
@Param('externalId', new StringPipe()) externalId: string,
@Body() patchedCustomObject: PatchCustomObjectDto,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@CurrentUserCompany() company: Company,
): Promise<PatchCustomObjectResponse> {
this.logger.log(
`Patch object ${externalId} ${patchedCustomObject.field} ${JSON.stringify(patchedCustomObject.overwriteValue)}`,
);
return this.customObjectsOverwritesService.patchCustomObject(
company,
authenticatedContext,
definition,
externalId,
patchedCustomObject,
);
}
@Post(':definition/records/:externalId/clear')
@CheckPolicies((ability) => canOverwriteData(ability))
public async clearCustomObject(
@Param('definition', new StringPipe()) definition: string,
@Param('externalId', new StringPipe()) externalId: string,
@Body() clearOverwriteDto: ClearOverwriteDto,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@CurrentUserCompany() company: Company,
): Promise<void> {
this.logger.log(`Clear custom object ${externalId} overwrite ${clearOverwriteDto.overwriteId}`);
await this.customObjectsOverwritesService.clearCustomObject(
company,
authenticatedContext,
externalId,
definition,
clearOverwriteDto.overwriteId,
);
}
@Post(':definitionMachineName/records/bulk-import-overwrites')
@CheckPolicies((ability) => canOverwriteData(ability))
public async bulkImportOverwrites(
@Param('definitionMachineName', new StringPipe()) definitionMachineName: string,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@Body() body: BulkImportDataOverwritesDto,
): Promise<DataOverwriteValidateResponse> {
return this.dataOverwriteBulkImportUseCase.execute({
authenticatedContext,
customObjectDefinitionMachineName: definitionMachineName,
dataOverwriteRowsToApply: body.rowsToImport,
dryRun: body.dryRun,
});
}
}
|