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 | 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 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 6x 6x 7x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 5x 5x 4x 4x 1x | import {
BadRequestException,
Body,
ConflictException,
Controller,
Get,
NotFoundException,
Param,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { EventBus } from '@nestjs/cqrs';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Company, User, type DataRefreshment } from '@amalia/core/models';
import { RefreshmentTrigger } from '@amalia/core/types';
import { DataConnectorsService } from '@amalia/data-capture/connectors/core';
import { StringPipe, UuidPipe } from '@amalia/kernel/api';
import {
AmaliaAuthGuard,
CheckPolicies,
CurrentAuthenticatedContext,
CurrentUser,
CurrentUserCompany,
PoliciesGuard,
} from '@amalia/kernel/auth/core';
import { canModifyData } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { RecordObject, RecordType } from '@amalia/tenants/monitoring/audit/types';
import { CreateDataRefreshmentDto } from './dto/createRefreshment.dto';
import { RefreshmentCreatedEvent } from './events/RefreshmentCreatedEvent';
import { DataRefreshmentsService } from './refreshments.service';
import { type ScheduleRefreshmentEvent } from './refreshments.types';
import { CreateRefreshmentUseCase } from './usecases/create-refreshment.use-case';
/**
* Controller to manage object definitions.
*/
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('data')
@Controller()
export class DataRefreshmentsController {
public constructor(
private readonly eventBus: EventBus,
private readonly dataRefreshmentService: DataRefreshmentsService,
private readonly createRefreshmentUseCase: CreateRefreshmentUseCase,
private readonly dataConnectorsService: DataConnectorsService,
) {}
@Get('/connectors/refreshments/last')
@CheckPolicies((ability) => canModifyData(ability))
public async getLastRefreshmentsOfEachObject(@CurrentUserCompany() company: Company): Promise<DataRefreshment[]> {
return this.dataRefreshmentService.getLastRefreshmentsOfEachObject(company);
}
/**
* Get company last refreshment.
* @param company
* @param id
* @param objectName
*/
@Get('/connectors/:connectorId/refreshments/last')
@CheckPolicies((ability) => canModifyData(ability))
public async getLast(
@CurrentUserCompany() company: Company,
@Param('connectorId', new UuidPipe()) id: string,
@Query('objectName', new StringPipe()) objectName: string,
): Promise<DataRefreshment | null> {
return this.dataRefreshmentService.getLastRefreshment(company, id, objectName);
}
/**
* Create refreshments on a connector.
*
* This endpoint is mostly used during the "Save & Synchronize" workflow,
* after editing a user connector configuration.
*
* @param company
* @param connectorId
* @param currentUser
* @param dataRefreshmentDto
*/
@Post('/connectors/:connectorId/refreshments')
@CheckPolicies((ability) => canModifyData(ability))
public async createForConnector(
@CurrentUserCompany() company: Company,
@Param('connectorId', new UuidPipe()) connectorId: string,
@CurrentUser() currentUser: User,
@Body() dataRefreshmentDto: CreateDataRefreshmentDto,
): Promise<void> {
if (await this.dataRefreshmentService.hasRefreshmentsOngoing(company)) {
throw new ConflictException('A refreshment is already running!');
}
// If we have a connector id from request => trigger a refreshment for this connector.
const connector = await this.dataConnectorsService.getConnectorById(company, connectorId);
if (!connector) {
throw new NotFoundException('Connector not found.');
}
await this.createRefreshmentUseCase.execute({
company,
options: dataRefreshmentDto,
trigger: RefreshmentTrigger.MANUAL_FROM_DATA,
connectorToRefresh: connector,
author: currentUser,
});
}
/**
* Create refreshment.
* @param dataRefreshmentDto
* @param company
* @param authenticatedContext
* @param currentUser
*/
@Post('/refreshments')
@CheckPolicies((ability) => canModifyData(ability))
public async create(
@CurrentUserCompany() company: Company,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@CurrentUser() currentUser: User,
@Body() dataRefreshmentDto: CreateDataRefreshmentDto,
): Promise<ScheduleRefreshmentEvent[]> {
if (await this.dataRefreshmentService.hasRefreshmentsOngoing(company)) {
throw new ConflictException('A refreshment is already running!');
}
// If we have no connector id, check if we have data connector objects to refresh.
if (!dataRefreshmentDto.dataConnectorObjectsNames.length) {
throw new BadRequestException('You need to chose at least an object to refresh');
}
// Create one refreshment per data connector object.
const createdRefreshments = await this.createRefreshmentUseCase.execute({
company,
options: dataRefreshmentDto,
trigger: RefreshmentTrigger.MANUAL_FROM_DATA,
author: currentUser,
});
// Audit one time for each created refreshment.
await Promise.all(
createdRefreshments.map(async (refreshment) => {
await this.eventBus.publish(
new RefreshmentCreatedEvent({
authenticatedContext,
object: RecordObject.DATA_REFRESHMENT,
type: RecordType.CREATE,
values: {
target: {
id: refreshment.id,
name: refreshment.object,
},
newValues: {
options: refreshment.options,
connector: refreshment.connector.type,
},
},
}),
);
}),
);
return createdRefreshments.map((r) => ({
companyId: r.company.id,
refreshmentId: r.id,
}));
}
}
|