78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import {
|
|
HttpStatus,
|
|
Injectable,
|
|
UnprocessableEntityException,
|
|
} from '@nestjs/common';
|
|
import { BaseUpdateStatusManager } from 'src/core/modules/domain/usecase/managers/base-update-status.manager';
|
|
import { RefundEntity } from '../../entities/refund.entity';
|
|
import {
|
|
EventTopics,
|
|
validateRelations,
|
|
} from 'src/core/strings/constants/interface.constants';
|
|
import { RefundModel } from '../../../data/models/refund.model';
|
|
import { RefundChangeStatusEvent } from '../../entities/event/refund-change-status.event';
|
|
import { STATUS } from 'src/core/strings/constants/base.constants';
|
|
|
|
@Injectable()
|
|
export class ConfirmRefundManager extends BaseUpdateStatusManager<RefundEntity> {
|
|
getResult(): string {
|
|
return `Success active data ${this.result.code}`;
|
|
}
|
|
|
|
async validateProcess(): Promise<void> {
|
|
if (![STATUS.DRAFT, STATUS.PENDING].includes(this.oldData.status)) {
|
|
throw new UnprocessableEntityException({
|
|
statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
|
|
message: `Failed! only data with status ${STATUS.DRAFT} and ${STATUS.PENDING} can be confirmed`,
|
|
error: 'Unprocessable Entity',
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
async beforeProcess(): Promise<void> {
|
|
const data = await this.dataService.getOneByOptions({
|
|
where: {
|
|
id: this.data.id,
|
|
},
|
|
relations: ['transaction'],
|
|
});
|
|
|
|
if (data.status == STATUS.DRAFT) {
|
|
Object.assign(this.data, {
|
|
code: `RF-${data.transaction?.invoice_code?.split('-')[1]}`,
|
|
request_date: new Date(),
|
|
status: STATUS.PENDING,
|
|
});
|
|
} else if (data.status == STATUS.PENDING) {
|
|
Object.assign(this.data, {
|
|
refund_date: new Date(),
|
|
status: STATUS.REFUNDED,
|
|
});
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
async afterProcess(): Promise<void> {
|
|
return;
|
|
}
|
|
|
|
get validateRelations(): validateRelations[] {
|
|
return [];
|
|
}
|
|
|
|
get entityTarget(): any {
|
|
return RefundModel;
|
|
}
|
|
|
|
get eventTopics(): EventTopics[] {
|
|
return [
|
|
{
|
|
topic: RefundChangeStatusEvent,
|
|
relations: ['transaction'],
|
|
},
|
|
];
|
|
}
|
|
}
|