This commit is contained in:
Ahmed Bouhuolia
2025-11-17 17:04:25 +02:00
parent 2383091b6e
commit 2c64e1b8ab
41 changed files with 709 additions and 87 deletions

View File

@@ -0,0 +1,54 @@
import { Inject, Injectable } from '@nestjs/common';
import { Knex } from 'knex';
import { TENANCY_DB_CONNECTION } from '../Tenancy/TenancyDB/TenancyDB.constants';
import { DeleteSaleReceipt } from './commands/DeleteSaleReceipt.service';
@Injectable()
export class ValidateBulkDeleteSaleReceiptsService {
constructor(
private readonly deleteSaleReceiptService: DeleteSaleReceipt,
@Inject(TENANCY_DB_CONNECTION)
private readonly tenantKnex: () => Knex,
) {}
public async validateBulkDeleteSaleReceipts(
saleReceiptIds: 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 saleReceiptId of saleReceiptIds) {
try {
await this.deleteSaleReceiptService.deleteSaleReceipt(saleReceiptId);
deletableIds.push(saleReceiptId);
} catch (error) {
nonDeletableIds.push(saleReceiptId);
}
}
await trx.rollback();
return {
deletableCount: deletableIds.length,
nonDeletableCount: nonDeletableIds.length,
deletableIds,
nonDeletableIds,
};
} catch (error) {
await trx.rollback();
throw error;
}
}
}