From 895dcdfd103290abc29f3604706c285bc9413d7a Mon Sep 17 00:00:00 2001 From: silvicir <127971996+silvicir@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:59:19 +0200 Subject: [PATCH] fix lint --- src/controllers/ioWalletController.ts | 85 +++++++++++++-------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/controllers/ioWalletController.ts b/src/controllers/ioWalletController.ts index 81e23c544..8042e039c 100644 --- a/src/controllers/ioWalletController.ts +++ b/src/controllers/ioWalletController.ts @@ -23,7 +23,8 @@ import { ResponseErrorInternal, } from "@pagopa/ts-commons/lib/responses"; -import { flow, pipe } from "fp-ts/lib/function"; +import { pipe } from "fp-ts/lib/function"; +import { Errors } from "io-ts"; import { FiscalCode, NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import { readableReport } from "@pagopa/ts-commons/lib/reporters"; import { UserDetailView } from "../../generated/io-wallet-api/UserDetailView"; @@ -36,7 +37,6 @@ import { CreateWalletAttestationBody } from "../../generated/io-wallet-api/Creat import { WalletAttestationView } from "../../generated/io-wallet-api/WalletAttestationView"; import { FF_IO_WALLET_TRIAL_ENABLED } from "../config"; import { SetCurrentWalletInstanceStatusBody } from "../../generated/io-wallet/SetCurrentWalletInstanceStatusBody"; -import { Errors } from "io-ts"; const toErrorRetrievingTheUserId = ResponseErrorInternal( "Error retrieving the user id" @@ -48,42 +48,6 @@ const toValidationError = (errors: Errors) => ); export default class IoWalletController { - private readonly ensureUserIsAllowed = ( - userId: NonEmptyString - ): TE.TaskEither => - FF_IO_WALLET_TRIAL_ENABLED - ? pipe( - TE.tryCatch( - () => this.ioWalletService.getSubscription(userId), - E.toError - ), - // if a successful response with state != "ACTIVE" or an error is returned, return left - TE.chain((response) => - response.kind === "IResponseSuccessJson" && - response.value.state === "ACTIVE" - ? TE.right(undefined) - : TE.left(new Error()) - ) - ) - : TE.right(undefined); - - // TODO SIW-1706 - private readonly ensureFiscalCodeIsAllowed: ( - fiscalCode: FiscalCode - ) => TE.TaskEither< - IResponseErrorInternal | IResponseErrorForbiddenNotAuthorized, - void - > = flow( - NonEmptyString.decode, - TE.fromEither, - TE.chainW(this.ensureUserIsAllowed), - TE.mapLeft(() => - getResponseErrorForbiddenNotAuthorized( - "Not authorized to perform this action" - ) - ) - ); - constructor(private readonly ioWalletService: IoWalletService) {} /** @@ -186,16 +150,16 @@ export default class IoWalletController { > => withUserFromRequest(req, async (user) => pipe( - sequenceS(TE.ApplyPar)({ - body: pipe( + this.ensureFiscalCodeIsAllowed(user.fiscal_code), + TE.chainW(() => + pipe( req.body, SetCurrentWalletInstanceStatusBody.decode, E.mapLeft(toValidationError), TE.fromEither - ), - fiscalCode: this.ensureFiscalCodeIsAllowed(user.fiscal_code), - }), - TE.map(({ body: { status } }) => + ) + ), + TE.map(({ status }) => this.ioWalletService.setCurrentWalletInstanceStatus( status, user.fiscal_code @@ -223,6 +187,39 @@ export default class IoWalletController { ) ); + private readonly ensureUserIsAllowed = ( + userId: NonEmptyString + ): TE.TaskEither => + FF_IO_WALLET_TRIAL_ENABLED + ? pipe( + TE.tryCatch( + () => this.ioWalletService.getSubscription(userId), + E.toError + ), + // if a successful response with state != "ACTIVE" or an error is returned, return left + TE.chain((response) => + response.kind === "IResponseSuccessJson" && + response.value.state === "ACTIVE" + ? TE.right(undefined) + : TE.left(new Error()) + ) + ) + : TE.right(undefined); + + // TODO SIW-1706 + private readonly ensureFiscalCodeIsAllowed = (fiscalCode: FiscalCode) => + pipe( + fiscalCode, + NonEmptyString.decode, + TE.fromEither, + TE.chainW(this.ensureUserIsAllowed), + TE.mapLeft(() => + getResponseErrorForbiddenNotAuthorized( + "Not authorized to perform this action" + ) + ) + ); + private readonly getAllowedUserId = (fiscalCode: FiscalCode) => pipe( fiscalCode,