import { ISessionPersistance , ISession , UserId } from "../../../defs" ; export class SessionPersistanceService implements ISessionPersistance { public db : ISession [ ] = [ ] ; async newSession ( userId , expiresAt , data ) { const token = Math . random ( ) . toString ( ) ; const session = { userId , expiresAt , token : token , } ; if ( data ) { Object . assign ( session , { data } ) ; } this . db . push ( session ) ; return token ; } async getSession ( token ) { return this . db . find ( ( s ) => s . token === token ) ; } async deleteSession ( token ) { this . db = this . db . filter ( ( s ) => s . token !== token ) ; } async deleteAllSessionsForUser ( userId ) { this . db = this . db . filter ( ( s ) => s . userId !== userId ) ; } async cleanExpiredTokens ( ) { const now = new Date ( ) . getTime ( ) ; this . db = this . db . filter ( ( s ) => s . expiresAt . getTime ( ) < now ) ; } }