mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { Vendor } from '../models/Vendor';
|
||||
import { events } from '@/common/events/events';
|
||||
import {
|
||||
IVendorEventDeletedPayload,
|
||||
IVendorEventDeletingPayload,
|
||||
} from '../types/Vendors.types';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteVendorService {
|
||||
/**
|
||||
* @param {EventEmitter2} eventPublisher - Event emitter service.
|
||||
* @param {UnitOfWork} uow - Unit of work service.
|
||||
* @param {typeof Vendor} contactModel - Vendor model.
|
||||
*/
|
||||
constructor(
|
||||
private eventPublisher: EventEmitter2,
|
||||
private uow: UnitOfWork,
|
||||
@Inject(Vendor.name) private vendorModel: TenantModelProxy<typeof Vendor>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Deletes the given vendor.
|
||||
* @param {number} vendorId
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async deleteVendor(vendorId: number) {
|
||||
// Retrieves the old vendor or throw not found service error.
|
||||
const oldVendor = await this.vendorModel()
|
||||
.query()
|
||||
.findById(vendorId)
|
||||
.throwIfNotFound();
|
||||
// .queryAndThrowIfHasRelations({
|
||||
// type: ERRORS.VENDOR_HAS_TRANSACTIONS,
|
||||
// });
|
||||
|
||||
// Triggers `onVendorDeleting` event.
|
||||
await this.eventPublisher.emitAsync(events.vendors.onDeleting, {
|
||||
vendorId,
|
||||
oldVendor,
|
||||
} as IVendorEventDeletingPayload);
|
||||
|
||||
// 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();
|
||||
|
||||
// Triggers `onVendorDeleted` event.
|
||||
await this.eventPublisher.emitAsync(events.vendors.onDeleted, {
|
||||
vendorId,
|
||||
oldVendor,
|
||||
trx,
|
||||
} as IVendorEventDeletedPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user