@tallyui/core 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import { J as CommandError, aA as OrderCreatePayload, H as CommandEnvelope, bn as RegisterCommandEnvelope, A as AnyCommandEnvelope, K as CommandResult, B as BatchTooLargeBody, G as CommandBatchResponse, C as CommandWarning, bo as RegisterCommandResult, aM as OrderRefundRejectionCode, aS as OrderReopenRejectionCode, aZ as OrderUpdateRejectionCode, aa as DisplaySignalling, b as SyncContext, ab as DisplaySignallingContext } from '../refund-share-BZ_Ff9Zk.js';
2
- export { N as CommandResultError, ae as EarlierRefund, aj as MAX_COMMANDS_PER_BATCH, ak as NEEDS_ADMIN_MESSAGE_MAX, ao as ORDER_NOTE_MAX, aw as OrderCreateDisplay, aD as OrderCreateTaxRate, bb as PricedRefund, bc as PricedRefundLine, bi as RefundableLine, bj as RefundableOrderLine, bz as RoundingMode, cs as currencyDecimals, c1 as deriveSessionFigures, c2 as deriveVariance, ct as majorToMinor, c7 as minorToMajor, c8 as minorUnitDigits, cf as parseCommandResult, ck as priceRefund, cl as ratePpmFromPercent, cm as refundableLines, cq as roundRatio } from '../refund-share-BZ_Ff9Zk.js';
1
+ import { J as CommandError, aA as OrderCreatePayload, H as CommandEnvelope, bo as RegisterCommandEnvelope, A as AnyCommandEnvelope, K as CommandResult, B as BatchTooLargeBody, G as CommandBatchResponse, C as CommandWarning, bp as RegisterCommandResult, aM as OrderRefundRejectionCode, aS as OrderReopenRejectionCode, aZ as OrderUpdateRejectionCode, aa as DisplaySignalling, b as SyncContext, ab as DisplaySignallingContext } from '../refund-share-Cv_TgE1p.js';
2
+ export { N as CommandResultError, ae as EarlierRefund, aj as MAX_COMMANDS_PER_BATCH, ak as NEEDS_ADMIN_MESSAGE_MAX, ao as ORDER_NOTE_MAX, aw as OrderCreateDisplay, aD as OrderCreateTaxRate, bb as PricedRefund, bc as PricedRefundLine, bi as RefundableLine, bj as RefundableOrderLine, bA as RoundingMode, cy as currencyDecimals, c7 as deriveSessionFigures, c8 as deriveVariance, cz as majorToMinor, cd as minorToMajor, ce as minorUnitDigits, cl as parseCommandResult, cq as priceRefund, cr as ratePpmFromPercent, cs as refundableLines, cw as roundRatio } from '../refund-share-Cv_TgE1p.js';
3
3
  import 'rxdb';
4
4
  import 'rxjs';
5
5
 
@@ -99,8 +99,10 @@ declare function validateBatch(body: unknown): {
99
99
  * claim, in the executor (order.create; register commands follow ADR-068). It pushes any result
100
100
  * one of those steps returns and moves on.
101
101
  *
102
- * For order.update the shape check is updatePayloadErrors (ADR-090): it adds the update's store ids, and below
103
- * createVersion 5 precheckCommand does not repeat it.
102
+ * For order.update the shape check is updatePayloadErrors (ADR-090): it adds the update's store ids; for order.refund
103
+ * refundPayloadErrors, for order.reopen reopenPayloadErrors, for a register command registerPayloadErrors.
104
+ * precheckCommand repeats the shape check for every command type at every version, after the version answer, as a
105
+ * backstop (#1344, #1346): for order.update's create part too.
104
106
  *
105
107
  * The replay lookup goes before precheckCommand so an already-applied command always replays
106
108
  * as `duplicate`, even once a later @tallyui/core tightens what precheckCommand accepts.
@@ -503,6 +503,224 @@ function shapeErrors() {
503
503
  return { errors, check: check2, noNul };
504
504
  }
505
505
 
506
+ // src/server/refund-payload-shape.ts
507
+ function refundPayloadErrors(payload) {
508
+ const { errors, check: check2, noNul } = shapeErrors();
509
+ if (!isPlainObject(payload)) return ["payload: expected an object"];
510
+ const fields = [
511
+ "clientRefundId",
512
+ "orderId",
513
+ "registerId",
514
+ "sessionId",
515
+ "clientOrderId",
516
+ "cashierRef",
517
+ "lines",
518
+ "shippingMinor",
519
+ "totalMinor",
520
+ "adjustmentMinor",
521
+ "destination",
522
+ "reason",
523
+ "createdAt"
524
+ ];
525
+ for (const key of Object.keys(payload)) {
526
+ if (!fields.includes(key) && errors.length < 10) {
527
+ errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
528
+ }
529
+ }
530
+ for (const field of ["clientRefundId", "orderId", "registerId", "sessionId", "clientOrderId", "cashierRef"]) {
531
+ const value = payload[field];
532
+ if (["clientOrderId", "cashierRef"].includes(field) && value === void 0) continue;
533
+ check2(
534
+ typeof value === "string" && value.length > 0 && value.length <= 64,
535
+ field,
536
+ "a non-empty string of at most 64 characters"
537
+ );
538
+ noNul(value, field);
539
+ }
540
+ check2(Array.isArray(payload.lines) && payload.lines.length <= 500, "lines", "an array of at most 500 lines");
541
+ const seen = /* @__PURE__ */ new Set();
542
+ if (Array.isArray(payload.lines)) payload.lines.forEach((line, index) => {
543
+ const path = `lines[${index}]`;
544
+ if (!isPlainObject(line)) {
545
+ check2(false, path, "an object");
546
+ return;
547
+ }
548
+ for (const key of Object.keys(line)) {
549
+ if (!["orderLineId", "quantity", "restock"].includes(key) && errors.length < 10) {
550
+ errors.push(`${path}.${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
551
+ }
552
+ }
553
+ check2(
554
+ typeof line.orderLineId === "string" && line.orderLineId.length > 0 && line.orderLineId.length <= 64,
555
+ `${path}.orderLineId`,
556
+ "a non-empty string of at most 64 characters"
557
+ );
558
+ noNul(line.orderLineId, `${path}.orderLineId`);
559
+ if (typeof line.orderLineId === "string") {
560
+ check2(!seen.has(line.orderLineId), `${path}.orderLineId`, "unique in lines");
561
+ seen.add(line.orderLineId);
562
+ }
563
+ check2(Number.isSafeInteger(line.quantity) && line.quantity >= 1, `${path}.quantity`, "a safe integer >= 1");
564
+ check2(typeof line.restock === "boolean", `${path}.restock`, "a boolean");
565
+ });
566
+ for (const field of ["shippingMinor", "totalMinor"]) {
567
+ check2(Number.isSafeInteger(payload[field]) && payload[field] >= 0, field, "a safe integer >= 0");
568
+ }
569
+ check2(Number.isSafeInteger(payload.adjustmentMinor), "adjustmentMinor", "a safe integer");
570
+ check2(payload.destination === "original_method" || payload.destination === "cash", "destination", "original_method or cash");
571
+ noNul(payload.destination, "destination");
572
+ check2(
573
+ typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
574
+ "reason",
575
+ "a non-empty string after trim of at most 500 characters"
576
+ );
577
+ noNul(payload.reason, "reason");
578
+ check2(
579
+ typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
580
+ "createdAt",
581
+ "a valid date"
582
+ );
583
+ noNul(payload.createdAt, "createdAt");
584
+ return errors;
585
+ }
586
+
587
+ // src/server/register-payload-shape.ts
588
+ function registerPayloadErrors(type, payload) {
589
+ const { errors, check: check2, noNul } = shapeErrors();
590
+ if (!isPlainObject(payload)) return ["payload: expected an object"];
591
+ const string = (field, optional = false) => {
592
+ const value = payload[field];
593
+ if (optional && value === void 0) return;
594
+ check2(typeof value === "string" && (optional || value.length > 0), field, optional ? "a string" : "a non-empty string");
595
+ if (typeof value === "string" && (field.endsWith("Id") || field === "voids")) check2(value.length <= 64, field, "at most 64 characters");
596
+ if (typeof value === "string" && ["openedAt", "at", "createdAt", "closedAt"].includes(field)) {
597
+ check2(!Number.isNaN(Date.parse(value)), field, "a valid date");
598
+ }
599
+ noNul(value, field);
600
+ };
601
+ const integer = (field, min = -Infinity) => check2(Number.isSafeInteger(payload[field]) && payload[field] >= min, field, `a safe integer >= ${min}`);
602
+ const record = (field) => {
603
+ const value = payload[field];
604
+ check2(isPlainObject(value), field, "a record of integers");
605
+ if (isPlainObject(value)) for (const [key, amount] of Object.entries(value)) {
606
+ check2(key.length > 0 && Number.isSafeInteger(amount), `${field}.${key}`, "a non-empty key with a safe integer");
607
+ noNul(key, `${field}.${JSON.stringify(key)}`);
608
+ }
609
+ };
610
+ string("sessionId");
611
+ switch (type) {
612
+ case "register.session.open":
613
+ for (const field of ["registerId", "openedAt"]) string(field);
614
+ for (const field of ["storeKey", "businessDay", "openedBy"]) string(field, true);
615
+ if (payload.deviceName !== void 0) {
616
+ check2(
617
+ typeof payload.deviceName === "string" && payload.deviceName.trim().length >= 1 && payload.deviceName.trim().length <= 64,
618
+ "deviceName",
619
+ "a string of 1 to 64 characters after trim"
620
+ );
621
+ noNul(payload.deviceName, "deviceName");
622
+ }
623
+ if (payload.supersedes !== void 0) {
624
+ check2(
625
+ typeof payload.supersedes === "string" && payload.supersedes.length > 0 && payload.supersedes.length <= 64,
626
+ "supersedes",
627
+ "a non-empty string of at most 64 characters"
628
+ );
629
+ noNul(payload.supersedes, "supersedes");
630
+ }
631
+ integer("countedFloatMinor", 0);
632
+ for (const field of ["expectedFloatMinor", "openingVarianceMinor"]) if (payload[field] !== void 0) integer(field);
633
+ break;
634
+ case "register.session.transition":
635
+ string("at");
636
+ check2(["open", "counting", "closed"].includes(payload.status), "status", "open, counting or closed");
637
+ for (const field of ["counted", "closedBy", "approvedBy"]) if (payload[field] !== void 0) {
638
+ check2(payload.status === "closed", field, "a closing transition");
639
+ if (field === "counted") record(field);
640
+ else string(field, true);
641
+ }
642
+ break;
643
+ case "register.movement.record":
644
+ case "register.movement.void":
645
+ for (const field of ["movementId", "createdAt"]) string(field);
646
+ string("createdBy", true);
647
+ if (type === "register.movement.void") string("voids");
648
+ else {
649
+ check2(
650
+ typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
651
+ "reason",
652
+ "a non-empty string after trim of at most 500 characters"
653
+ );
654
+ noNul(payload.reason, "reason");
655
+ check2(["paid_in", "paid_out", "no_sale"].includes(payload.type), "type", "paid_in, paid_out or no_sale");
656
+ integer("amountMinor", payload.type === "no_sale" ? 0 : 1);
657
+ if (payload.type === "no_sale") check2(payload.amountMinor === 0, "amountMinor", "0 for no_sale");
658
+ }
659
+ break;
660
+ case "register.closure.submit":
661
+ for (const field of ["closureId", "registerId", "openedAt", "closedAt", "softwareVersion"]) string(field);
662
+ for (const field of ["businessDay", "closedBy", "approvedBy"]) string(field, true);
663
+ if (payload.approval !== void 0) {
664
+ check2(typeof payload.approval === "string" && payload.approval.length > 0, "approval", "a non-empty string");
665
+ noNul(payload.approval, "approval");
666
+ }
667
+ integer("number", 1);
668
+ for (const field of [
669
+ "periodSalesTotalMinor",
670
+ "periodRefundsTotalMinor",
671
+ "perpetualSalesTotalMinor",
672
+ "perpetualRefundsTotalMinor",
673
+ "unsyncedCount",
674
+ "unsyncedTotalMinor"
675
+ ]) integer(field, 0);
676
+ for (const field of ["number", "unsyncedCount"])
677
+ check2(payload[field] <= 2147483647, field, "at most 2147483647");
678
+ for (const field of ["tillExpected", "counted"]) record(field);
679
+ for (const field of ["orderIds", "movementIds"]) {
680
+ const value = payload[field];
681
+ check2(Array.isArray(value), field, "an array of ids");
682
+ if (Array.isArray(value)) value.forEach((id, index) => {
683
+ check2(typeof id === "string" && id.length > 0 && id.length <= 64, `${field}[${index}]`, "a non-empty string of at most 64 characters");
684
+ noNul(id, `${field}[${index}]`);
685
+ });
686
+ }
687
+ break;
688
+ default:
689
+ check2(false, "type", "a register command type");
690
+ }
691
+ return errors;
692
+ }
693
+
694
+ // src/server/reopen-payload-shape.ts
695
+ function reopenPayloadErrors(payload) {
696
+ const { errors, check: check2, noNul } = shapeErrors();
697
+ if (!isPlainObject(payload)) return ["payload: expected an object"];
698
+ const fields = ["orderId", "clientOrderId", "baseRevision", "cashierRef", "registerId", "storeId", "createdAt"];
699
+ for (const key of Object.keys(payload)) {
700
+ if (!fields.includes(key) && errors.length < 10) {
701
+ errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.reopen version 1`);
702
+ }
703
+ }
704
+ for (const field of ["orderId", "cashierRef", "registerId", "clientOrderId", "baseRevision", "storeId"]) {
705
+ const value = payload[field];
706
+ if (["clientOrderId", "baseRevision", "storeId"].includes(field) && value === void 0) continue;
707
+ const max = field === "baseRevision" ? 128 : 64;
708
+ check2(
709
+ typeof value === "string" && value.length > 0 && value.length <= max,
710
+ field,
711
+ `a non-empty string of at most ${max} characters`
712
+ );
713
+ noNul(value, field);
714
+ }
715
+ check2(
716
+ typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
717
+ "createdAt",
718
+ "a valid date"
719
+ );
720
+ noNul(payload.createdAt, "createdAt");
721
+ return errors;
722
+ }
723
+
506
724
  // src/server/update-payload-shape.ts
507
725
  function updateOnlyErrors(payload) {
508
726
  const { errors, check: check2 } = shapeErrors();
@@ -589,6 +807,7 @@ function validateBatch(body) {
589
807
  }
590
808
  var rejected = (id, code, message, data) => ({ id, status: "rejected", error: data === void 0 ? { code, message } : { code, message, data } });
591
809
  var firstTen = (errors) => errors.slice(0, 10).join("; ");
810
+ var shapeRefusal = (id, errors) => errors.length ? rejected(id, "invalid_payload", firstTen(errors)) : void 0;
592
811
  function unsupportedOptional(envelope, key, versions, noun) {
593
812
  if (versions.includes(envelope.version)) return void 0;
594
813
  return rejected(
@@ -602,8 +821,12 @@ function precheckCommand(envelope, supported) {
602
821
  for (const key of ["orderCreate", "register"]) {
603
822
  if (!supported[key]?.length) throw new TypeError(`precheckCommand: supported.${key} must list at least one version`);
604
823
  }
605
- if (envelope.type === "order.refund") return unsupportedOptional(envelope, "orderRefund", supported.orderRefund ?? [], "refunds");
606
- if (envelope.type === "order.reopen") return unsupportedOptional(envelope, "orderReopen", supported.orderReopen ?? [], "reopens");
824
+ if (envelope.type === "order.refund") {
825
+ return unsupportedOptional(envelope, "orderRefund", supported.orderRefund ?? [], "refunds") ?? shapeRefusal(envelope.id, refundPayloadErrors(envelope.payload));
826
+ }
827
+ if (envelope.type === "order.reopen") {
828
+ return unsupportedOptional(envelope, "orderReopen", supported.orderReopen ?? [], "reopens") ?? shapeRefusal(envelope.id, reopenPayloadErrors(envelope.payload));
829
+ }
607
830
  if (envelope.type === "order.update") {
608
831
  const refusal = unsupportedOptional(envelope, "orderUpdate", supported.orderUpdate ?? [], "updates");
609
832
  if (refusal) return refusal;
@@ -623,7 +846,7 @@ function precheckCommand(envelope, supported) {
623
846
  { register: Math.max(...supported.register) }
624
847
  );
625
848
  }
626
- return void 0;
849
+ return shapeRefusal(envelope.id, registerPayloadErrors(envelope.type, envelope.payload));
627
850
  }
628
851
  const command = envelope;
629
852
  if (!supported.orderCreate.includes(command.version)) {
@@ -680,7 +903,7 @@ function precheckCommand(envelope, supported) {
680
903
  return rejected(command.id, "invalid_payload", firstTen(bounds));
681
904
  }
682
905
  const shape = payloadShapeErrors(payload);
683
- const errors = command.version >= 5 && shape.length ? shape : v3 && display !== void 0 && taxByRate !== void 0 && shape.length === 0 ? fiscalFiguresErrors(payload) : [];
906
+ const errors = shape.length ? shape : v3 && display !== void 0 && taxByRate !== void 0 ? fiscalFiguresErrors(payload) : [];
684
907
  if (errors.length) {
685
908
  return rejected(command.id, "invalid_payload", firstTen(errors));
686
909
  }
@@ -703,224 +926,6 @@ function totalWarnings(expectedMinor, serverMinor) {
703
926
  return serverMinor === expectedMinor ? [] : [{ code: "total_mismatch", expectedMinor, serverMinor }];
704
927
  }
705
928
 
706
- // src/server/register-payload-shape.ts
707
- function registerPayloadErrors(type, payload) {
708
- const { errors, check: check2, noNul } = shapeErrors();
709
- if (!isPlainObject(payload)) return ["payload: expected an object"];
710
- const string = (field, optional = false) => {
711
- const value = payload[field];
712
- if (optional && value === void 0) return;
713
- check2(typeof value === "string" && (optional || value.length > 0), field, optional ? "a string" : "a non-empty string");
714
- if (typeof value === "string" && (field.endsWith("Id") || field === "voids")) check2(value.length <= 64, field, "at most 64 characters");
715
- if (typeof value === "string" && ["openedAt", "at", "createdAt", "closedAt"].includes(field)) {
716
- check2(!Number.isNaN(Date.parse(value)), field, "a valid date");
717
- }
718
- noNul(value, field);
719
- };
720
- const integer = (field, min = -Infinity) => check2(Number.isSafeInteger(payload[field]) && payload[field] >= min, field, `a safe integer >= ${min}`);
721
- const record = (field) => {
722
- const value = payload[field];
723
- check2(isPlainObject(value), field, "a record of integers");
724
- if (isPlainObject(value)) for (const [key, amount] of Object.entries(value)) {
725
- check2(key.length > 0 && Number.isSafeInteger(amount), `${field}.${key}`, "a non-empty key with a safe integer");
726
- noNul(key, `${field}.${JSON.stringify(key)}`);
727
- }
728
- };
729
- string("sessionId");
730
- switch (type) {
731
- case "register.session.open":
732
- for (const field of ["registerId", "openedAt"]) string(field);
733
- for (const field of ["storeKey", "businessDay", "openedBy"]) string(field, true);
734
- if (payload.deviceName !== void 0) {
735
- check2(
736
- typeof payload.deviceName === "string" && payload.deviceName.trim().length >= 1 && payload.deviceName.trim().length <= 64,
737
- "deviceName",
738
- "a string of 1 to 64 characters after trim"
739
- );
740
- noNul(payload.deviceName, "deviceName");
741
- }
742
- if (payload.supersedes !== void 0) {
743
- check2(
744
- typeof payload.supersedes === "string" && payload.supersedes.length > 0 && payload.supersedes.length <= 64,
745
- "supersedes",
746
- "a non-empty string of at most 64 characters"
747
- );
748
- noNul(payload.supersedes, "supersedes");
749
- }
750
- integer("countedFloatMinor", 0);
751
- for (const field of ["expectedFloatMinor", "openingVarianceMinor"]) if (payload[field] !== void 0) integer(field);
752
- break;
753
- case "register.session.transition":
754
- string("at");
755
- check2(["open", "counting", "closed"].includes(payload.status), "status", "open, counting or closed");
756
- for (const field of ["counted", "closedBy", "approvedBy"]) if (payload[field] !== void 0) {
757
- check2(payload.status === "closed", field, "a closing transition");
758
- if (field === "counted") record(field);
759
- else string(field, true);
760
- }
761
- break;
762
- case "register.movement.record":
763
- case "register.movement.void":
764
- for (const field of ["movementId", "createdAt"]) string(field);
765
- string("createdBy", true);
766
- if (type === "register.movement.void") string("voids");
767
- else {
768
- check2(
769
- typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
770
- "reason",
771
- "a non-empty string after trim of at most 500 characters"
772
- );
773
- noNul(payload.reason, "reason");
774
- check2(["paid_in", "paid_out", "no_sale"].includes(payload.type), "type", "paid_in, paid_out or no_sale");
775
- integer("amountMinor", payload.type === "no_sale" ? 0 : 1);
776
- if (payload.type === "no_sale") check2(payload.amountMinor === 0, "amountMinor", "0 for no_sale");
777
- }
778
- break;
779
- case "register.closure.submit":
780
- for (const field of ["closureId", "registerId", "openedAt", "closedAt", "softwareVersion"]) string(field);
781
- for (const field of ["businessDay", "closedBy", "approvedBy"]) string(field, true);
782
- if (payload.approval !== void 0) {
783
- check2(typeof payload.approval === "string" && payload.approval.length > 0, "approval", "a non-empty string");
784
- noNul(payload.approval, "approval");
785
- }
786
- integer("number", 1);
787
- for (const field of [
788
- "periodSalesTotalMinor",
789
- "periodRefundsTotalMinor",
790
- "perpetualSalesTotalMinor",
791
- "perpetualRefundsTotalMinor",
792
- "unsyncedCount",
793
- "unsyncedTotalMinor"
794
- ]) integer(field, 0);
795
- for (const field of ["number", "unsyncedCount"])
796
- check2(payload[field] <= 2147483647, field, "at most 2147483647");
797
- for (const field of ["tillExpected", "counted"]) record(field);
798
- for (const field of ["orderIds", "movementIds"]) {
799
- const value = payload[field];
800
- check2(Array.isArray(value), field, "an array of ids");
801
- if (Array.isArray(value)) value.forEach((id, index) => {
802
- check2(typeof id === "string" && id.length > 0 && id.length <= 64, `${field}[${index}]`, "a non-empty string of at most 64 characters");
803
- noNul(id, `${field}[${index}]`);
804
- });
805
- }
806
- break;
807
- default:
808
- check2(false, "type", "a register command type");
809
- }
810
- return errors;
811
- }
812
-
813
- // src/server/refund-payload-shape.ts
814
- function refundPayloadErrors(payload) {
815
- const { errors, check: check2, noNul } = shapeErrors();
816
- if (!isPlainObject(payload)) return ["payload: expected an object"];
817
- const fields = [
818
- "clientRefundId",
819
- "orderId",
820
- "registerId",
821
- "sessionId",
822
- "clientOrderId",
823
- "cashierRef",
824
- "lines",
825
- "shippingMinor",
826
- "totalMinor",
827
- "adjustmentMinor",
828
- "destination",
829
- "reason",
830
- "createdAt"
831
- ];
832
- for (const key of Object.keys(payload)) {
833
- if (!fields.includes(key) && errors.length < 10) {
834
- errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
835
- }
836
- }
837
- for (const field of ["clientRefundId", "orderId", "registerId", "sessionId", "clientOrderId", "cashierRef"]) {
838
- const value = payload[field];
839
- if (["clientOrderId", "cashierRef"].includes(field) && value === void 0) continue;
840
- check2(
841
- typeof value === "string" && value.length > 0 && value.length <= 64,
842
- field,
843
- "a non-empty string of at most 64 characters"
844
- );
845
- noNul(value, field);
846
- }
847
- check2(Array.isArray(payload.lines) && payload.lines.length <= 500, "lines", "an array of at most 500 lines");
848
- const seen = /* @__PURE__ */ new Set();
849
- if (Array.isArray(payload.lines)) payload.lines.forEach((line, index) => {
850
- const path = `lines[${index}]`;
851
- if (!isPlainObject(line)) {
852
- check2(false, path, "an object");
853
- return;
854
- }
855
- for (const key of Object.keys(line)) {
856
- if (!["orderLineId", "quantity", "restock"].includes(key) && errors.length < 10) {
857
- errors.push(`${path}.${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
858
- }
859
- }
860
- check2(
861
- typeof line.orderLineId === "string" && line.orderLineId.length > 0 && line.orderLineId.length <= 64,
862
- `${path}.orderLineId`,
863
- "a non-empty string of at most 64 characters"
864
- );
865
- noNul(line.orderLineId, `${path}.orderLineId`);
866
- if (typeof line.orderLineId === "string") {
867
- check2(!seen.has(line.orderLineId), `${path}.orderLineId`, "unique in lines");
868
- seen.add(line.orderLineId);
869
- }
870
- check2(Number.isSafeInteger(line.quantity) && line.quantity >= 1, `${path}.quantity`, "a safe integer >= 1");
871
- check2(typeof line.restock === "boolean", `${path}.restock`, "a boolean");
872
- });
873
- for (const field of ["shippingMinor", "totalMinor"]) {
874
- check2(Number.isSafeInteger(payload[field]) && payload[field] >= 0, field, "a safe integer >= 0");
875
- }
876
- check2(Number.isSafeInteger(payload.adjustmentMinor), "adjustmentMinor", "a safe integer");
877
- check2(payload.destination === "original_method" || payload.destination === "cash", "destination", "original_method or cash");
878
- noNul(payload.destination, "destination");
879
- check2(
880
- typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
881
- "reason",
882
- "a non-empty string after trim of at most 500 characters"
883
- );
884
- noNul(payload.reason, "reason");
885
- check2(
886
- typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
887
- "createdAt",
888
- "a valid date"
889
- );
890
- noNul(payload.createdAt, "createdAt");
891
- return errors;
892
- }
893
-
894
- // src/server/reopen-payload-shape.ts
895
- function reopenPayloadErrors(payload) {
896
- const { errors, check: check2, noNul } = shapeErrors();
897
- if (!isPlainObject(payload)) return ["payload: expected an object"];
898
- const fields = ["orderId", "clientOrderId", "baseRevision", "cashierRef", "registerId", "storeId", "createdAt"];
899
- for (const key of Object.keys(payload)) {
900
- if (!fields.includes(key) && errors.length < 10) {
901
- errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.reopen version 1`);
902
- }
903
- }
904
- for (const field of ["orderId", "cashierRef", "registerId", "clientOrderId", "baseRevision", "storeId"]) {
905
- const value = payload[field];
906
- if (["clientOrderId", "baseRevision", "storeId"].includes(field) && value === void 0) continue;
907
- const max = field === "baseRevision" ? 128 : 64;
908
- check2(
909
- typeof value === "string" && value.length > 0 && value.length <= max,
910
- field,
911
- `a non-empty string of at most ${max} characters`
912
- );
913
- noNul(value, field);
914
- }
915
- check2(
916
- typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
917
- "createdAt",
918
- "a valid date"
919
- );
920
- noNul(payload.createdAt, "createdAt");
921
- return errors;
922
- }
923
-
924
929
  // src/server/rejection-codes.ts
925
930
  function platformErrorResult(id, platformCode, platformMessage) {
926
931
  return { id, status: "rejected", error: { code: "platform_error", message: `${platformCode}: ${platformMessage}`, data: { platformCode, platformMessage } } };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tallyui/core",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "type": "module",
5
5
  "description": "Core interfaces, traits, and context providers for Tally UI",
6
6
  "main": "./dist/index.js",
package/src/index.ts CHANGED
@@ -30,7 +30,7 @@ export type {
30
30
  StoreTaxRate,
31
31
  StoreTaxRateLocation,
32
32
  } from './types';
33
- export type { SignInResult, ServerCapabilities, TaxRounding } from './types/connector';
33
+ export type { SignInResult, ServerCapabilities, TaxRounding, SettleOutcome, SettleCancelRequest, SettlePayment, SettlePayRequest, SettlePayResult, RefusedOrderSettlement } from './types/connector';
34
34
  export type {
35
35
  DisplaySignalling, DisplaySignallingContext, DisplaySignallingErrorCode, DisplayAdvertisement, IceServer,
36
36
  PendingPairing, DisplayRecord, SignalMessage, TillDevice,
@@ -2,6 +2,9 @@ import { MAX_COMMANDS_PER_BATCH } from '../commands'
2
2
  import type { AnyCommandEnvelope, BatchTooLargeBody, CommandEnvelope, CommandResult, OrderCreatePayload, RegisterCommandEnvelope } from '../types'
3
3
  import { fiscalFiguresErrors } from './fiscal-figures'
4
4
  import { payloadBoundErrors, payloadShapeErrors } from './order-payload-shape'
5
+ import { refundPayloadErrors } from './refund-payload-shape'
6
+ import { registerPayloadErrors } from './register-payload-shape'
7
+ import { reopenPayloadErrors } from './reopen-payload-shape'
5
8
  import { orderCreatePart, updatePayloadErrors } from './update-payload-shape'
6
9
 
7
10
  /** An envelope validateBatch accepted: every field's shape is checked, and its version is any positive
@@ -51,6 +54,8 @@ const rejected = (id: string, code: 'unsupported_version' | 'invalid_payload', m
51
54
  ({ id, status: 'rejected', error: data === undefined ? { code, message } : { code, message, data } })
52
55
  // A server lists at most ten payload errors in one message.
53
56
  const firstTen = (errors: readonly string[]) => errors.slice(0, 10).join('; ')
57
+ // The `invalid_payload` result for a shape function's errors, or undefined when there are none.
58
+ const shapeRefusal = (id: string, errors: readonly string[]) => errors.length ? rejected(id, 'invalid_payload', firstTen(errors)) : undefined
54
59
 
55
60
  /** The `unsupported_version` result for a command whose version list the server may leave out (absent or empty: it takes none). */
56
61
  function unsupportedOptional(envelope: { id: string; type: string; version: number },
@@ -71,8 +76,10 @@ function unsupportedOptional(envelope: { id: string; type: string; version: numb
71
76
  * claim, in the executor (order.create; register commands follow ADR-068). It pushes any result
72
77
  * one of those steps returns and moves on.
73
78
  *
74
- * For order.update the shape check is updatePayloadErrors (ADR-090): it adds the update's store ids, and below
75
- * createVersion 5 precheckCommand does not repeat it.
79
+ * For order.update the shape check is updatePayloadErrors (ADR-090): it adds the update's store ids; for order.refund
80
+ * refundPayloadErrors, for order.reopen reopenPayloadErrors, for a register command registerPayloadErrors.
81
+ * precheckCommand repeats the shape check for every command type at every version, after the version answer, as a
82
+ * backstop (#1344, #1346): for order.update's create part too.
76
83
  *
77
84
  * The replay lookup goes before precheckCommand so an already-applied command always replays
78
85
  * as `duplicate`, even once a later @tallyui/core tightens what precheckCommand accepts.
@@ -89,8 +96,14 @@ export function precheckCommand(envelope: Pick<AnyCommandEnvelope | ValidatedCom
89
96
  for (const key of ['orderCreate', 'register'] as const) {
90
97
  if (!supported[key]?.length) throw new TypeError(`precheckCommand: supported.${key} must list at least one version`)
91
98
  }
92
- if (envelope.type === 'order.refund') return unsupportedOptional(envelope, 'orderRefund', supported.orderRefund ?? [], 'refunds')
93
- if (envelope.type === 'order.reopen') return unsupportedOptional(envelope, 'orderReopen', supported.orderReopen ?? [], 'reopens')
99
+ if (envelope.type === 'order.refund') {
100
+ return unsupportedOptional(envelope, 'orderRefund', supported.orderRefund ?? [], 'refunds')
101
+ ?? shapeRefusal(envelope.id, refundPayloadErrors(envelope.payload))
102
+ }
103
+ if (envelope.type === 'order.reopen') {
104
+ return unsupportedOptional(envelope, 'orderReopen', supported.orderReopen ?? [], 'reopens')
105
+ ?? shapeRefusal(envelope.id, reopenPayloadErrors(envelope.payload))
106
+ }
94
107
  if (envelope.type === 'order.update') {
95
108
  const refusal = unsupportedOptional(envelope, 'orderUpdate', supported.orderUpdate ?? [], 'updates')
96
109
  if (refusal) return refusal
@@ -107,7 +120,7 @@ export function precheckCommand(envelope: Pick<AnyCommandEnvelope | ValidatedCom
107
120
  `register version ${envelope.version} is not supported; this server supports ${supported.register.join(', ')}`,
108
121
  { register: Math.max(...supported.register) })
109
122
  }
110
- return undefined
123
+ return shapeRefusal(envelope.id, registerPayloadErrors(envelope.type, envelope.payload))
111
124
  }
112
125
  const command = envelope as CommandEnvelope<OrderCreatePayload>
113
126
  if (!supported.orderCreate.includes(command.version)) {
@@ -183,8 +196,8 @@ export function precheckCommand(envelope: Pick<AnyCommandEnvelope | ValidatedCom
183
196
  return rejected(command.id, 'invalid_payload', firstTen(bounds))
184
197
  }
185
198
  const shape = payloadShapeErrors(payload)
186
- const errors = command.version >= 5 && shape.length ? shape
187
- : v3 && display !== undefined && taxByRate !== undefined && shape.length === 0 ? fiscalFiguresErrors(payload) : []
199
+ const errors = shape.length ? shape
200
+ : v3 && display !== undefined && taxByRate !== undefined ? fiscalFiguresErrors(payload) : []
188
201
  if (errors.length) {
189
202
  return rejected(command.id, 'invalid_payload', firstTen(errors))
190
203
  }