mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
Compare commits
1 Commits
BIG-419-de
...
avoid-dele
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bd30abddb |
@@ -1,18 +1,15 @@
|
|||||||
import { Inject, Service } from 'typedi';
|
import { Inject, Service } from 'typedi';
|
||||||
import { uniq } from 'lodash';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ICurrencyEditDTO,
|
ICurrencyEditDTO,
|
||||||
ICurrencyDTO,
|
ICurrencyDTO,
|
||||||
ICurrenciesService,
|
ICurrenciesService,
|
||||||
ICurrency,
|
ICurrency,
|
||||||
} from '@/interfaces';
|
} from '@/interfaces';
|
||||||
import {
|
|
||||||
EventDispatcher,
|
|
||||||
EventDispatcherInterface,
|
|
||||||
} from 'decorators/eventDispatcher';
|
|
||||||
import { ServiceError } from '@/exceptions';
|
import { ServiceError } from '@/exceptions';
|
||||||
import TenancyService from '@/services/Tenancy/TenancyService';
|
import TenancyService from '@/services/Tenancy/TenancyService';
|
||||||
|
import { Tenant } from '@/system/models';
|
||||||
|
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||||
|
import { CurrencyTransformer } from './CurrencyTransformer';
|
||||||
|
|
||||||
const ERRORS = {
|
const ERRORS = {
|
||||||
CURRENCY_NOT_FOUND: 'currency_not_found',
|
CURRENCY_NOT_FOUND: 'currency_not_found',
|
||||||
@@ -23,14 +20,11 @@ const ERRORS = {
|
|||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class CurrenciesService implements ICurrenciesService {
|
export default class CurrenciesService implements ICurrenciesService {
|
||||||
@Inject('logger')
|
@Inject()
|
||||||
logger: any;
|
private tenancy: TenancyService;
|
||||||
|
|
||||||
@EventDispatcher()
|
|
||||||
eventDispatcher: EventDispatcherInterface;
|
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
tenancy: TenancyService;
|
private transformer: TransformerInjectable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve currency by given currency code or throw not found error.
|
* Retrieve currency by given currency code or throw not found error.
|
||||||
@@ -105,7 +99,7 @@ export default class CurrenciesService implements ICurrenciesService {
|
|||||||
*/
|
*/
|
||||||
public async newCurrency(tenantId: number, currencyDTO: ICurrencyDTO) {
|
public async newCurrency(tenantId: number, currencyDTO: ICurrencyDTO) {
|
||||||
const { Currency } = this.tenancy.models(tenantId);
|
const { Currency } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
// Validate currency code uniquiness.
|
// Validate currency code uniquiness.
|
||||||
await this.validateCurrencyCodeUniquiness(
|
await this.validateCurrencyCodeUniquiness(
|
||||||
tenantId,
|
tenantId,
|
||||||
@@ -141,13 +135,15 @@ export default class CurrenciesService implements ICurrenciesService {
|
|||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {string} currencyCode
|
* @param {string} currencyCode
|
||||||
*/
|
*/
|
||||||
validateCannotDeleteBaseCurrency(tenantId: number, currencyCode: string) {
|
private async validateCannotDeleteBaseCurrency(
|
||||||
const settings = this.tenancy.settings(tenantId);
|
tenantId: number,
|
||||||
const baseCurrency = settings.get({
|
currencyCode: string
|
||||||
group: 'organization',
|
) {
|
||||||
key: 'base_currency',
|
const tenant = await Tenant.query()
|
||||||
});
|
.findById(tenantId)
|
||||||
if (baseCurrency === currencyCode) {
|
.withGraphFetched('metadata');
|
||||||
|
|
||||||
|
if (tenant.metadata.baseCurrency === currencyCode) {
|
||||||
throw new ServiceError(ERRORS.CANNOT_DELETE_BASE_CURRENCY);
|
throw new ServiceError(ERRORS.CANNOT_DELETE_BASE_CURRENCY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,7 +152,7 @@ export default class CurrenciesService implements ICurrenciesService {
|
|||||||
* Delete the given currency code.
|
* Delete the given currency code.
|
||||||
* @param {number} tenantId
|
* @param {number} tenantId
|
||||||
* @param {string} currencyCode
|
* @param {string} currencyCode
|
||||||
* @return {Promise<}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
public async deleteCurrency(
|
public async deleteCurrency(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
@@ -180,19 +176,13 @@ export default class CurrenciesService implements ICurrenciesService {
|
|||||||
public async listCurrencies(tenantId: number): Promise<ICurrency[]> {
|
public async listCurrencies(tenantId: number): Promise<ICurrency[]> {
|
||||||
const { Currency } = this.tenancy.models(tenantId);
|
const { Currency } = this.tenancy.models(tenantId);
|
||||||
|
|
||||||
const settings = this.tenancy.settings(tenantId);
|
|
||||||
const baseCurrency = settings.get({
|
|
||||||
group: 'organization',
|
|
||||||
key: 'base_currency',
|
|
||||||
});
|
|
||||||
|
|
||||||
const currencies = await Currency.query().onBuild((query) => {
|
const currencies = await Currency.query().onBuild((query) => {
|
||||||
query.orderBy('createdAt', 'ASC');
|
query.orderBy('createdAt', 'ASC');
|
||||||
});
|
});
|
||||||
const formattedCurrencies = currencies.map((currency) => ({
|
return this.transformer.transform(
|
||||||
isBaseCurrency: baseCurrency === currency.currencyCode,
|
tenantId,
|
||||||
...currency,
|
currencies,
|
||||||
}));
|
new CurrencyTransformer()
|
||||||
return formattedCurrencies;
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { Transformer } from '@/lib/Transformer/Transformer';
|
||||||
|
|
||||||
|
export class CurrencyTransformer extends Transformer {
|
||||||
|
/**
|
||||||
|
* Include these attributes to sale invoice object.
|
||||||
|
* @returns {Array}
|
||||||
|
*/
|
||||||
|
public includeAttributes = (): string[] => {
|
||||||
|
return ['isBaseCurrency'];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detarmines whether the currency is base currency.
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
public isBaseCurrency(currency): boolean {
|
||||||
|
return this.context.organization.baseCurrency === currency.currencyCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user