aboutsummaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-01-08 14:24:07 -0300
committerSebastian <sebasjm@gmail.com>2024-01-08 14:24:20 -0300
commitb0610d24571593909cd6683b340fa266de046e31 (patch)
tree691abc2d9228735eb0a25526eab628168fa8d5a0 /packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx
parent35cbcce79e22d7c3cd9ac1c676ad9b3480bd3fe1 (diff)
downloadwallet-core-b0610d24571593909cd6683b340fa266de046e31.tar.xz
order config using relative time
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx')
-rw-r--r--packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx178
1 files changed, 83 insertions, 95 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx b/packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx
index 2b1741276..a30f79169 100644
--- a/packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx
+++ b/packages/merchant-backoffice-ui/src/paths/instance/orders/create/CreatePage.tsx
@@ -19,32 +19,32 @@
* @author Sebastian Javier Marchano (sebasjm)
*/
-import { Amounts } from "@gnu-taler/taler-util";
+import { AbsoluteTime, Amounts, Duration, TalerProtocolDuration } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
-import { add, isAfter, isBefore, isFuture } from "date-fns";
-import { Fragment, h, VNode } from "preact";
+import { format, isFuture } from "date-fns";
+import { Fragment, VNode, h } from "preact";
import { useEffect, useState } from "preact/hooks";
import {
FormErrors,
FormProvider,
} from "../../../../components/form/FormProvider.js";
import { Input } from "../../../../components/form/Input.js";
-import { InputBoolean } from "../../../../components/form/InputBoolean.js";
import { InputCurrency } from "../../../../components/form/InputCurrency.js";
import { InputDate } from "../../../../components/form/InputDate.js";
+import { InputDuration } from "../../../../components/form/InputDuration.js";
import { InputGroup } from "../../../../components/form/InputGroup.js";
import { InputLocation } from "../../../../components/form/InputLocation.js";
import { InputNumber } from "../../../../components/form/InputNumber.js";
+import { InputToggle } from "../../../../components/form/InputToggle.js";
import { InventoryProductForm } from "../../../../components/product/InventoryProductForm.js";
import { NonInventoryProductFrom } from "../../../../components/product/NonInventoryProductForm.js";
import { ProductList } from "../../../../components/product/ProductList.js";
import { useConfigContext } from "../../../../context/config.js";
-import { Duration, MerchantBackend, WithId } from "../../../../declaration.js";
+import { MerchantBackend, WithId } from "../../../../declaration.js";
+import { useSettings } from "../../../../hooks/useSettings.js";
import { OrderCreateSchema as schema } from "../../../../schemas/index.js";
import { rate } from "../../../../utils/amount.js";
import { undefinedIfEmpty } from "../../../../utils/table.js";
-import { useSettings } from "../../../../hooks/useSettings.js";
-import { InputToggle } from "../../../../components/form/InputToggle.js";
interface Props {
onCreate: (d: MerchantBackend.Orders.PostOrderRequest) => void;
@@ -54,23 +54,13 @@ interface Props {
}
interface InstanceConfig {
use_stefan: boolean;
- default_pay_delay: Duration;
- default_wire_transfer_delay: Duration;
+ default_pay_delay: TalerProtocolDuration;
+ default_wire_transfer_delay: TalerProtocolDuration;
}
function with_defaults(config: InstanceConfig, currency: string): Partial<Entity> {
- const defaultPayDeadline =
- !config.default_pay_delay || config.default_pay_delay.d_us === "forever"
- ? undefined
- : add(new Date(), {
- seconds: config.default_pay_delay.d_us / (1000 * 1000),
- });
- const defaultWireDeadline =
- !config.default_wire_transfer_delay || config.default_wire_transfer_delay.d_us === "forever"
- ? undefined
- : add(new Date(), {
- seconds: config.default_wire_transfer_delay.d_us / (1000 * 1000),
- });
+ const defaultPayDeadline = Duration.fromTalerProtocolDuration(config.default_pay_delay);
+ const defaultWireDeadline = Duration.fromTalerProtocolDuration(config.default_wire_transfer_delay);
return {
inventoryProducts: {},
@@ -78,10 +68,10 @@ function with_defaults(config: InstanceConfig, currency: string): Partial<Entity
pricing: {},
payments: {
max_fee: undefined,
- pay_deadline: defaultPayDeadline,
- refund_deadline: defaultPayDeadline,
createToken: true,
- wire_transfer_deadline: defaultWireDeadline,
+ pay_deadline: (defaultPayDeadline),
+ refund_deadline: (defaultPayDeadline),
+ wire_transfer_deadline: (defaultWireDeadline),
},
shipping: {},
extra: {},
@@ -107,10 +97,10 @@ interface Shipping {
fullfilment_url?: string;
}
interface Payments {
- refund_deadline?: Date;
- pay_deadline?: Date;
- wire_transfer_deadline?: Date;
- auto_refund_deadline?: Date;
+ refund_deadline: Duration;
+ pay_deadline: Duration;
+ wire_transfer_deadline: Duration;
+ auto_refund_deadline: Duration;
max_fee?: string;
createToken: boolean;
minimum_age?: number;
@@ -164,53 +154,41 @@ export function CreatePage({
? i18n.str`must be greater than 0`
: undefined,
}),
- // extra:
- // value.extra && !stringIsValidJSON(value.extra)
- // ? i18n.str`not a valid json`
- // : undefined,
payments: undefinedIfEmpty({
refund_deadline: !value.payments?.refund_deadline
? undefined
- : !isFuture(value.payments.refund_deadline)
- ? i18n.str`should be in the future`
- : value.payments.pay_deadline &&
- isBefore(value.payments.refund_deadline, value.payments.pay_deadline)
- ? i18n.str`refund deadline cannot be before pay deadline`
- : value.payments.wire_transfer_deadline &&
- isBefore(
- value.payments.wire_transfer_deadline,
- value.payments.refund_deadline,
- )
- ? i18n.str`wire transfer deadline cannot be before refund deadline`
- : undefined,
- pay_deadline: !value.payments?.pay_deadline
- ? i18n.str`required`
- : !isFuture(value.payments.pay_deadline)
- ? i18n.str`should be in the future`
+ : value.payments.pay_deadline &&
+ Duration.cmp(value.payments.refund_deadline, value.payments.pay_deadline) === -1
+ ? i18n.str`refund deadline cannot be before pay deadline`
: value.payments.wire_transfer_deadline &&
- isBefore(
+ Duration.cmp(
value.payments.wire_transfer_deadline,
- value.payments.pay_deadline,
- )
- ? i18n.str`wire transfer deadline cannot be before pay deadline`
+ value.payments.refund_deadline,
+ ) === -1
+ ? i18n.str`wire transfer deadline cannot be before refund deadline`
: undefined,
- wire_transfer_deadline: !value.payments?.wire_transfer_deadline
+ pay_deadline: !value.payments?.pay_deadline
? i18n.str`required`
- : !isFuture(value.payments.wire_transfer_deadline)
- ? i18n.str`should be in the future`
+ : value.payments.wire_transfer_deadline &&
+ Duration.cmp(
+ value.payments.wire_transfer_deadline,
+ value.payments.pay_deadline,
+ ) === -1
+ ? i18n.str`wire transfer deadline cannot be before pay deadline`
: undefined,
+ wire_transfer_deadline: !value.payments?.wire_transfer_deadline
+ ? i18n.str`required`
+ : undefined,
auto_refund_deadline: !value.payments?.auto_refund_deadline
? undefined
- : !isFuture(value.payments.auto_refund_deadline)
- ? i18n.str`should be in the future`
- : !value.payments?.refund_deadline
- ? i18n.str`should have a refund deadline`
- : !isAfter(
- value.payments.refund_deadline,
- value.payments.auto_refund_deadline,
- )
- ? i18n.str`auto refund cannot be after refund deadline`
- : undefined,
+ : !value.payments?.refund_deadline
+ ? i18n.str`should have a refund deadline`
+ : Duration.cmp(
+ value.payments.refund_deadline,
+ value.payments.auto_refund_deadline,
+ ) == -1
+ ? i18n.str`auto refund cannot be after refund deadline`
+ : undefined,
}),
shipping: undefinedIfEmpty({
@@ -226,7 +204,7 @@ export function CreatePage({
);
const submit = (): void => {
- const order = schema.cast(value);
+ const order = value as any; //schema.cast(value);
if (!value.payments) return;
if (!value.shipping) return;
@@ -236,29 +214,18 @@ export function CreatePage({
summary: order.pricing.summary,
products: productList,
extra: undefinedIfEmpty(value.extra),
- pay_deadline: value.payments.pay_deadline
- ? {
- t_s: Math.floor(value.payments.pay_deadline.getTime() / 1000),
- }
- : undefined,
+ pay_deadline: !value.payments.pay_deadline ?
+ i18n.str`required` :
+ AbsoluteTime.toProtocolTimestamp(AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.pay_deadline))
+ ,// : undefined,
wire_transfer_deadline: value.payments.wire_transfer_deadline
- ? {
- t_s: Math.floor(
- value.payments.wire_transfer_deadline.getTime() / 1000,
- ),
- }
+ ? AbsoluteTime.toProtocolTimestamp(AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.wire_transfer_deadline))
: undefined,
refund_deadline: value.payments.refund_deadline
- ? {
- t_s: Math.floor(value.payments.refund_deadline.getTime() / 1000),
- }
+ ? AbsoluteTime.toProtocolTimestamp(AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.refund_deadline))
: undefined,
auto_refund: value.payments.auto_refund_deadline
- ? {
- d_us: Math.floor(
- value.payments.auto_refund_deadline.getTime() * 1000,
- ),
- }
+ ? Duration.toTalerProtocolDuration(value.payments.auto_refund_deadline)
: undefined,
max_fee: value.payments.max_fee as string,
@@ -360,10 +327,18 @@ export function CreatePage({
0,
);
+ // if there is no default pay deadline
const noDefault_payDeadline = !instance_default.payments || !instance_default.payments.pay_deadline
+ // and there is no defailt wire deadline
const noDefault_wireDeadline = !instance_default.payments || !instance_default.payments.wire_transfer_deadline
+ // user required to set the taler options
const requiresSomeTalerOptions = noDefault_payDeadline || noDefault_wireDeadline
+ const whenPay = !value.payments?.pay_deadline ? undefined : AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.pay_deadline)
+ const whenRefund = !value.payments?.refund_deadline ? undefined : AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.refund_deadline)
+ const whenWire = !value.payments?.wire_transfer_deadline ? undefined : AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.wire_transfer_deadline)
+ const whenAutoRefund = !value.payments?.auto_refund_deadline ? undefined : AbsoluteTime.addDuration(AbsoluteTime.now(), value.payments.auto_refund_deadline)
+
return (
<div>
@@ -522,10 +497,13 @@ export function CreatePage({
label={i18n.str`Taler payment options`}
tooltip={i18n.str`Override default Taler payment settings for this order`}
>
- {(settings.advanceOrderMode || noDefault_payDeadline) && <InputDate
+ {(settings.advanceOrderMode || noDefault_payDeadline) && <InputDuration
name="payments.pay_deadline"
- label={i18n.str`Payment deadline`}
- tooltip={i18n.str`Deadline for the customer to pay for the offer before it expires. Inventory products will be reserved until this deadline.`}
+ label={i18n.str`Payment time`}
+ help={whenPay && whenPay.t_ms !== "never" ? i18n.str`Deadline at ${format(whenPay.t_ms, "dd/MM/yy HH:mm")}` : i18n.str`Without deadline`}
+ withForever
+ withoutClear
+ tooltip={i18n.str`Time for the customer to pay for the offer before it expires. Inventory products will be reserved until this deadline. Time start to run after the order is created.`}
side={
<span>
<button class="button" onClick={() => {
@@ -543,10 +521,13 @@ export function CreatePage({
</span>
}
/>}
- {settings.advanceOrderMode && <InputDate
+ {settings.advanceOrderMode && <InputDuration
name="payments.refund_deadline"
- label={i18n.str`Refund deadline`}
- tooltip={i18n.str`Time until which the order can be refunded by the merchant.`}
+ label={i18n.str`Refund time`}
+ help={whenRefund && whenRefund.t_ms !== "never" ? i18n.str`Deadline at ${format(whenRefund.t_ms, "dd/MM/yy HH:mm")}` : i18n.str`Without deadline`}
+ withForever
+ withoutClear
+ tooltip={i18n.str`Time while the order can be refunded by the merchant. Time starts after the order is created.`}
side={
<span>
<button class="button" onClick={() => {
@@ -563,10 +544,13 @@ export function CreatePage({
</span>
}
/>}
- {(settings.advanceOrderMode || noDefault_wireDeadline) && <InputDate
+ {(settings.advanceOrderMode || noDefault_wireDeadline) && <InputDuration
name="payments.wire_transfer_deadline"
- label={i18n.str`Wire transfer deadline`}
- tooltip={i18n.str`Deadline for the exchange to make the wire transfer.`}
+ label={i18n.str`Wire transfer time`}
+ help={whenWire && whenWire.t_ms !== "never" ? i18n.str`Deadline at ${format(whenWire.t_ms, "dd/MM/yy HH:mm")}` : i18n.str`Without deadline`}
+ withoutClear
+ withForever
+ tooltip={i18n.str`Time for the exchange to make the wire transfer. Time starts after the order is created.`}
side={
<span>
<button class="button" onClick={() => {
@@ -583,10 +567,12 @@ export function CreatePage({
</span>
}
/>}
- {settings.advanceOrderMode && <InputDate
+ {settings.advanceOrderMode && <InputDuration
name="payments.auto_refund_deadline"
- label={i18n.str`Auto-refund deadline`}
+ label={i18n.str`Auto-refund interval`}
+ help={whenAutoRefund && whenAutoRefund.t_ms !== "never" ? i18n.str`Deadline at ${format(whenAutoRefund.t_ms, "dd/MM/yy HH:mm")}` : i18n.str`Without deadline`}
tooltip={i18n.str`Time until which the wallet will automatically check for refunds without user interaction.`}
+ withForever
/>}
{settings.advanceOrderMode && <InputCurrency
@@ -703,3 +689,5 @@ function asProduct(p: ProductAndQuantity): MerchantBackend.Product {
minimum_age: p.product.minimum_age,
};
}
+
+