mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
- feat: Optimize tenancy software architecture.
This commit is contained in:
38
server/src/services/Sales/JournalPosterService.ts
Normal file
38
server/src/services/Sales/JournalPosterService.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
|
||||
@Service()
|
||||
export default class JournalPosterService {
|
||||
@Inject()
|
||||
tenancy: TenancyService;
|
||||
|
||||
/**
|
||||
* Deletes the journal transactions that associated to the given reference id.
|
||||
* @param {number} tenantId - The given tenant id.
|
||||
* @param {number} referenceId - The transaction reference id.
|
||||
* @param {string} referenceType - The transaction reference type.
|
||||
* @return {Promise}
|
||||
*/
|
||||
async deleteJournalTransactions(
|
||||
tenantId: number,
|
||||
referenceId: number,
|
||||
referenceType: string
|
||||
) {
|
||||
const { Account, AccountTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
const transactions = await AccountTransaction.tenant()
|
||||
.query()
|
||||
.whereIn('reference_type', [referenceType])
|
||||
.where('reference_id', referenceId)
|
||||
.withGraphFetched('account.type');
|
||||
|
||||
const accountsDepGraph = await Account.tenant().depGraph().query();
|
||||
const journal = new JournalPoster(accountsDepGraph);
|
||||
|
||||
journal.loadEntries(transactions);
|
||||
journal.removeEntries();
|
||||
|
||||
await Promise.all([journal.deleteEntries(), journal.saveBalance()]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user