diff --git a/server/src/api/controllers/Contacts/Vendors.ts b/server/src/api/controllers/Contacts/Vendors.ts index 685638c74..f14b4fa03 100644 --- a/server/src/api/controllers/Contacts/Vendors.ts +++ b/server/src/api/controllers/Contacts/Vendors.ts @@ -237,7 +237,11 @@ export default class VendorsController extends ContactsController { try { await this.vendorsService.deleteBulkVendors(tenantId, contactsIds) - return res.status(200).send({ ids: contactsIds }); + + return res.status(200).send({ + ids: contactsIds, + message: 'The vendors have been deleted successfully.', + }); } catch (error) { next(error); } @@ -294,12 +298,12 @@ export default class VendorsController extends ContactsController { } if (error.errorType === 'some_vendors_have_bills') { return res.boom.badRequest(null, { - errors: [{ type: 'SOME.VENDORS.HAVE.BILLS', code: 300 }], + errors: [{ type: 'SOME.VENDORS.HAVE.ASSOCIATED.BILLS', code: 300 }], }); } if (error.errorType === 'vendor_has_bills') { return res.status(400).send({ - errors: [{ type: 'VENDOR.HAS.BILLS', code: 400 }], + errors: [{ type: 'VENDOR.HAS.ASSOCIATED.BILLS', code: 400 }], }); } if (error.errorType === 'OPENING_BALANCE_DATE_REQUIRED') { diff --git a/server/src/services/Contacts/VendorsService.ts b/server/src/services/Contacts/VendorsService.ts index 92f2c9dda..2d024ea29 100644 --- a/server/src/services/Contacts/VendorsService.ts +++ b/server/src/services/Contacts/VendorsService.ts @@ -1,5 +1,5 @@ import { Inject, Service } from 'typedi'; -import { difference, defaultTo } from 'lodash'; +import { intersection, defaultTo } from 'lodash'; import { EventDispatcher, EventDispatcherInterface, @@ -220,10 +220,14 @@ export default class VendorsService { ): Promise { const { Contact } = this.tenancy.models(tenantId); + // Validate the given vendors exists on the storage. await this.getVendorsOrThrowErrorNotFound(tenantId, vendorsIds); + + // Validate the given vendors have no assocaited bills. await this.vendorsHaveNoBillsOrThrowError(tenantId, vendorsIds); await Contact.query().whereIn('id', vendorsIds).delete(); + await this.eventDispatcher.dispatch(events.vendors.onBulkDeleted, { tenantId, vendorsIds, @@ -262,7 +266,7 @@ export default class VendorsService { */ private async vendorsHaveNoBillsOrThrowError( tenantId: number, - vendorsIds: number[] + vendorsIds: number[], ) { const { billRepository } = this.tenancy.repositories(tenantId); @@ -273,8 +277,8 @@ export default class VendorsService { ); const billsVendorsIds = vendorsBills.map((bill) => bill.vendorId); - // The difference between the vendors ids and bills vendors ids. - const vendorsHaveInvoices = difference(vendorsIds, billsVendorsIds); + // The intersection between vendors and vendors that have bills. + const vendorsHaveInvoices = intersection(vendorsIds, billsVendorsIds); if (vendorsHaveInvoices.length > 0) { throw new ServiceError('some_vendors_have_bills'); diff --git a/server/src/services/Items/ItemsService.ts b/server/src/services/Items/ItemsService.ts index 8d2f900f5..b922b67aa 100644 --- a/server/src/services/Items/ItemsService.ts +++ b/server/src/services/Items/ItemsService.ts @@ -483,10 +483,14 @@ export default class ItemsService implements IItemsService { tenantId, itemsIds, }); + /// Validates the given items exist on the storage. await this.validateItemsIdsExists(tenantId, itemsIds); + + // Validate the items have no associated invoices or bills. await this.validateHasNoInvoicesOrBills(tenantId, itemsIds); await Item.query().whereIn('id', itemsIds).delete(); + this.logger.info('[items] deleted successfully in bulk.', { tenantId, itemsIds,