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 | 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 | import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { VariableType } from '@amalia/amalia-lang/tokens/types';
import { UpsertQuotaAssignmentRequest } from '@amalia/assignments/quotas/types';
import { Variable, VariableValue, type Company } from '@amalia/core/models';
import { CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { assert, promiseDotAllChunk } from '@amalia/ext/typescript';
@Injectable()
export class QuotaConnectorService {
public constructor(
@InjectRepository(Variable)
private readonly variableRepository: Repository<Variable>,
@InjectRepository(VariableValue)
private readonly variableValueRepository: Repository<VariableValue>,
) {}
/**
* Find all user quota variables for a given company.
* @param company
*/
public async findAllUserQuotas(company: Company): Promise<Variable[]> {
return this.variableRepository.findBy({
company: { id: company.id },
type: VariableType.user,
});
}
public async saveQuotaValues(
company: Company,
quotas: UpsertQuotaAssignmentRequest[],
quotaVariables: Variable[],
): Promise<void> {
await promiseDotAllChunk(
quotas,
async (quotaValue: UpsertQuotaAssignmentRequest) => {
const variable = quotaVariables.find((quotaVariable) => quotaVariable.id === quotaValue.variableId);
assert(variable, 'In QuotaConnector, variable not found');
// Check that variable value doesn't already exist.
const existingVariableValue = await this.variableValueRepository.findOne({
where: {
userId: quotaValue.userId,
variable: { id: variable.id },
company: { id: company.id },
...(quotaValue.startDate && { startDate: quotaValue.startDate }),
...(quotaValue.endDate && { endDate: quotaValue.endDate }),
},
relations: ['variable'],
});
if (existingVariableValue) {
// Update value.
existingVariableValue.value = quotaValue.value;
// Fallback on company currency if relevant.
existingVariableValue.currency = quotaValue.currency || company.currency;
return this.variableValueRepository.save(existingVariableValue);
}
const variableValue = this.variableValueRepository.create({
startDate: quotaValue.startDate,
endDate: quotaValue.endDate,
currency: quotaValue.currency || CurrencySymbolsEnum.EUR,
value: quotaValue.value,
company,
userId: quotaValue.userId,
variable,
} as VariableValue);
return this.variableValueRepository.save(variableValue);
},
20,
);
}
}
|