-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Carta Giovani Nazionale): [#176959318,#177244438,#177244411] Add…
… OTP generation network / actions / saga / reducer (#2882) * [#176959318] add otp generation api / actions / saga / reducer / test * [#176959318] update comments * [#176959318] restore * [#176959318] refactoring * [#176959318] fix name conlficts * [#176959318] fix name conlficts Co-authored-by: Cristiano Tofani <[email protected]>
- Loading branch information
1 parent
f856dba
commit 04c9905
Showing
8 changed files
with
182 additions
and
4 deletions.
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
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,41 @@ | ||
import { call, put } from "redux-saga/effects"; | ||
import { BackendCGN } from "../../../api/backendCgn"; | ||
import { SagaCallReturnType } from "../../../../../../types/utils"; | ||
import { getNetworkError } from "../../../../../../utils/errors"; | ||
import { cgnGenerateOtp as cgnGenerateOtpAction } from "../../../store/actions/otp"; | ||
import { readablePrivacyReport } from "../../../../../../utils/reporters"; | ||
|
||
// handle the request for CGN Otp code generation | ||
export function* cgnGenerateOtp( | ||
generateOtp: ReturnType<typeof BackendCGN>["generateOtp"] | ||
) { | ||
try { | ||
const generateOtpResult: SagaCallReturnType<typeof generateOtp> = yield call( | ||
generateOtp, | ||
{} | ||
); | ||
if (generateOtpResult.isRight()) { | ||
if (generateOtpResult.value.status === 200) { | ||
yield put(cgnGenerateOtpAction.success(generateOtpResult.value.value)); | ||
} else { | ||
yield put( | ||
cgnGenerateOtpAction.failure( | ||
getNetworkError( | ||
new Error(`response status ${generateOtpResult.value.status}`) | ||
) | ||
) | ||
); | ||
} | ||
} else { | ||
yield put( | ||
cgnGenerateOtpAction.failure( | ||
getNetworkError( | ||
new Error(readablePrivacyReport(generateOtpResult.value)) | ||
) | ||
) | ||
); | ||
} | ||
} catch (e) { | ||
yield put(cgnGenerateOtpAction.failure(getNetworkError(e))); | ||
} | ||
} |
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,14 @@ | ||
import { ActionType, createAsyncAction } from "typesafe-actions"; | ||
import { NetworkError } from "../../../../../utils/errors"; | ||
import { Otp } from "../../../../../../definitions/cgn/Otp"; | ||
|
||
/** | ||
* handle CGN Otp generation | ||
*/ | ||
export const cgnGenerateOtp = createAsyncAction( | ||
"CGN_GENERATE_OTP_REQUEST", | ||
"CGN_GENERATE_OTP_SUCCESS", | ||
"CGN_GENERATE_OTP_FAILURE" | ||
)<void, Otp, NetworkError>(); | ||
|
||
export type CgnOtpActions = ActionType<typeof cgnGenerateOtp>; |
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,47 @@ | ||
import { GlobalState } from "../../../../../../store/reducers/types"; | ||
import { appReducer } from "../../../../../../store/reducers"; | ||
import { | ||
isReady, | ||
remoteError, | ||
remoteLoading, | ||
remoteReady | ||
} from "../../../../bpd/model/RemoteValue"; | ||
import { cgnGenerateOtp } from "../../actions/otp"; | ||
import { cgnOtpDataSelector } from "../otp"; | ||
import { OtpCode } from "../../../../../../../definitions/cgn/OtpCode"; | ||
import { getGenericError } from "../../../../../../utils/errors"; | ||
|
||
describe("cgnOtpReducer", () => { | ||
it("should be loading", () => { | ||
const globalState: GlobalState = appReducer( | ||
undefined, | ||
cgnGenerateOtp.request() | ||
); | ||
expect(cgnOtpDataSelector(globalState)).toEqual(remoteLoading); | ||
}); | ||
|
||
it("should be ready", () => { | ||
const otpData = { | ||
code: "M6R3E4PNG36" as OtpCode, | ||
expires_at: new Date("2021-09-08T00:53:12.966Z"), | ||
ttl: 100 | ||
}; | ||
const globalState: GlobalState = appReducer( | ||
undefined, | ||
cgnGenerateOtp.success(otpData) | ||
); | ||
expect(isReady(cgnOtpDataSelector(globalState))).toBeTruthy(); | ||
if (isReady(cgnOtpDataSelector(globalState))) { | ||
expect(cgnOtpDataSelector(globalState)).toEqual(remoteReady(otpData)); | ||
} | ||
}); | ||
|
||
it("should be error", () => { | ||
const genericError = getGenericError(new Error("an error")); | ||
const globalState: GlobalState = appReducer( | ||
undefined, | ||
cgnGenerateOtp.failure(genericError) | ||
); | ||
expect(cgnOtpDataSelector(globalState)).toEqual(remoteError(genericError)); | ||
}); | ||
}); |
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,50 @@ | ||
import { getType } from "typesafe-actions"; | ||
import { Action } from "../../../../../store/actions/types"; | ||
import { GlobalState } from "../../../../../store/reducers/types"; | ||
import { NetworkError } from "../../../../../utils/errors"; | ||
import { Otp } from "../../../../../../definitions/cgn/Otp"; | ||
import { cgnGenerateOtp } from "../actions/otp"; | ||
import { | ||
remoteError, | ||
remoteLoading, | ||
remoteReady, | ||
remoteUndefined, | ||
RemoteValue | ||
} from "../../../bpd/model/RemoteValue"; | ||
|
||
export type CgnOtpState = { | ||
data: RemoteValue<Otp, NetworkError>; | ||
}; | ||
|
||
const INITIAL_STATE: CgnOtpState = { | ||
data: remoteUndefined | ||
}; | ||
|
||
const reducer = ( | ||
state: CgnOtpState = INITIAL_STATE, | ||
action: Action | ||
): CgnOtpState => { | ||
switch (action.type) { | ||
case getType(cgnGenerateOtp.request): | ||
return { | ||
...state, | ||
data: remoteLoading | ||
}; | ||
case getType(cgnGenerateOtp.success): | ||
return { | ||
...state, | ||
data: remoteReady(action.payload) | ||
}; | ||
case getType(cgnGenerateOtp.failure): | ||
return { | ||
...state, | ||
data: remoteError(action.payload) | ||
}; | ||
} | ||
return state; | ||
}; | ||
|
||
export default reducer; | ||
|
||
export const cgnOtpDataSelector = (state: GlobalState): CgnOtpState["data"] => | ||
state.bonus.cgn.otp.data; |