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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 2x 2x 2x 2x 2x 69x 69x 69x 69x 69x 69x 66x 79x 79x 79x 79x 79x 79x 79x 66x 69x 69x 69x 69x 69x 69x 69x 2x 2x 2x 69x 69x 69x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x | import { type Server as CoreHttpServer } from 'node:http';
import { type INestApplication } from '@nestjs/common';
import request from 'supertest';
import { DataSource } from 'typeorm';
import { PlanAssignment, Statement, StatementDataset, type Calculation } from '@amalia/core/models';
import { CalculationStatus, CalculationTrigger, CalculationType, type StatementResponse } from '@amalia/core/types';
import { type CalculationPayload } from '@amalia/kernel/queue/core';
import { authenticateAs, companyAbTasty } from '@amalia/kernel/testing/server/bed';
import { type UserContract } from '@amalia/tenants/users/types';
import { StatementSaveModule } from '../engine/statementSave/statementSave.module';
import { StatementSaveService } from '../engine/statementSave/statementSave.service';
import { RunCalculationBatchUseCase } from '../engine/usecases/RunCalculationBatch.use-case';
export const calculateForPeriod = async (
app: INestApplication<CoreHttpServer>,
userToConnect: Pick<UserContract, 'email'>,
periodId: string,
filters?: { userIds?: string[]; planIds?: string[] },
type: CalculationType = CalculationType.STATEMENT,
trigger: CalculationTrigger = CalculationTrigger.MANUAL,
): Promise<Calculation> => {
const { body: calculation } = (await request(app.getHttpServer())
.post('/calculations/')
.set(authenticateAs(userToConnect))
.send({
periodId,
userIds: filters?.userIds,
planIds: filters?.planIds,
type,
trigger,
})
.expect(201)) as { body: Calculation };
if (calculation.deletedStatementIds?.length) {
await app
.select(StatementSaveModule)
.get(StatementSaveService)
.deleteStatements(calculation.companyId, calculation.deletedStatementIds);
}
expect(calculation.status).toEqual(CalculationStatus.PENDING);
const calculationHandler = app.get<RunCalculationBatchUseCase>(RunCalculationBatchUseCase);
for (let indexStep = 0; indexStep < calculation.descriptor.length; indexStep++) {
for (let indexBatch = 0; indexBatch < calculation.descriptor[indexStep].batches.length; indexBatch++) {
await calculationHandler.handle({
calculationId: calculation.id,
companyId: calculation.company!.id,
step: indexStep,
batch: indexBatch,
} as CalculationPayload);
}
}
const { body: newCalculation } = (await request(app.getHttpServer())
.get(`/calculations/${periodId}/${calculation.id}`)
.set(authenticateAs(userToConnect))
.expect(200)) as { body: Calculation };
if (newCalculation.status === CalculationStatus.ERROR) {
// eslint-disable-next-line no-console -- Test utils.
console.error(newCalculation.error);
}
return newCalculation;
};
export const fetchStatements = async (
app: INestApplication<CoreHttpServer>,
userToConnect: Pick<UserContract, 'email'>,
statementIds: string[],
): Promise<StatementResponse[]> =>
Promise.all(
statementIds.map(async (s) => {
const { body: statement } = (await request(app.getHttpServer())
.get(`/statements/${s}`)
.set(authenticateAs(userToConnect))
.expect(200)) as { body: StatementResponse };
return statement;
}),
);
export const getPeriodStatements = async (
app: INestApplication<CoreHttpServer>,
userToConnect: Pick<UserContract, 'email'>,
periodId: string,
relations?: string[],
planId?: string,
): Promise<StatementResponse[]> => {
const { body: statements } = (await request(app.getHttpServer())
.get('/statements')
.query({
periodId,
planId,
relations,
})
.set(authenticateAs(userToConnect))
.expect(200)) as { body: StatementResponse[] };
return statements;
};
export const setupCalculationTest = async (
app: INestApplication<CoreHttpServer>,
planId: string,
userId: string,
mainTeamId?: string,
) => {
await app.get(DataSource).getRepository(PlanAssignment).deleteAll();
await app.get(DataSource).getRepository(StatementDataset).deleteAll();
await app.get(DataSource).getRepository(Statement).deleteAll();
await app
.get(DataSource)
.getRepository(PlanAssignment)
.save({
company: { id: companyAbTasty.id },
planId,
userId,
mainTeamId,
});
};
export const runCalculationAndGetStatementResult = async (
app: INestApplication<CoreHttpServer>,
userToConnect: Pick<UserContract, 'email'>,
periodId: string,
) => {
const { status } = await calculateForPeriod(app, userToConnect, periodId);
const statements = await getPeriodStatements(app, userToConnect, periodId);
expect(status).toEqual(CalculationStatus.SUCCESS);
// This helper shouldn't be used if you expect more than 1 statement.
expect(statements).toHaveLength(1);
const { body: computedStatement } = (await request(app.getHttpServer())
.get(`/statements/${statements[0].id}`)
.set(authenticateAs(userToConnect))
.expect(200)) as { body: Statement };
return computedStatement;
};
export const getFullStatement = async (
app: INestApplication<CoreHttpServer>,
userToConnect: Pick<UserContract, 'email'>,
statementId: string,
) => {
const { body: statement } = (await request(app.getHttpServer())
.get(`/statements/${statementId}`)
.set(authenticateAs(userToConnect))
.expect(200)) as { body: Statement };
return statement;
};
export const getStatementDataset = async (
app: INestApplication<CoreHttpServer>,
statementId: string,
datasetId: string,
) =>
app.get(DataSource).getRepository(StatementDataset).findOneBy({
statementId,
datasetId,
});
|