-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: [IOPLT-515] Adds the saga to handle api requests on trial syst…
…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
1 parent
62a99b1
commit 5538ed0
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
ts/features/trialSystem/store/sagas/watchTrialSystemSaga.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters