aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-11-05 18:04:22 -0300
committerSebastian <sebasjm@gmail.com>2023-11-05 18:04:22 -0300
commitb58d53dd93bd8e97aecc28fae788c5c7051fd73d (patch)
tree7a402fafa4ae19a64f10eeb3042147f5f4733081 /packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts
parent31cf3187e447e2c4ec8a473362c5bacc07a874f1 (diff)
downloadwallet-core-b58d53dd93bd8e97aecc28fae788c5c7051fd73d.tar.xz
sharing components in web-util
Diffstat (limited to 'packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts')
-rw-r--r--packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts103
1 files changed, 21 insertions, 82 deletions
diff --git a/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts b/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts
index 980a35f21..9db1e2aec 100644
--- a/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts
+++ b/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts
@@ -1,34 +1,29 @@
import {
HttpResponse,
- HttpResponseOk,
- RequestError
+ HttpResponseOk
} from "@gnu-taler/web-util/browser";
import { AmlExchangeBackend } from "../types.js";
// FIX default import https://github.com/microsoft/TypeScript/issues/49189
+import { AmountString, OfficerAccount, PaytoString, TalerExchangeApi, TalerExchangeResultByMethod, TalerHttpError } from "@gnu-taler/taler-util";
import _useSWR, { SWRHook, useSWRConfig } from "swr";
-import { AccountId } from "../account.js";
+import { useExchangeApiContext } from "../context/config.js";
import { usePublicBackend } from "./useBackend.js";
+import { useOfficer } from "./useOfficer.js";
const useSWR = _useSWR as unknown as SWRHook;
-export function useCaseDetails(
- account: AccountId,
- paytoHash: string,
- signature: string | undefined,
-): HttpResponse<
- AmlExchangeBackend.AmlDecisionDetails,
- AmlExchangeBackend.AmlError
-> {
- const { fetcher } = usePublicBackend();
+export function useCaseDetails(paytoHash: string) {
+ const officer = useOfficer();
+ const session = officer.state === "ready" ? officer.account : undefined;
- const { data, error } = useSWR<
- HttpResponseOk<AmlExchangeBackend.AmlDecisionDetails>,
- RequestError<AmlExchangeBackend.AmlError>
->( [
- `aml/${account}/decision/${(paytoHash)}`,
- signature,
-],
-fetcher, {
+ const { api } = useExchangeApiContext();
+
+ async function fetcher([officer, account]: [OfficerAccount, PaytoString]) {
+ return await api.getDecisionDetails(officer, account)
+ }
+
+ const { data, error } = useSWR<TalerExchangeResultByMethod<"getDecisionDetails">, TalerHttpError>(
+ !session ? undefined : [session, paytoHash], fetcher, {
refreshInterval: 0,
refreshWhenHidden: false,
revalidateOnFocus: false,
@@ -41,11 +36,11 @@ fetcher, {
});
if (data) return data;
- if (error) return error.cause;
- return { loading: true };
+ if (error) return error;
+ return undefined;
}
-const example1: AmlExchangeBackend.AmlDecisionDetails = {
+const example1: TalerExchangeApi.AmlDecisionDetails = {
aml_history: [
{
justification: "Lack of documentation",
@@ -54,7 +49,7 @@ const example1: AmlExchangeBackend.AmlDecisionDetails = {
t_s: Date.now() / 1000,
},
new_state: 2,
- new_threshold: "USD:0",
+ new_threshold: "USD:0" as AmountString,
},
{
justification: "Doing a transfer of high amount",
@@ -63,7 +58,7 @@ const example1: AmlExchangeBackend.AmlDecisionDetails = {
t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 6,
},
new_state: 1,
- new_threshold: "USD:2000",
+ new_threshold: "USD:2000" as AmountString,
},
{
justification: "Account is known to the system",
@@ -72,7 +67,7 @@ const example1: AmlExchangeBackend.AmlDecisionDetails = {
t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 9,
},
new_state: 0,
- new_threshold: "USD:100",
+ new_threshold: "USD:100" as AmountString,
},
],
kyc_attributes: [
@@ -103,60 +98,4 @@ const example1: AmlExchangeBackend.AmlDecisionDetails = {
],
};
-export const exampleResponse: HttpResponse<AmlExchangeBackend.AmlDecisionDetails,AmlExchangeBackend.AmlError> = {
- ok: true,
- data: example1,
-}
-
-
-export function useAmlCasesAPI(): AmlCaseAPI {
- const { request } = usePublicBackend();
- const mutateAll = useMatchMutate();
-
- const updateDecision = async (
- officer: AccountId,
- data: AmlExchangeBackend.AmlDecision,
- ): Promise<HttpResponseOk<void>> => {
- const res = await request<void>(`aml/${officer}/decision`, {
- method: "POST",
- data,
- contentType: "json",
- });
- await mutateAll(/.*aml.*/);
- return res;
- };
-
- return {
- updateDecision,
- };
-}
-
-export interface AmlCaseAPI {
- updateDecision: (
- officer: AccountId,
- data: AmlExchangeBackend.AmlDecision,
- ) => Promise<HttpResponseOk<void>>;
-}
-
-function useMatchMutate(): (
- re: RegExp,
- value?: unknown,
-) => Promise<any> {
- const { cache, mutate } = useSWRConfig();
-
- if (!(cache instanceof Map)) {
- throw new Error(
- "matchMutate requires the cache provider to be a Map instance",
- );
- }
-
- return function matchRegexMutate(re: RegExp, value?: unknown) {
- const allKeys = Array.from(cache.keys());
- const keys = allKeys.filter((key) => re.test(key));
- const mutations = keys.map((key) => {
- return mutate(key, value, true);
- });
- return Promise.all(mutations);
- };
-}