fix: delete customer with associated opening journal entries.

This commit is contained in:
a.bouhuolia
2021-01-02 13:17:43 +02:00
parent b5a849abda
commit db70d04743
6 changed files with 70 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
import { Inject, Service } from 'typedi';
import { omit, difference, defaultTo } from 'lodash';
import { omit, intersection, defaultTo } from 'lodash';
import {
EventDispatcher,
EventDispatcherInterface,
@@ -71,6 +71,10 @@ export default class CustomersService {
};
}
/**
* Transforms the contact model to customer model.
* @param {IContact} contactModel
*/
private transformContactToCustomer(contactModel: IContact) {
return {
...omit(contactModel.toJSON(), ['contactService', 'contactType']),
@@ -172,6 +176,7 @@ export default class CustomersService {
await this.contactService.deleteContact(tenantId, customerId, 'customer');
// Throws `onCustomerDeleted` event.
await this.eventDispatcher.dispatch(events.customers.onDeleted, {
tenantId,
customerId,
@@ -182,29 +187,6 @@ export default class CustomersService {
});
}
/**
* Reverts customer opening balance journal entries.
* @param {number} tenantId -
* @param {number} customerId -
* @return {Promise<void>}
*/
public async revertOpeningBalanceEntries(
tenantId: number,
customerId: number | number[]
) {
const id = Array.isArray(customerId) ? customerId : [customerId];
this.logger.info(
'[customer] trying to revert opening balance journal entries.',
{ tenantId, customerId }
);
await this.contactService.revertJEntriesContactsOpeningBalance(
tenantId,
id,
'customer'
);
}
/**
* Retrieve the given customer details.
* @param {number} tenantId
@@ -262,16 +244,43 @@ export default class CustomersService {
public async writeCustomerOpeningBalanceJournal(
tenantId: number,
customerId: number,
openingBalance: number
openingBalance: number,
openingBalanceAt: Date | string
) {
const journal = new JournalPoster(tenantId);
const journalCommands = new JournalCommands(journal);
await journalCommands.customerOpeningBalance(customerId, openingBalance);
await journalCommands.customerOpeningBalance(
customerId,
openingBalance,
openingBalanceAt
);
await Promise.all([journal.saveBalance(), journal.saveEntries()]);
}
/**
* Reverts customer opening balance journal entries.
* @param {number} tenantId -
* @param {number} customerId -
* @return {Promise<void>}
*/
public async revertOpeningBalanceEntries(
tenantId: number,
customerId: number | number[]
) {
const id = Array.isArray(customerId) ? customerId : [customerId];
this.logger.info(
'[customer] trying to revert opening balance journal entries.',
{ tenantId, customerId }
);
await this.contactService.revertJEntriesContactsOpeningBalance(
tenantId,
id,
'customer'
);
}
/**
* Retrieve the given customer by id or throw not found.
* @param {number} tenantId
@@ -310,11 +319,20 @@ export default class CustomersService {
public async deleteBulkCustomers(tenantId: number, customersIds: number[]) {
const { Contact } = this.tenancy.models(tenantId);
// Validate the customers existance on the storage.
await this.getCustomersOrThrowErrorNotFound(tenantId, customersIds);
// Validate the customers have no associated invoices.
await this.customersHaveNoInvoicesOrThrowError(tenantId, customersIds);
// Deletes the given customers.
await Contact.query().whereIn('id', customersIds).delete();
await this.eventDispatcher.dispatch(events.customers.onBulkDeleted);
// Triggers `onCustomersBulkDeleted` event.
await this.eventDispatcher.dispatch(events.customers.onBulkDeleted, {
tenantId,
customersIds,
});
}
/**
@@ -361,12 +379,10 @@ export default class CustomersService {
const customersIdsWithInvoice = customersInvoices.map(
(saleInvoice: ISaleInvoice) => saleInvoice.customerId
);
const customersHaveInvoices = difference(
const customersHaveInvoices = intersection(
customersIds,
customersIdsWithInvoice
);
if (customersHaveInvoices.length > 0) {
throw new ServiceError('some_customers_have_invoices');
}