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 {
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') {

View File

@@ -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<void> {
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');

View File

@@ -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,