fix: save payee contact to expense transaction.

This commit is contained in:
a.bouhuolia
2020-12-16 12:23:07 +02:00
parent 76488008c1
commit ddf31feb72
6 changed files with 45 additions and 14 deletions

View File

@@ -31,13 +31,17 @@ export default class ContactsService {
* @param {TContactService} contactService
* @return {Promise<IContact>}
*/
public async getContactByIdOrThrowError(tenantId: number, contactId: number, contactService: TContactService) {
public async getContactByIdOrThrowError(
tenantId: number,
contactId: number,
contactService?: TContactService
) {
const { contactRepository } = this.tenancy.repositories(tenantId);
this.logger.info('[contact] trying to validate contact existance.', { tenantId, contactId });
const contact = await contactRepository.findOne({
id: contactId,
contactService: contactService,
...(contactService) && ({ contactService }),
});
if (!contact) {

View File

@@ -12,6 +12,7 @@ import JournalCommands from 'services/Accounting/JournalCommands';
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";
const ERRORS = {
EXPENSE_NOT_FOUND: 'expense_not_found',
@@ -38,6 +39,9 @@ export default class ExpensesService implements IExpensesService {
@EventDispatcher()
eventDispatcher: EventDispatcherInterface;
@Inject()
contactsService: ContactsService;
/**
* Retrieve the payment account details or returns not found server error in case the
* given account not found on the storage.
@@ -323,6 +327,13 @@ export default class ExpensesService implements IExpensesService {
// - 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);
@@ -346,8 +357,9 @@ export default class ExpensesService implements IExpensesService {
* 2. Validate expense accounts exist on the storage.
* 3. Validate payment account type.
* 4. Validate expenses accounts type.
* 5. Validate the given expense categories not equal zero.
* 6. Stores the expense to the storage.
* 5. Validate the expense payee contact id existance on storage.
* 6. Validate the given expense categories not equal zero.
* 7. Stores the expense to the storage.
* ---------
* @param {number} tenantId
* @param {IExpenseDTO} expenseDTO
@@ -359,26 +371,33 @@ export default class ExpensesService implements IExpensesService {
): Promise<IExpense> {
const { expenseRepository } = this.tenancy.repositories(tenantId);
// 1. Validate payment account existance on the storage.
// - Validate payment account existance on the storage.
const paymentAccount = await this.getPaymentAccountOrThrowError(
tenantId,
expenseDTO.paymentAccountId,
);
// 2. Validate expense accounts exist on the storage.
// - Validate expense accounts exist on the storage.
const expensesAccounts = await this.getExpensesAccountsOrThrowError(
tenantId,
this.mapExpensesAccountsIdsFromDTO(expenseDTO),
);
// 3. Validate payment account type.
// - Validate payment account type.
await this.validatePaymentAccountType(tenantId, paymentAccount);
// 4. Validate expenses accounts type.
// - Validate expenses accounts type.
await this.validateExpensesAccountsType(tenantId, expensesAccounts);
// 5. Validate the given expense categories not equal zero.
// - 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);
// 6. Save the expense to the storage.
// - Save the expense to the storage.
const expenseObj = this.expenseDTOToModel(expenseDTO, authorizedUser);
const expenseModel = await expenseRepository.upsertGraph(expenseObj);