/**
 * This controller returns data about the current user session
 */ import * as express from "express" ; import { IResponseErrorInternal , IResponseErrorValidation , IResponseSuccessJson , ResponseErrorInternal , ResponseErrorValidation , ResponseSuccessJson } from "italia-ts-commons/lib/responses" ; import { sequenceT } from "fp-ts/lib/Apply" ; import { toError } from "fp-ts/lib/Either" ; import { identity } from "fp-ts/lib/function" ; import { fromEither , taskEither , tryCatch } from "fp-ts/lib/TaskEither" ; import { readableReport } from "italia-ts-commons/lib/reporters" ; import { FiscalCode } from "italia-ts-commons/lib/strings" ; import { SuccessResponse } from "src/types/commons" ; import RedisSessionStorage from "../services/redisSessionStorage" ; import RedisUserMetadataStorage from "../services/redisUserMetadataStorage" ; export default class SessionLockController { constructor ( private readonly sessionStorage , private readonly metadataStorage ) { } param req returns public readonly lockUserSession = ( req ) : Promise < IResponseErrorInternal | IResponseErrorValidation | IResponseSuccessJson < SuccessResponse > > => taskEither . of < IResponseErrorInternal | IResponseErrorValidation , void > ( void 0 ) . chain ( _ => fromEither ( FiscalCode . decode ( req . params . fiscal_code ) . mapLeft ( err => ResponseErrorValidation ( "Invalid fiscal code" , readableReport ( err ) ) ) ) ) . chain ( fiscalCode => sequenceT ( taskEither ) ( tryCatch ( ( ) => this . sessionStorage . setBlockedUser ( fiscalCode ) , toError ) . chain ( fromEither ) , tryCatch ( ( ) => this . sessionStorage . delUserAllSessions ( fiscalCode ) , toError ) . chain ( fromEither ) , tryCatch ( ( ) => this . metadataStorage . del ( fiscalCode ) , toError ) . chain ( fromEither ) ) . mapLeft ( err => ResponseErrorInternal ( err . message ) ) ) . fold < IResponseErrorInternal | IResponseErrorValidation | IResponseSuccessJson < SuccessResponse > > ( identity , _ => ResponseSuccessJson ( { message : "ok" } ) ) . run ( ) ; param req returns public readonly unlockUserSession = ( req ) : Promise < IResponseErrorInternal | IResponseErrorValidation | IResponseSuccessJson < SuccessResponse > > => taskEither . of < IResponseErrorInternal | IResponseErrorValidation , void > ( void 0 ) . chain ( _ => fromEither ( FiscalCode . decode ( req . params . fiscal_code ) . mapLeft ( err => ResponseErrorValidation ( "Invalid fiscal code" , readableReport ( err ) ) ) ) ) . chain ( fiscalCode => tryCatch ( ( ) => this . sessionStorage . unsetBlockedUser ( fiscalCode ) , toError ) . chain ( fromEither ) . mapLeft ( err => ResponseErrorInternal ( err . message ) ) ) . fold < IResponseErrorInternal | IResponseErrorValidation | IResponseSuccessJson < SuccessResponse > > ( identity , _ => ResponseSuccessJson ( { message : "ok" } ) ) . run ( ) ; }