From 7e60a3c6ab92665cc64837ddcdd178d19fd051fd Mon Sep 17 00:00:00 2001
From: Alice Di Rico <83651704+Ladirico@users.noreply.github.com>
Date: Fri, 6 Dec 2024 17:52:07 +0100
Subject: [PATCH] fix: [IOPID-2543] Add logic to merge data during zendesk
polling (#6515)
## Short description
In the as-is when polling, the data in the redux store was overwritten
and the zendeskToken was no longer available. This led to an error when
sending messages to support, within the chat with support. (only for
logged in users)
## Demo
| .env.local | .env.production |
| - | - |
|
|
|
> [!Warning]
> In order to test this PR using .env.local, you need to use this branch
on the dev-server https://github.com/pagopa/io-dev-api-server/pull/445
## How to test
Run the application using or .env.local or .env.production or both
Activate the fast-login settings
follow the video demo
---
ts/features/zendesk/saga/index.ts | 3 ++-
.../__tests__/watchCheckSessionSaga.test.ts | 11 ++++++----
ts/sagas/startup/watchCheckSessionSaga.ts | 21 ++++++++++++++++---
3 files changed, 27 insertions(+), 8 deletions(-)
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;
}