2025-08-22 17:02:49 +09:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
2025-09-04 18:34:28 +09:00
|
|
|
Post,
|
2025-08-22 17:02:49 +09:00
|
|
|
Param,
|
|
|
|
|
Query,
|
2025-09-04 18:34:28 +09:00
|
|
|
Body,
|
2025-08-20 18:02:50 +09:00
|
|
|
Request,
|
|
|
|
|
ParseIntPipe,
|
|
|
|
|
BadRequestException,
|
2025-09-24 18:00:49 +09:00
|
|
|
UsePipes,
|
2025-10-29 13:29:28 +09:00
|
|
|
Header,
|
2025-08-22 17:02:49 +09:00
|
|
|
} from "@nestjs/common";
|
|
|
|
|
import { SubscriptionsService } from "./subscriptions.service";
|
2025-09-04 18:34:28 +09:00
|
|
|
import { SimManagementService } from "./sim-management.service";
|
2025-11-18 11:14:05 +09:00
|
|
|
import { SimTopUpPricingService } from "./sim-management/services/sim-topup-pricing.service";
|
2025-08-28 16:57:57 +09:00
|
|
|
|
2025-09-25 11:44:10 +09:00
|
|
|
import {
|
|
|
|
|
Subscription,
|
|
|
|
|
SubscriptionList,
|
2025-10-08 16:31:42 +09:00
|
|
|
SubscriptionStats,
|
|
|
|
|
SimActionResponse,
|
|
|
|
|
SimPlanChangeResult,
|
2025-10-03 16:37:52 +09:00
|
|
|
subscriptionQuerySchema,
|
|
|
|
|
type SubscriptionQuery,
|
|
|
|
|
} from "@customer-portal/domain/subscriptions";
|
2025-10-21 17:52:23 +09:00
|
|
|
import { InvoiceList } from "@customer-portal/domain/billing";
|
|
|
|
|
import { createPaginationSchema } from "@customer-portal/domain/toolkit/validation/helpers";
|
|
|
|
|
import type { z } from "zod";
|
2025-10-03 16:37:52 +09:00
|
|
|
import {
|
2025-09-19 16:34:10 +09:00
|
|
|
simTopupRequestSchema,
|
|
|
|
|
simChangePlanRequestSchema,
|
|
|
|
|
simCancelRequestSchema,
|
|
|
|
|
simFeaturesRequestSchema,
|
|
|
|
|
type SimTopupRequest,
|
|
|
|
|
type SimChangePlanRequest,
|
|
|
|
|
type SimCancelRequest,
|
2025-09-25 11:44:10 +09:00
|
|
|
type SimFeaturesRequest,
|
2025-10-03 16:37:52 +09:00
|
|
|
} from "@customer-portal/domain/sim";
|
2025-11-04 13:28:36 +09:00
|
|
|
import { ZodValidationPipe } from "@customer-portal/validation/nestjs";
|
2025-09-17 18:43:43 +09:00
|
|
|
import type { RequestWithUser } from "@bff/modules/auth/auth.types";
|
2025-08-20 18:02:50 +09:00
|
|
|
|
2025-10-21 17:52:23 +09:00
|
|
|
const subscriptionInvoiceQuerySchema = createPaginationSchema({
|
|
|
|
|
defaultLimit: 10,
|
|
|
|
|
maxLimit: 100,
|
|
|
|
|
minLimit: 1,
|
|
|
|
|
});
|
|
|
|
|
type SubscriptionInvoiceQuery = z.infer<typeof subscriptionInvoiceQuerySchema>;
|
|
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
@Controller("subscriptions")
|
2025-08-20 18:02:50 +09:00
|
|
|
export class SubscriptionsController {
|
2025-09-04 18:34:28 +09:00
|
|
|
constructor(
|
|
|
|
|
private readonly subscriptionsService: SubscriptionsService,
|
2025-11-18 11:14:05 +09:00
|
|
|
private readonly simManagementService: SimManagementService,
|
|
|
|
|
private readonly simTopUpPricingService: SimTopUpPricingService
|
2025-09-04 18:34:28 +09:00
|
|
|
) {}
|
2025-08-20 18:02:50 +09:00
|
|
|
|
|
|
|
|
@Get()
|
2025-10-29 13:29:28 +09:00
|
|
|
@Header("Cache-Control", "private, max-age=300") // 5 minutes, user-specific
|
2025-10-02 16:33:25 +09:00
|
|
|
@UsePipes(new ZodValidationPipe(subscriptionQuerySchema))
|
2025-08-20 18:02:50 +09:00
|
|
|
async getSubscriptions(
|
2025-08-23 17:24:37 +09:00
|
|
|
@Request() req: RequestWithUser,
|
2025-10-02 16:33:25 +09:00
|
|
|
@Query() query: SubscriptionQuery
|
2025-10-21 11:44:06 +09:00
|
|
|
): Promise<SubscriptionList> {
|
|
|
|
|
const { status } = query;
|
|
|
|
|
return this.subscriptionsService.getSubscriptions(req.user.id, { status });
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
@Get("active")
|
2025-10-29 13:29:28 +09:00
|
|
|
@Header("Cache-Control", "private, max-age=300") // 5 minutes, user-specific
|
2025-08-23 17:24:37 +09:00
|
|
|
async getActiveSubscriptions(@Request() req: RequestWithUser): Promise<Subscription[]> {
|
2025-08-27 10:54:05 +09:00
|
|
|
return this.subscriptionsService.getActiveSubscriptions(req.user.id);
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
@Get("stats")
|
2025-10-29 13:29:28 +09:00
|
|
|
@Header("Cache-Control", "private, max-age=300") // 5 minutes, user-specific
|
2025-10-08 16:31:42 +09:00
|
|
|
async getSubscriptionStats(@Request() req: RequestWithUser): Promise<SubscriptionStats> {
|
2025-08-27 10:54:05 +09:00
|
|
|
return this.subscriptionsService.getSubscriptionStats(req.user.id);
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
@Get(":id")
|
2025-10-29 13:29:28 +09:00
|
|
|
@Header("Cache-Control", "private, max-age=300") // 5 minutes, user-specific
|
2025-08-20 18:02:50 +09:00
|
|
|
async getSubscriptionById(
|
2025-08-23 17:24:37 +09:00
|
|
|
@Request() req: RequestWithUser,
|
2025-08-22 17:02:49 +09:00
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number
|
2025-08-20 18:02:50 +09:00
|
|
|
): Promise<Subscription> {
|
2025-08-27 10:54:05 +09:00
|
|
|
return this.subscriptionsService.getSubscriptionById(req.user.id, subscriptionId);
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
2025-08-22 17:02:49 +09:00
|
|
|
@Get(":id/invoices")
|
2025-10-29 13:29:28 +09:00
|
|
|
@Header("Cache-Control", "private, max-age=60") // 1 minute, may update with payments
|
2025-08-20 18:02:50 +09:00
|
|
|
async getSubscriptionInvoices(
|
2025-08-23 17:24:37 +09:00
|
|
|
@Request() req: RequestWithUser,
|
2025-08-22 17:02:49 +09:00
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
2025-10-21 17:52:23 +09:00
|
|
|
@Query(new ZodValidationPipe(subscriptionInvoiceQuerySchema)) query: SubscriptionInvoiceQuery
|
2025-08-20 18:02:50 +09:00
|
|
|
): Promise<InvoiceList> {
|
2025-10-02 16:33:25 +09:00
|
|
|
return this.subscriptionsService.getSubscriptionInvoices(req.user.id, subscriptionId, query);
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
2025-09-04 18:34:28 +09:00
|
|
|
|
|
|
|
|
// ==================== SIM Management Endpoints ====================
|
|
|
|
|
|
2025-11-18 11:14:05 +09:00
|
|
|
@Get("sim/top-up/pricing")
|
|
|
|
|
@Header("Cache-Control", "public, max-age=3600") // 1 hour, pricing is relatively static
|
|
|
|
|
async getSimTopUpPricing() {
|
|
|
|
|
const pricing = await this.simTopUpPricingService.getTopUpPricing();
|
|
|
|
|
return { success: true, data: pricing };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("sim/top-up/pricing/preview")
|
|
|
|
|
@Header("Cache-Control", "public, max-age=3600") // 1 hour, pricing calculation is deterministic
|
|
|
|
|
async previewSimTopUpPricing(@Query("quotaMb") quotaMb: string) {
|
|
|
|
|
const quotaMbNum = parseInt(quotaMb, 10);
|
|
|
|
|
if (isNaN(quotaMbNum) || quotaMbNum <= 0) {
|
|
|
|
|
throw new BadRequestException("Invalid quotaMb parameter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const preview = await this.simTopUpPricingService.calculatePricingPreview(quotaMbNum);
|
|
|
|
|
return { success: true, data: preview };
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-21 18:41:14 +09:00
|
|
|
@Get("debug/sim-details/:account")
|
|
|
|
|
async debugSimDetails(@Param("account") account: string) {
|
|
|
|
|
return await this.simManagementService.getSimDetailsDebug(account);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 18:34:28 +09:00
|
|
|
@Get(":id/sim/debug")
|
|
|
|
|
async debugSimSubscription(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number
|
2025-09-09 15:59:30 +09:00
|
|
|
): Promise<Record<string, unknown>> {
|
2025-09-04 18:34:28 +09:00
|
|
|
return this.simManagementService.debugSimSubscription(req.user.id, subscriptionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get(":id/sim")
|
|
|
|
|
async getSimInfo(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number
|
2025-11-21 18:41:14 +09:00
|
|
|
) {
|
2025-09-04 18:34:28 +09:00
|
|
|
return this.simManagementService.getSimInfo(req.user.id, subscriptionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get(":id/sim/details")
|
|
|
|
|
async getSimDetails(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number
|
|
|
|
|
) {
|
|
|
|
|
return this.simManagementService.getSimDetails(req.user.id, subscriptionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get(":id/sim/usage")
|
|
|
|
|
async getSimUsage(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number
|
|
|
|
|
) {
|
|
|
|
|
return this.simManagementService.getSimUsage(req.user.id, subscriptionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get(":id/sim/top-up-history")
|
|
|
|
|
async getSimTopUpHistory(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
|
|
|
|
@Query("fromDate") fromDate: string,
|
|
|
|
|
@Query("toDate") toDate: string
|
|
|
|
|
) {
|
|
|
|
|
if (!fromDate || !toDate) {
|
|
|
|
|
throw new BadRequestException("fromDate and toDate are required");
|
|
|
|
|
}
|
2025-09-09 15:45:03 +09:00
|
|
|
|
2025-09-04 18:34:28 +09:00
|
|
|
return this.simManagementService.getSimTopUpHistory(req.user.id, subscriptionId, {
|
|
|
|
|
fromDate,
|
|
|
|
|
toDate,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post(":id/sim/top-up")
|
2025-09-24 18:00:49 +09:00
|
|
|
@UsePipes(new ZodValidationPipe(simTopupRequestSchema))
|
2025-09-04 18:34:28 +09:00
|
|
|
async topUpSim(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
2025-09-24 18:00:49 +09:00
|
|
|
@Body() body: SimTopupRequest
|
2025-10-08 16:31:42 +09:00
|
|
|
): Promise<SimActionResponse> {
|
2025-09-04 18:34:28 +09:00
|
|
|
await this.simManagementService.topUpSim(req.user.id, subscriptionId, body);
|
|
|
|
|
return { success: true, message: "SIM top-up completed successfully" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post(":id/sim/change-plan")
|
2025-09-24 18:00:49 +09:00
|
|
|
@UsePipes(new ZodValidationPipe(simChangePlanRequestSchema))
|
2025-09-04 18:34:28 +09:00
|
|
|
async changeSimPlan(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
2025-09-24 18:00:49 +09:00
|
|
|
@Body() body: SimChangePlanRequest
|
2025-10-08 16:31:42 +09:00
|
|
|
): Promise<SimPlanChangeResult> {
|
2025-09-04 18:34:28 +09:00
|
|
|
const result = await this.simManagementService.changeSimPlan(req.user.id, subscriptionId, body);
|
2025-09-09 15:45:03 +09:00
|
|
|
return {
|
|
|
|
|
success: true,
|
2025-09-04 18:34:28 +09:00
|
|
|
message: "SIM plan change completed successfully",
|
2025-09-09 15:45:03 +09:00
|
|
|
...result,
|
2025-09-04 18:34:28 +09:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post(":id/sim/cancel")
|
2025-09-24 18:00:49 +09:00
|
|
|
@UsePipes(new ZodValidationPipe(simCancelRequestSchema))
|
2025-09-04 18:34:28 +09:00
|
|
|
async cancelSim(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
2025-09-24 18:00:49 +09:00
|
|
|
@Body() body: SimCancelRequest
|
2025-10-08 16:31:42 +09:00
|
|
|
): Promise<SimActionResponse> {
|
2025-09-04 18:34:28 +09:00
|
|
|
await this.simManagementService.cancelSim(req.user.id, subscriptionId, body);
|
|
|
|
|
return { success: true, message: "SIM cancellation completed successfully" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post(":id/sim/reissue-esim")
|
|
|
|
|
async reissueEsimProfile(
|
|
|
|
|
@Request() req: RequestWithUser,
|
2025-09-09 18:50:31 +09:00
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
2025-11-21 18:41:14 +09:00
|
|
|
@Body() body: { newEid?: string } = {}
|
2025-10-08 16:31:42 +09:00
|
|
|
): Promise<SimActionResponse> {
|
2025-11-21 18:41:14 +09:00
|
|
|
await this.simManagementService.reissueEsimProfile(req.user.id, subscriptionId, body.newEid);
|
2025-09-04 18:34:28 +09:00
|
|
|
return { success: true, message: "eSIM profile reissue completed successfully" };
|
|
|
|
|
}
|
2025-09-05 15:39:43 +09:00
|
|
|
|
|
|
|
|
@Post(":id/sim/features")
|
2025-09-24 18:00:49 +09:00
|
|
|
@UsePipes(new ZodValidationPipe(simFeaturesRequestSchema))
|
2025-09-05 15:39:43 +09:00
|
|
|
async updateSimFeatures(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Param("id", ParseIntPipe) subscriptionId: number,
|
2025-09-24 18:00:49 +09:00
|
|
|
@Body() body: SimFeaturesRequest
|
2025-10-08 16:31:42 +09:00
|
|
|
): Promise<SimActionResponse> {
|
2025-09-05 15:39:43 +09:00
|
|
|
await this.simManagementService.updateSimFeatures(req.user.id, subscriptionId, body);
|
|
|
|
|
return { success: true, message: "SIM features updated successfully" };
|
|
|
|
|
}
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|