Skip to content

Commit bb0d1f7

Browse files
authored
chore: added trial system FF
Refs: #1172 SIW-1849
1 parent a1e5fc4 commit bb0d1f7

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/controllers/__tests__/trialController.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/* tslint:disable:no-any */
22
/* tslint:disable:no-object-mutation */
33

4-
import { ResponseSuccessAccepted, ResponseSuccessJson } from "@pagopa/ts-commons/lib/responses";
4+
import {
5+
ResponseSuccessAccepted,
6+
ResponseSuccessJson,
7+
} from "@pagopa/ts-commons/lib/responses";
58

69
import mockReq from "../../__mocks__/request";
710
import { mockedUser } from "../../__mocks__/user_mock";
811
import TrialController from "../trialController";
912
import TrialService from "../../services/trialService";
1013
import { SubscriptionStateEnum } from "../../../generated/trial-system/SubscriptionState";
1114

12-
const aTrialId: string = "trial-id";
15+
const aTrialId: string = "IO_WALLET_TRIAL_ID";
1316
const nowDate = new Date();
1417

1518
const aValidSubscription = {
@@ -22,7 +25,7 @@ const mockGetSubscription = jest.fn();
2225

2326
const trialServiceMock = {
2427
createSubscription: mockCreateSubscription,
25-
getSubscription: mockGetSubscription
28+
getSubscription: mockGetSubscription,
2629
} as any as TrialService;
2730

2831
describe("trialController#createTrialSubscription", () => {
@@ -43,10 +46,13 @@ describe("trialController#createTrialSubscription", () => {
4346

4447
const response = await controller.createTrialSubscription(req);
4548

46-
expect(mockCreateSubscription).toHaveBeenCalledWith(mockedUser.fiscal_code, aTrialId);
49+
expect(mockCreateSubscription).toHaveBeenCalledWith(
50+
mockedUser.fiscal_code,
51+
aTrialId
52+
);
4753
expect(response).toEqual({
4854
apply: expect.any(Function),
49-
kind: "IResponseSuccessAccepted"
55+
kind: "IResponseSuccessAccepted",
5056
});
5157
});
5258

@@ -63,7 +69,7 @@ describe("trialController#createTrialSubscription", () => {
6369
expect(response).toEqual({
6470
apply: expect.any(Function),
6571
kind: "IResponseErrorValidation",
66-
detail: expect.stringContaining("Bad request")
72+
detail: expect.stringContaining("Bad request"),
6773
});
6874
});
6975
});
@@ -86,11 +92,14 @@ describe("trialController#getTrialSubscription", () => {
8692

8793
const response = await controller.getTrialSubscription(req);
8894

89-
expect(mockGetSubscription).toHaveBeenCalledWith(mockedUser.fiscal_code, aTrialId);
95+
expect(mockGetSubscription).toHaveBeenCalledWith(
96+
mockedUser.fiscal_code,
97+
aTrialId
98+
);
9099
expect(response).toEqual({
91100
apply: expect.any(Function),
92101
kind: "IResponseSuccessJson",
93-
value: aValidSubscription
102+
value: aValidSubscription,
94103
});
95104
});
96105

@@ -107,7 +116,7 @@ describe("trialController#getTrialSubscription", () => {
107116
expect(response).toEqual({
108117
apply: expect.any(Function),
109118
kind: "IResponseErrorValidation",
110-
detail: expect.stringContaining("Bad request")
119+
detail: expect.stringContaining("Bad request"),
111120
});
112121
});
113122
});

src/controllers/trialController.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ import {
1212
IResponseSuccessAccepted,
1313
IResponseSuccessJson,
1414
IResponseSuccessRedirectToResource,
15+
ResponseSuccessJson,
1516
} from "@pagopa/ts-commons/lib/responses";
1617

1718
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
1819
import TrialService from "src/services/trialService";
20+
import {
21+
FF_IO_WALLET_TRIAL_ENABLED,
22+
IO_WALLET_TRIAL_ID,
23+
} from "../../src/config";
1924
import { TrialId } from "../../generated/trial-system-api/TrialId";
2025
import { withUserFromRequest } from "../types/user";
2126

2227
import { withValidatedOrValidationError } from "../utils/responses";
2328
import { Subscription } from "../../generated/trial-system/Subscription";
29+
import { SubscriptionStateEnum } from "../../generated/trial-system/SubscriptionState";
2430

2531
export default class TrialController {
2632
// eslint-disable-next-line max-params
@@ -59,10 +65,19 @@ export default class TrialController {
5965
withValidatedOrValidationError(
6066
TrialId.decode(req.params.trialId),
6167
(trialId) =>
62-
withValidatedOrValidationError(
63-
NonEmptyString.decode(user.fiscal_code),
64-
(userId) => this.trialService.getSubscription(userId, trialId)
65-
)
68+
// 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
69+
trialId !== IO_WALLET_TRIAL_ID || FF_IO_WALLET_TRIAL_ENABLED
70+
? withValidatedOrValidationError(
71+
NonEmptyString.decode(user.fiscal_code),
72+
(userId) => this.trialService.getSubscription(userId, trialId)
73+
)
74+
: Promise.resolve(
75+
ResponseSuccessJson({
76+
createdAt: new Date(),
77+
state: SubscriptionStateEnum.ACTIVE,
78+
trialId,
79+
})
80+
)
6681
)
6782
);
6883
}

0 commit comments

Comments
 (0)