This commit is contained in:
@@ -28,6 +28,8 @@ import {
|
||||
import { Roles } from "../../common/decorators";
|
||||
import { PrismaService } from "../../database/prisma.service";
|
||||
import { PaginationDto } from "../../common/dto/pagination.dto";
|
||||
import { AdminUsersQueryDto } from "./dto/admin-users-query.dto";
|
||||
import { UpdateUserSubscriptionDto } from "./dto/update-user-subscription.dto";
|
||||
import {
|
||||
ApiResponse,
|
||||
createSuccessResponse,
|
||||
@@ -57,17 +59,33 @@ export class AdminController {
|
||||
@ApiOperation({ summary: "Get all users (admin)" })
|
||||
@SwaggerResponse({ status: 200, type: [UserResponseDto] })
|
||||
async getAllUsers(
|
||||
@Query() pagination: PaginationDto,
|
||||
@Query() query: AdminUsersQueryDto,
|
||||
): Promise<ApiResponse<PaginatedData<UserResponseDto>>> {
|
||||
const { skip, take, orderBy } = pagination;
|
||||
const { skip, take, orderBy, search, role, subscriptionStatus } = query;
|
||||
|
||||
const where: any = {};
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ email: { contains: search, mode: "insensitive" } },
|
||||
{ firstName: { contains: search, mode: "insensitive" } },
|
||||
{ lastName: { contains: search, mode: "insensitive" } },
|
||||
];
|
||||
}
|
||||
if (role) {
|
||||
where.role = role;
|
||||
}
|
||||
if (subscriptionStatus) {
|
||||
where.subscriptionStatus = subscriptionStatus;
|
||||
}
|
||||
|
||||
const [users, total] = await Promise.all([
|
||||
this.prisma.user.findMany({
|
||||
where,
|
||||
skip,
|
||||
take,
|
||||
orderBy,
|
||||
}),
|
||||
this.prisma.user.count(),
|
||||
this.prisma.user.count({ where }),
|
||||
]);
|
||||
|
||||
const dtos = plainToInstance(
|
||||
@@ -78,8 +96,8 @@ export class AdminController {
|
||||
return createPaginatedResponse(
|
||||
dtos,
|
||||
total,
|
||||
pagination.page || 1,
|
||||
pagination.limit || 10,
|
||||
query.page || 1,
|
||||
query.limit || 10,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -284,20 +302,41 @@ export class AdminController {
|
||||
@SwaggerResponse({ status: 200 })
|
||||
async updateUserSubscription(
|
||||
@Param("userId") userId: string,
|
||||
@Body() data: { plan: string },
|
||||
@Body() data: UpdateUserSubscriptionDto,
|
||||
): Promise<ApiResponse<null>> {
|
||||
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];
|
||||
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) {
|
||||
throw new BadRequestException("EXPIRES_AT_CANNOT_BE_IN_PAST");
|
||||
}
|
||||
|
||||
updateData.subscriptionExpiresAt = parsedDate;
|
||||
} else if (data.expiresAt === null) {
|
||||
updateData.subscriptionExpiresAt = null;
|
||||
}
|
||||
|
||||
await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: { subscriptionStatus: newPlan },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
await this.subscriptionsService.syncLimitsWithPlan(userId, newPlan);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { IsOptional, IsString } from "class-validator";
|
||||
import { PaginationDto } from "../../../common/dto/pagination.dto";
|
||||
|
||||
export class AdminUsersQueryDto extends PaginationDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
role?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
subscriptionStatus?: string;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IsString, IsOptional, IsEnum, IsISO8601 } from "class-validator";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
|
||||
export class UpdateUserSubscriptionDto {
|
||||
@ApiProperty({ description: "Subscription Plan" })
|
||||
@IsString()
|
||||
plan: string;
|
||||
|
||||
@ApiProperty({ description: "Expiration Date in ISO format", required: false })
|
||||
@IsOptional()
|
||||
@IsISO8601()
|
||||
expiresAt?: string | null;
|
||||
}
|
||||
@@ -623,6 +623,8 @@ export class MatchesService {
|
||||
score: {
|
||||
home: liveMatch.scoreHome,
|
||||
away: liveMatch.scoreAway,
|
||||
htHome: (liveMatch as any).htScoreHome ?? null,
|
||||
htAway: (liveMatch as any).htScoreAway ?? null,
|
||||
},
|
||||
date: new Date(Number(liveMatch.mstUtc)),
|
||||
// Fill missing relations with empty arrays
|
||||
@@ -802,7 +804,12 @@ export class MatchesService {
|
||||
teamStats: normalizedTeamStats,
|
||||
mstUtc: Number(match.mstUtc),
|
||||
date: match.date || new Date(Number(match.mstUtc)),
|
||||
score: match.score || { home: match.scoreHome, away: match.scoreAway },
|
||||
score: match.score || {
|
||||
home: match.scoreHome,
|
||||
away: match.scoreAway,
|
||||
htHome: match.htScoreHome ?? null,
|
||||
htAway: match.htScoreAway ?? null,
|
||||
},
|
||||
homeTeam: {
|
||||
...match.homeTeam,
|
||||
logo: match.homeTeamId
|
||||
|
||||
@@ -218,7 +218,12 @@ export class SubscriptionsService {
|
||||
// Sync user subscription status
|
||||
await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: { subscriptionStatus: effectivePlan },
|
||||
data: {
|
||||
subscriptionStatus: effectivePlan,
|
||||
subscriptionExpiresAt: currentBillingPeriod?.ends_at
|
||||
? new Date(currentBillingPeriod.ends_at)
|
||||
: null,
|
||||
},
|
||||
});
|
||||
|
||||
// Sync usage limits with plan
|
||||
|
||||
@@ -116,6 +116,9 @@ export class UserResponseDto {
|
||||
@Expose()
|
||||
subscriptionStatus: string;
|
||||
|
||||
@Expose()
|
||||
subscriptionExpiresAt: Date | null;
|
||||
|
||||
@Expose()
|
||||
createdAt: Date;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user