diff --git a/ts/features/zendesk/saga/index.ts b/ts/features/zendesk/saga/index.ts index 5baaf78199b..bd614ca57b9 100644 --- a/ts/features/zendesk/saga/index.ts +++ b/ts/features/zendesk/saga/index.ts @@ -51,7 +51,8 @@ function* zendeskGetSessionPollingLoop( const checkSessionResponse = yield* call( checkSession, getSession, - formatRequestedTokenString() + formatRequestedTokenString(), + true ); if (checkSessionResponse === 401) { break; diff --git a/ts/sagas/startup/__tests__/watchCheckSessionSaga.test.ts b/ts/sagas/startup/__tests__/watchCheckSessionSaga.test.ts index cab1f5d9e9d..7bd0ed6f8f3 100644 --- a/ts/sagas/startup/__tests__/watchCheckSessionSaga.test.ts +++ b/ts/sagas/startup/__tests__/watchCheckSessionSaga.test.ts @@ -13,6 +13,7 @@ import { } from "../watchCheckSessionSaga"; import { handleSessionExpiredSaga } from "../../../features/fastLogin/saga/utils"; import { isFastLoginEnabledSelector } from "../../../features/fastLogin/store/selectors"; +import { sessionInfoSelector } from "../../../store/reducers/authentication"; describe("checkSession", () => { const getSessionValidity = jest.fn(); @@ -29,7 +30,7 @@ describe("checkSession", () => { const fields = "(zendeskToken,walletToken,lollipopAssertionRef)"; - testSaga(testableCheckSession!, getSessionValidity, fields) + testSaga(testableCheckSession!, getSessionValidity, fields, false) .next() .call(getSessionValidity, { fields }) .next(responseOK) @@ -39,6 +40,8 @@ describe("checkSession", () => { }) ) .next() + .select(sessionInfoSelector) + .next() .put(sessionInformationLoadSuccess(responseValue as PublicSession)) .next() .isDone(); @@ -49,7 +52,7 @@ describe("checkSession", () => { const fields = "(zendeskToken,walletToken,lollipopAssertionRef)"; - testSaga(testableCheckSession!, getSessionValidity, fields) + testSaga(testableCheckSession!, getSessionValidity, fields, false) .next() .call(getSessionValidity, { fields }) .next(responseUnauthorized) @@ -67,7 +70,7 @@ describe("checkSession", () => { const fields = undefined; - testSaga(testableCheckSession!, getSessionValidity, fields) + testSaga(testableCheckSession!, getSessionValidity, fields, false) .next() .call(getSessionValidity, { fields }) .next(response500) @@ -89,7 +92,7 @@ describe("checkSession", () => { const fields = "(zendeskToken,walletToken,lollipopAssertionRef)"; - testSaga(testableCheckSession!, getSessionValidity, fields) + testSaga(testableCheckSession!, getSessionValidity, fields, false) .next() .call(getSessionValidity, { fields }) .next(responseLeft) diff --git a/ts/sagas/startup/watchCheckSessionSaga.ts b/ts/sagas/startup/watchCheckSessionSaga.ts index 7335d65bb42..f6029c9a63d 100644 --- a/ts/sagas/startup/watchCheckSessionSaga.ts +++ b/ts/sagas/startup/watchCheckSessionSaga.ts @@ -1,8 +1,9 @@ import { readableReport } from "@pagopa/ts-commons/lib/reporters"; import { TypeOfApiResponseStatus } from "@pagopa/ts-commons/lib/requests"; import * as E from "fp-ts/lib/Either"; +import * as O from "fp-ts/lib/Option"; import { SagaIterator } from "redux-saga"; -import { call, put, takeLatest } from "typed-redux-saga/macro"; +import { call, put, select, takeLatest } from "typed-redux-saga/macro"; import { getType } from "typesafe-actions"; import { GetSessionStateT } from "../../../definitions/session_manager/requestTypes"; import { BackendClient } from "../../api/backend"; @@ -14,10 +15,13 @@ import { ReduxSagaEffect, SagaCallReturnType } from "../../types/utils"; import { isTestEnv } from "../../utils/environment"; import { convertUnknownToError } from "../../utils/errors"; import { handleSessionExpiredSaga } from "../../features/fastLogin/saga/utils"; +import { getOnlyNotAlreadyExistentValues } from "../../features/zendesk/utils"; +import { sessionInfoSelector } from "../../store/reducers/authentication"; export function* checkSession( getSessionValidity: ReturnType["getSession"], - fields?: string // the `fields` parameter is optional and it defaults to an empty object + fields?: string, // the `fields` parameter is optional and it defaults to an empty object + mergeOldAndNewValues: boolean = false ): Generator< ReduxSagaEffect, TypeOfApiResponseStatus | undefined, @@ -40,7 +44,18 @@ export function* checkSession( ); if (response.right.status === 200) { - yield* put(sessionInformationLoadSuccess(response.right.value)); + const currentSessionInfo = yield* select(sessionInfoSelector); + + yield* put( + sessionInformationLoadSuccess( + mergeOldAndNewValues && O.isSome(currentSessionInfo) + ? getOnlyNotAlreadyExistentValues( + response.right.value, + currentSessionInfo.value + ) + : response.right.value + ) + ); } return response.right.status; }