Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
silvicir committed Oct 3, 2024
1 parent ac73423 commit 895dcdf
Showing 1 changed file with 41 additions and 44 deletions.
85 changes: 41 additions & 44 deletions src/controllers/ioWalletController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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"
Expand All @@ -48,42 +48,6 @@ const toValidationError = (errors: Errors) =>
);

export default class IoWalletController {
private readonly ensureUserIsAllowed = (
userId: NonEmptyString
): TE.TaskEither<Error, void> =>
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) {}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -223,6 +187,39 @@ export default class IoWalletController {
)
);

private readonly ensureUserIsAllowed = (
userId: NonEmptyString
): TE.TaskEither<Error, void> =>
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,
Expand Down

0 comments on commit 895dcdf

Please sign in to comment.