89 lines
2.2 KiB
TypeScript
89 lines
2.2 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { UserStatus, Prisma } from '@prisma/client';
|
||
|
|
import { PrismaService } from '../prisma/prisma.service';
|
||
|
|
|
||
|
|
type UserWithProfile = Prisma.UserGetPayload<{
|
||
|
|
include: { userProfile: true };
|
||
|
|
}>;
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class UsersService {
|
||
|
|
constructor(private readonly prisma: PrismaService) {}
|
||
|
|
|
||
|
|
async findAll(page = 1, limit = 10) {
|
||
|
|
const skip = (page - 1) * limit;
|
||
|
|
|
||
|
|
const [users, total] = await Promise.all([
|
||
|
|
this.prisma.user.findMany({
|
||
|
|
skip,
|
||
|
|
take: limit,
|
||
|
|
orderBy: { createdAt: 'desc' },
|
||
|
|
include: {
|
||
|
|
userProfile: true,
|
||
|
|
},
|
||
|
|
}) as Promise<UserWithProfile[]>,
|
||
|
|
this.prisma.user.count(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
users: users.map((user) => ({
|
||
|
|
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: user.userProfile
|
||
|
|
? {
|
||
|
|
firstName: user.userProfile.firstName,
|
||
|
|
lastName: user.userProfile.lastName,
|
||
|
|
avatar: user.userProfile.avatar,
|
||
|
|
phone: user.userProfile.phone,
|
||
|
|
city: user.userProfile.city,
|
||
|
|
state: user.userProfile.state,
|
||
|
|
country: user.userProfile.country,
|
||
|
|
}
|
||
|
|
: null,
|
||
|
|
})),
|
||
|
|
total,
|
||
|
|
page,
|
||
|
|
limit,
|
||
|
|
totalPages: Math.ceil(total / limit),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
async findOne(id: string) {
|
||
|
|
const user = (await this.prisma.user.findUnique({
|
||
|
|
where: { id },
|
||
|
|
include: {
|
||
|
|
userProfile: true,
|
||
|
|
},
|
||
|
|
})) as UserWithProfile | null;
|
||
|
|
|
||
|
|
if (!user) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
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: user.userProfile,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
async updateStatus(id: string, status: UserStatus) {
|
||
|
|
return this.prisma.user.update({
|
||
|
|
where: { id },
|
||
|
|
data: { status },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|