@@ -307,28 +307,37 @@ export class AdminController {
|
||||
const user = await this.prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) throw new NotFoundException("USER_NOT_FOUND");
|
||||
|
||||
const validPlans = [PlanType.FREE, PlanType.PLUS, PlanType.PREMIUM, "past_due", "cancelled"];
|
||||
const validPlans = [
|
||||
PlanType.FREE,
|
||||
PlanType.PLUS,
|
||||
PlanType.PREMIUM,
|
||||
"past_due",
|
||||
"cancelled",
|
||||
];
|
||||
const newPlan = data.plan as PlanType;
|
||||
if (!validPlans.includes(newPlan)) {
|
||||
throw new BadRequestException("INVALID_PLAN_TYPE");
|
||||
}
|
||||
|
||||
const updateData: any = { subscriptionStatus: newPlan };
|
||||
|
||||
|
||||
if (data.expiresAt) {
|
||||
const parsedDate = new Date(data.expiresAt);
|
||||
|
||||
|
||||
// Business Logic: If upgrading to Premium/Plus, the expiry date cannot be in the past
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0); // Strip time
|
||||
|
||||
|
||||
const expiry = new Date(parsedDate);
|
||||
expiry.setHours(0, 0, 0, 0);
|
||||
|
||||
if ((newPlan === PlanType.PREMIUM || newPlan === PlanType.PLUS) && expiry < today) {
|
||||
if (
|
||||
(newPlan === PlanType.PREMIUM || newPlan === PlanType.PLUS) &&
|
||||
expiry < today
|
||||
) {
|
||||
throw new BadRequestException("EXPIRES_AT_CANNOT_BE_IN_PAST");
|
||||
}
|
||||
|
||||
|
||||
updateData.subscriptionExpiresAt = parsedDate;
|
||||
} else if (data.expiresAt === null) {
|
||||
updateData.subscriptionExpiresAt = null;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsString, IsOptional, IsEnum, IsISO8601 } from "class-validator";
|
||||
import { IsString, IsOptional, IsISO8601 } from "class-validator";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
|
||||
export class UpdateUserSubscriptionDto {
|
||||
@@ -6,7 +6,10 @@ export class UpdateUserSubscriptionDto {
|
||||
@IsString()
|
||||
plan: string;
|
||||
|
||||
@ApiProperty({ description: "Expiration Date in ISO format", required: false })
|
||||
@ApiProperty({
|
||||
description: "Expiration Date in ISO format",
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsISO8601()
|
||||
expiresAt?: string | null;
|
||||
|
||||
@@ -108,14 +108,10 @@ export class SubscriptionsService {
|
||||
await this.handleSubscriptionResumed(data);
|
||||
break;
|
||||
case "transaction.completed":
|
||||
this.logger.log(
|
||||
`Transaction completed: ${(data as Record<string, unknown>).id}`,
|
||||
);
|
||||
this.logger.log(`Transaction completed: ${data.id}`);
|
||||
break;
|
||||
case "transaction.payment_failed":
|
||||
this.logger.warn(
|
||||
`Payment failed for transaction: ${(data as Record<string, unknown>).id}`,
|
||||
);
|
||||
this.logger.warn(`Payment failed for transaction: ${data.id}`);
|
||||
break;
|
||||
default:
|
||||
this.logger.debug(`Unhandled Paddle event: ${eventType}`);
|
||||
@@ -218,7 +214,7 @@ export class SubscriptionsService {
|
||||
// Sync user subscription status
|
||||
await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
data: {
|
||||
subscriptionStatus: effectivePlan,
|
||||
subscriptionExpiresAt: currentBillingPeriod?.ends_at
|
||||
? new Date(currentBillingPeriod.ends_at)
|
||||
|
||||
Reference in New Issue
Block a user