2025-08-22 17:02:49 +09:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Get,
|
|
|
|
|
Param,
|
|
|
|
|
Query,
|
2025-08-20 18:02:50 +09:00
|
|
|
Request,
|
|
|
|
|
ParseIntPipe,
|
|
|
|
|
BadRequestException,
|
2025-08-22 17:02:49 +09:00
|
|
|
} from "@nestjs/common";
|
|
|
|
|
import {
|
|
|
|
|
ApiTags,
|
|
|
|
|
ApiOperation,
|
|
|
|
|
ApiResponse,
|
|
|
|
|
ApiQuery,
|
2025-08-20 18:02:50 +09:00
|
|
|
ApiBearerAuth,
|
|
|
|
|
ApiParam,
|
2025-08-22 17:02:49 +09:00
|
|
|
} from "@nestjs/swagger";
|
|
|
|
|
import { SubscriptionsService } from "./subscriptions.service";
|
2025-08-28 16:57:57 +09:00
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
import { Subscription, SubscriptionList, InvoiceList } from "@customer-portal/shared";
|
2025-08-23 17:24:37 +09:00
|
|
|
import { RequestWithUser } from "../auth/auth.types";
|
2025-08-20 18:02:50 +09:00
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiTags("subscriptions")
|
|
|
|
|
@Controller("subscriptions")
|
2025-08-20 18:02:50 +09:00
|
|
|
@ApiBearerAuth()
|
|
|
|
|
export class SubscriptionsController {
|
|
|
|
|
constructor(private readonly subscriptionsService: SubscriptionsService) {}
|
|
|
|
|
|
|
|
|
|
@Get()
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiOperation({
|
|
|
|
|
summary: "Get all user subscriptions",
|
|
|
|
|
description: "Retrieves all subscriptions/services for the authenticated user",
|
2025-08-20 18:02:50 +09:00
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiQuery({
|
|
|
|
|
name: "status",
|
|
|
|
|
required: false,
|
|
|
|
|
type: String,
|
|
|
|
|
description: "Filter by subscription status",
|
|
|
|
|
})
|
|
|
|
|
@ApiResponse({
|
|
|
|
|
status: 200,
|
|
|
|
|
description: "List of user subscriptions",
|
2025-08-20 18:02:50 +09:00
|
|
|
type: Object, // Would be SubscriptionList if we had proper DTO decorators
|
|
|
|
|
})
|
|
|
|
|
async getSubscriptions(
|
2025-08-23 17:24:37 +09:00
|
|
|
@Request() req: RequestWithUser,
|
2025-08-22 17:02:49 +09:00
|
|
|
@Query("status") status?: string
|
2025-08-20 18:02:50 +09:00
|
|
|
): Promise<SubscriptionList | Subscription[]> {
|
|
|
|
|
// Validate status if provided
|
2025-08-22 17:02:49 +09:00
|
|
|
if (status && !["Active", "Suspended", "Terminated", "Cancelled", "Pending"].includes(status)) {
|
|
|
|
|
throw new BadRequestException("Invalid status filter");
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status) {
|
2025-08-22 17:02:49 +09:00
|
|
|
const subscriptions = await this.subscriptionsService.getSubscriptionsByStatus(
|
2025-08-27 10:54:05 +09:00
|
|
|
req.user.id,
|
2025-08-22 17:02:49 +09:00
|
|
|
status
|
|
|
|
|
);
|
2025-08-20 18:02:50 +09:00
|
|
|
return subscriptions;
|
|
|
|
|
}
|
2025-08-22 17:02:49 +09:00
|
|
|
|
2025-08-27 10:54:05 +09:00
|
|
|
return this.subscriptionsService.getSubscriptions(req.user.id);
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
@Get("active")
|
|
|
|
|
@ApiOperation({
|
|
|
|
|
summary: "Get active subscriptions only",
|
|
|
|
|
description: "Retrieves only active subscriptions for the authenticated user",
|
2025-08-20 18:02:50 +09:00
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiResponse({
|
|
|
|
|
status: 200,
|
|
|
|
|
description: "List of active subscriptions",
|
2025-08-20 18:02:50 +09:00
|
|
|
type: [Object], // Would be Subscription[] if we had proper DTO decorators
|
|
|
|
|
})
|
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")
|
|
|
|
|
@ApiOperation({
|
|
|
|
|
summary: "Get subscription statistics",
|
|
|
|
|
description: "Retrieves subscription count statistics by status",
|
2025-08-20 18:02:50 +09:00
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiResponse({
|
|
|
|
|
status: 200,
|
|
|
|
|
description: "Subscription statistics",
|
2025-08-20 18:02:50 +09:00
|
|
|
type: Object,
|
|
|
|
|
})
|
2025-08-23 17:24:37 +09:00
|
|
|
async getSubscriptionStats(@Request() req: RequestWithUser): Promise<{
|
2025-08-20 18:02:50 +09:00
|
|
|
total: number;
|
|
|
|
|
active: number;
|
|
|
|
|
suspended: number;
|
|
|
|
|
cancelled: number;
|
|
|
|
|
pending: number;
|
|
|
|
|
}> {
|
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")
|
|
|
|
|
@ApiOperation({
|
|
|
|
|
summary: "Get subscription details by ID",
|
|
|
|
|
description: "Retrieves detailed information for a specific subscription",
|
2025-08-20 18:02:50 +09:00
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiParam({ name: "id", type: Number, description: "Subscription ID" })
|
|
|
|
|
@ApiResponse({
|
|
|
|
|
status: 200,
|
|
|
|
|
description: "Subscription details",
|
2025-08-20 18:02:50 +09:00
|
|
|
type: Object, // Would be Subscription if we had proper DTO decorators
|
|
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiResponse({ status: 404, description: "Subscription not found" })
|
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> {
|
|
|
|
|
if (subscriptionId <= 0) {
|
2025-08-22 17:02:49 +09:00
|
|
|
throw new BadRequestException("Subscription ID must be a positive number");
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
2025-08-22 17:02:49 +09:00
|
|
|
|
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")
|
|
|
|
|
@ApiOperation({
|
|
|
|
|
summary: "Get invoices for a specific subscription",
|
|
|
|
|
description: "Retrieves all invoices related to a specific subscription",
|
2025-08-20 18:02:50 +09:00
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiParam({ name: "id", type: Number, description: "Subscription ID" })
|
|
|
|
|
@ApiQuery({
|
|
|
|
|
name: "page",
|
|
|
|
|
required: false,
|
|
|
|
|
type: Number,
|
|
|
|
|
description: "Page number (default: 1)",
|
|
|
|
|
})
|
|
|
|
|
@ApiQuery({
|
|
|
|
|
name: "limit",
|
|
|
|
|
required: false,
|
|
|
|
|
type: Number,
|
|
|
|
|
description: "Items per page (default: 10)",
|
|
|
|
|
})
|
|
|
|
|
@ApiResponse({
|
|
|
|
|
status: 200,
|
|
|
|
|
description: "List of invoices for the subscription",
|
2025-08-20 18:02:50 +09:00
|
|
|
type: Object, // Would be InvoiceList if we had proper DTO decorators
|
|
|
|
|
})
|
2025-08-22 17:02:49 +09:00
|
|
|
@ApiResponse({ status: 404, description: "Subscription not found" })
|
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,
|
|
|
|
|
@Query("page") page?: string,
|
|
|
|
|
@Query("limit") limit?: string
|
2025-08-20 18:02:50 +09:00
|
|
|
): Promise<InvoiceList> {
|
|
|
|
|
if (subscriptionId <= 0) {
|
2025-08-22 17:02:49 +09:00
|
|
|
throw new BadRequestException("Subscription ID must be a positive number");
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate and sanitize input
|
2025-08-22 17:02:49 +09:00
|
|
|
const pageNum = this.validatePositiveInteger(page, 1, "page");
|
|
|
|
|
const limitNum = this.validatePositiveInteger(limit, 10, "limit");
|
|
|
|
|
|
2025-08-20 18:02:50 +09:00
|
|
|
// Limit max page size for performance
|
|
|
|
|
if (limitNum > 100) {
|
2025-08-22 17:02:49 +09:00
|
|
|
throw new BadRequestException("Limit cannot exceed 100 items per page");
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
2025-08-22 17:02:49 +09:00
|
|
|
|
2025-08-27 10:54:05 +09:00
|
|
|
return this.subscriptionsService.getSubscriptionInvoices(req.user.id, subscriptionId, {
|
2025-08-22 17:02:49 +09:00
|
|
|
page: pageNum,
|
|
|
|
|
limit: limitNum,
|
|
|
|
|
});
|
2025-08-20 18:02:50 +09:00
|
|
|
}
|
|
|
|
|
|
2025-08-22 17:02:49 +09:00
|
|
|
private validatePositiveInteger(
|
|
|
|
|
value: string | undefined,
|
|
|
|
|
defaultValue: number,
|
|
|
|
|
fieldName: string
|
|
|
|
|
): number {
|
2025-08-20 18:02:50 +09:00
|
|
|
if (!value) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsed = parseInt(value, 10);
|
|
|
|
|
if (isNaN(parsed) || parsed <= 0) {
|
|
|
|
|
throw new BadRequestException(`${fieldName} must be a positive integer`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
}
|
|
|
|
|
}
|