first commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { Controller, Get, Param, Delete, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard, Session } from '@thallesp/nestjs-better-auth';
|
||||
import type { UserSession } from '@thallesp/nestjs-better-auth';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
@Controller('users')
|
||||
@UseGuards(AuthGuard)
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Get()
|
||||
async getAllUsers() {
|
||||
return this.userService.getAllUsers();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async getUserById(@Param('id') id: string) {
|
||||
return this.userService.getUserById(id);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteUser(@Param('id') id: string) {
|
||||
return this.userService.deleteUser(id);
|
||||
}
|
||||
|
||||
@Get(':id/sessions')
|
||||
async getSessionsByUser(@Param('id') id: string) {
|
||||
return this.userService.getSessionsByUserId(id);
|
||||
}
|
||||
|
||||
@Delete(':id/sessions/:sessionId')
|
||||
async removeSession(
|
||||
@Param('id') userId: string,
|
||||
@Param('sessionId') sessionId: string,
|
||||
) {
|
||||
return this.userService.removeSession(userId, sessionId);
|
||||
}
|
||||
|
||||
@Delete(':id/sessions')
|
||||
async removeAllSessions(@Param('id') id: string) {
|
||||
return this.userService.removeAllSessions(id);
|
||||
}
|
||||
|
||||
@Get('session')
|
||||
async getSession(@Session() session: UserSession) {
|
||||
return session;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserController } from './user.controller';
|
||||
import { UserService } from './user.service';
|
||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
})
|
||||
export class UserModule {}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getAllUsers() {
|
||||
return this.prisma.user.findMany({
|
||||
include: { accounts: true, sessions: true, restaurants: true },
|
||||
});
|
||||
}
|
||||
|
||||
async getUserById(id: string) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: { accounts: true, sessions: true, restaurants: true },
|
||||
});
|
||||
if (!user) throw new NotFoundException('User not found');
|
||||
return user;
|
||||
}
|
||||
|
||||
async deleteUser(id: string) {
|
||||
// Delete related sessions, accounts, and restaurants first (cascade-like behavior)
|
||||
await this.prisma.session.deleteMany({ where: { userId: id } });
|
||||
await this.prisma.account.deleteMany({ where: { userId: id } });
|
||||
await this.prisma.restaurant.deleteMany({ where: { ownerId: id } });
|
||||
return this.prisma.user.delete({ where: { id } });
|
||||
}
|
||||
|
||||
async getSessionsByUserId(id: string) {
|
||||
const sessions = await this.prisma.session.findMany({
|
||||
where: { userId: id },
|
||||
});
|
||||
if (sessions.length === 0)
|
||||
throw new NotFoundException('No sessions found for this user');
|
||||
return sessions;
|
||||
}
|
||||
|
||||
async removeSession(userId: string, sessionId: string) {
|
||||
const session = await this.prisma.session.findFirst({
|
||||
where: { id: sessionId, userId },
|
||||
});
|
||||
if (!session) throw new NotFoundException('Session not found');
|
||||
return this.prisma.session.delete({ where: { id: sessionId } });
|
||||
}
|
||||
|
||||
async removeAllSessions(userId: string) {
|
||||
await this.prisma.session.deleteMany({ where: { userId } });
|
||||
return { message: 'All user sessions removed' };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user