File

src/session/session-config.service.ts

Description

Service for managing session storage configuration per tenant.

Index

Methods

Constructor

constructor(tenantRepository: Repository<TenantEntity>, configService: ConfigService)
Parameters :
Name Type Optional
tenantRepository Repository<TenantEntity> No
configService ConfigService No

Methods

Async getConfig
getConfig(tenantId: string)

Get the session storage configuration for a tenant.

Parameters :
Name Type Optional Description
tenantId string No

The tenant ID

The session storage configuration or null if not set (uses global defaults)

getDefaultCleanupMode
getDefaultCleanupMode()

Get the default cleanup mode from environment configuration.

Returns : SessionCleanupMode
getDefaultTtlSeconds
getDefaultTtlSeconds()

Get the default TTL from environment configuration.

Returns : number
Async getEffectiveConfig
getEffectiveConfig(tenantId: string)

Get the effective session storage configuration for a tenant, with defaults applied if no custom config exists.

Parameters :
Name Type Optional Description
tenantId string No

The tenant ID

The effective session storage configuration with defaults applied

Async resetConfig
resetConfig(tenantId: string)

Reset the session storage configuration for a tenant to defaults.

Parameters :
Name Type Optional Description
tenantId string No

The tenant ID

Returns : Promise<void>
Async updateConfig
updateConfig(tenantId: string, config: UpdateSessionConfigDto)

Update the session storage configuration for a tenant.

Parameters :
Name Type Optional Description
tenantId string No

The tenant ID

config UpdateSessionConfigDto No

The new session storage configuration

The updated session storage configuration

import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import {
    SessionCleanupMode,
    SessionStorageConfig,
} from "../auth/tenant/entitites/session-storage-config";
import { TenantEntity } from "../auth/tenant/entitites/tenant.entity";
import { UpdateSessionConfigDto } from "./dto/update-session-config.dto";

/**
 * Service for managing session storage configuration per tenant.
 */
@Injectable()
export class SessionConfigService {
    constructor(
        @InjectRepository(TenantEntity)
        private readonly tenantRepository: Repository<TenantEntity>,
        private readonly configService: ConfigService,
    ) {}

    /**
     * Get the default TTL from environment configuration.
     */
    getDefaultTtlSeconds(): number {
        return this.configService.getOrThrow<number>("SESSION_TTL");
    }

    /**
     * Get the default cleanup mode from environment configuration.
     */
    getDefaultCleanupMode(): SessionCleanupMode {
        const mode = this.configService.getOrThrow<string>(
            "SESSION_CLEANUP_MODE",
        );
        return mode === "anonymize"
            ? SessionCleanupMode.Anonymize
            : SessionCleanupMode.Full;
    }

    /**
     * Get the session storage configuration for a tenant.
     * @param tenantId The tenant ID
     * @returns The session storage configuration or null if not set (uses global defaults)
     */
    async getConfig(tenantId: string): Promise<SessionStorageConfig | null> {
        const tenant = await this.tenantRepository.findOneByOrFail({
            id: tenantId,
        });
        return tenant.sessionConfig ?? null;
    }

    /**
     * Get the effective session storage configuration for a tenant,
     * with defaults applied if no custom config exists.
     * @param tenantId The tenant ID
     * @returns The effective session storage configuration with defaults applied
     */
    async getEffectiveConfig(tenantId: string): Promise<SessionStorageConfig> {
        const config = await this.getConfig(tenantId);
        return {
            ttlSeconds: config?.ttlSeconds ?? this.getDefaultTtlSeconds(),
            cleanupMode: config?.cleanupMode ?? this.getDefaultCleanupMode(),
        };
    }

    /**
     * Update the session storage configuration for a tenant.
     * @param tenantId The tenant ID
     * @param config The new session storage configuration
     * @returns The updated session storage configuration
     */
    async updateConfig(
        tenantId: string,
        config: UpdateSessionConfigDto,
    ): Promise<SessionStorageConfig> {
        const tenant = await this.tenantRepository.findOneByOrFail({
            id: tenantId,
        });

        // Build updated config, handling null values
        const updatedConfig: SessionStorageConfig = {
            ...tenant.sessionConfig,
        };

        // Handle ttlSeconds: null means reset to default, number means set value
        if (config.ttlSeconds === null) {
            delete updatedConfig.ttlSeconds;
        } else if (config.ttlSeconds !== undefined) {
            updatedConfig.ttlSeconds = config.ttlSeconds;
        }

        // Handle cleanupMode
        if (config.cleanupMode !== undefined) {
            updatedConfig.cleanupMode = config.cleanupMode;
        }

        await this.tenantRepository.update(
            { id: tenantId },
            { sessionConfig: updatedConfig },
        );

        return updatedConfig;
    }

    /**
     * Reset the session storage configuration for a tenant to defaults.
     * @param tenantId The tenant ID
     */
    async resetConfig(tenantId: string): Promise<void> {
        const tenant = await this.tenantRepository.findOneByOrFail({
            id: tenantId,
        });
        tenant.sessionConfig = null;
        await this.tenantRepository.save(tenant);
    }
}

results matching ""

    No results matching ""