2025-09-25 15:11:28 +09:00
|
|
|
import { Injectable, Inject, BadRequestException } from "@nestjs/common";
|
|
|
|
|
import { Logger } from "nestjs-pino";
|
2025-09-25 16:38:21 +09:00
|
|
|
import { FreebitOrchestratorService } from "@bff/integrations/freebit/services/freebit-orchestrator.service";
|
2025-09-25 15:11:28 +09:00
|
|
|
import { SimValidationService } from "./sim-validation.service";
|
|
|
|
|
import { getErrorMessage } from "@bff/core/utils/error.util";
|
2025-10-08 16:31:42 +09:00
|
|
|
import type { SimCancelRequest } from "@customer-portal/domain/sim";
|
2025-11-18 10:57:36 +09:00
|
|
|
import { SimScheduleService } from "./sim-schedule.service";
|
|
|
|
|
import { SimActionRunnerService } from "./sim-action-runner.service";
|
2025-09-25 15:11:28 +09:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SimCancellationService {
|
|
|
|
|
constructor(
|
2025-09-25 16:38:21 +09:00
|
|
|
private readonly freebitService: FreebitOrchestratorService,
|
2025-09-25 15:11:28 +09:00
|
|
|
private readonly simValidation: SimValidationService,
|
2025-11-18 10:57:36 +09:00
|
|
|
private readonly simSchedule: SimScheduleService,
|
|
|
|
|
private readonly simActionRunner: SimActionRunnerService,
|
2025-09-25 15:11:28 +09:00
|
|
|
@Inject(Logger) private readonly logger: Logger
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cancel SIM service
|
|
|
|
|
*/
|
|
|
|
|
async cancelSim(
|
|
|
|
|
userId: string,
|
|
|
|
|
subscriptionId: number,
|
|
|
|
|
request: SimCancelRequest = {}
|
|
|
|
|
): Promise<void> {
|
2025-11-18 10:57:36 +09:00
|
|
|
let account = "";
|
2025-09-25 15:11:28 +09:00
|
|
|
|
2025-11-18 10:57:36 +09:00
|
|
|
await this.simActionRunner.run(
|
|
|
|
|
"Cancel SIM",
|
|
|
|
|
{
|
|
|
|
|
baseContext: {
|
|
|
|
|
userId,
|
|
|
|
|
subscriptionId,
|
|
|
|
|
scheduledAt: request.scheduledAt,
|
|
|
|
|
},
|
|
|
|
|
enrichSuccess: result => ({
|
|
|
|
|
account: result.account,
|
|
|
|
|
runDate: result.runDate,
|
|
|
|
|
}),
|
|
|
|
|
enrichError: () => ({
|
|
|
|
|
account,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
async () => {
|
|
|
|
|
const validation = await this.simValidation.validateSimSubscription(userId, subscriptionId);
|
|
|
|
|
account = validation.account;
|
2025-09-25 15:11:28 +09:00
|
|
|
|
2025-11-18 10:57:36 +09:00
|
|
|
const scheduleResolution = this.simSchedule.resolveScheduledDate(request.scheduledAt);
|
2025-09-25 15:11:28 +09:00
|
|
|
|
2025-11-18 10:57:36 +09:00
|
|
|
await this.freebitService.cancelSim(account, scheduleResolution.date);
|
2025-09-25 15:11:28 +09:00
|
|
|
|
2025-11-18 10:57:36 +09:00
|
|
|
this.logger.log(`Successfully cancelled SIM for subscription ${subscriptionId}`, {
|
|
|
|
|
userId,
|
|
|
|
|
subscriptionId,
|
|
|
|
|
account,
|
|
|
|
|
runDate: scheduleResolution.date,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
account,
|
|
|
|
|
runDate: scheduleResolution.date,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-09-25 15:11:28 +09:00
|
|
|
}
|
|
|
|
|
}
|