Skip to content

Commit

Permalink
chore: added trial system FF
Browse files Browse the repository at this point in the history
Refs: #1172 SIW-1849
  • Loading branch information
silvicir authored Nov 19, 2024
1 parent a1e5fc4 commit bb0d1f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
27 changes: 18 additions & 9 deletions src/controllers/__tests__/trialController.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* 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";
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 = {
Expand All @@ -22,7 +25,7 @@ const mockGetSubscription = jest.fn();

const trialServiceMock = {
createSubscription: mockCreateSubscription,
getSubscription: mockGetSubscription
getSubscription: mockGetSubscription,
} as any as TrialService;

describe("trialController#createTrialSubscription", () => {
Expand All @@ -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",
});
});

Expand All @@ -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"),
});
});
});
Expand All @@ -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,
});
});

Expand All @@ -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"),
});
});
});
23 changes: 19 additions & 4 deletions src/controllers/trialController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
)
)
);
}

0 comments on commit bb0d1f7

Please sign in to comment.