feat: bulk transcations delete

This commit is contained in:
Ahmed Bouhuolia
2025-11-03 21:40:24 +02:00
parent 8161439365
commit a0bc9db9a6
107 changed files with 2213 additions and 156 deletions

View File

@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { Knex } from 'knex';
import { PromisePool } from '@supercharge/promise-pool';
import { castArray, uniq } from 'lodash';
import { DeleteManualJournalService } from './commands/DeleteManualJournal.service';
@Injectable()
export class BulkDeleteManualJournalsService {
constructor(
private readonly deleteManualJournalService: DeleteManualJournalService,
) {}
async bulkDeleteManualJournals(
manualJournalIds: number | Array<number>,
trx?: Knex.Transaction,
): Promise<void> {
const journalsIds = uniq(castArray(manualJournalIds));
const results = await PromisePool.withConcurrency(1)
.for(journalsIds)
.process(async (manualJournalId: number) => {
await this.deleteManualJournalService.deleteManualJournal(
manualJournalId,
);
});
if (results.errors && results.errors.length > 0) {
throw results.errors[0].raw;
}
}
}

View File

@@ -0,0 +1,55 @@
import { Injectable, Inject } from '@nestjs/common';
import { Knex } from 'knex';
import { TENANCY_DB_CONNECTION } from '../Tenancy/TenancyDB/TenancyDB.constants';
import { DeleteManualJournalService } from './commands/DeleteManualJournal.service';
@Injectable()
export class ValidateBulkDeleteManualJournalsService {
constructor(
private readonly deleteManualJournalService: DeleteManualJournalService,
@Inject(TENANCY_DB_CONNECTION)
private readonly tenantKnex: () => Knex,
) {}
public async validateBulkDeleteManualJournals(
manualJournalIds: number[],
): Promise<{
deletableCount: number;
nonDeletableCount: number;
deletableIds: number[];
nonDeletableIds: number[];
}> {
const trx = await this.tenantKnex().transaction({
isolationLevel: 'read uncommitted',
});
try {
const deletableIds: number[] = [];
const nonDeletableIds: number[] = [];
for (const manualJournalId of manualJournalIds) {
try {
await this.deleteManualJournalService.deleteManualJournal(
manualJournalId,
);
deletableIds.push(manualJournalId);
} catch (error) {
nonDeletableIds.push(manualJournalId);
}
}
await trx.rollback();
return {
deletableCount: deletableIds.length,
nonDeletableCount: nonDeletableIds.length,
deletableIds,
nonDeletableIds,
};
} catch (error) {
await trx.rollback();
throw error;
}
}
}