diff options
author | Sebastian <sebasjm@gmail.com> | 2024-04-05 17:53:39 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-04-05 17:53:39 -0300 |
commit | cc38998803141c42511e878441a5a8b15a387436 (patch) | |
tree | 8a9279ccd6349aac98e649ca0b7394c0630a805d /packages/merchant-backoffice-ui/src/paths/instance/accounts/create | |
parent | f5747b394d14f65d9bee342eb30edf47a36d9751 (diff) | |
download | wallet-core-cc38998803141c42511e878441a5a8b15a387436.tar.xz |
fix #8276
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/accounts/create')
-rw-r--r-- | packages/merchant-backoffice-ui/src/paths/instance/accounts/create/CreatePage.tsx | 25 | ||||
-rw-r--r-- | packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx | 162 |
2 files changed, 98 insertions, 89 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/CreatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/CreatePage.tsx index 255caa375..d05375b6c 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/CreatePage.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/CreatePage.tsx @@ -32,6 +32,7 @@ import { Input } from "../../../../components/form/Input.js"; import { InputPaytoForm } from "../../../../components/form/InputPaytoForm.js"; import { InputSelector } from "../../../../components/form/InputSelector.js"; import { undefinedIfEmpty } from "../../../../utils/table.js"; +import { safeConvertURL } from "../update/UpdatePage.js"; type Entity = TalerMerchantApi.AccountAddDetails & { repeatPassword: string }; @@ -42,19 +43,11 @@ interface Props { const accountAuthType = ["none", "basic"]; -function isValidURL(s: string): boolean { - try { - const parsed = new URL("/", s); - return parsed instanceof URL; - } catch (e) { - return false; - } -} - export function CreatePage({ onCreate, onBack }: Props): VNode { const { i18n } = useTranslationContext(); const [state, setState] = useState<Partial<Entity>>({}); + const facadeURL = safeConvertURL(state.credit_facade_url); const errors: FormErrors<Entity> = { payto_uri: !state.payto_uri ? i18n.str`required` : undefined, @@ -74,9 +67,15 @@ export function CreatePage({ onCreate, onBack }: Props): VNode { }), credit_facade_url: !state.credit_facade_url ? undefined - : !isValidURL(state.credit_facade_url) - ? i18n.str`not valid url` - : undefined, + : !facadeURL + ? i18n.str`Invalid url` + : !facadeURL.href.endsWith("/") + ? i18n.str`URL should end with a '/'` + : facadeURL.searchParams.size > 0 + ? i18n.str`URL should not contain params` + : facadeURL.hash + ? i18n.str`URL should not hash param` + : undefined, repeatPassword: !state.credit_facade_credentials ? undefined : state.credit_facade_credentials.type === "basic" && @@ -94,7 +93,7 @@ export function CreatePage({ onCreate, onBack }: Props): VNode { if (hasErrors) return Promise.reject(); const credit_facade_url = !state.credit_facade_url ? undefined - : new URL("/", state.credit_facade_url).href; + : facadeURL?.href; const credit_facade_credentials: | TalerMerchantApi.FacadeCredentials | undefined = diff --git a/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx b/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx index 96ca8bf5e..fb50ab995 100644 --- a/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/instance/accounts/create/index.tsx @@ -62,7 +62,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { onCreate={async (request: Entity) => { const revenueAPI = !request.credit_facade_url ? undefined - : new URL("/", request.credit_facade_url); + : new URL("./", request.credit_facade_url); if (revenueAPI) { const resp = await testRevenueAPI( @@ -71,7 +71,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { ); if (resp.type === "fail") { switch (resp.case) { - case "no-config": { + case TestRevenueErrorType.NO_CONFIG: { setNotif({ message: i18n.str`Could not create account`, type: "ERROR", @@ -79,7 +79,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { }); return; } - case "client-bad-request": { + case TestRevenueErrorType.CLIENT_BAD_REQUEST: { setNotif({ message: i18n.str`Could not create account`, type: "ERROR", @@ -87,7 +87,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { }); return; } - case "unauthorized": { + case TestRevenueErrorType.UNAUTHORIZED: { setNotif({ message: i18n.str`Could not create account`, type: "ERROR", @@ -95,7 +95,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { }); return; } - case "not-found": { + case TestRevenueErrorType.NOT_FOUND: { setNotif({ message: i18n.str`Could not create account`, type: "ERROR", @@ -103,7 +103,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { }); return; } - case "error": { + case TestRevenueErrorType.GENERIC_ERROR: { setNotif({ message: i18n.str`Could not create account`, type: "ERROR", @@ -112,7 +112,7 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { return; } default: { - assertUnreachable(resp) + assertUnreachable(resp.case); } } } @@ -136,92 +136,102 @@ export default function CreateValidator({ onConfirm, onBack }: Props): VNode { ); } +export enum TestRevenueErrorType { + NO_CONFIG, + CLIENT_BAD_REQUEST, + UNAUTHORIZED, + NOT_FOUND, + GENERIC_ERROR, +} + export async function testRevenueAPI( revenueAPI: URL, creds: FacadeCredentials | undefined, -): Promise< - | OperationOk<void> - | OperationFail<"no-config"> - | OperationFail<"client-bad-request"> - | OperationFail<"unauthorized"> - | OperationFail<"not-found"> - | OperationFail<"error"> -> { +): Promise<OperationOk<void> | OperationFail<TestRevenueErrorType>> { const api = new TalerRevenueHttpClient( revenueAPI.href, new BrowserFetchHttpLib(), ); + const auth = + creds === undefined + ? undefined + : creds.type === "none" + ? undefined + : creds.type === "basic" + ? { + username: creds.username, + password: creds.password, + } + : undefined; + try { - const config = await api.getConfig(); + const config = await api.getConfig(auth); + if (config.type === "fail") { - return { - type: "fail", - case: "no-config", - detail: { - code: 1, - }, - }; + switch (config.case) { + case HttpStatusCode.Unauthorized: { + return { + type: "fail", + case: TestRevenueErrorType.UNAUTHORIZED, + detail: { + code: 1, + }, + }; + } + case HttpStatusCode.NotFound: { + return { + type: "fail", + case: TestRevenueErrorType.NO_CONFIG, + detail: { + code: 1, + }, + }; + } + } + } + + const history = await api.getHistory(auth); + + if (history.type === "fail") { + switch (history.case) { + case HttpStatusCode.BadRequest: { + return { + type: "fail", + case: TestRevenueErrorType.CLIENT_BAD_REQUEST, + detail: { + code: 1, + }, + }; + } + case HttpStatusCode.Unauthorized: { + return { + type: "fail", + case: TestRevenueErrorType.UNAUTHORIZED, + detail: { + code: 1, + }, + }; + } + case HttpStatusCode.NotFound: { + return { + type: "fail", + case: TestRevenueErrorType.NOT_FOUND, + detail: { + code: 1, + }, + }; + } + } } } catch (err) { if (err instanceof TalerError) { return { type: "fail", - case: "error", + case: TestRevenueErrorType.GENERIC_ERROR, detail: err.errorDetail, }; } } - if (creds) { - const auth = - creds.type === "basic" - ? { - username: creds.username, - password: creds.password, - } - : undefined; - - try { - const history = await api.getHistory(auth); - if (history.type === "fail") { - switch (history.case) { - case HttpStatusCode.BadRequest: { - return { - type: "fail", - case: "client-bad-request", - detail: { - code: 1, - }, - }; - } - case HttpStatusCode.Unauthorized: { - return { - type: "fail", - case: "unauthorized", - detail: { - code: 1, - }, - }; - } - case HttpStatusCode.NotFound: { - return { - type: "fail", - case: "not-found", - detail: { - code: 1, - }, - }; - } - } - } - } catch (err) { - if (err instanceof TalerError) { - return { - type: "fail", - case: "error", - detail: err.errorDetail, - }; - } - } - } + return opFixedSuccess(undefined); } |