refactor: 调整userSession服务及实体至Auth模块下

This commit is contained in:
2025-12-18 12:16:09 +08:00
parent d6bf4d3cb3
commit 77b7bf8ab2
8 changed files with 17 additions and 16 deletions

View File

@@ -1,27 +0,0 @@
import {
Column,
CreateDateColumn,
DeleteDateColumn,
Entity,
Index,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
@Index(['sessionId', 'userId'])
export class UserSession {
@PrimaryGeneratedColumn('uuid')
sessionId: string;
@Column({ length: 36 })
userId: string;
@Column({ nullable: true })
disabledReason?: string;
@CreateDateColumn({ precision: 3 })
createdAt: Date;
@DeleteDateColumn({ nullable: true, precision: 3 })
deletedAt: Date;
}

View File

@@ -1,39 +0,0 @@
import { InjectRepository } from '@nestjs/typeorm';
import { Injectable } from '@nestjs/common';
import { UserSession } from '../entities/user-session.entity';
import { Repository } from 'typeorm';
@Injectable()
export class UserSessionService {
constructor(
@InjectRepository(UserSession)
private readonly userSessionRepository: Repository<UserSession>,
) { }
async createSession(userId: string): Promise<UserSession> {
const session = this.userSessionRepository.create({
userId,
});
return this.userSessionRepository.save(session);
}
async getSession(sessionId: string) {
const session = await this.userSessionRepository.findOne({
where: {
sessionId,
},
});
return session;
}
async invalidateSession(sessionId: string, reason?: string): Promise<void> {
await this.userSessionRepository.update(
{ sessionId, deletedAt: null },
{
deletedAt: new Date(),
disabledReason: reason || null,
}
)
}
}

View File

@@ -3,17 +3,15 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { UserSession } from './entities/user-session.entity';
import { AuthModule } from 'src/auth/auth.module';
import { UserSessionService } from './services/user-session.service';
@Module({
imports: [
TypeOrmModule.forFeature([User, UserSession]),
TypeOrmModule.forFeature([User]),
forwardRef(() => AuthModule), // 解决循环依赖问题
],
controllers: [UserController],
providers: [UserService, UserSessionService],
exports: [UserService, UserSessionService],
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
export class UserModule { }