This commit is contained in:
elforjani3
2021-01-02 14:53:18 +02:00
3 changed files with 19 additions and 7 deletions

View File

@@ -237,7 +237,11 @@ export default class VendorsController extends ContactsController {
try { try {
await this.vendorsService.deleteBulkVendors(tenantId, contactsIds) 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) { } catch (error) {
next(error); next(error);
} }
@@ -294,12 +298,12 @@ export default class VendorsController extends ContactsController {
} }
if (error.errorType === 'some_vendors_have_bills') { if (error.errorType === 'some_vendors_have_bills') {
return res.boom.badRequest(null, { 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') { if (error.errorType === 'vendor_has_bills') {
return res.status(400).send({ 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') { if (error.errorType === 'OPENING_BALANCE_DATE_REQUIRED') {

View File

@@ -1,5 +1,5 @@
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import { difference, defaultTo } from 'lodash'; import { intersection, defaultTo } from 'lodash';
import { import {
EventDispatcher, EventDispatcher,
EventDispatcherInterface, EventDispatcherInterface,
@@ -220,10 +220,14 @@ export default class VendorsService {
): Promise<void> { ): Promise<void> {
const { Contact } = this.tenancy.models(tenantId); const { Contact } = this.tenancy.models(tenantId);
// Validate the given vendors exists on the storage.
await this.getVendorsOrThrowErrorNotFound(tenantId, vendorsIds); await this.getVendorsOrThrowErrorNotFound(tenantId, vendorsIds);
// Validate the given vendors have no assocaited bills.
await this.vendorsHaveNoBillsOrThrowError(tenantId, vendorsIds); await this.vendorsHaveNoBillsOrThrowError(tenantId, vendorsIds);
await Contact.query().whereIn('id', vendorsIds).delete(); await Contact.query().whereIn('id', vendorsIds).delete();
await this.eventDispatcher.dispatch(events.vendors.onBulkDeleted, { await this.eventDispatcher.dispatch(events.vendors.onBulkDeleted, {
tenantId, tenantId,
vendorsIds, vendorsIds,
@@ -262,7 +266,7 @@ export default class VendorsService {
*/ */
private async vendorsHaveNoBillsOrThrowError( private async vendorsHaveNoBillsOrThrowError(
tenantId: number, tenantId: number,
vendorsIds: number[] vendorsIds: number[],
) { ) {
const { billRepository } = this.tenancy.repositories(tenantId); const { billRepository } = this.tenancy.repositories(tenantId);
@@ -273,8 +277,8 @@ export default class VendorsService {
); );
const billsVendorsIds = vendorsBills.map((bill) => bill.vendorId); const billsVendorsIds = vendorsBills.map((bill) => bill.vendorId);
// The difference between the vendors ids and bills vendors ids. // The intersection between vendors and vendors that have bills.
const vendorsHaveInvoices = difference(vendorsIds, billsVendorsIds); const vendorsHaveInvoices = intersection(vendorsIds, billsVendorsIds);
if (vendorsHaveInvoices.length > 0) { if (vendorsHaveInvoices.length > 0) {
throw new ServiceError('some_vendors_have_bills'); throw new ServiceError('some_vendors_have_bills');

View File

@@ -483,10 +483,14 @@ export default class ItemsService implements IItemsService {
tenantId, tenantId,
itemsIds, itemsIds,
}); });
/// Validates the given items exist on the storage.
await this.validateItemsIdsExists(tenantId, itemsIds); await this.validateItemsIdsExists(tenantId, itemsIds);
// Validate the items have no associated invoices or bills.
await this.validateHasNoInvoicesOrBills(tenantId, itemsIds); await this.validateHasNoInvoicesOrBills(tenantId, itemsIds);
await Item.query().whereIn('id', itemsIds).delete(); await Item.query().whereIn('id', itemsIds).delete();
this.logger.info('[items] deleted successfully in bulk.', { this.logger.info('[items] deleted successfully in bulk.', {
tenantId, tenantId,
itemsIds, itemsIds,