mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
add server to monorepo.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
IManualJournal,
|
||||
IManualJournalEventDeletedPayload,
|
||||
IManualJournalDeletingPayload,
|
||||
} from '@/interfaces';
|
||||
import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
import events from '@/subscribers/events';
|
||||
import UnitOfWork from '@/services/UnitOfWork';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
|
||||
@Service()
|
||||
export class DeleteManualJournal {
|
||||
@Inject()
|
||||
private tenancy: TenancyService;
|
||||
|
||||
@Inject()
|
||||
private eventPublisher: EventPublisher;
|
||||
|
||||
@Inject()
|
||||
private uow: UnitOfWork;
|
||||
|
||||
/**
|
||||
* Deletes the given manual journal
|
||||
* @param {number} tenantId
|
||||
* @param {number} manualJournalId
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public deleteManualJournal = async (
|
||||
tenantId: number,
|
||||
manualJournalId: number
|
||||
): Promise<{
|
||||
oldManualJournal: IManualJournal;
|
||||
}> => {
|
||||
const { ManualJournal, ManualJournalEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
// Validate the manual journal exists on the storage.
|
||||
const oldManualJournal = await ManualJournal.query()
|
||||
.findById(manualJournalId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Deletes the manual journal with associated transactions under unit-of-work envirement.
|
||||
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
|
||||
// Triggers `onManualJournalDeleting` event.
|
||||
await this.eventPublisher.emitAsync(events.manualJournals.onDeleting, {
|
||||
tenantId,
|
||||
oldManualJournal,
|
||||
trx,
|
||||
} as IManualJournalDeletingPayload);
|
||||
|
||||
// Deletes the manual journal entries.
|
||||
await ManualJournalEntry.query(trx)
|
||||
.where('manualJournalId', manualJournalId)
|
||||
.delete();
|
||||
|
||||
// Deletes the manual journal transaction.
|
||||
await ManualJournal.query(trx).findById(manualJournalId).delete();
|
||||
|
||||
// Triggers `onManualJournalDeleted` event.
|
||||
await this.eventPublisher.emitAsync(events.manualJournals.onDeleted, {
|
||||
tenantId,
|
||||
manualJournalId,
|
||||
oldManualJournal,
|
||||
trx,
|
||||
} as IManualJournalEventDeletedPayload);
|
||||
|
||||
return { oldManualJournal };
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user