import { Repository } from 'typeorm'; import { UserSession } from '../entity/user-session.entity'; import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; @Injectable() export class UserSessionRepository { constructor( @InjectRepository(UserSession) private userSessionRepo: Repository, ) {} async findBySessionKey(sessionKey: string): Promise { return this.userSessionRepo.findOne({ where: { session_key: sessionKey } }); } async findActiveSessionByUserId(userId: number): Promise { return this.userSessionRepo.findOne({ where: { user_id: userId, is_session_loggedout: 0 }, }); } async saveSession(session: UserSession): Promise { return this.userSessionRepo.save(session); } async logoutSession(sessionKey: string): Promise { await this.userSessionRepo.update( { session_key: sessionKey }, { is_session_loggedout: 1, logout_time: new Date() }, ); } }