diff options
author | Sebastian <sebasjm@gmail.com> | 2023-07-21 15:50:53 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-07-21 15:50:53 -0300 |
commit | 7e37b347447b6fc418f11160d439a7596b039680 (patch) | |
tree | 31b57eedb262316264974991a937ca22bfa626b6 /packages/aml-backoffice-ui/src/hooks | |
parent | e90f1b4206e8843b85655ebe47485c70dbdab3f6 (diff) |
case details and missing decision encryption
Diffstat (limited to 'packages/aml-backoffice-ui/src/hooks')
-rw-r--r-- | packages/aml-backoffice-ui/src/hooks/useBackend.ts | 8 | ||||
-rw-r--r-- | packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts | 162 | ||||
-rw-r--r-- | packages/aml-backoffice-ui/src/hooks/useCases.ts | 53 |
3 files changed, 220 insertions, 3 deletions
diff --git a/packages/aml-backoffice-ui/src/hooks/useBackend.ts b/packages/aml-backoffice-ui/src/hooks/useBackend.ts index e68efb2e3..b9d66fca6 100644 --- a/packages/aml-backoffice-ui/src/hooks/useBackend.ts +++ b/packages/aml-backoffice-ui/src/hooks/useBackend.ts @@ -14,7 +14,7 @@ interface useBackendType { path: string, options?: RequestOptions, ) => Promise<HttpResponseOk<T>>; - fetcher: <T>(endpoint: string) => Promise<HttpResponseOk<T>>; + fetcher: <T>(args: [string, string]) => Promise<HttpResponseOk<T>>; paginatedFetcher: <T>( args: [string, number, number, string], ) => Promise<HttpResponseOk<T>>; @@ -35,8 +35,10 @@ export function usePublicBackend(): useBackendType { ); const fetcher = useCallback( - function fetcherImpl<T>(endpoint: string): Promise<HttpResponseOk<T>> { - return requestHandler<T>(baseUrl, endpoint); + function fetcherImpl<T>([endpoint, talerAmlOfficerSignature]: [string,string]): Promise<HttpResponseOk<T>> { + return requestHandler<T>(baseUrl, endpoint, { + talerAmlOfficerSignature + }); }, [baseUrl], ); diff --git a/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts b/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts new file mode 100644 index 000000000..980a35f21 --- /dev/null +++ b/packages/aml-backoffice-ui/src/hooks/useCaseDetails.ts @@ -0,0 +1,162 @@ + +import { + HttpResponse, + HttpResponseOk, + RequestError +} from "@gnu-taler/web-util/browser"; +import { AmlExchangeBackend } from "../types.js"; +// FIX default import https://github.com/microsoft/TypeScript/issues/49189 +import _useSWR, { SWRHook, useSWRConfig } from "swr"; +import { AccountId } from "../account.js"; +import { usePublicBackend } from "./useBackend.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(); + + const { data, error } = useSWR< + HttpResponseOk<AmlExchangeBackend.AmlDecisionDetails>, + RequestError<AmlExchangeBackend.AmlError> +>( [ + `aml/${account}/decision/${(paytoHash)}`, + signature, +], +fetcher, { + refreshInterval: 0, + refreshWhenHidden: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + refreshWhenOffline: false, + errorRetryCount: 0, + errorRetryInterval: 1, + shouldRetryOnError: false, + keepPreviousData: true, + }); + + if (data) return data; + if (error) return error.cause; + return { loading: true }; +} + +const example1: AmlExchangeBackend.AmlDecisionDetails = { + aml_history: [ + { + justification: "Lack of documentation", + decider_pub: "ASDASDASD", + decision_time: { + t_s: Date.now() / 1000, + }, + new_state: 2, + new_threshold: "USD:0", + }, + { + justification: "Doing a transfer of high amount", + decider_pub: "ASDASDASD", + decision_time: { + t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 6, + }, + new_state: 1, + new_threshold: "USD:2000", + }, + { + justification: "Account is known to the system", + decider_pub: "ASDASDASD", + decision_time: { + t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 9, + }, + new_state: 0, + new_threshold: "USD:100", + }, + ], + kyc_attributes: [ + { + collection_time: { + t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 8, + }, + expiration_time: { + t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 4, + }, + provider_section: "asdasd", + attributes: { + name: "Sebastian", + }, + }, + { + collection_time: { + t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 5, + }, + expiration_time: { + t_s: Date.now() / 1000 - 60 * 60 * 24 * 30 * 2, + }, + provider_section: "asdasd", + attributes: { + creditCard: "12312312312", + }, + }, + ], +}; + +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); + }; +} diff --git a/packages/aml-backoffice-ui/src/hooks/useCases.ts b/packages/aml-backoffice-ui/src/hooks/useCases.ts index 04b7c383b..c5a0fc489 100644 --- a/packages/aml-backoffice-ui/src/hooks/useCases.ts +++ b/packages/aml-backoffice-ui/src/hooks/useCases.ts @@ -92,3 +92,56 @@ export function useCases( } return { loading: true }; } + +const example1: AmlExchangeBackend.AmlRecords = { + records: [ + { + current_state: 0, + h_payto: "QWEQWEQWEQWEWQE", + rowid: 1, + threshold: "USD 100", + }, + { + current_state: 1, + h_payto: "ASDASDASD", + rowid: 1, + threshold: "USD 100", + }, + { + current_state: 2, + h_payto: "ZXCZXCZXCXZC", + rowid: 1, + threshold: "USD 1000", + }, + { + current_state: 0, + h_payto: "QWEQWEQWEQWEWQE", + rowid: 1, + threshold: "USD 100", + }, + { + current_state: 1, + h_payto: "ASDASDASD", + rowid: 1, + threshold: "USD 100", + }, + { + current_state: 2, + h_payto: "ZXCZXCZXCXZC", + rowid: 1, + threshold: "USD 1000", + }, + ].map((e, idx) => { + e.rowid = idx; + e.threshold = `${e.threshold}${idx}`; + return e; + }), +}; + +export const exampleResponse: HttpResponsePaginated<AmlExchangeBackend.AmlRecords,AmlExchangeBackend.AmlError> = { + ok: true, + data: example1, + loadMore: () => {}, + loadMorePrev: () => {}, +} + |