aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-11-20 12:38:16 -0300
committerSebastian <sebasjm@gmail.com>2023-11-20 12:38:16 -0300
commit6138846050563e0dca95b0b6d792776925e4c35f (patch)
treeb33cd36acf4b38d3a016506d4f7fa681c83beb63 /packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx
parent7ed3e78f790837479fc2bb2eb6ddc40c78ce59b5 (diff)
downloadwallet-core-6138846050563e0dca95b0b6d792776925e4c35f.tar.xz
new forms api
Diffstat (limited to 'packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx')
-rw-r--r--packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx152
1 files changed, 121 insertions, 31 deletions
diff --git a/packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx b/packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx
index faf9671bb..d1fb3b895 100644
--- a/packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx
+++ b/packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx
@@ -1,5 +1,5 @@
-import { AbsoluteTime, AmountJson, Amounts } from "@gnu-taler/taler-util";
-import { useTranslationContext } from "@gnu-taler/web-util/browser";
+import { AbsoluteTime, AmountJson, Amounts, Codec, OperationResult, buildCodecForObject, codecForNumber, codecForString, codecOptional } from "@gnu-taler/taler-util";
+import { FlexibleForm, useTranslationContext } from "@gnu-taler/web-util/browser";
import { h } from "preact";
import { NiceForm } from "../NiceForm.js";
import { v1 as form_902_11e_v1 } from "../forms/902_11e.js";
@@ -10,29 +10,21 @@ import { v1 as form_902_1e_v1 } from "../forms/902_1e.js";
import { v1 as form_902_4e_v1 } from "../forms/902_4e.js";
import { v1 as form_902_5e_v1 } from "../forms/902_5e.js";
import { v1 as form_902_9e_v1 } from "../forms/902_9e.js";
-import { v1 as simplest } from "../forms/simplest.js";
+import { Simplest, v1 as simplest } from "../forms/simplest.js";
import { Pages } from "../pages.js";
import { AmlExchangeBackend } from "../types.js";
import { useExchangeApiContext } from "../context/config.js";
-export type Justification = {
- // form index in the list of forms
- index: number;
- // form name
- name: string;
- // form values
- value: any;
-}
-
-export function AntiMoneyLaunderingForm({ account, selectedForm, onSubmit }: { account: string, selectedForm: number, onSubmit: (justification: Justification, state: AmlExchangeBackend.AmlState, threshold: AmountJson) => Promise<void>; }) {
+export function AntiMoneyLaunderingForm({ account, formId, onSubmit }: { account: string, formId: string, onSubmit: (justification: Justification, state: AmlExchangeBackend.AmlState, threshold: AmountJson) => Promise<void>; }) {
const { i18n } = useTranslationContext()
- const showingFrom = allForms[selectedForm].impl;
- const formName = allForms[selectedForm].name
+ const theForm = allForms.find((v) => v.id === formId)
+ if (!theForm) {
+ return <div>form with id {formId} not found</div>
+ }
const { config } = useExchangeApiContext()
const initial = {
- fullName: "loggedIn_user_fullname",
when: AbsoluteTime.now(),
state: AmlExchangeBackend.AmlState.pending,
threshold: Amounts.zeroOfCurrency(config.currency),
@@ -40,16 +32,17 @@ export function AntiMoneyLaunderingForm({ account, selectedForm, onSubmit }: { a
return (
<NiceForm
initial={initial}
- form={showingFrom(initial)}
+ form={theForm.impl(initial)}
onUpdate={() => { }}
onSubmit={(formValue) => {
if (formValue.state === undefined || formValue.threshold === undefined) return;
const st = formValue.state;
const amount = formValue.threshold;
- const justification = {
- index: selectedForm,
- name: formName,
+ const justification: Justification = {
+ id: theForm.id,
+ label: theForm.label,
+ version: theForm.version,
value: formValue
}
@@ -75,7 +68,7 @@ export function AntiMoneyLaunderingForm({ account, selectedForm, onSubmit }: { a
);
}
-export interface State {
+export interface BaseForm {
state: AmlExchangeBackend.AmlState;
threshold: AmountJson;
}
@@ -85,49 +78,146 @@ const DocumentDuplicateIcon = <svg xmlns="http://www.w3.org/2000/svg" fill="none
</svg>
-export const allForms = [
+export type FormMetadata = {
+ label: string,
+ id: string,
+ version: number,
+ icon: h.JSX.Element,
+ impl: (current: BaseForm) => FlexibleForm<BaseForm>
+}
+
+export type Justification<T = any> = {
+ // form values
+ value: T;
+} & Omit<Omit<FormMetadata, "icon">, "impl">
+
+export function stringifyJustification(j: Justification): string {
+ return JSON.stringify(j)
+}
+
+
+type SimpleFormMetadata = {
+ version?: number,
+ id?: string,
+}
+
+export const codecForSimpleFormMetadata = (): Codec<SimpleFormMetadata> =>
+ buildCodecForObject<SimpleFormMetadata>()
+ .property("id", codecOptional(codecForString()))
+ .property("version", codecOptional(codecForNumber()))
+ .build("SimpleFormMetadata");
+
+type ParseJustificationFail =
+ "not-json" |
+ "id-not-found" |
+ "form-not-found" |
+ "version-not-found";
+
+export function parseJustification(s: string, listOfAllKnownForms: FormMetadata[]): OperationResult<{ justification: Justification, metadata: FormMetadata }, ParseJustificationFail> {
+ try {
+ const justification = JSON.parse(s)
+ const info = codecForSimpleFormMetadata().decode(justification)
+ if (!info.id) {
+ return {
+ type: "fail",
+ case: "id-not-found",
+ detail: {} as any
+ }
+ }
+ if (!info.version) {
+ return {
+ type: "fail",
+ case: "version-not-found",
+ detail: {} as any
+ }
+ }
+ const found = listOfAllKnownForms.find((f) => {
+ return f.id === info.id && f.version === info.version
+ })
+ if (!found) {
+ return {
+ type: "fail",
+ case: "form-not-found",
+ detail: {} as any
+ }
+ }
+ return {
+ type: "ok",
+ body: {
+ justification, metadata: found
+ }
+ }
+ } catch (e) {
+ return {
+ type: "fail",
+ case: "not-json",
+ detail: {} as any
+ }
+ }
+
+}
+
+export const allForms: Array<FormMetadata> = [
{
- name: "Simple comment",
+ label: "Simple comment",
+ id: "simple_comment",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: simplest,
},
{
- name: "Identification form (902.1e)",
+ label: "Identification form",
+ id: "902.1e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_1e_v1,
},
{
- name: "Operational legal entity or partnership (902.11e)",
+ label: "Operational legal entity or partnership",
+ id: "902.11e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_11e_v1,
},
{
- name: "Foundations (902.12e)",
+ label: "Foundations",
+ id: "902.12e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_12e_v1,
},
{
- name: "Declaration for trusts (902.13e)",
+ label: "Declaration for trusts",
+ id: "902.13e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_13e_v1,
},
{
- name: "Information on life insurance policies (902.15e)",
+ label: "Information on life insurance policies",
+ id: "902.15e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_15e_v1,
},
{
- name: "Declaration of beneficial owner (902.9e)",
+ label: "Declaration of beneficial owner",
+ id: "902.9e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_9e_v1,
},
{
- name: "Customer profile (902.5e)",
+ label: "Customer profile",
+ id: "902.5e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_5e_v1,
},
{
- name: "Risk profile (902.4e)",
+ label: "Risk profile",
+ id: "902.4e",
+ version: 1,
icon: DocumentDuplicateIcon,
impl: form_902_4e_v1,
},