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

@@ -182,14 +182,14 @@ export default class ContactsService {
async getContactsOrThrowErrorNotFound(
tenantId: number,
contactsIds: number[],
contactService: TContactService
contactService: TContactService,
) {
const { Contact } = this.tenancy.models(tenantId);
const contacts = await Contact.query()
.whereIn('id', contactsIds)
.where('contact_service', contactService);
const storedContactsIds = contacts.map((contact: IContact) => contact.id);
const storedContactsIds = contacts.map((contact: IContact) => contact.id);
const notFoundCustomers = difference(contactsIds, storedContactsIds);
if (notFoundCustomers.length > 0) {
@@ -211,6 +211,8 @@ export default class ContactsService {
contactService: TContactService
) {
const { contactRepository } = this.tenancy.repositories(tenantId);
// Retrieve the given contacts or throw not found service error.
this.getContactsOrThrowErrorNotFound(tenantId, contactsIds, contactService);
await contactRepository.deleteWhereIdIn(contactsIds);
@@ -230,11 +232,12 @@ export default class ContactsService {
const { AccountTransaction } = this.tenancy.models(tenantId);
const journal = new JournalPoster(tenantId);
// Loads the contact opening balance journal transactions.
const contactsTransactions = await AccountTransaction.query()
.whereIn('reference_id', contactsIds)
.where('reference_type', `${upperFirst(contactService)}OpeningBalance`);
journal.loadEntries(contactsTransactions);
journal.fromTransactions(contactsTransactions);
journal.removeEntries();
await Promise.all([journal.saveBalance(), journal.deleteEntries()]);