-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added IoWalletAPIClient and IoWalletService
Refs: #1125 SIW-1111
- Loading branch information
Showing
5 changed files
with
238 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Client, createClient } from "../../generated/io-wallet-api/client"; | ||
|
||
type Fetch = ( | ||
input: RequestInfo | URL, | ||
init?: RequestInit | undefined | ||
) => Promise<Response>; | ||
|
||
export function IoWalletAPIClient( | ||
token: string, | ||
basePath: string, | ||
baseUrl: string, | ||
fetchApi: Fetch | ||
): Client<"FunctionsKey"> { | ||
return createClient<"FunctionsKey">({ | ||
basePath, | ||
baseUrl, | ||
fetchApi, | ||
withDefaults: (op) => (params) => | ||
op({ | ||
...params, | ||
FunctionsKey: token, | ||
}), | ||
}); | ||
} | ||
|
||
export type IoWalletAPIClient = typeof IoWalletAPIClient; |
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,35 @@ | ||
/** | ||
* This controller handles the IO_WALLET requests from the | ||
* app by forwarding the call to the API system. | ||
*/ | ||
|
||
import * as TE from "fp-ts/TaskEither"; | ||
import * as E from "fp-ts/Either"; | ||
|
||
import { IResponseSuccessJson } from "@pagopa/ts-commons/lib/responses"; | ||
|
||
import { pipe } from "fp-ts/lib/function"; | ||
import { FiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
import { UserDetailView } from "generated/io-wallet-api/UserDetailView"; | ||
import IoWalletService from "../services/ioWalletService"; | ||
|
||
export const retrieveUserId = ( | ||
ioWalletService: IoWalletService, | ||
fiscalCode: FiscalCode | ||
) => | ||
pipe( | ||
TE.tryCatch( | ||
() => ioWalletService.getUserByFiscalCode(fiscalCode), | ||
E.toError | ||
), | ||
TE.chain( | ||
TE.fromPredicate( | ||
(r): r is IResponseSuccessJson<UserDetailView> => | ||
r.kind === "IResponseSuccessJson", | ||
(e) => | ||
new Error( | ||
`An error occurred while retrieving the User id. | ${e.detail}` | ||
) | ||
) | ||
) | ||
); |
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,115 @@ | ||
import * as t from "io-ts"; | ||
import IoWalletService from "../ioWalletService"; | ||
import { FiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
|
||
const mockGetEntityConfiguration = jest.fn(); | ||
const mockGetNonce = jest.fn(); | ||
const mockGetUserByFiscalCode = jest.fn(); | ||
const mockCreateWalletInstance = jest.fn(); | ||
const mockCreateWalletAttestation = jest.fn(); | ||
const mockHealthCheck = jest.fn(); | ||
|
||
mockGetUserByFiscalCode.mockImplementation(() => | ||
t.success({ | ||
status: 200, | ||
value: { | ||
id: "000000000000", | ||
}, | ||
}) | ||
); | ||
|
||
const api = { | ||
getEntityConfiguration: mockGetEntityConfiguration, | ||
getNonce: mockGetNonce, | ||
getUserByFiscalCode: mockGetUserByFiscalCode, | ||
createWalletInstance: mockCreateWalletInstance, | ||
createWalletAttestation: mockCreateWalletAttestation, | ||
healthCheck: mockHealthCheck, | ||
}; | ||
|
||
const aFiscalCode = "GRBGPP87L04L741X" as FiscalCode; | ||
|
||
describe("IoWalletService#getUserByFiscalCode", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should make the correct api call", async () => { | ||
const service = new IoWalletService(api); | ||
|
||
await service.getUserByFiscalCode(aFiscalCode); | ||
|
||
expect(mockGetUserByFiscalCode).toHaveBeenCalledWith({ | ||
body: { | ||
fiscal_code: aFiscalCode, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should handle a success response", async () => { | ||
const service = new IoWalletService(api); | ||
|
||
const res = await service.getUserByFiscalCode(aFiscalCode); | ||
|
||
expect(res).toMatchObject({ | ||
kind: "IResponseSuccessJson", | ||
}); | ||
}); | ||
|
||
it("should handle an internal error when the API client returns 422", async () => { | ||
mockGetUserByFiscalCode.mockImplementationOnce(() => | ||
t.success({ status: 422 }) | ||
); | ||
|
||
const service = new IoWalletService(api); | ||
|
||
const res = await service.getUserByFiscalCode(aFiscalCode); | ||
|
||
expect(res).toMatchObject({ | ||
kind: "IResponseErrorGeneric", | ||
}); | ||
}); | ||
|
||
it("should handle an internal error when the API client returns 500", async () => { | ||
const aGenericProblem = {}; | ||
mockGetUserByFiscalCode.mockImplementationOnce(() => | ||
t.success({ status: 500, value: aGenericProblem }) | ||
); | ||
|
||
const service = new IoWalletService(api); | ||
|
||
const res = await service.getUserByFiscalCode(aFiscalCode); | ||
|
||
expect(res).toMatchObject({ | ||
kind: "IResponseErrorInternal", | ||
}); | ||
}); | ||
|
||
it("should handle an internal error when the API client returns a code not specified in spec", async () => { | ||
const aGenericProblem = {}; | ||
mockGetUserByFiscalCode.mockImplementationOnce(() => | ||
t.success({ status: 599, value: aGenericProblem }) | ||
); | ||
|
||
const service = new IoWalletService(api); | ||
|
||
const res = await service.getUserByFiscalCode(aFiscalCode); | ||
|
||
expect(res).toMatchObject({ | ||
kind: "IResponseErrorInternal", | ||
}); | ||
}); | ||
|
||
it("should return an error if the api call throws an error", async () => { | ||
mockGetUserByFiscalCode.mockImplementationOnce(() => { | ||
throw new Error(); | ||
}); | ||
const service = new IoWalletService(api); | ||
|
||
const res = await service.getUserByFiscalCode(aFiscalCode); | ||
|
||
expect(res).toMatchObject({ | ||
kind: "IResponseErrorInternal", | ||
}); | ||
}); | ||
}); |
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,61 @@ | ||
/** | ||
* This service interacts with the IO Wallet API | ||
*/ | ||
|
||
import { | ||
IResponseErrorGeneric, | ||
IResponseErrorInternal, | ||
IResponseSuccessJson, | ||
ResponseErrorGeneric, | ||
ResponseErrorInternal, | ||
ResponseSuccessJson, | ||
} from "@pagopa/ts-commons/lib/responses"; | ||
|
||
import { FiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
import { UserDetailView } from "generated/io-wallet-api/UserDetailView"; | ||
import { IoWalletAPIClient } from "../clients/io-wallet"; | ||
import { | ||
ResponseErrorStatusNotDefinedInSpec, | ||
withCatchAsInternalError, | ||
withValidatedOrInternalError, | ||
} from "../utils/responses"; | ||
|
||
export default class IoWalletService { | ||
constructor( | ||
private readonly ioWalletApiClient: ReturnType<IoWalletAPIClient> | ||
) {} | ||
|
||
/** | ||
* Get the Wallet User id. | ||
*/ | ||
public readonly getUserByFiscalCode = ( | ||
fiscalCode: FiscalCode | ||
): Promise< | ||
| IResponseErrorInternal | ||
| IResponseErrorGeneric | ||
| IResponseSuccessJson<UserDetailView> | ||
> => | ||
withCatchAsInternalError(async () => { | ||
const validated = await this.ioWalletApiClient.getUserByFiscalCode({ | ||
body: { fiscal_code: fiscalCode }, | ||
}); | ||
return withValidatedOrInternalError(validated, (response) => { | ||
switch (response.status) { | ||
case 200: | ||
return ResponseSuccessJson(response.value); | ||
case 422: | ||
return ResponseErrorGeneric( | ||
response.status, | ||
"Unprocessable Content", | ||
"Your request didn't validate" | ||
); | ||
case 500: | ||
return ResponseErrorInternal( | ||
`Internal server error | ${response.value}` | ||
); | ||
default: | ||
return ResponseErrorStatusNotDefinedInSpec(response); | ||
} | ||
}); | ||
}); | ||
} |