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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 4x 4x 1x | import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, In, Repository } from 'typeorm';
import { getSchema, StatementDataset, type Company, type Statement } from '@amalia/core/models';
import { CalculationType, type DatasetRow } from '@amalia/core/types';
import { PaginatedQuery, PaginatedResponse } from '@amalia/core-http-types';
import { DatasetType } from '@amalia/payout-calculation/types';
import { DatasetRowsPaginatorQueryBuilder } from './DatasetRowsPaginator.queryBuilder';
@Injectable()
export class StatementDatasetsService {
public constructor(
private readonly connection: DataSource,
@InjectRepository(StatementDataset)
private readonly statementDatasetRepository: Repository<StatementDataset>,
) {}
public async paginateDataset(
company: Company,
statement: Statement,
datasetId: string,
queryOptions: PaginatedQuery,
calculationType: CalculationType,
): Promise<PaginatedResponse<DatasetRow>> {
const queryBuilder = new DatasetRowsPaginatorQueryBuilder(this.connection, this.statementDatasetRepository);
return queryBuilder.paginate(company, statement, datasetId, queryOptions, calculationType);
}
/**
* Get dataset with all rows.
* We can retrieve either dataset rows or forecast dataset rows.
* @param company
* @param statement
* @param datasetId
* @param cacheTag
* @param calculationType
*/
public async getFullNonPaginatedDataset(
company: Company,
statement: Statement,
datasetId: string,
cacheTag?: string,
calculationType?: CalculationType,
): Promise<StatementDataset | null> {
return this.statementDatasetRepository.findOne({
where: {
company: { id: company.id },
statementId: statement.id,
datasetId,
cacheTag,
calculationType,
},
});
}
public async getDatasetsForStatements(
company: Company,
statementIds: string[],
datasetTypes: DatasetType[] = [DatasetType.filter, DatasetType.metrics, DatasetType.relation, DatasetType.quota],
): Promise<Partial<Record<Statement['id'], StatementDataset[]>>> {
if (statementIds.length === 0) {
return {};
}
const results = await this.connection.getRepository(StatementDataset).findBy({
company: { id: company.id },
statementId: In(statementIds),
datasetType: In(datasetTypes),
calculationType: CalculationType.STATEMENT,
});
return Object.groupBy(results, (item) => item.statementId);
}
public async getDatasetRow(
company: Company,
statementId: string,
datasetId: string,
rowExternalId: string,
calculationType: CalculationType,
): Promise<DatasetRow> {
const rowRaw = await this.connection.query<{ row?: DatasetRow }[]>(
`
WITH elements AS (
SELECT jsonb_array_elements("rows") as row
FROM ${getSchema(this.connection)}.statement_dataset
WHERE "companyId" = $1 AND "statementId" = $2 AND "datasetId" = $3 AND "calculationType" = $4
)
SELECT *
FROM elements
WHERE "row"->>'externalId' = $5
`,
[company.id, statementId, datasetId, calculationType, rowExternalId],
);
const row = rowRaw.at(0)?.row;
if (!row) {
throw new NotFoundException(`Dataset ${datasetId} - Row ${rowExternalId} not found.`);
}
return row;
}
}
|