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

@@ -8,23 +8,31 @@ import { DeletePaymentReceivedService } from './commands/DeletePaymentReceived.s
export class BulkDeletePaymentReceivedService {
constructor(
private readonly deletePaymentReceivedService: DeletePaymentReceivedService,
) {}
) { }
async bulkDeletePaymentReceived(
paymentReceiveIds: number | Array<number>,
options?: { skipUndeletable?: boolean },
trx?: Knex.Transaction,
): Promise<void> {
const { skipUndeletable = false } = options ?? {};
const paymentsIds = uniq(castArray(paymentReceiveIds));
const results = await PromisePool.withConcurrency(1)
.for(paymentsIds)
.process(async (paymentReceiveId: number) => {
await this.deletePaymentReceivedService.deletePaymentReceive(
paymentReceiveId,
);
try {
await this.deletePaymentReceivedService.deletePaymentReceive(
paymentReceiveId,
);
} 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

@@ -81,9 +81,13 @@ export class PaymentReceivesApplication {
* Deletes multiple payment receives.
* @param {number[]} paymentReceiveIds
*/
public bulkDeletePaymentReceives(paymentReceiveIds: number[]) {
public bulkDeletePaymentReceives(
paymentReceiveIds: number[],
options?: { skipUndeletable?: boolean },
) {
return this.bulkDeletePaymentReceivedService.bulkDeletePaymentReceived(
paymentReceiveIds,
options,
);
}

View File

@@ -1,6 +1,7 @@
import {
ApiExtraModels,
ApiOperation,
ApiQuery,
ApiResponse,
ApiTags,
getSchemaPath,
@@ -13,10 +14,12 @@ import {
Headers,
HttpCode,
Param,
ParseBoolPipe,
ParseIntPipe,
Post,
Put,
Query,
DefaultValuePipe,
} from '@nestjs/common';
import { PaymentReceivesApplication } from './PaymentReceived.application';
import {
@@ -171,13 +174,25 @@ export class PaymentReceivesController {
@Post('bulk-delete')
@ApiOperation({ summary: 'Deletes multiple payments received.' })
@ApiQuery({
name: 'skip_undeletable',
required: false,
type: Boolean,
description:
'When true, undeletable payments will be skipped and only deletable ones will be removed.',
})
@ApiResponse({
status: 200,
description: 'Payments received deleted successfully.',
})
public bulkDeletePaymentsReceived(@Body() bulkDeleteDto: BulkDeleteDto) {
public bulkDeletePaymentsReceived(
@Body() bulkDeleteDto: BulkDeleteDto,
@Query('skip_undeletable', new DefaultValuePipe(false), ParseBoolPipe)
skipUndeletable: boolean,
) {
return this.paymentReceivesApplication.bulkDeletePaymentReceives(
bulkDeleteDto.ids,
{ skipUndeletable },
);
}