File

shared/common/online-state.service.ts

Index

Properties
Methods

Constructor

constructor(redisService: IOnlineStateRedisService)
Parameters :
Name Type Optional Description
redisService IOnlineStateRedisService No

An instance of AppRedisService (must implement the required Redis hash methods)

Properties

Private Readonly KEY_PREFIX
Type : string
Default value : 'online_state'
Private Readonly redisService
Type : IOnlineStateRedisService

Methods

Private getKey
getKey(context: string, contextId: string)
Parameters :
Name Type Optional
context string No
contextId string No
Returns : string
Async getOnlineList
getOnlineList(context: string, contextId: string)
Parameters :
Name Type Optional
context string No
contextId string No
Async getOnlineStatus
getOnlineStatus(context: string, contextId: string, userId: string)
Parameters :
Name Type Optional
context string No
contextId string No
userId string No
Async setOffline
setOffline(context: string, contextId: string, userId: string)
Parameters :
Name Type Optional
context string No
contextId string No
userId string No
Returns : Promise<void>
Async setOnline
setOnline(context: string, contextId: string, userId: string, data, ttlSeconds: number)
Parameters :
Name Type Optional Default value
context string No
contextId string No
userId string No
data No
ttlSeconds number No 3600
Returns : Promise<void>
import { OnlineState } from '../interfaces/online-state';
import { OnlineStateStatus } from './enums/online-state-status.enum';
import { IOnlineStateRedisService } from './redis-service.interface';

export class OnlineStateService {
  private readonly KEY_PREFIX = 'online_state';
  private readonly redisService: IOnlineStateRedisService;

  /**
   * @param redisService An instance of AppRedisService (must implement the required Redis hash methods)
   */
  constructor(redisService: IOnlineStateRedisService) {
    this.redisService = redisService;
  }

  private getKey(context: string, contextId: string): string {
    return `${this.KEY_PREFIX}_${context}_${contextId}`;
  }

  async setOnline(
    context: string,
    contextId: string,
    userId: string,
    data: Omit<OnlineState, 'status'> & { status?: OnlineStateStatus },
    ttlSeconds = 3600,
  ): Promise<void> {
    const key = this.getKey(context, contextId);
    const value: OnlineState = {
      ...data,
      userId,
      status: data.status || OnlineStateStatus.ONLINE,
      lastActivityAt: new Date().toISOString(),
    };
    await this.redisService.rhset(key, userId, value);
    await this.redisService.rexpirein(key, ttlSeconds);
  }

  async setOffline(context: string, contextId: string, userId: string): Promise<void> {
    const key = this.getKey(context, contextId);
    await this.redisService.rhdel(key, userId);
  }

  async getOnlineList(context: string, contextId: string): Promise<OnlineState[]> {
    const key = this.getKey(context, contextId);
    const values = await this.redisService.rhvals(key);
    return values.map((val: string) => JSON.parse(val));
  }

  async getOnlineStatus(context: string, contextId: string, userId: string): Promise<OnlineState | null> {
    const key = this.getKey(context, contextId);
    const value = await this.redisService.rhget(key, userId);
    return value ? JSON.parse(value) : null;
  }
}

results matching ""

    No results matching ""