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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 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 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x | import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Company as CompanyEntity, CompanyCurrency, Currency } from '@amalia/core/models';
import { CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { type Company } from '@amalia/tenants/companies/types';
import { type UserContract } from '@amalia/tenants/users/types';
import { sampleCompanyCurrencies, sampleCurrencyDate } from '../../../assets/sampleData/companyCurrencies';
@Injectable()
export class SampleDataCurrenciesService {
public constructor(
@InjectRepository(CompanyEntity)
private readonly companyRepository: Repository<CompanyEntity>,
@InjectRepository(Currency)
private readonly currencyRepository: Repository<Currency>,
@InjectRepository(CompanyCurrency)
private readonly companyCurrencyRepository: Repository<CompanyCurrency>,
) {}
/**
* Loads sample data for a given company.
* @param company company to create
* @param admin
*/
public async load(company: Company, admin: UserContract): Promise<void> {
// Update company, set its currencies.
await this.companyRepository.update(
{ id: company.id },
{
symbols: [
CurrencySymbolsEnum.EUR,
CurrencySymbolsEnum.AUD,
CurrencySymbolsEnum.GBP,
CurrencySymbolsEnum.SGD,
CurrencySymbolsEnum.USD,
],
},
);
await this.loadCompanyCurrencies(company, admin);
}
public async purge(company: Company): Promise<void> {
await this.companyCurrencyRepository.delete({
company: { id: company.id },
});
}
private async loadCompanyCurrencies(company: Company, admin: UserContract) {
const allTimestamps = [sampleCurrencyDate];
// Load company if they exist, create them if they don't.
const currencies: Currency[] = await Promise.all(
allTimestamps.map(async (date) => {
let existingCurrency = await this.currencyRepository.findOne({ where: { date } });
if (!existingCurrency) {
existingCurrency = this.currencyRepository.create({
date,
rates: {},
});
await this.currencyRepository.save(existingCurrency);
}
return existingCurrency;
}),
);
// And now apply currencies overwrites.
await Promise.all(
sampleCompanyCurrencies.map(async (companyCurrency) => {
const currency = currencies.find((c) => c.date === sampleCurrencyDate);
await this.companyCurrencyRepository.save(
this.companyCurrencyRepository.create({
...companyCurrency,
company,
currency,
creator: { id: admin.id },
}),
);
}),
);
}
}
|