summaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/pages/AntiMoneyLaunderingForm.tsx
blob: c42b1e7af9cd31a4a276213562fc0cddf7f5356d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import {
  AbsoluteTime,
  AmountJson,
  Amounts,
  Codec,
  OperationFail,
  OperationOk,
  OperationResult,
  buildCodecForObject,
  codecForNumber,
  codecForString,
  codecOptional,
} from "@gnu-taler/taler-util";
import {
  DefaultForm,
  useTranslationContext,
} from "@gnu-taler/web-util/browser";
import { h } from "preact";
import { useExchangeApiContext } from "../context/config.js";
import { FormMetadata, uiForms } from "../forms/declaration.js";
import { Pages } from "../pages.js";
import { AmlExchangeBackend } from "../utils/types.js";

export function AntiMoneyLaunderingForm({
  account,
  formId,
  onSubmit,
}: {
  account: string;
  formId: string;
  onSubmit: (
    justification: Justification,
    state: AmlExchangeBackend.AmlState,
    threshold: AmountJson,
  ) => Promise<void>;
}) {
  const { i18n } = useTranslationContext();
  const theForm = uiForms.forms(i18n).find((v) => v.id === formId);
  if (!theForm) {
    return <div>form with id {formId} not found</div>;
  }

  const { config } = useExchangeApiContext();

  const initial = {
    when: AbsoluteTime.now(),
    state: AmlExchangeBackend.AmlState.pending,
    threshold: Amounts.zeroOfCurrency(config.currency),
  };
  return (
    <DefaultForm
      initial={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: Justification = {
          id: theForm.id,
          label: theForm.label,
          version: theForm.version,
          value: formValue,
        };

        onSubmit(justification, st, amount);
      }}
    >
      <div class="mt-6 flex items-center justify-end gap-x-6">
        <a
          href={Pages.account.url({ account })}
          class="text-sm font-semibold leading-6 text-gray-900"
        >
          <i18n.Translate>Cancel</i18n.Translate>
        </a>
        <button
          type="submit"
          class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
        >
          <i18n.Translate>Confirm</i18n.Translate>
        </button>
      </div>
    </DefaultForm>
  );
}

export type Justification<T = any> = {
  // form values
  value: T;
} & Omit<Omit<FormMetadata<any>, "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<any>[],
): OperationOk<{ justification: Justification; metadata: FormMetadata<any> }> | OperationFail<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,
    };
  }
}