This commit is contained in:
Ahmed Bouhuolia
2025-11-19 22:59:21 +02:00
parent 17bcc14231
commit 2b384b2f6f
31 changed files with 405 additions and 62 deletions

View File

@@ -10,17 +10,25 @@ export class BulkDeleteSaleEstimatesService {
async bulkDeleteSaleEstimates(
saleEstimateIds: number | Array<number>,
options?: { skipUndeletable?: boolean },
trx?: Knex.Transaction,
): Promise<void> {
const { skipUndeletable = false } = options ?? {};
const estimatesIds = uniq(castArray(saleEstimateIds));
const results = await PromisePool.withConcurrency(1)
.for(estimatesIds)
.process(async (saleEstimateId: number) => {
await this.deleteSaleEstimateService.deleteEstimate(saleEstimateId);
try {
await this.deleteSaleEstimateService.deleteEstimate(saleEstimateId);
} catch (error) {
if (!skipUndeletable) {
throw error;
}
}
});
if (results.errors && results.errors.length > 0) {
if (!skipUndeletable && results.errors && results.errors.length > 0) {
throw results.errors[0].raw;
}
}

View File

@@ -39,7 +39,7 @@ export class SaleEstimatesApplication {
private readonly getSaleEstimateMailStateService: GetSaleEstimateMailStateService,
private readonly bulkDeleteSaleEstimatesService: BulkDeleteSaleEstimatesService,
private readonly validateBulkDeleteSaleEstimatesService: ValidateBulkDeleteSaleEstimatesService,
) {}
) { }
/**
* Create a sale estimate.
@@ -77,9 +77,13 @@ export class SaleEstimatesApplication {
* @param {number[]} saleEstimateIds
* @return {Promise<void>}
*/
public bulkDeleteSaleEstimates(saleEstimateIds: number[]) {
public bulkDeleteSaleEstimates(
saleEstimateIds: number[],
options?: { skipUndeletable?: boolean },
) {
return this.bulkDeleteSaleEstimatesService.bulkDeleteSaleEstimates(
saleEstimateIds,
options,
);
}

View File

@@ -2,6 +2,7 @@ import {
ApiExtraModels,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
getSchemaPath,
@@ -11,9 +12,11 @@ import {
Controller,
Delete,
Get,
DefaultValuePipe,
Headers,
HttpCode,
Param,
ParseBoolPipe,
ParseIntPipe,
Post,
Put,
@@ -72,15 +75,25 @@ export class SaleEstimatesController {
@Post('bulk-delete')
@ApiOperation({ summary: 'Deletes multiple sale estimates.' })
@ApiQuery({
name: 'skip_undeletable',
required: false,
type: Boolean,
description:
'When true, undeletable estimates will be skipped and only deletable ones will be removed.',
})
@ApiResponse({
status: 200,
description: 'Sale estimates deleted successfully',
})
public bulkDeleteSaleEstimates(
@Body() bulkDeleteDto: BulkDeleteDto,
@Query('skip_undeletable', new DefaultValuePipe(false), ParseBoolPipe)
skipUndeletable: boolean,
): Promise<void> {
return this.saleEstimatesApplication.bulkDeleteSaleEstimates(
bulkDeleteDto.ids,
{ skipUndeletable },
);
}