2025-12-20 19:22:30 +05:30
|
|
|
import { Injectable } from '@nestjs/common';
|
2025-12-22 13:26:39 +05:30
|
|
|
import { UserStatus, UserRole, Prisma } from '@prisma/client';
|
2025-12-20 19:22:30 +05:30
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
|
|
2025-12-22 13:26:39 +05:30
|
|
|
type UserWithProfiles = Prisma.UserGetPayload<{
|
|
|
|
|
include: { userProfile: true; agentProfile: true };
|
2025-12-20 19:22:30 +05:30
|
|
|
}>;
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class UsersService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
2025-12-22 13:26:39 +05:30
|
|
|
async findAll(page = 1, limit = 10, role?: UserRole) {
|
2025-12-20 19:22:30 +05:30
|
|
|
const skip = (page - 1) * limit;
|
|
|
|
|
|
2025-12-22 13:26:39 +05:30
|
|
|
// Build where clause for optional role filter
|
|
|
|
|
const where: Prisma.UserWhereInput = role ? { role } : {};
|
|
|
|
|
|
2025-12-20 19:22:30 +05:30
|
|
|
const [users, total] = await Promise.all([
|
|
|
|
|
this.prisma.user.findMany({
|
2025-12-22 13:26:39 +05:30
|
|
|
where,
|
2025-12-20 19:22:30 +05:30
|
|
|
skip,
|
|
|
|
|
take: limit,
|
|
|
|
|
orderBy: { createdAt: 'desc' },
|
|
|
|
|
include: {
|
|
|
|
|
userProfile: true,
|
2025-12-22 13:26:39 +05:30
|
|
|
agentProfile: true,
|
2025-12-20 19:22:30 +05:30
|
|
|
},
|
2025-12-22 13:26:39 +05:30
|
|
|
}) as Promise<UserWithProfiles[]>,
|
|
|
|
|
this.prisma.user.count({ where }),
|
2025-12-20 19:22:30 +05:30
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
2025-12-22 13:26:39 +05:30
|
|
|
users: users.map((user) => {
|
|
|
|
|
// Get the appropriate profile based on role
|
|
|
|
|
const profile = user.role === UserRole.AGENT ? user.agentProfile : user.userProfile;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: user.id,
|
|
|
|
|
email: user.email,
|
|
|
|
|
role: user.role,
|
|
|
|
|
status: user.status,
|
|
|
|
|
emailVerified: user.emailVerified,
|
|
|
|
|
authProvider: user.authProvider,
|
|
|
|
|
createdAt: user.createdAt,
|
|
|
|
|
lastLoginAt: user.lastLoginAt,
|
|
|
|
|
profile: profile
|
|
|
|
|
? {
|
|
|
|
|
firstName: profile.firstName,
|
|
|
|
|
lastName: profile.lastName,
|
|
|
|
|
avatar: profile.avatar,
|
|
|
|
|
phone: profile.phone,
|
|
|
|
|
city: profile.city,
|
|
|
|
|
state: profile.state,
|
|
|
|
|
country: profile.country,
|
|
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
};
|
|
|
|
|
}),
|
2025-12-20 19:22:30 +05:30
|
|
|
total,
|
|
|
|
|
page,
|
|
|
|
|
limit,
|
|
|
|
|
totalPages: Math.ceil(total / limit),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(id: string) {
|
|
|
|
|
const user = (await this.prisma.user.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
include: {
|
|
|
|
|
userProfile: true,
|
2025-12-22 13:26:39 +05:30
|
|
|
agentProfile: true,
|
2025-12-20 19:22:30 +05:30
|
|
|
},
|
2025-12-22 13:26:39 +05:30
|
|
|
})) as UserWithProfiles | null;
|
2025-12-20 19:22:30 +05:30
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 13:26:39 +05:30
|
|
|
// Get the appropriate profile based on role
|
|
|
|
|
const profile = user.role === UserRole.AGENT ? user.agentProfile : user.userProfile;
|
|
|
|
|
|
2025-12-20 19:22:30 +05:30
|
|
|
return {
|
|
|
|
|
id: user.id,
|
|
|
|
|
email: user.email,
|
|
|
|
|
role: user.role,
|
|
|
|
|
status: user.status,
|
|
|
|
|
emailVerified: user.emailVerified,
|
|
|
|
|
authProvider: user.authProvider,
|
|
|
|
|
createdAt: user.createdAt,
|
|
|
|
|
lastLoginAt: user.lastLoginAt,
|
2025-12-22 13:26:39 +05:30
|
|
|
profile: profile
|
|
|
|
|
? {
|
|
|
|
|
firstName: profile.firstName,
|
|
|
|
|
lastName: profile.lastName,
|
|
|
|
|
avatar: profile.avatar,
|
|
|
|
|
phone: profile.phone,
|
|
|
|
|
city: profile.city,
|
|
|
|
|
state: profile.state,
|
|
|
|
|
country: profile.country,
|
|
|
|
|
}
|
|
|
|
|
: null,
|
2025-12-20 19:22:30 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateStatus(id: string, status: UserStatus) {
|
|
|
|
|
return this.prisma.user.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data: { status },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|