mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat(ManualJournals): Auto-increment.
fix(BillPayment): Validate the opened payment bills. fix(redux): presist redux state. fix(useRequestQuery): hook.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Inject, Container, Service } from 'typedi';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import Currencies from 'js-money/lib/currency';
|
||||
import {
|
||||
ICurrencyEditDTO,
|
||||
ICurrencyDTO,
|
||||
@@ -15,6 +16,7 @@ import TenancyService from 'services/Tenancy/TenancyService';
|
||||
const ERRORS = {
|
||||
CURRENCY_NOT_FOUND: 'currency_not_found',
|
||||
CURRENCY_CODE_EXISTS: 'currency_code_exists',
|
||||
BASE_CURRENCY_INVALID: 'BASE_CURRENCY_INVALID'
|
||||
};
|
||||
|
||||
@Service()
|
||||
@@ -129,7 +131,6 @@ export default class CurrenciesService implements ICurrenciesService {
|
||||
tenantId,
|
||||
currencyDTO,
|
||||
});
|
||||
|
||||
await this.validateCurrencyCodeUniquiness(
|
||||
tenantId,
|
||||
currencyDTO.currencyCode
|
||||
@@ -211,4 +212,25 @@ export default class CurrenciesService implements ICurrenciesService {
|
||||
});
|
||||
return currencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds the given base currency to the currencies list.
|
||||
* @param {number} tenantId
|
||||
* @param {string} baseCurrency
|
||||
*/
|
||||
public async seedBaseCurrency(tenantId: number, baseCurrency: string) {
|
||||
const { Currency } = this.tenancy.models(tenantId);
|
||||
const currencyMeta = Currencies[baseCurrency];
|
||||
|
||||
const foundBaseCurrency = await Currency.query().findOne(
|
||||
'currency_code',
|
||||
baseCurrency
|
||||
);
|
||||
if (!foundBaseCurrency) {
|
||||
await Currency.query().insert({
|
||||
currency_code: currencyMeta.code,
|
||||
currency_name: currencyMeta.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,11 +244,11 @@ export default class ItemsService implements IItemsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate item type in edit item mode, cannot change item inventory type.
|
||||
* Validate edit item type from inventory to another type that not allowed.
|
||||
* @param {IItemDTO} itemDTO
|
||||
* @param {IItem} oldItem
|
||||
*/
|
||||
private validateEditItemInventoryType(itemDTO: IItemDTO, oldItem: IItem) {
|
||||
private validateEditItemFromInventory(itemDTO: IItemDTO, oldItem: IItem) {
|
||||
if (
|
||||
itemDTO.type &&
|
||||
oldItem.type === 'inventory' &&
|
||||
@@ -258,6 +258,36 @@ export default class ItemsService implements IItemsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates edit item type from service/non-inventory to inventory.
|
||||
* Should item has no any relations with accounts transactions.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} itemId - Item id.
|
||||
*/
|
||||
private async validateEditItemTypeToInventory(
|
||||
tenantId: number,
|
||||
oldItem: IItem,
|
||||
newItemDTO: IItemDTO
|
||||
) {
|
||||
const { AccountTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
// We have no problem in case the item type not modified.
|
||||
if (newItemDTO.type === oldItem.type || oldItem.type === 'inventory') {
|
||||
return;
|
||||
}
|
||||
// Retrieve all transactions that associated to the given item id.
|
||||
const itemTransactionsCount = await AccountTransaction.query()
|
||||
.where('item_id', oldItem.id)
|
||||
.count('item_id', { as: 'transactions' })
|
||||
.first();
|
||||
|
||||
if (itemTransactionsCount.transactions > 0) {
|
||||
throw new ServiceError(
|
||||
ERRORS.TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new item.
|
||||
* @param {number} tenantId DTO
|
||||
@@ -319,7 +349,11 @@ export default class ItemsService implements IItemsService {
|
||||
// Validates the given item existance on the storage.
|
||||
const oldItem = await this.getItemOrThrowError(tenantId, itemId);
|
||||
|
||||
this.validateEditItemInventoryType(itemDTO, oldItem);
|
||||
// Validate edit item type from inventory type.
|
||||
this.validateEditItemFromInventory(itemDTO, oldItem);
|
||||
|
||||
// Validate edit item type to inventory type.
|
||||
await this.validateEditItemTypeToInventory(tenantId, oldItem, itemDTO);
|
||||
|
||||
// Transform the edit item DTO to model.
|
||||
const itemModel = this.transformEditItemDTOToModel(itemDTO, oldItem);
|
||||
@@ -580,8 +614,7 @@ export default class ItemsService implements IItemsService {
|
||||
const { ItemEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
const ids = Array.isArray(itemId) ? itemId : [itemId];
|
||||
const foundItemEntries = await ItemEntry.query()
|
||||
.whereIn('item_id', ids);
|
||||
const foundItemEntries = await ItemEntry.query().whereIn('item_id', ids);
|
||||
|
||||
if (foundItemEntries.length > 0) {
|
||||
throw new ServiceError(
|
||||
|
||||
@@ -18,4 +18,5 @@ export const ERRORS = {
|
||||
ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT:
|
||||
'ITEM_HAS_ASSOCIATED_INVENTORY_ADJUSTMENT',
|
||||
ITEM_CANNOT_CHANGE_INVENTORY_TYPE: 'ITEM_CANNOT_CHANGE_INVENTORY_TYPE',
|
||||
TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS: 'TYPE_CANNOT_CHANGE_WITH_ITEM_HAS_TRANSACTIONS',
|
||||
};
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
import JournalPoster from 'services/Accounting/JournalPoster';
|
||||
import JournalCommands from 'services/Accounting/JournalCommands';
|
||||
import JournalPosterService from 'services/Sales/JournalPosterService';
|
||||
import AutoIncrementOrdersService from 'services/Sales/AutoIncrementOrdersService';
|
||||
import { ERRORS } from './constants';
|
||||
|
||||
@Service()
|
||||
@@ -40,6 +41,9 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
@EventDispatcher()
|
||||
eventDispatcher: EventDispatcherInterface;
|
||||
|
||||
@Inject()
|
||||
autoIncrementOrdersService: AutoIncrementOrdersService;
|
||||
|
||||
/**
|
||||
* Validates the manual journal existance.
|
||||
* @param {number} tenantId
|
||||
@@ -157,18 +161,18 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
*/
|
||||
private async validateManualJournalNoUnique(
|
||||
tenantId: number,
|
||||
manualJournalDTO: IManualJournalDTO,
|
||||
journalNumber: string,
|
||||
notId?: number
|
||||
) {
|
||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
||||
const journalNumber = await ManualJournal.query()
|
||||
.where('journal_number', manualJournalDTO.journalNumber)
|
||||
const journals = await ManualJournal.query()
|
||||
.where('journal_number', journalNumber)
|
||||
.onBuild((builder) => {
|
||||
if (notId) {
|
||||
builder.whereNot('id', notId);
|
||||
}
|
||||
});
|
||||
if (journalNumber.length > 0) {
|
||||
if (journals.length > 0) {
|
||||
throw new ServiceError(ERRORS.JOURNAL_NUMBER_EXISTS);
|
||||
}
|
||||
}
|
||||
@@ -206,7 +210,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
);
|
||||
// Throw error in case one of entries that has invalid contact type.
|
||||
if (entriesNoContact.length > 0) {
|
||||
const indexes = entriesNoContact.map(e => e.index);
|
||||
const indexes = entriesNoContact.map((e) => e.index);
|
||||
|
||||
throw new ServiceError(ERRORS.ENTRIES_SHOULD_ASSIGN_WITH_CONTACT, '', {
|
||||
accountSlug: accountBySlug,
|
||||
@@ -291,18 +295,58 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the next journal number.
|
||||
*/
|
||||
getNextJournalNumber(tenantId: number): string {
|
||||
return this.autoIncrementOrdersService.getNextTransactionNumber(
|
||||
tenantId,
|
||||
'manual_journals'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the manual journal number.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
incrementNextJournalNumber(tenantId: number) {
|
||||
return this.autoIncrementOrdersService.incrementSettingsNextNumber(
|
||||
tenantId,
|
||||
'manual_journals'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the manual journal number require.
|
||||
* @param {string} journalNumber
|
||||
*/
|
||||
private validateJournalNoRequire(journalNumber: string) {
|
||||
if (!journalNumber) {
|
||||
throw new ServiceError(ERRORS.MANUAL_JOURNAL_NO_REQUIRED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the new manual journal DTO to upsert graph operation.
|
||||
* @param {IManualJournalDTO} manualJournalDTO - Manual jorunal DTO.
|
||||
* @param {ISystemUser} authorizedUser
|
||||
*/
|
||||
private transformNewDTOToModel(
|
||||
tenantId,
|
||||
manualJournalDTO: IManualJournalDTO,
|
||||
authorizedUser: ISystemUser
|
||||
) {
|
||||
const amount = sumBy(manualJournalDTO.entries, 'credit') || 0;
|
||||
const date = moment(manualJournalDTO.date).format('YYYY-MM-DD');
|
||||
|
||||
// Retrieve the next manual journal number.
|
||||
const autoNextNumber = this.getNextJournalNumber(tenantId);
|
||||
|
||||
const journalNumber = manualJournalDTO.journalNumber || autoNextNumber;
|
||||
|
||||
// Validate manual journal number require.
|
||||
this.validateJournalNoRequire(journalNumber);
|
||||
|
||||
return {
|
||||
...omit(manualJournalDTO, ['publish']),
|
||||
...(manualJournalDTO.publish
|
||||
@@ -310,6 +354,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
: {}),
|
||||
amount,
|
||||
date,
|
||||
journalNumber,
|
||||
userId: authorizedUser.id,
|
||||
};
|
||||
}
|
||||
@@ -350,6 +395,12 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
): Promise<{ manualJournal: IManualJournal }> {
|
||||
const { ManualJournal } = this.tenancy.models(tenantId);
|
||||
|
||||
// Transformes the next DTO to model.
|
||||
const manualJournalObj = this.transformNewDTOToModel(
|
||||
tenantId,
|
||||
manualJournalDTO,
|
||||
authorizedUser
|
||||
);
|
||||
// Validate the total credit should equals debit.
|
||||
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
||||
|
||||
@@ -360,8 +411,10 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
||||
|
||||
// Validate manual journal uniquiness on the storage.
|
||||
await this.validateManualJournalNoUnique(tenantId, manualJournalDTO);
|
||||
|
||||
await this.validateManualJournalNoUnique(
|
||||
tenantId,
|
||||
manualJournalObj.journalNumber
|
||||
);
|
||||
// Validate accounts with contact type from the given config.
|
||||
await this.dynamicValidateAccountsWithContactType(
|
||||
tenantId,
|
||||
@@ -371,10 +424,7 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
'[manual_journal] trying to save manual journal to the storage.',
|
||||
{ tenantId, manualJournalDTO }
|
||||
);
|
||||
const manualJournalObj = this.transformNewDTOToModel(
|
||||
manualJournalDTO,
|
||||
authorizedUser
|
||||
);
|
||||
|
||||
const manualJournal = await ManualJournal.query().upsertGraph({
|
||||
...manualJournalObj,
|
||||
});
|
||||
@@ -415,6 +465,11 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
tenantId,
|
||||
manualJournalId
|
||||
);
|
||||
// Transform manual journal DTO to model.
|
||||
const manualJournalObj = this.transformEditDTOToModel(
|
||||
manualJournalDTO,
|
||||
oldManualJournal
|
||||
);
|
||||
// Validates the total credit and debit to be equals.
|
||||
this.valdiateCreditDebitTotalEquals(manualJournalDTO);
|
||||
|
||||
@@ -425,21 +480,19 @@ export default class ManualJournalsService implements IManualJournalsService {
|
||||
await this.validateAccountsExistance(tenantId, manualJournalDTO);
|
||||
|
||||
// Validates the manual journal number uniquiness.
|
||||
await this.validateManualJournalNoUnique(
|
||||
tenantId,
|
||||
manualJournalDTO,
|
||||
manualJournalId
|
||||
);
|
||||
if (manualJournalDTO.journalNumber) {
|
||||
await this.validateManualJournalNoUnique(
|
||||
tenantId,
|
||||
manualJournalDTO.journalNumber,
|
||||
manualJournalId
|
||||
);
|
||||
}
|
||||
// Validate accounts with contact type from the given config.
|
||||
await this.dynamicValidateAccountsWithContactType(
|
||||
tenantId,
|
||||
manualJournalDTO.entries
|
||||
);
|
||||
// Transform manual journal DTO to model.
|
||||
const manualJournalObj = this.transformEditDTOToModel(
|
||||
manualJournalDTO,
|
||||
oldManualJournal
|
||||
);
|
||||
|
||||
await ManualJournal.query().upsertGraph({
|
||||
...manualJournalObj,
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ export const ERRORS = {
|
||||
CONTACTS_NOT_FOUND: 'contacts_not_found',
|
||||
ENTRIES_CONTACTS_NOT_FOUND: 'ENTRIES_CONTACTS_NOT_FOUND',
|
||||
MANUAL_JOURNAL_ALREADY_PUBLISHED: 'MANUAL_JOURNAL_ALREADY_PUBLISHED',
|
||||
MANUAL_JOURNAL_NO_REQUIRED: 'MANUAL_JOURNAL_NO_REQUIRED'
|
||||
};
|
||||
|
||||
export const CONTACTS_CONFIG = [
|
||||
|
||||
@@ -172,6 +172,15 @@ export default class BillPaymentsService {
|
||||
if (notFoundBillsIds.length > 0) {
|
||||
throw new ServiceError(ERRORS.BILL_ENTRIES_IDS_NOT_FOUND);
|
||||
}
|
||||
|
||||
// Validate the not opened bills.
|
||||
const notOpenedBills = storedBills.filter((bill) => !bill.openedAt);
|
||||
|
||||
if (notOpenedBills.length > 0) {
|
||||
throw new ServiceError(ERRORS.BILLS_NOT_OPENED_YET, null, {
|
||||
notOpenedBills
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,4 +9,5 @@ export const ERRORS = {
|
||||
BILL_PAYMENT_ENTRIES_NOT_FOUND: 'BILL_PAYMENT_ENTRIES_NOT_FOUND',
|
||||
INVALID_BILL_PAYMENT_AMOUNT: 'INVALID_BILL_PAYMENT_AMOUNT',
|
||||
PAYMENT_NUMBER_SHOULD_NOT_MODIFY: 'PAYMENT_NUMBER_SHOULD_NOT_MODIFY',
|
||||
BILLS_NOT_OPENED_YET: 'BILLS_NOT_OPENED_YET'
|
||||
};
|
||||
|
||||
@@ -139,6 +139,26 @@ export default class BillsService extends SalesInvoicesCost {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the bill has no payment entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billId - Bill id.
|
||||
*/
|
||||
private async validateBillHasNoEntries(
|
||||
tenantId,
|
||||
billId: number,
|
||||
) {
|
||||
const { BillPaymentEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retireve the bill associate payment made entries.
|
||||
const entries = await BillPaymentEntry.query().where('bill_id', billId);
|
||||
|
||||
if (entries.length > 0) {
|
||||
throw new ServiceError(ERRORS.BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the bill number require.
|
||||
* @param {string} billNo -
|
||||
@@ -354,6 +374,9 @@ export default class BillsService extends SalesInvoicesCost {
|
||||
// Retrieve the given bill or throw not found error.
|
||||
const oldBill = await this.getBillOrThrowError(tenantId, billId);
|
||||
|
||||
// Validate the purchase bill has no assocaited payments transactions.
|
||||
await this.validateBillHasNoEntries(tenantId, billId);
|
||||
|
||||
// Delete all associated bill entries.
|
||||
const deleteBillEntriesOper = ItemEntry.query()
|
||||
.where('reference_type', 'Bill')
|
||||
|
||||
@@ -7,5 +7,6 @@ export const ERRORS = {
|
||||
BILL_ENTRIES_IDS_NOT_FOUND: 'BILL_ENTRIES_IDS_NOT_FOUND',
|
||||
NOT_PURCHASE_ABLE_ITEMS: 'NOT_PURCHASE_ABLE_ITEMS',
|
||||
BILL_ALREADY_OPEN: 'BILL_ALREADY_OPEN',
|
||||
BILL_NO_IS_REQUIRED: 'BILL_NO_IS_REQUIRED'
|
||||
BILL_NO_IS_REQUIRED: 'BILL_NO_IS_REQUIRED',
|
||||
BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES: 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES'
|
||||
};
|
||||
|
||||
@@ -23,6 +23,7 @@ export default class AutoIncrementOrdersService {
|
||||
|
||||
// Settings service transaction number and prefix.
|
||||
const autoIncrement = settings.get({ group, key: 'auto_increment' }, false);
|
||||
|
||||
const settingNo = settings.get({ group, key: 'next_number' }, '');
|
||||
const settingPrefix = settings.get({ group, key: 'number_prefix' }, '');
|
||||
|
||||
@@ -37,11 +38,12 @@ export default class AutoIncrementOrdersService {
|
||||
*/
|
||||
async incrementSettingsNextNumber(tenantId: number, group: string) {
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
const settingNo = settings.get({ group, key: 'next_number' });
|
||||
const autoIncrement = settings.get({ group, key: 'auto_increment' });
|
||||
|
||||
// Can't continue if the auto-increment of the service was disabled.
|
||||
if (!autoIncrement) return;
|
||||
if (!autoIncrement) { return; }
|
||||
|
||||
settings.set(
|
||||
{ group, key: 'next_number' },
|
||||
|
||||
@@ -522,7 +522,7 @@ export default class PaymentReceiveService {
|
||||
public async deletePaymentReceive(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number,
|
||||
authorizedUser: ISystemUser
|
||||
authorizedUser: ISystemUser,
|
||||
) {
|
||||
const { PaymentReceive, PaymentReceiveEntry } = this.tenancy.models(
|
||||
tenantId
|
||||
@@ -541,6 +541,7 @@ export default class PaymentReceiveService {
|
||||
// Deletes the payment receive transaction.
|
||||
await PaymentReceive.query().findById(paymentReceiveId).delete();
|
||||
|
||||
// Triggers `onPaymentReceiveDeleted` event.
|
||||
await this.eventDispatcher.dispatch(events.paymentReceive.onDeleted, {
|
||||
tenantId,
|
||||
paymentReceiveId,
|
||||
|
||||
120
server/src/services/Setup/SetupService.ts
Normal file
120
server/src/services/Setup/SetupService.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import Currencies from 'js-money/lib/currency';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
import { IOrganizationSetupDTO, ITenant } from 'interfaces';
|
||||
|
||||
import CurrenciesService from 'services/Currencies/CurrenciesService';
|
||||
import TenantsManagerService from 'services/Tenancy/TenantsManager';
|
||||
import { ServiceError } from 'exceptions';
|
||||
|
||||
const ERRORS = {
|
||||
TENANT_IS_ALREADY_SETUPED: 'TENANT_IS_ALREADY_SETUPED',
|
||||
BASE_CURRENCY_INVALID: 'BASE_CURRENCY_INVALID',
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class SetupService {
|
||||
@Inject()
|
||||
tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
currenciesService: CurrenciesService;
|
||||
|
||||
@Inject()
|
||||
tenantsManager: TenantsManagerService;
|
||||
|
||||
@Inject('repositories')
|
||||
sysRepositories: any;
|
||||
|
||||
/**
|
||||
* Transformes the setup DTO to settings.
|
||||
* @param {IOrganizationSetupDTO} setupDTO
|
||||
* @returns
|
||||
*/
|
||||
private transformSetupDTOToOptions(setupDTO: IOrganizationSetupDTO) {
|
||||
return [
|
||||
{ key: 'name', value: setupDTO.organizationName },
|
||||
{ key: 'base_currency', value: setupDTO.baseCurrency },
|
||||
{ key: 'time_zone', value: setupDTO.timeZone },
|
||||
{ key: 'industry', value: setupDTO.industry },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets organization setup settings.
|
||||
* @param {number} tenantId
|
||||
* @param {IOrganizationSetupDTO} organizationSetupDTO
|
||||
*/
|
||||
private setOrganizationSetupSettings(
|
||||
tenantId: number,
|
||||
organizationSetupDTO: IOrganizationSetupDTO
|
||||
) {
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
|
||||
// Can't continue if app is already configured.
|
||||
if (settings.get('app_configured')) {
|
||||
return;
|
||||
}
|
||||
settings.set([
|
||||
...this.transformSetupDTOToOptions(organizationSetupDTO)
|
||||
.filter((option) => typeof option.value !== 'undefined')
|
||||
.map((option) => ({
|
||||
...option,
|
||||
group: 'organization',
|
||||
})),
|
||||
{ key: 'app_configured', value: true },
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the base currency code.
|
||||
* @param {string} baseCurrency
|
||||
*/
|
||||
public validateBaseCurrencyCode(baseCurrency: string) {
|
||||
if (typeof Currencies[baseCurrency] === 'undefined') {
|
||||
throw new ServiceError(ERRORS.BASE_CURRENCY_INVALID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization setup DTO.
|
||||
* @param {IOrganizationSetupDTO} organizationSetupDTO
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async organizationSetup(
|
||||
tenantId: number,
|
||||
organizationSetupDTO: IOrganizationSetupDTO,
|
||||
): Promise<void> {
|
||||
const { tenantRepository } = this.sysRepositories;
|
||||
|
||||
// Find tenant model by the given id.
|
||||
const tenant = await tenantRepository.findOneById(tenantId);
|
||||
|
||||
// Validate base currency code.
|
||||
this.validateBaseCurrencyCode(organizationSetupDTO.baseCurrency);
|
||||
|
||||
// Validate tenant not already seeded.
|
||||
this.validateTenantNotSeeded(tenant);
|
||||
|
||||
// Seeds the base currency to the currencies list.
|
||||
this.currenciesService.seedBaseCurrency(
|
||||
tenantId,
|
||||
organizationSetupDTO.baseCurrency
|
||||
);
|
||||
// Sets organization setup settings.
|
||||
await this.setOrganizationSetupSettings(tenantId, organizationSetupDTO);
|
||||
|
||||
// Seed tenant.
|
||||
await this.tenantsManager.seedTenant(tenant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates tenant not seeded.
|
||||
* @param {ITenant} tenant
|
||||
*/
|
||||
private validateTenantNotSeeded(tenant: ITenant) {
|
||||
if (tenant.seededAt) {
|
||||
throw new ServiceError(ERRORS.TENANT_IS_ALREADY_SETUPED);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user