pos-be/src/modules/transaction/refund/domain/usecases/managers/create-refund.manager.ts

88 lines
2.3 KiB
TypeScript

import {
HttpStatus,
Injectable,
UnprocessableEntityException,
} from '@nestjs/common';
import {
EventTopics,
columnUniques,
validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { RefundEntity } from '../../entities/refund.entity';
import { RefundModel } from '../../../data/models/refund.model';
import { BaseCreateManager } from 'src/core/modules/domain/usecase/managers/base-create.manager';
import { RefundCreatedEvent } from '../../entities/event/refund-created.event';
import { STATUS } from 'src/core/strings/constants/base.constants';
@Injectable()
export class CreateRefundManager extends BaseCreateManager<RefundEntity> {
async beforeProcess(): Promise<void> {
const refund_items = this.data.refund_items?.map((item) => {
return {
transaction_item: item,
qty_refund: item.qty_refund,
refund_total: item.refund_total,
refund_sub_total: item.refund_sub_total,
};
});
Object.assign(this.data, {
refund_items: refund_items,
});
const exist = await this.dataService.getOneByOptions({
where: {
transaction_id: this.data.transaction.id,
},
});
if (exist) {
throw new UnprocessableEntityException({
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
message: `Failed! refund transaction with invoice ${this.data.transaction.invoice_code} already exist`,
error: 'Unprocessable Entity',
});
}
const transaction = await this.dataServiceFirstOpt.getOneByOptions({
where: {
id: this.data.transaction.id,
status: STATUS.SETTLED,
},
});
if (!transaction) {
throw new UnprocessableEntityException({
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
message: `Failed! only transaction with status ${STATUS.SETTLED} can be refund`,
error: 'Unprocessable Entity',
});
}
return;
}
async afterProcess(): Promise<void> {
return;
}
get validateRelations(): validateRelations[] {
return [];
}
get uniqueColumns(): columnUniques[] {
return [];
}
get eventTopics(): EventTopics[] {
return [
{
topic: RefundCreatedEvent,
data: this.data,
},
];
}
get entityTarget(): any {
return RefundModel;
}
}