feat: deleteIfNoRelations

This commit is contained in:
Ahmed Bouhuolia
2025-06-28 22:35:29 +02:00
parent 0ca98c7ae4
commit fa5c3bd955
11 changed files with 229 additions and 143 deletions

View File

@@ -9,6 +9,7 @@ import {
IVendorEventDeletingPayload,
} from '../types/Vendors.types';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { ERRORS } from '../constants';
@Injectable()
export class DeleteVendorService {
@@ -34,9 +35,6 @@ export class DeleteVendorService {
.query()
.findById(vendorId)
.throwIfNotFound();
// .queryAndThrowIfHasRelations({
// type: ERRORS.VENDOR_HAS_TRANSACTIONS,
// });
// Triggers `onVendorDeleting` event.
await this.eventPublisher.emitAsync(events.vendors.onDeleting, {
@@ -47,8 +45,13 @@ export class DeleteVendorService {
// Deletes vendor contact under unit-of-work.
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Deletes the vendor contact from the storage.
await this.vendorModel().query(trx).findById(vendorId).delete();
await this.vendorModel()
.query(trx)
.findById(vendorId)
.deleteIfNoRelations({
type: ERRORS.VENDOR_HAS_TRANSACTIONS,
message: 'Vendor has associated transactions',
});
// Triggers `onVendorDeleted` event.
await this.eventPublisher.emitAsync(events.vendors.onDeleted, {
vendorId,