File

apps/recallassess/recallassess-api/src/api/shared/services/context.service.ts

Description

Service to easily access client request context Provides helper methods to get participant auth data from current request

Index

Methods
Accessors

Methods

getContext
getContext()

Get the current request context

CLRequestContext or undefined if not available

getFullName
getFullName()

Get participant's full name

Returns : string

Full name or empty string if not authenticated

getRequestInfo
getRequestInfo()

Get request URL information

Returns : { url: any; protocol: any; host: any; }

Object with request URL details

isActive
isActive()

Check if participant's account is active

Returns : boolean

True if active, false otherwise

isAuthenticated
isAuthenticated()

Check if current request is authenticated

Returns : boolean

True if participant is authenticated, false otherwise

requireAuth
requireAuth()

Require authentication and return both company ID and participant ID

Returns : literal type

Object with companyId and participantId

Accessors

participantLoggedIn
getparticipantLoggedIn()

Get the authenticated participant from context

accessToken
getaccessToken()

Get the access token from context

companyId
getcompanyId()

Get company ID from authenticated participant

Returns : number
participantId
getparticipantId()

Get participant ID from authenticated participant

Returns : number
import { CLRequestContext, getCLRequestContext } from "@api/shared/context";
import { Injectable, UnauthorizedException } from "@nestjs/common";

/**
 * Service to easily access client request context
 * Provides helper methods to get participant auth data from current request
 */
@Injectable()
export class CLContextService {
  /**
   * Get the current request context
   * @returns CLRequestContext or undefined if not available
   */
  getContext(): CLRequestContext | undefined {
    return getCLRequestContext();
  }

  /**
   * Get the authenticated participant from context
   * @returns Participant object or undefined if not authenticated
   */
  get participantLoggedIn() {
    const context = this.getContext();
    return context?.participantLoggedIn;
  }

  /**
   * Get the access token from context
   * @returns Access token string or undefined
   */
  get accessToken() {
    const context = this.getContext();
    return context?.accessToken;
  }

  /**
   * Get company ID from authenticated participant
   * @throws UnauthorizedException if no authenticated participant
   * @returns Company ID
   */
  get companyId(): number {
    const participant = this.participantLoggedIn;
    if (!participant) {
      throw new UnauthorizedException("Authentication required: No authenticated participant found");
    }
    return participant.company_id;
  }

  /**
   * Get participant ID from authenticated participant
   * @throws UnauthorizedException if no authenticated participant
   * @returns Participant ID
   */
  get participantId(): number {
    const participant = this.participantLoggedIn;
    if (!participant) {
      throw new UnauthorizedException("Authentication required: No authenticated participant found");
    }
    return participant.id;
  }

  /**
   * Require authentication and return both company ID and participant ID
   * @throws UnauthorizedException if not authenticated
   * @returns Object with companyId and participantId
   */
  requireAuth(): { companyId: number; participantId: number } {
    const participant = this.participantLoggedIn;
    if (!participant) {
      throw new UnauthorizedException("Authentication required");
    }

    return {
      companyId: participant.company_id,
      participantId: participant.id,
    };
  }

  /**
   * Check if current request is authenticated
   * @returns True if participant is authenticated, false otherwise
   */
  isAuthenticated(): boolean {
    return !!this.participantLoggedIn;
  }

  /**
   * Get participant's full name
   * @returns Full name or empty string if not authenticated
   */
  getFullName(): string {
    const participant = this.participantLoggedIn;
    if (!participant) {
      return "";
    }
    return `${participant.first_name} ${participant.last_name}`.trim();
  }

  /**
   * Check if participant's account is active
   * @returns True if active, false otherwise
   */
  isActive(): boolean {
    const participant = this.participantLoggedIn;
    return participant?.is_active ?? false;
  }

  /**
   * Get request URL information
   * @returns Object with request URL details
   */
  getRequestInfo() {
    const context = this.getContext();
    return {
      url: context?.requestUrl,
      protocol: context?.requestProtocol,
      host: context?.requestHost,
    };
  }
}

results matching ""

    No results matching ""