57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { eq } from 'drizzle-orm';
|
|
import { DrizzleService } from '../../database/drizzle.service';
|
|
import { user, profile, role } from '../../database/schema';
|
|
|
|
@Injectable()
|
|
export class UsersService {
|
|
constructor(private readonly drizzleService: DrizzleService) {}
|
|
|
|
async findById(id: string) {
|
|
const [found] = await this.drizzleService.db
|
|
.select()
|
|
.from(user)
|
|
.where(eq(user.id, id))
|
|
.limit(1);
|
|
|
|
if (!found) throw new NotFoundException('User not found');
|
|
const { password, ...rest } = found;
|
|
return rest;
|
|
}
|
|
|
|
async getMe(userId: string) {
|
|
const [found] = await this.drizzleService.db
|
|
.select()
|
|
.from(user)
|
|
.leftJoin(role, eq(role.id, user.roleId))
|
|
.where(eq(user.id, userId))
|
|
.limit(1);
|
|
|
|
if (!found) throw new NotFoundException('User not found');
|
|
const { password, ...userFields } = found.user;
|
|
|
|
const profiles = await this.drizzleService.db
|
|
.select({ id: profile.id, name: profile.name, gender: profile.gender })
|
|
.from(profile)
|
|
.where(eq(profile.userId, userId));
|
|
|
|
return { ...userFields, role: found.role, profiles };
|
|
}
|
|
|
|
async banUser(userId: string) {
|
|
await this.drizzleService.db
|
|
.update(user)
|
|
.set({ status: 'banned' } as any)
|
|
.where(eq(user.id, userId));
|
|
return { message: 'User banned' };
|
|
}
|
|
|
|
async activateUser(userId: string) {
|
|
await this.drizzleService.db
|
|
.update(user)
|
|
.set({ status: 'active' } as any)
|
|
.where(eq(user.id, userId));
|
|
return { message: 'User activated' };
|
|
}
|
|
}
|