Skip to content

Commit

Permalink
fix: [IOPID-2543] Add logic to merge data during zendesk polling (#6515)
Browse files Browse the repository at this point in the history
## 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 |
| - | - |
| <video
src="https://github.com/user-attachments/assets/4580715e-08f7-488d-9146-27b424efaa97"/>
| <video
src="https://github.com/user-attachments/assets/6d1b63d3-5c3f-4aa1-baeb-4029dfa2e6e4"/>
|

> [!Warning]
> In order to test this PR using .env.local, you need to use this branch
on the dev-server pagopa/io-dev-api-server#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
  • Loading branch information
Ladirico authored Dec 6, 2024
1 parent 9439d7d commit 7e60a3c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion ts/features/zendesk/saga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function* zendeskGetSessionPollingLoop(
const checkSessionResponse = yield* call(
checkSession,
getSession,
formatRequestedTokenString()
formatRequestedTokenString(),
true
);
if (checkSessionResponse === 401) {
break;
Expand Down
11 changes: 7 additions & 4 deletions ts/sagas/startup/__tests__/watchCheckSessionSaga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -39,6 +40,8 @@ describe("checkSession", () => {
})
)
.next()
.select(sessionInfoSelector)
.next()
.put(sessionInformationLoadSuccess(responseValue as PublicSession))
.next()
.isDone();
Expand All @@ -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)
Expand All @@ -67,7 +70,7 @@ describe("checkSession", () => {

const fields = undefined;

testSaga(testableCheckSession!, getSessionValidity, fields)
testSaga(testableCheckSession!, getSessionValidity, fields, false)
.next()
.call(getSessionValidity, { fields })
.next(response500)
Expand All @@ -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)
Expand Down
21 changes: 18 additions & 3 deletions ts/sagas/startup/watchCheckSessionSaga.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<typeof BackendClient>["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<GetSessionStateT> | undefined,
Expand All @@ -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;
}
Expand Down

0 comments on commit 7e60a3c

Please sign in to comment.