import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { RestClient } from '../../../clients'; import { ICompleteUserData } from '../../iam'; import { ISessionClientData, ISessionData, IUserSessionData } from '../models/interfaces'; import { generateSessionKeyword, generateUserClientPermissionKeyword, generateUserClientsKeyword, generateUserSessionTokenKeyword } from '../functions'; import { RESTError } from '../../../models/wrappers/rest-wrapper.class'; import { RedisService } from './redis.service'; @Injectable() export class SessionV2Service { //#region Private Members private readonly _client: RestClient; //#endregion //#region Constructor constructor( private readonly _configService: ConfigService, private readonly _redisService: RedisService, ) { this._client = new RestClient({ name: 'SESSION_SERVICE', baseUrl: this._configService.get('EH_SESSIONS__REST__BASE_URL', ''), timeout: 30000, accessToken: this._configService.get('EH_SESSIONS__REST__ACCESS_TOKEN', ''), }); } //#endregion //#region Public APIs public async initPrivateSchool(data: ICompleteUserData) { const requestBody = { data }; return await this._client.post(`/internals/v1/sessions/private-schools`, requestBody); } public async readBySessionToken(token: string): Promise { const userKey = generateSessionKeyword(token); const response = await this._redisService.get(userKey); if (!response) { throw new RESTError('session_not_found', null, 401); } const parsedUserResponse = JSON.parse(response); const userClientKey = generateUserClientsKeyword(parsedUserResponse.userId); const clientResponse = await this._redisService.get(userClientKey); const parsedClient = JSON.parse(clientResponse); return { sessionToken: token, userInfo: parsedUserResponse, userClients: parsedClient, }; } public async deleteBySessionToken(token: string) { const userKey = generateSessionKeyword(token); const userInfoRaw = await this._redisService.get(userKey); if (!userInfoRaw) { return; } const userInfo: IUserSessionData = JSON.parse(userInfoRaw); const userId = userInfo.userId; const userClientKey = generateUserClientsKeyword(userId); const userSessionTokenKey = generateUserSessionTokenKeyword(userId); // Delete per-client permission keys try { const clientsRaw: string = await this._redisService.get(userClientKey); if (clientsRaw) { const clients: { [key: string]: ISessionClientData } = JSON.parse(clientsRaw); for (const key in clients) { const userClientPermissionsKey = generateUserClientPermissionKeyword(userId, clients[key].clientId); await this._redisService.del(userClientPermissionsKey); } } } catch (_) { // Continue cleanup even if client permissions deletion fails } // Always delete the main session keys await this._redisService.del(userClientKey); await this._redisService.del(userSessionTokenKey); await this._redisService.del(userKey); } public async readByUserId(userId: string): Promise { const token = await this.validateUsesSessionToken(userId); return await this.readBySessionToken(token); } public async deleteByUserId(userId: string) { const token = await this.validateUsesSessionToken(userId); return await this.deleteBySessionToken(token); } public async readUserSessionToken(userId: string) { const userSessionTokenKey = generateUserSessionTokenKeyword(userId); return await this._redisService.get(userSessionTokenKey); } public async validateUsesSessionToken(userId: string): Promise { const userSessionToken = await this.readUserSessionToken(userId); if (!userSessionToken) { throw new RESTError('user_session_token_not_found', userId, 400); } return userSessionToken; } public async readUserClient(userId: string, clientId: string): Promise { const key = generateUserClientPermissionKeyword(userId, clientId); const response = await this._redisService.get(key); if (!response) { throw new RESTError('user_client_not_found', clientId, 400); } const result: any = JSON.parse(response); if (!result || !result.client) { throw new RESTError('user_client_not_found', 'json parse', 400); } return { clientId: result.client._id, type: result.client.type, name: result.client.name, address: result.client.address, highland: result.client.highland, code: result.client.code, contacts: result.client.contacts, region: result.client.region, district: result.client.district, languageSectors: result.client.languageSectors, levels: result.client.levels, subjects: result.client.subjects, permissions: result.permissions } } public async readUserClients(userId: string): Promise { const key = generateUserClientsKeyword(userId); const response = await this._redisService.get(key); if (!response) { throw new RESTError('user_clients_not_found', userId, 400); } const result: any = JSON.parse(response); if (!result) { throw new RESTError('user_clients_not_found', 'json parse', 400); } const clients: ISessionClientData[] = []; for (const key in result) { clients.push(result[key]); } return clients; } //#endregion }