first commit
This commit is contained in:
71
src/modules/users/users.service.ts
Normal file
71
src/modules/users/users.service.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { DrizzleService } from '../../database/drizzle.service';
|
||||
import { user, profile, media, 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 findByIdWithProfile(id: string) {
|
||||
const [found] = await this.drizzleService.db
|
||||
.select()
|
||||
.from(user)
|
||||
.leftJoin(profile, eq(profile.userId, user.id))
|
||||
.where(eq(user.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (!found) throw new NotFoundException('User not found');
|
||||
return found;
|
||||
}
|
||||
|
||||
async getMyProfile(userId: string) {
|
||||
const result = await this.drizzleService.db
|
||||
.select()
|
||||
.from(user)
|
||||
.leftJoin(profile, eq(profile.userId, user.id))
|
||||
.leftJoin(role, eq(role.id, user.roleId))
|
||||
.where(eq(user.id, userId))
|
||||
.limit(1);
|
||||
|
||||
if (!result.length) throw new NotFoundException('User not found');
|
||||
const row = result[0];
|
||||
const { password, ...userFields } = row.user;
|
||||
return { ...userFields, profile: row.profile, role: row.role };
|
||||
}
|
||||
|
||||
async getMediaByUserId(userId: string) {
|
||||
return this.drizzleService.db
|
||||
.select()
|
||||
.from(media)
|
||||
.where(eq(media.userId, userId));
|
||||
}
|
||||
|
||||
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' };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user