Skip to content

Commit

Permalink
chore: [IOPLT-515] Adds the saga to handle api requests on trial syst…
Browse files Browse the repository at this point in the history
…em (#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 <[email protected]>
  • Loading branch information
CrisTofani and hevelius authored Jun 27, 2024
1 parent 62a99b1 commit 5538ed0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions ts/features/trialSystem/api/client.ts
Original file line number Diff line number Diff line change
@@ -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<typeof createTrialSystemClient>;
101 changes: 101 additions & 0 deletions ts/features/trialSystem/store/sagas/watchTrialSystemSaga.ts
Original file line number Diff line number Diff line change
@@ -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<typeof trialSystemActivationStatusUpsert.request>
) {
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<typeof trialSystemActivationStatus.request>
) {
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
);
}
4 changes: 4 additions & 0 deletions ts/sagas/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 5538ed0

Please sign in to comment.