diff --git a/src/controllers/__tests__/trialController.test.ts b/src/controllers/__tests__/trialController.test.ts index 40a0eea23..feedbbc12 100644 --- a/src/controllers/__tests__/trialController.test.ts +++ b/src/controllers/__tests__/trialController.test.ts @@ -1,7 +1,10 @@ /* tslint:disable:no-any */ /* tslint:disable:no-object-mutation */ -import { ResponseSuccessAccepted, ResponseSuccessJson } from "@pagopa/ts-commons/lib/responses"; +import { + ResponseSuccessAccepted, + ResponseSuccessJson, +} from "@pagopa/ts-commons/lib/responses"; import mockReq from "../../__mocks__/request"; import { mockedUser } from "../../__mocks__/user_mock"; @@ -9,7 +12,7 @@ import TrialController from "../trialController"; import TrialService from "../../services/trialService"; import { SubscriptionStateEnum } from "../../../generated/trial-system/SubscriptionState"; -const aTrialId: string = "trial-id"; +const aTrialId: string = "IO_WALLET_TRIAL_ID"; const nowDate = new Date(); const aValidSubscription = { @@ -22,7 +25,7 @@ const mockGetSubscription = jest.fn(); const trialServiceMock = { createSubscription: mockCreateSubscription, - getSubscription: mockGetSubscription + getSubscription: mockGetSubscription, } as any as TrialService; describe("trialController#createTrialSubscription", () => { @@ -43,10 +46,13 @@ describe("trialController#createTrialSubscription", () => { const response = await controller.createTrialSubscription(req); - expect(mockCreateSubscription).toHaveBeenCalledWith(mockedUser.fiscal_code, aTrialId); + expect(mockCreateSubscription).toHaveBeenCalledWith( + mockedUser.fiscal_code, + aTrialId + ); expect(response).toEqual({ apply: expect.any(Function), - kind: "IResponseSuccessAccepted" + kind: "IResponseSuccessAccepted", }); }); @@ -63,7 +69,7 @@ describe("trialController#createTrialSubscription", () => { expect(response).toEqual({ apply: expect.any(Function), kind: "IResponseErrorValidation", - detail: expect.stringContaining("Bad request") + detail: expect.stringContaining("Bad request"), }); }); }); @@ -86,11 +92,14 @@ describe("trialController#getTrialSubscription", () => { const response = await controller.getTrialSubscription(req); - expect(mockGetSubscription).toHaveBeenCalledWith(mockedUser.fiscal_code, aTrialId); + expect(mockGetSubscription).toHaveBeenCalledWith( + mockedUser.fiscal_code, + aTrialId + ); expect(response).toEqual({ apply: expect.any(Function), kind: "IResponseSuccessJson", - value: aValidSubscription + value: aValidSubscription, }); }); @@ -107,7 +116,7 @@ describe("trialController#getTrialSubscription", () => { expect(response).toEqual({ apply: expect.any(Function), kind: "IResponseErrorValidation", - detail: expect.stringContaining("Bad request") + detail: expect.stringContaining("Bad request"), }); }); }); diff --git a/src/controllers/trialController.ts b/src/controllers/trialController.ts index b5ad9fc7e..273a4e773 100644 --- a/src/controllers/trialController.ts +++ b/src/controllers/trialController.ts @@ -12,15 +12,21 @@ import { IResponseSuccessAccepted, IResponseSuccessJson, IResponseSuccessRedirectToResource, + ResponseSuccessJson, } from "@pagopa/ts-commons/lib/responses"; import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import TrialService from "src/services/trialService"; +import { + FF_IO_WALLET_TRIAL_ENABLED, + IO_WALLET_TRIAL_ID, +} from "../../src/config"; import { TrialId } from "../../generated/trial-system-api/TrialId"; import { withUserFromRequest } from "../types/user"; import { withValidatedOrValidationError } from "../utils/responses"; import { Subscription } from "../../generated/trial-system/Subscription"; +import { SubscriptionStateEnum } from "../../generated/trial-system/SubscriptionState"; export default class TrialController { // eslint-disable-next-line max-params @@ -59,10 +65,19 @@ export default class TrialController { withValidatedOrValidationError( TrialId.decode(req.params.trialId), (trialId) => - withValidatedOrValidationError( - NonEmptyString.decode(user.fiscal_code), - (userId) => this.trialService.getSubscription(userId, trialId) - ) + // if the trialId is not the one from the wallet, the trial system is always called; otherwise, we check the value of FF_IO_WALLET_TRIAL_ENABLED + trialId !== IO_WALLET_TRIAL_ID || FF_IO_WALLET_TRIAL_ENABLED + ? withValidatedOrValidationError( + NonEmptyString.decode(user.fiscal_code), + (userId) => this.trialService.getSubscription(userId, trialId) + ) + : Promise.resolve( + ResponseSuccessJson({ + createdAt: new Date(), + state: SubscriptionStateEnum.ACTIVE, + trialId, + }) + ) ) ); }