feat: journal entries with expenses operations.

This commit is contained in:
a.bouhuolia
2021-01-03 12:47:20 +02:00
parent f18ab184e2
commit a2284945f1
9 changed files with 658 additions and 303 deletions

View File

@@ -225,12 +225,15 @@ export default class JournalCommands {
* Writes journal entries of expense model object.
* @param {IExpense} expense
*/
expense(expense: IExpense) {
expense(
expense: IExpense,
userId: number,
) {
const mixinEntry = {
referenceType: 'Expense',
referenceId: expense.id,
date: expense.paymentDate,
userId: expense.userId,
userId,
draft: !expense.publishedAt,
};
const paymentJournalEntry = new JournalEntry({

View File

@@ -16,6 +16,22 @@ import {
import DynamicListingService from 'services/DynamicListing/DynamicListService';
import events from 'subscribers/events';
const ERRORS = {
ACCOUNT_NOT_FOUND: 'account_not_found',
ACCOUNT_TYPE_NOT_FOUND: 'account_type_not_found',
PARENT_ACCOUNT_NOT_FOUND: 'parent_account_not_found',
ACCOUNT_CODE_NOT_UNIQUE: 'account_code_not_unique',
ACCOUNT_NAME_NOT_UNIQUE: 'account_name_not_unqiue',
PARENT_ACCOUNT_HAS_DIFFERENT_TYPE: 'parent_has_different_type',
ACCOUNT_TYPE_NOT_ALLOWED_TO_CHANGE: 'account_type_not_allowed_to_changed',
ACCOUNT_PREDEFINED: 'account_predefined',
ACCOUNT_HAS_ASSOCIATED_TRANSACTIONS: 'account_has_associated_transactions',
PREDEFINED_ACCOUNTS: 'predefined_accounts',
ACCOUNTS_HAVE_TRANSACTIONS: 'accounts_have_transactions',
CLOSE_ACCOUNT_AND_TO_ACCOUNT_NOT_SAME_TYPE: 'close_account_and_to_account_not_same_type',
ACCOUNTS_NOT_FOUND: 'accounts_not_found',
}
@Service()
export default class AccountsService {
@Inject()
@@ -50,7 +66,7 @@ export default class AccountsService {
if (!accountType) {
this.logger.info('[accounts] account type not found.');
throw new ServiceError('account_type_not_found');
throw new ServiceError(ERRORS.ACCOUNT_TYPE_NOT_FOUND);
}
return accountType;
}
@@ -85,7 +101,7 @@ export default class AccountsService {
tenantId,
accountId,
});
throw new ServiceError('parent_account_not_found');
throw new ServiceError(ERRORS.PARENT_ACCOUNT_NOT_FOUND);
}
return parentAccount;
}
@@ -124,7 +140,7 @@ export default class AccountsService {
tenantId,
accountCode,
});
throw new ServiceError('account_code_not_unique');
throw new ServiceError(ERRORS.ACCOUNT_CODE_NOT_UNIQUE);
}
}
@@ -138,7 +154,7 @@ export default class AccountsService {
parentAccount: IAccount
) {
if (accountDTO.accountTypeId !== parentAccount.accountTypeId) {
throw new ServiceError('parent_has_different_type');
throw new ServiceError(ERRORS.PARENT_ACCOUNT_HAS_DIFFERENT_TYPE);
}
}
@@ -161,7 +177,7 @@ export default class AccountsService {
this.logger.info('[accounts] the given account not found.', {
accountId,
});
throw new ServiceError('account_not_found');
throw new ServiceError(ERRORS.ACCOUNT_NOT_FOUND);
}
return account;
}
@@ -178,7 +194,7 @@ export default class AccountsService {
newAccount: IAccount | IAccountDTO
) {
if (oldAccount.accountTypeId !== newAccount.accountTypeId) {
throw new ServiceError('account_type_not_allowed_to_changed');
throw new ServiceError(ERRORS.ACCOUNT_TYPE_NOT_ALLOWED_TO_CHANGE);
}
}
@@ -208,7 +224,7 @@ export default class AccountsService {
}
});
if (foundAccount) {
throw new ServiceError('account_name_not_unqiue');
throw new ServiceError(ERRORS.ACCOUNT_NAME_NOT_UNIQUE);
}
}
@@ -346,7 +362,7 @@ export default class AccountsService {
*/
private throwErrorIfAccountPredefined(account: IAccount) {
if (account.predefined) {
throw new ServiceError('account_predefined');
throw new ServiceError(ERRORS.ACCOUNT_PREDEFINED);
}
}
@@ -384,7 +400,7 @@ export default class AccountsService {
accountId
);
if (accountTransactions.length > 0) {
throw new ServiceError('account_has_associated_transactions');
throw new ServiceError(ERRORS.ACCOUNT_HAS_ASSOCIATED_TRANSACTIONS);
}
}
@@ -441,7 +457,7 @@ export default class AccountsService {
tenantId,
notFoundAccounts,
});
throw new ServiceError('accounts_not_found');
throw new ServiceError(ERRORS.ACCOUNTS_NOT_FOUND);
}
return storedAccounts;
}
@@ -458,7 +474,7 @@ export default class AccountsService {
if (predefined.length > 0) {
this.logger.error('[accounts] some accounts predefined.', { predefined });
throw new ServiceError('predefined_accounts');
throw new ServiceError(ERRORS.PREDEFINED_ACCOUNTS);
}
return predefined;
}
@@ -487,7 +503,7 @@ export default class AccountsService {
}
});
if (accountsHasTransactions.length > 0) {
throw new ServiceError('accounts_have_transactions');
throw new ServiceError(ERRORS.ACCOUNTS_HAVE_TRANSACTIONS);
}
}
@@ -677,7 +693,7 @@ export default class AccountsService {
);
if (accountType.rootType !== toAccountType.rootType) {
throw new ServiceError('close_account_and_to_account_not_same_type');
throw new ServiceError(ERRORS.CLOSE_ACCOUNT_AND_TO_ACCOUNT_NOT_SAME_TYPE);
}
const updateAccountBalanceOper = await accountRepository.balanceChange(
accountId,

View File

@@ -1,18 +1,26 @@
import { Service, Inject } from "typedi";
import { difference, sumBy, omit } from 'lodash';
import moment from "moment";
import { Service, Inject } from 'typedi';
import { difference, sumBy, omit, map } from 'lodash';
import moment from 'moment';
import {
EventDispatcher,
EventDispatcherInterface,
} from 'decorators/eventDispatcher';
import { ServiceError } from "exceptions";
import { ServiceError } from 'exceptions';
import TenancyService from 'services/Tenancy/TenancyService';
import JournalPoster from 'services/Accounting/JournalPoster';
import JournalCommands from 'services/Accounting/JournalCommands';
import { IExpense, IExpensesFilter, IAccount, IExpenseDTO, IExpensesService, ISystemUser, IPaginationMeta } from 'interfaces';
import {
IExpense,
IExpensesFilter,
IAccount,
IExpenseDTO,
IExpensesService,
ISystemUser,
IPaginationMeta,
} from 'interfaces';
import DynamicListingService from 'services/DynamicListing/DynamicListService';
import events from 'subscribers/events';
import ContactsService from "services/Contacts/ContactsService";
import ContactsService from 'services/Contacts/ContactsService';
const ERRORS = {
EXPENSE_NOT_FOUND: 'expense_not_found',
@@ -43,20 +51,31 @@ export default class ExpensesService implements IExpensesService {
contactsService: ContactsService;
/**
* Retrieve the payment account details or returns not found server error in case the
* Retrieve the payment account details or returns not found server error in case the
* given account not found on the storage.
* @param {number} tenantId
* @param {number} paymentAccountId
* @param {number} tenantId
* @param {number} paymentAccountId
* @returns {Promise<IAccount>}
*/
private async getPaymentAccountOrThrowError(tenantId: number, paymentAccountId: number) {
this.logger.info('[expenses] trying to get the given payment account.', { tenantId, paymentAccountId });
private async getPaymentAccountOrThrowError(
tenantId: number,
paymentAccountId: number
) {
this.logger.info('[expenses] trying to get the given payment account.', {
tenantId,
paymentAccountId,
});
const { accountRepository } = this.tenancy.repositories(tenantId);
const paymentAccount = await accountRepository.findOneById(paymentAccountId)
const paymentAccount = await accountRepository.findOneById(
paymentAccountId
);
if (!paymentAccount) {
this.logger.info('[expenses] the given payment account not found.', { tenantId, paymentAccountId });
this.logger.info('[expenses] the given payment account not found.', {
tenantId,
paymentAccountId,
});
throw new ServiceError(ERRORS.PAYMENT_ACCOUNT_NOT_FOUND);
}
return paymentAccount;
@@ -65,25 +84,37 @@ export default class ExpensesService implements IExpensesService {
/**
* Retrieve expense accounts or throw error in case one of the given accounts
* not found not the storage.
* @param {number} tenantId
* @param {number} expenseAccountsIds
* @param {number} tenantId
* @param {number} expenseAccountsIds
* @throws {ServiceError}
* @returns {Promise<IAccount[]>}
*/
private async getExpensesAccountsOrThrowError(tenantId: number, expenseAccountsIds: number[]) {
this.logger.info('[expenses] trying to get expenses accounts.', { tenantId, expenseAccountsIds });
private async getExpensesAccountsOrThrowError(
tenantId: number,
expenseAccountsIds: number[]
) {
this.logger.info('[expenses] trying to get expenses accounts.', {
tenantId,
expenseAccountsIds,
});
const { accountRepository } = this.tenancy.repositories(tenantId);
const storedExpenseAccounts = await accountRepository.findWhereIn(
'id', expenseAccountsIds,
'id',
expenseAccountsIds
);
const storedExpenseAccountsIds = storedExpenseAccounts.map(
(a: IAccount) => a.id
);
const storedExpenseAccountsIds = storedExpenseAccounts.map((a: IAccount) => a.id);
const notStoredAccountsIds = difference(
expenseAccountsIds,
storedExpenseAccountsIds
);
if (notStoredAccountsIds.length > 0) {
this.logger.info('[expenses] some of expense accounts not found.', { tenantId, expenseAccountsIds });
this.logger.info('[expenses] some of expense accounts not found.', {
tenantId,
expenseAccountsIds,
});
throw new ServiceError(ERRORS.SOME_ACCOUNTS_NOT_FOUND);
}
return storedExpenseAccounts;
@@ -91,33 +122,44 @@ export default class ExpensesService implements IExpensesService {
/**
* Validates expense categories not equals zero.
* @param {IExpenseDTO|ServiceError} expenseDTO
* @param {IExpenseDTO|ServiceError} expenseDTO
* @throws {ServiceError}
*/
private validateCategoriesNotEqualZero(expenseDTO: IExpenseDTO) {
this.logger.info('[expenses] validate the expenses categoires not equal zero.', { expenseDTO });
this.logger.info(
'[expenses] validate the expenses categoires not equal zero.',
{ expenseDTO }
);
const totalAmount = sumBy(expenseDTO.categories, 'amount') || 0;
if (totalAmount <= 0) {
this.logger.info('[expenses] the given expense categories equal zero.', { expenseDTO });
this.logger.info('[expenses] the given expense categories equal zero.', {
expenseDTO,
});
throw new ServiceError(ERRORS.TOTAL_AMOUNT_EQUALS_ZERO);
}
}
/**
* Validate expenses accounts type.
* @param {number} tenantId
* @param {number[]} expensesAccountsIds
* @param {number} tenantId
* @param {number[]} expensesAccountsIds
*/
private async validateExpensesAccountsType(tenantId: number, expensesAccounts: number[]) {
this.logger.info('[expenses] trying to validate expenses accounts type.', { tenantId, expensesAccounts });
private async validateExpensesAccountsType(
tenantId: number,
expensesAccounts: number[]
) {
this.logger.info('[expenses] trying to validate expenses accounts type.', {
tenantId,
expensesAccounts,
});
const { accountTypeRepository } = this.tenancy.repositories(tenantId);
// Retrieve accounts types of the given root type.
const expensesTypes = await accountTypeRepository.getByRootType('expense');
const expensesTypesIds = expensesTypes.map(t => t.id);
const expensesTypesIds = expensesTypes.map((t) => t.id);
const invalidExpenseAccounts: number[] = [];
expensesAccounts.forEach((expenseAccount) => {
@@ -132,68 +174,83 @@ export default class ExpensesService implements IExpensesService {
/**
* Validates payment account type in case has invalid type throws errors.
* @param {number} tenantId
* @param {number} paymentAccountId
* @param {number} tenantId
* @param {number} paymentAccountId
* @throws {ServiceError}
*/
private async validatePaymentAccountType(tenantId: number, paymentAccount: number[]) {
this.logger.info('[expenses] trying to validate payment account type.', { tenantId, paymentAccount });
private async validatePaymentAccountType(
tenantId: number,
paymentAccount: number[]
) {
this.logger.info('[expenses] trying to validate payment account type.', {
tenantId,
paymentAccount,
});
const { accountTypeRepository } = this.tenancy.repositories(tenantId);
// Retrieve account tpy eof the given key.
const validAccountsType = await accountTypeRepository.getByKeys([
'current_asset', 'fixed_asset',
'current_asset',
'fixed_asset',
]);
const validAccountsTypeIds = validAccountsType.map(t => t.id);
const validAccountsTypeIds = validAccountsType.map((t) => t.id);
if (validAccountsTypeIds.indexOf(paymentAccount.accountTypeId) === -1) {
this.logger.info('[expenses] the given payment account has invalid type', { tenantId, paymentAccount });
this.logger.info(
'[expenses] the given payment account has invalid type',
{ tenantId, paymentAccount }
);
throw new ServiceError(ERRORS.PAYMENT_ACCOUNT_HAS_INVALID_TYPE);
}
}
/**
* Reverts expense journal entries.
* @param {number} tenantId
* @param {number} expenseId
* @param {number} tenantId
* @param {number} expenseId
*/
public async revertJournalEntries(
tenantId: number,
expenseId: number|number[],
expenseId: number | number[]
): Promise<void> {
const journal = new JournalPoster(tenantId);
const journalCommands = new JournalCommands(journal);
await journalCommands.revertJournalEntries(expenseId, 'Expense');
await Promise.all([
journal.saveBalance(),
journal.deleteEntries(),
]);
await Promise.all([journal.saveBalance(), journal.deleteEntries()]);
}
/**
* Writes expense journal entries.
* @param {number} tenantId
* @param {IExpense} expense
* @param {IUser} authorizedUser
* @param {number} tenantId
* @param {IExpense} expense
* @param {IUser} authorizedUser
*/
public async writeJournalEntries(
tenantId: number,
expense: IExpense,
revertOld: boolean,
) {
this.logger.info('[expense[ trying to write expense journal entries.', { tenantId, expense });
expense: IExpense | IExpense[],
authorizedUserId: number,
override: boolean = false
): Promise<void> {
this.logger.info('[expense] trying to write expense journal entries.', {
tenantId,
expense,
});
const journal = new JournalPoster(tenantId);
const journalCommands = new JournalCommands(journal);
if (revertOld) {
await journalCommands.revertJournalEntries(expense.id, 'Expense');
const expenses = Array.isArray(expense) ? expense : [expense];
const expensesIds = expenses.map((expense) => expense.id);
if (override) {
await journalCommands.revertJournalEntries(expensesIds, 'Expense');
}
journalCommands.expense(expense);
return Promise.all([
expenses.forEach((expense: IExpense) => {
journalCommands.expense(expense, authorizedUserId);
});
await Promise.all([
journal.saveBalance(),
journal.saveEntries(),
journal.deleteEntries(),
@@ -202,20 +259,26 @@ export default class ExpensesService implements IExpensesService {
/**
* Retrieve the given expenses or throw not found error.
* @param {number} tenantId
* @param {number} expenseId
* @param {number} tenantId
* @param {number} expenseId
* @returns {IExpense|ServiceError}
*/
private async getExpenseOrThrowError(tenantId: number, expenseId: number) {
const { expenseRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[expense] trying to get the given expense.', { tenantId, expenseId });
this.logger.info('[expense] trying to get the given expense.', {
tenantId,
expenseId,
});
// Retrieve the given expense by id.
const expense = await expenseRepository.findOneById(expenseId);
if (!expense) {
this.logger.info('[expense] the given expense not found.', { tenantId, expenseId });
this.logger.info('[expense] the given expense not found.', {
tenantId,
expenseId,
});
throw new ServiceError(ERRORS.EXPENSE_NOT_FOUND);
}
return expense;
@@ -229,24 +292,31 @@ export default class ExpensesService implements IExpensesService {
async getExpensesOrThrowError(
tenantId: number,
expensesIds: number[]
): Promise<IExpense> {
): Promise<IExpense[]> {
const { expenseRepository } = this.tenancy.repositories(tenantId);
const storedExpenses = expenseRepository.findWhereIn('id', expensesIds);
const storedExpenses = await expenseRepository.findWhereIn(
'id',
expensesIds,
'categories'
);
const storedExpensesIds = storedExpenses.map((expense) => expense.id);
const notFoundExpenses = difference(expensesIds, storedExpensesIds);
// In case there is not found expenses throw service error.
if (notFoundExpenses.length > 0) {
this.logger.info('[expense] the give expenses ids not found.', { tenantId, expensesIds });
throw new ServiceError(ERRORS.EXPENSES_NOT_FOUND)
this.logger.info('[expense] the give expenses ids not found.', {
tenantId,
expensesIds,
});
throw new ServiceError(ERRORS.EXPENSES_NOT_FOUND);
}
return storedExpenses;
}
/**
* Validates expenses is not already published before.
* @param {IExpense} expense
* @param {IExpense} expense
*/
private validateExpenseIsNotPublished(expense: IExpense) {
if (expense.publishedAt) {
@@ -256,7 +326,7 @@ export default class ExpensesService implements IExpensesService {
/**
* Mapping expense DTO to model.
* @param {IExpenseDTO} expenseDTO
* @param {IExpenseDTO} expenseDTO
* @param {ISystemUser} authorizedUser
* @return {IExpense}
*/
@@ -268,88 +338,28 @@ export default class ExpensesService implements IExpensesService {
...omit(expenseDTO, ['publish']),
totalAmount,
paymentDate: moment(expenseDTO.paymentDate).toMySqlDateTime(),
...(user) ? {
userId: user.id,
} : {},
...(expenseDTO.publish) ? {
publishedAt: moment().toMySqlDateTime(),
} : {},
}
...(user
? {
userId: user.id,
}
: {}),
...(expenseDTO.publish
? {
publishedAt: moment().toMySqlDateTime(),
}
: {}),
};
}
/**
* Mapping the expenses accounts ids from expense DTO.
* @param {IExpenseDTO} expenseDTO
* @param {IExpenseDTO} expenseDTO
* @return {number[]}
*/
mapExpensesAccountsIdsFromDTO(expenseDTO: IExpenseDTO) {
return expenseDTO.categories.map((category) => category.expenseAccountId);
}
/**
* Precedures.
* ---------
* 1. Validate expense existance.
* 2. Validate payment account existance on the storage.
* 3. Validate expense accounts exist on the storage.
* 4. Validate payment account type.
* 5. Validate expenses accounts type.
* 6. Validate the given expense categories not equal zero.
* 7. Stores the expense to the storage.
* ---------
* @param {number} tenantId
* @param {number} expenseId
* @param {IExpenseDTO} expenseDTO
* @param {ISystemUser} authorizedUser
*/
public async editExpense(
tenantId: number,
expenseId: number,
expenseDTO: IExpenseDTO,
authorizedUser: ISystemUser
): Promise<IExpense> {
const { expenseRepository } = this.tenancy.repositories(tenantId);
const expense = await this.getExpenseOrThrowError(tenantId, expenseId);
// - Validate payment account existance on the storage.
const paymentAccount = await this.getPaymentAccountOrThrowError(
tenantId,
expenseDTO.paymentAccountId,
);
// - Validate expense accounts exist on the storage.
const expensesAccounts = await this.getExpensesAccountsOrThrowError(
tenantId,
this.mapExpensesAccountsIdsFromDTO(expenseDTO),
);
// - Validate payment account type.
await this.validatePaymentAccountType(tenantId, paymentAccount);
// - Validate expenses accounts type.
await this.validateExpensesAccountsType(tenantId, expensesAccounts);
// - Validate the expense payee contact id existance on storage.
if (expenseDTO.payeeId) {
await this.contactsService.getContactByIdOrThrowError(
tenantId,
expenseDTO.payeeId,
)
}
// - Validate the given expense categories not equal zero.
this.validateCategoriesNotEqualZero(expenseDTO);
// - Update the expense on the storage.
const expenseObj = this.expenseDTOToModel(expenseDTO);
// - Upsert the expense object with expense entries.
const expenseModel = await expenseRepository.upsertGraph({
id: expenseId,
...expenseObj,
});
this.logger.info('[expense] the expense updated on the storage successfully.', { tenantId, expenseDTO });
return expenseModel;
}
/**
* Precedures.
* ---------
@@ -361,25 +371,94 @@ export default class ExpensesService implements IExpensesService {
* 6. Validate the given expense categories not equal zero.
* 7. Stores the expense to the storage.
* ---------
* @param {number} tenantId
* @param {IExpenseDTO} expenseDTO
* @param {number} tenantId
* @param {IExpenseDTO} expenseDTO
*/
public async newExpense(
tenantId: number,
expenseDTO: IExpenseDTO,
authorizedUser: ISystemUser,
authorizedUser: ISystemUser
): Promise<IExpense> {
const { expenseRepository } = this.tenancy.repositories(tenantId);
// Validate payment account existance on the storage.
const paymentAccount = await this.getPaymentAccountOrThrowError(
tenantId,
expenseDTO.paymentAccountId
);
// Validate expense accounts exist on the storage.
const expensesAccounts = await this.getExpensesAccountsOrThrowError(
tenantId,
this.mapExpensesAccountsIdsFromDTO(expenseDTO)
);
// Validate payment account type.
await this.validatePaymentAccountType(tenantId, paymentAccount);
// Validate expenses accounts type.
await this.validateExpensesAccountsType(tenantId, expensesAccounts);
// Validate the expense payee contact id existance on storage.
if (expenseDTO.payeeId) {
await this.contactsService.getContactByIdOrThrowError(
tenantId,
expenseDTO.payeeId
);
}
// Validate the given expense categories not equal zero.
this.validateCategoriesNotEqualZero(expenseDTO);
// Save the expense to the storage.
const expenseObj = this.expenseDTOToModel(expenseDTO, authorizedUser);
const expense = await expenseRepository.upsertGraph(expenseObj);
this.logger.info(
'[expense] the expense stored to the storage successfully.',
{ tenantId, expenseDTO }
);
// Triggers `onExpenseCreated` event.
this.eventDispatcher.dispatch(events.expenses.onCreated, {
tenantId,
expenseId: expense.id,
authorizedUser,
expense,
});
return expense;
}
/**
* Precedures.
* ---------
* 1. Validate expense existance.
* 2. Validate payment account existance on the storage.
* 3. Validate expense accounts exist on the storage.
* 4. Validate payment account type.
* 5. Validate expenses accounts type.
* 6. Validate the given expense categories not equal zero.
* 7. Stores the expense to the storage.
* ---------
* @param {number} tenantId
* @param {number} expenseId
* @param {IExpenseDTO} expenseDTO
* @param {ISystemUser} authorizedUser
*/
public async editExpense(
tenantId: number,
expenseId: number,
expenseDTO: IExpenseDTO,
authorizedUser: ISystemUser
): Promise<IExpense> {
const { expenseRepository } = this.tenancy.repositories(tenantId);
const oldExpense = await this.getExpenseOrThrowError(tenantId, expenseId);
// - Validate payment account existance on the storage.
const paymentAccount = await this.getPaymentAccountOrThrowError(
tenantId,
expenseDTO.paymentAccountId,
expenseDTO.paymentAccountId
);
// - Validate expense accounts exist on the storage.
const expensesAccounts = await this.getExpensesAccountsOrThrowError(
tenantId,
this.mapExpensesAccountsIdsFromDTO(expenseDTO),
this.mapExpensesAccountsIdsFromDTO(expenseDTO)
);
// - Validate payment account type.
await this.validatePaymentAccountType(tenantId, paymentAccount);
@@ -391,140 +470,299 @@ export default class ExpensesService implements IExpensesService {
if (expenseDTO.payeeId) {
await this.contactsService.getContactByIdOrThrowError(
tenantId,
expenseDTO.payeeId,
)
expenseDTO.payeeId
);
}
// - Validate the given expense categories not equal zero.
this.validateCategoriesNotEqualZero(expenseDTO);
// - Save the expense to the storage.
const expenseObj = this.expenseDTOToModel(expenseDTO, authorizedUser);
const expenseModel = await expenseRepository.upsertGraph(expenseObj);
// - Update the expense on the storage.
const expenseObj = this.expenseDTOToModel(expenseDTO);
this.logger.info('[expense] the expense stored to the storage successfully.', { tenantId, expenseDTO });
// - Upsert the expense object with expense entries.
const expense = await expenseRepository.upsertGraph({
id: expenseId,
...expenseObj,
});
this.logger.info(
'[expense] the expense updated on the storage successfully.',
{ tenantId, expenseId }
);
// Triggers `onExpenseCreated` event.
this.eventDispatcher.dispatch(events.expenses.onCreated, { tenantId, expenseId: expenseModel.id });
return expenseModel;
this.eventDispatcher.dispatch(events.expenses.onEdited, {
tenantId,
expenseId,
expense,
expenseDTO,
authorizedUser,
oldExpense,
});
return expense;
}
/**
* Publish the given expense.
* @param {number} tenantId
* @param {number} expenseId
* @param {number} tenantId
* @param {number} expenseId
* @param {ISystemUser} authorizedUser
* @return {Promise<void>}
*/
public async publishExpense(tenantId: number, expenseId: number, authorizedUser: ISystemUser) {
public async publishExpense(
tenantId: number,
expenseId: number,
authorizedUser: ISystemUser
) {
const { expenseRepository } = this.tenancy.repositories(tenantId);
const expense = await this.getExpenseOrThrowError(tenantId, expenseId);
const oldExpense = await this.getExpenseOrThrowError(tenantId, expenseId);
if (expense instanceof ServiceError) {
throw expense;
if (oldExpense instanceof ServiceError) {
throw oldExpense;
}
this.validateExpenseIsNotPublished(expense);
this.validateExpenseIsNotPublished(oldExpense);
this.logger.info('[expense] trying to publish the expense.', { tenantId, expenseId });
this.logger.info('[expense] trying to publish the expense.', {
tenantId,
expenseId,
});
// Publish the given expense on the storage.
await expenseRepository.publish(expenseId);
this.logger.info('[expense] the expense published successfully.', { tenantId, expenseId });
// Retrieve the new expense after modification.
const expense = await expenseRepository.findOneById(
expenseId,
'categories'
);
this.logger.info('[expense] the expense published successfully.', {
tenantId,
expenseId,
});
// Triggers `onExpensePublished` event.
this.eventDispatcher.dispatch(events.expenses.onPublished, { tenantId, expenseId });
this.eventDispatcher.dispatch(events.expenses.onPublished, {
tenantId,
expenseId,
oldExpense,
expense,
authorizedUser,
});
}
/**
* Deletes the given expense.
* @param {number} tenantId
* @param {number} expenseId
* @param {number} tenantId
* @param {number} expenseId
* @param {ISystemUser} authorizedUser
*/
public async deleteExpense(tenantId: number, expenseId: number, authorizedUser: ISystemUser) {
const expense = await this.getExpenseOrThrowError(tenantId, expenseId);
const { expenseRepository } = this.tenancy.repositories(tenantId);
public async deleteExpense(
tenantId: number,
expenseId: number,
authorizedUser: ISystemUser
): Promise<void> {
const oldExpense = await this.getExpenseOrThrowError(tenantId, expenseId);
const {
expenseRepository,
expenseEntryRepository,
} = this.tenancy.repositories(tenantId);
this.logger.info('[expense] trying to delete the expense.', { tenantId, expenseId });
this.logger.info('[expense] trying to delete the expense.', {
tenantId,
expenseId,
});
await expenseEntryRepository.deleteBy({ expenseId });
await expenseRepository.deleteById(expenseId);
this.logger.info('[expense] the expense deleted successfully.', { tenantId, expenseId });
this.logger.info('[expense] the expense deleted successfully.', {
tenantId,
expenseId,
});
// Triggers `onExpenseDeleted` event.
this.eventDispatcher.dispatch(events.expenses.onDeleted, { tenantId, expenseId });
this.eventDispatcher.dispatch(events.expenses.onDeleted, {
tenantId,
expenseId,
authorizedUser,
oldExpense,
});
}
/**
* Deletes the given expenses in bulk.
* @param {number} tenantId
* @param {number[]} expensesIds
* @param {number} tenantId
* @param {number[]} expensesIds
* @param {ISystemUser} authorizedUser
*/
public async deleteBulkExpenses(tenantId: number, expensesIds: number[], authorizedUser: ISystemUser) {
const expenses = await this.getExpensesOrThrowError(tenantId, expensesIds);
const { expenseRepository } = this.tenancy.repositories(tenantId);
public async deleteBulkExpenses(
tenantId: number,
expensesIds: number[],
authorizedUser: ISystemUser
) {
const {
expenseRepository,
expenseEntryRepository,
} = this.tenancy.repositories(tenantId);
this.logger.info('[expense] trying to delete the given expenses.', { tenantId, expensesIds });
// Retrieve olds expenses.
const oldExpenses = await this.getExpensesOrThrowError(
tenantId,
expensesIds
);
this.logger.info('[expense] trying to delete the given expenses.', {
tenantId,
expensesIds,
});
await expenseEntryRepository.deleteWhereIn('expenseId', expensesIds);
await expenseRepository.deleteWhereIdIn(expensesIds);
this.logger.info('[expense] the given expenses deleted successfully.', { tenantId, expensesIds });
this.logger.info('[expense] the given expenses deleted successfully.', {
tenantId,
expensesIds,
});
// Triggers `onExpenseBulkDeleted` event.
this.eventDispatcher.dispatch(events.expenses.onBulkDeleted, { tenantId, expensesIds });
this.eventDispatcher.dispatch(events.expenses.onBulkDeleted, {
tenantId,
expensesIds,
oldExpenses,
authorizedUser,
});
}
/**
* Filters the not published expenses.
* @param {IExpense[]} expenses -
*/
public getNonePublishedExpenses(expenses: IExpense[]): IExpense[] {
return expenses.filter((expense) => !expense.publishedAt);
}
/**
* Filtesr the published expenses.
* @param {IExpense[]} expenses -
* @return {IExpense[]}
*/
public getPublishedExpenses(expenses: IExpense[]): IExpense[] {
return expenses.filter((expense) => expense.publishedAt);
}
/**
* Deletes the given expenses in bulk.
* @param {number} tenantId
* @param {number[]} expensesIds
* @param {number} tenantId
* @param {number[]} expensesIds
* @param {ISystemUser} authorizedUser
*/
public async publishBulkExpenses(tenantId: number, expensesIds: number[], authorizedUser: ISystemUser) {
const expenses = await this.getExpensesOrThrowError(tenantId, expensesIds);
public async publishBulkExpenses(
tenantId: number,
expensesIds: number[],
authorizedUser: ISystemUser
): Promise<{
meta: {
alreadyPublished: number;
published: number;
total: number,
},
}> {
const oldExpenses = await this.getExpensesOrThrowError(
tenantId,
expensesIds
);
const { expenseRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[expense] trying to publish the given expenses.', { tenantId, expensesIds });
await expenseRepository.whereIdInPublish(expensesIds);
// Filters the not published expenses.
const notPublishedExpenses = this.getNonePublishedExpenses(oldExpenses);
this.logger.info('[expense] the given expenses ids published successfully.', { tenantId, expensesIds });
// Filters the published expenses.
const publishedExpenses = this.getPublishedExpenses(oldExpenses);
// Mappes the published expenses to get id.
const notPublishedExpensesIds = map(notPublishedExpenses, 'id');
if (notPublishedExpensesIds.length > 0) {
this.logger.info('[expense] trying to publish the given expenses.', {
tenantId,
expensesIds,
});
await expenseRepository.whereIdInPublish(notPublishedExpensesIds);
this.logger.info(
'[expense] the given expenses ids published successfully.',
{ tenantId, expensesIds }
);
}
// Retrieve the new expenses after modification.
const expenses = await expenseRepository.findWhereIn(
'id',
expensesIds,
'categories'
);
// Triggers `onExpenseBulkDeleted` event.
this.eventDispatcher.dispatch(events.expenses.onBulkPublished, { tenantId, expensesIds });
this.eventDispatcher.dispatch(events.expenses.onBulkPublished, {
tenantId,
expensesIds,
oldExpenses,
expenses,
authorizedUser,
});
return {
meta: {
alreadyPublished: publishedExpenses.length,
published: notPublishedExpenses.length,
total: oldExpenses.length,
},
};
}
/**
* Retrieve expenses datatable lsit.
* @param {number} tenantId
* @param {IExpensesFilter} expensesFilter
* @param {number} tenantId
* @param {IExpensesFilter} expensesFilter
* @return {IExpense[]}
*/
public async getExpensesList(
tenantId: number,
expensesFilter: IExpensesFilter
): Promise<{ expenses: IExpense[], pagination: IPaginationMeta, filterMeta: IFilterMeta }> {
): Promise<{
expenses: IExpense[];
pagination: IPaginationMeta;
filterMeta: IFilterMeta;
}> {
const { Expense } = this.tenancy.models(tenantId);
const dynamicFilter = await this.dynamicListService.dynamicList(tenantId, Expense, expensesFilter);
const dynamicFilter = await this.dynamicListService.dynamicList(
tenantId,
Expense,
expensesFilter
);
this.logger.info('[expense] trying to get expenses datatable list.', { tenantId, expensesFilter });
const { results, pagination } = await Expense.query().onBuild((builder) => {
builder.withGraphFetched('paymentAccount');
builder.withGraphFetched('categories.expenseAccount');
dynamicFilter.buildQuery()(builder);
}).pagination(expensesFilter.page - 1, expensesFilter.pageSize);
this.logger.info('[expense] trying to get expenses datatable list.', {
tenantId,
expensesFilter,
});
const { results, pagination } = await Expense.query()
.onBuild((builder) => {
builder.withGraphFetched('paymentAccount');
builder.withGraphFetched('categories.expenseAccount');
dynamicFilter.buildQuery()(builder);
})
.pagination(expensesFilter.page - 1, expensesFilter.pageSize);
return {
expenses: results,
pagination, filterMeta:
dynamicFilter.getResponseMeta(),
pagination,
filterMeta: dynamicFilter.getResponseMeta(),
};
}
/**
* Retrieve expense details.
* @param {number} tenantId
* @param {number} expenseId
* @param {number} tenantId
* @param {number} expenseId
* @return {Promise<IExpense>}
*/
public async getExpense(tenantId: number, expenseId: number): Promise<IExpense> {
public async getExpense(
tenantId: number,
expenseId: number
): Promise<IExpense> {
const { expenseRepository } = this.tenancy.repositories(tenantId);
const expense = await expenseRepository.findOneById(expenseId, [
@@ -537,4 +775,4 @@ export default class ExpensesService implements IExpensesService {
}
return expense;
}
}
}

View File

@@ -400,7 +400,11 @@ export default class ItemsService implements IItemsService {
const { Item } = this.tenancy.models(tenantId);
this.logger.info('[items] trying to delete item.', { tenantId, itemId });
// Retreive the given item or throw not found service error.
await this.getItemOrThrowError(tenantId, itemId);
// Validate the item has no associated invoices or bills.
await this.validateHasNoInvoicesOrBills(tenantId, itemId);
await Item.query().findById(itemId).delete();