From 5538ed076b58cc7234f150aed6a190b43d206cc6 Mon Sep 17 00:00:00 2001 From: Cristiano Tofani Date: Thu, 27 Jun 2024 15:10:30 +0200 Subject: [PATCH] chore: [IOPLT-515] Adds the saga to handle api requests on trial system (#5777) > [!warning] > This PR depends on #5776 ## Short description This PR adds the saga to handle the requests on Trial System workflow ## List of changes proposed in this pull request - Create `watchTrialSystemSaga` - Fork the new saga to the startupSaga ## How to test Check if the API call are properly dispatched --------- Co-authored-by: Mario Perrotta --- package.json | 2 +- ts/features/trialSystem/api/client.ts | 16 +++ .../store/sagas/watchTrialSystemSaga.ts | 101 ++++++++++++++++++ ts/sagas/startup.ts | 4 + 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 ts/features/trialSystem/api/client.ts create mode 100644 ts/features/trialSystem/store/sagas/watchTrialSystemSaga.ts diff --git a/package.json b/package.json index 04a9554057a..1565926ce2b 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "pagopa_api_ecommerce": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.64.0/src/domains/ecommerce-app/api/ecommerce-io/v2/_openapi.json.tpl", "pagopa_api_biz_events": "https://raw.githubusercontent.com/pagopa/pagopa-biz-events-service/0.1.37/openapi/openapi_io_patch.json", "pagopa_api_platform": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.64.0/src/domains/shared-app/api/session-wallet/v1/_openapi.json.tpl", - "trial_system": "https://raw.githubusercontent.com/pagopa/io-backend/features/add-openapi-trial-service/api_trial_system.yaml", + "trial_system": "https://raw.githubusercontent.com/pagopa/io-backend/v13.39.1-RELEASE/api_trial_system.yaml", "private": true, "scripts": { "start": "react-native start", diff --git a/ts/features/trialSystem/api/client.ts b/ts/features/trialSystem/api/client.ts new file mode 100644 index 00000000000..a231c0d541e --- /dev/null +++ b/ts/features/trialSystem/api/client.ts @@ -0,0 +1,16 @@ +import { createClient } from "../../../../definitions/trial_systwem/client"; +import { SessionToken } from "../../../types/SessionToken"; +import { defaultRetryingFetch } from "../../../utils/fetch"; + +export const createTrialSystemClient = (baseUrl: string, token: SessionToken) => + createClient<"Bearer">({ + baseUrl, + fetchApi: defaultRetryingFetch(), + withDefaults: op => params => + op({ + ...params, + Bearer: `Bearer ${token}` + }) + }); + +export type TrialSystemClient = ReturnType; diff --git a/ts/features/trialSystem/store/sagas/watchTrialSystemSaga.ts b/ts/features/trialSystem/store/sagas/watchTrialSystemSaga.ts new file mode 100644 index 00000000000..fa406dc5d74 --- /dev/null +++ b/ts/features/trialSystem/store/sagas/watchTrialSystemSaga.ts @@ -0,0 +1,101 @@ +import * as E from "fp-ts/lib/Either"; +import { SagaIterator } from "redux-saga"; +import { call, put, takeLatest } from "typed-redux-saga/macro"; +import { ActionType } from "typesafe-actions"; +import { readableReport } from "@pagopa/ts-commons/lib/reporters"; +import { SessionToken } from "../../../../types/SessionToken"; +import { TrialSystemClient, createTrialSystemClient } from "../../api/client"; +import { apiUrlPrefix } from "../../../../config"; +import { + trialSystemActivationStatus, + trialSystemActivationStatusUpsert +} from "../actions"; +import { getError } from "../../../../utils/errors"; + +function* handleTrialSystemActivationStatusUpsert( + upsertTrialSystemActivationStatus: TrialSystemClient["createSubscription"], + action: ActionType +) { + try { + const result = yield* call(upsertTrialSystemActivationStatus, { + trialId: action.payload + }); + + if (E.isLeft(result)) { + yield* put( + trialSystemActivationStatusUpsert.failure({ + trialId: action.payload, + error: new Error(readableReport(result.left)) + }) + ); + } else if (result.right.status === 201) { + yield* put(trialSystemActivationStatusUpsert.success(result.right.value)); + } else { + yield* put( + trialSystemActivationStatusUpsert.failure({ + trialId: action.payload, + error: new Error(`response status ${result.right.status}`) + }) + ); + } + } catch (e) { + yield* put( + trialSystemActivationStatusUpsert.failure({ + trialId: action.payload, + error: getError(e) + }) + ); + } +} + +function* handleTrialSystemActivationStatus( + trialSystemActivationStatusRequest: TrialSystemClient["getSubscription"], + action: ActionType +) { + try { + const result = yield* call(trialSystemActivationStatusRequest, { + trialId: action.payload + }); + + if (E.isLeft(result)) { + yield* put( + trialSystemActivationStatus.failure({ + trialId: action.payload, + error: new Error(readableReport(result.left)) + }) + ); + } else if (result.right.status === 200) { + yield* put(trialSystemActivationStatus.success(result.right.value)); + } else { + yield* put( + trialSystemActivationStatus.failure({ + trialId: action.payload, + error: new Error(`response status ${result.right.status}`) + }) + ); + } + } catch (e) { + yield* put( + trialSystemActivationStatus.failure({ + trialId: action.payload, + error: getError(e) + }) + ); + } +} + +export function* watchTrialSystemSaga(bearerToken: SessionToken): SagaIterator { + const trialSystemClient = createTrialSystemClient(apiUrlPrefix, bearerToken); + + yield* takeLatest( + trialSystemActivationStatusUpsert.request, + handleTrialSystemActivationStatusUpsert, + trialSystemClient.createSubscription + ); + + yield* takeLatest( + trialSystemActivationStatus.request, + handleTrialSystemActivationStatus, + trialSystemClient.getSubscription + ); +} diff --git a/ts/sagas/startup.ts b/ts/sagas/startup.ts index eb7fc41c200..0864c0b8b00 100644 --- a/ts/sagas/startup.ts +++ b/ts/sagas/startup.ts @@ -100,6 +100,7 @@ import { handleIsKeyStrongboxBacked } from "../features/lollipop/utils/crypto"; import { watchWalletSaga as watchNewWalletSaga } from "../features/newWallet/saga"; import { watchServicesSaga } from "../features/services/common/saga"; import { watchItwSaga } from "../features/itwallet/common/saga"; +import { watchTrialSystemSaga } from "../features/trialSystem/store/sagas/watchTrialSystemSaga"; import { handlePendingMessageStateIfAllowedSaga, updateInstallationSaga @@ -551,6 +552,9 @@ export function* initializeApplicationSaga( yield* fork(watchIDPaySaga, maybeSessionInformation.value.bpdToken); } + // Start watching for trial system saga + yield* fork(watchTrialSystemSaga, sessionToken); + // Start watching for itw saga yield* fork(watchItwSaga);