mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: bulk transcations delete
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
import { castArray, uniq } from 'lodash';
|
||||
import { DeleteExpense } from './commands/DeleteExpense.service';
|
||||
|
||||
@Injectable()
|
||||
export class BulkDeleteExpensesService {
|
||||
constructor(private readonly deleteExpenseService: DeleteExpense) {}
|
||||
|
||||
async bulkDeleteExpenses(
|
||||
expenseIds: number | Array<number>,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const expensesIds = uniq(castArray(expenseIds));
|
||||
|
||||
const results = await PromisePool.withConcurrency(1)
|
||||
.for(expensesIds)
|
||||
.process(async (expenseId: number) => {
|
||||
await this.deleteExpenseService.deleteExpense(expenseId);
|
||||
});
|
||||
|
||||
if (results.errors && results.errors.length > 0) {
|
||||
throw results.errors[0].raw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Injectable, Inject } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { TENANCY_DB_CONNECTION } from '../Tenancy/TenancyDB/TenancyDB.constants';
|
||||
import { DeleteExpense } from './commands/DeleteExpense.service';
|
||||
|
||||
@Injectable()
|
||||
export class ValidateBulkDeleteExpensesService {
|
||||
constructor(
|
||||
private readonly deleteExpenseService: DeleteExpense,
|
||||
@Inject(TENANCY_DB_CONNECTION)
|
||||
private readonly tenantKnex: () => Knex,
|
||||
) { }
|
||||
|
||||
public async validateBulkDeleteExpenses(expenseIds: 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 expenseId of expenseIds) {
|
||||
try {
|
||||
await this.deleteExpenseService.deleteExpense(expenseId);
|
||||
deletableIds.push(expenseId);
|
||||
} catch (error) {
|
||||
nonDeletableIds.push(expenseId);
|
||||
}
|
||||
}
|
||||
|
||||
await trx.rollback();
|
||||
|
||||
return {
|
||||
deletableCount: deletableIds.length,
|
||||
nonDeletableCount: nonDeletableIds.length,
|
||||
deletableIds,
|
||||
nonDeletableIds,
|
||||
};
|
||||
} catch (error) {
|
||||
await trx.rollback();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user