@curless/hotel-storefront 0.74.1 → 0.74.3

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.
@@ -629,7 +629,68 @@ export const cancelOutcome = (out) => {
629
629
  .toUpperCase();
630
630
  if (!s)
631
631
  return 'unknown';
632
- return CANCELLED_STATUSES.has(s) ? 'done' : 'refused';
632
+ // ⚠️ A PAID ORDER CANCELLED MAY BE ANSWERED BY ITS REFUND (0.74.2): "REFUNDED" from 9 is
633
+ // a cancellation done and its money on the way back - never "it stands as it was".
634
+ return CANCELLED_STATUSES.has(s) || REFUND_STATUSES.has(s) ? 'done' : 'refused';
635
+ };
636
+ /** 9's (and 12's) statuses that say a paid order's money is going back or is back. */
637
+ export const REFUND_STATUSES = new Set([
638
+ 'REFUNDED',
639
+ 'REFUNDING',
640
+ 'REFUND_REQUESTED',
641
+ 'REFUND_PENDING',
642
+ 'REFUND_PROCESSING',
643
+ 'REFUND_SUBMITTED',
644
+ ]);
645
+ /**
646
+ * Whether 9's answer for a PAID order says its money is going back (0.74.2, robin 11-17 through
647
+ * 总线程: one Cancel, which asks the refund too): `refunded` - its status says it is back;
648
+ * `requested` - a refund status, its own words saying the refund was taken, or an amount going
649
+ * back above nothing; undefined - none of these, so 9.1 is asked in the same cancel. `by` says
650
+ * which: ⚠️ AN AMOUNT ALONE IS TAKEN, AND CHECKED (0.74.3, 总线程's review of 0.74.2, P2-3) - the
651
+ * document has 9 answer `refundAmount` anyway, so whether 9 refunds by itself is questions #51;
652
+ * until it is answered such a cancel is logged CHECK and counted (`refundAssumed`).
653
+ *
654
+ * ⚠️ WORDS THAT REFUSE A REFUND ARE NOT ONE: 「退款申请失败」 has 「退款申请」 in it - and with them,
655
+ * an amount says nothing either.
656
+ */
657
+ export const cancelRefundSaid = (out) => {
658
+ const s = String(out?.status ?? '')
659
+ .trim()
660
+ .toUpperCase();
661
+ if (s === 'REFUNDED')
662
+ return { state: 'refunded', by: 'status' };
663
+ if (REFUND_STATUSES.has(s))
664
+ return { state: 'requested', by: 'status' };
665
+ const msg = String(out?.message ?? '');
666
+ if (/退款[^。.!!,,;;]{0,4}(失败|未|不|没有|无法|拒)|(无需|不予|不能|无法|未|没有?)(申请)?退款|(不|无法|不能|不会)(会)?(原路)?退(回|还|款)|\brefund(s|ed)?\b[^.,;]{0,20}\b(fail(ed)?|not|unable|denied|rejected|error)\b|\bno refund\b/i.test(msg))
667
+ return undefined;
668
+ if (/已(经)?(全额)?退款|退款(申请)?(已|已经)?(受理|提交|申请成功|发起|处理中|成功)|退款(处理)?中|已(申请|发起|提交)(了)?退款|(将|会)(会)?(全额)?原路退(回|还)|原路退(回|还)|\brefund(s|ed)?\b[^.]{0,30}\b(accepted|requested|submitted|initiated|processing|pending|issued|completed|on its way)\b|\b(was|been|is) refunded\b/i.test(msg))
669
+ return { state: 'requested', by: 'words' };
670
+ const n = Number(out?.refundAmount);
671
+ if (Number.isFinite(n) && n > 0)
672
+ return { state: 'requested', by: 'amount' };
673
+ return undefined;
674
+ };
675
+ /**
676
+ * What 9.1's answer says of the refund (0.74.3, 总线程's review of 0.74.2, P3): `sent` - taken
677
+ * (its status may say it is back already: `refunded`); `not-sent` - refused (`success` false, or
678
+ * a status that says it failed); `unsure` - an answer that says nothing at all, which is not a
679
+ * refund taken.
680
+ */
681
+ export const refundOutcome = (out) => {
682
+ if (out?.success === false)
683
+ return 'not-sent';
684
+ const s = String(out?.status ?? '')
685
+ .trim()
686
+ .toUpperCase();
687
+ if (/FAIL|REJECT|DENIED|DECLINED|REFUSED|ERROR|INVALID/.test(s))
688
+ return 'not-sent';
689
+ if (s === 'REFUNDED')
690
+ return 'refunded';
691
+ if (!s && out?.success !== true)
692
+ return 'unsure';
693
+ return 'sent';
633
694
  };
634
695
  /**
635
696
  * The backend refusing because the guest's membership of that group is not linked - or its
@@ -685,6 +746,12 @@ export const createOtalessBookings = (deps) => {
685
746
  termsSent: 0,
686
747
  termsAgreed: 0,
687
748
  sessionRetried: 0,
749
+ /** Cancelled, and the refund the cancel asked (9.1) not taken - a person takes it up (0.74.2). */
750
+ refundNotSent: 0,
751
+ /** …or not answered: whether it was sent is not known (0.74.3). */
752
+ refundUnsure: 0,
753
+ /** Cancelled, and 9's `refundAmount` alone taken as its refund asked - 9.1 not sent (0.74.3, #51). */
754
+ refundAssumed: 0,
688
755
  };
689
756
  const keyOf = (who) => who.tripOwner ?? who.sessionId;
690
757
  const accountFor = (who, group) => {
@@ -901,6 +968,12 @@ export const createOtalessBookings = (deps) => {
901
968
  ...(r.refund.amount && !same(r.refund.amount, amount)
902
969
  ? { refundAmount: r.refund.amount }
903
970
  : {}),
971
+ // 0.74.2: the cancel's 9.1 not taken - said as held up, a person seeing to it.
972
+ ...(r.refund.status === 'not-sent' && !r.refund.asking ? { refundStuck: true } : {}),
973
+ // 0.74.3: not answered (or still being asked) - whether it was sent is being checked.
974
+ ...(r.refund.status === 'unsure' || r.refund.asking ? { refundUnsure: true } : {}),
975
+ // Asked of the group, which reviews it before it goes back (9.1's own document).
976
+ ...(r.refund.status !== 'refunded' ? { groupReviews: true } : {}),
904
977
  };
905
978
  if (r.paid)
906
979
  return { method: 'card', amount, status: 'paid' };
@@ -1408,6 +1481,19 @@ export const createOtalessBookings = (deps) => {
1408
1481
  for (const r of [...orders.values()])
1409
1482
  if (r.held && !r.released)
1410
1483
  holdFor(r);
1484
+ // ⚠️ A REFUND NOT SENT IS SAID AGAIN WHEN THE SERVICE STARTS (0.74.3, 总线程's review of 0.74.2,
1485
+ // P3) - and one a restart cut short (`asking` kept) is one nobody knows the fate of: `unsure`.
1486
+ for (const r of [...orders.values()]) {
1487
+ const f = r.refund;
1488
+ if (!f || (f.status !== 'not-sent' && f.status !== 'unsure'))
1489
+ continue;
1490
+ const cut = f.asking === true;
1491
+ if (cut) {
1492
+ r.refund = { ...(f.amount ? { amount: f.amount } : {}), status: 'unsure' };
1493
+ save();
1494
+ }
1495
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${r.group} is cancelled, and its refund (9.1) ${cut ? 'was being asked when the service stopped - whether it was sent is not known' : f.status === 'unsure' ? 'was not answered - whether it was sent is not known' : 'was not sent'}`);
1496
+ }
1411
1497
  /**
1412
1498
  * ⚠️ AN ORDER 8 NAMES THAT IS ONE OF OURS ALREADY IS NOT TOUCHED (总线程's
1413
1499
  * review of 0.68.2, P2): a refusal - "already booked", say - may name the
@@ -2393,7 +2479,11 @@ export const createOtalessBookings = (deps) => {
2393
2479
  terms.push(termsBefore(ask, unsaid));
2394
2480
  }
2395
2481
  // Nothing booked, nothing waiting: the sign-in or link the backend asked for is the answer.
2396
- if (stayRefusals[0] && !booked.length && !terms.length)
2482
+ // ⚠️ UNLESS ANOTHER STAY FAILED BESIDE IT (0.74.2, 总线程's review of 0.74.1, P3): thrown,
2483
+ // the refusal took that stay's failure out of the answer - an order 8 made and we could not
2484
+ // cancel (「取消没成功、需要人工、不要付款」), still in the bookings and in the way of booking it
2485
+ // again. Then every failure is the answer, the refused stay's with its groups.
2486
+ if (stayRefusals[0] && !booked.length && !terms.length && failed.length <= stayRefusals.length)
2397
2487
  throw stayRefusals[0];
2398
2488
  return {
2399
2489
  booked,
@@ -2885,11 +2975,28 @@ export const createOtalessBookings = (deps) => {
2885
2975
  const t = forTheLog(s ?? '').slice(0, REASON_MAX);
2886
2976
  return t ? t : undefined;
2887
2977
  };
2978
+ /**
2979
+ * Cancellations under way, by order id (0.74.3, 总线程's review of 0.74.2, P2-1): the card and
2980
+ * the model confirming at once, two cards, a host retrying - each reached 9 in the other's
2981
+ * round trip, and a 9 that answers again without a refund had 9.1 asked twice, the second
2982
+ * refused and the first's "submitted" turned into "not submitted". One cancel per order is
2983
+ * sent; whoever asks while it is out is answered with its outcome.
2984
+ */
2985
+ const cancelling = new Map();
2888
2986
  const cancelBooking = async (ref, who, reason) => {
2889
- // ⚠️ OURS, THIS GUEST'S, OR NOTHING IS SENT (总线程 11-10, rule 6).
2987
+ // ⚠️ OURS, THIS GUEST'S, OR NOTHING IS SENT (总线程 11-10, rule 6) - checked for every
2988
+ // caller, the one who waits on another's cancel too.
2890
2989
  const own = ownOrder(ref, who);
2891
2990
  if (!own)
2892
2991
  return null;
2992
+ const out = cancelling.get(own.r.id);
2993
+ if (out)
2994
+ return out;
2995
+ const once = cancelOnce(own, who, reason).finally(() => cancelling.delete(own.r.id));
2996
+ cancelling.set(own.r.id, once);
2997
+ return once;
2998
+ };
2999
+ const cancelOnce = async (own, who, reason) => {
2893
3000
  const { r, acct } = own;
2894
3001
  if (r.status === 'cancelled' || r.status === 'expired' || r.status === 'failed')
2895
3002
  return toBooking(r);
@@ -2915,12 +3022,85 @@ export const createOtalessBookings = (deps) => {
2915
3022
  for (const [k, p] of pending)
2916
3023
  if (p.orderId === r.id)
2917
3024
  pending.delete(k);
2918
- const back = moneyOf(out?.refundAmount, out?.refundCurrency);
2919
- if (!unpaid && r.paid)
2920
- r.refund = { status: 'requested', ...(back ? { amount: back } : {}) };
3025
+ if (!unpaid && r.paid) {
3026
+ const said = cancelRefundSaid(out);
3027
+ const back = moneyOf(out?.refundAmount, out?.refundCurrency);
3028
+ if (said || !deps.refundsOn) {
3029
+ // 9 said its money is going back (or is back) - never asked twice. Refunds off: what
3030
+ // 9 said, as it was before 0.74.2.
3031
+ r.refund = {
3032
+ status: said?.state === 'refunded' ? 'refunded' : 'requested',
3033
+ ...(back && back.minor > 0 ? { amount: back } : {}),
3034
+ };
3035
+ // ⚠️ AN AMOUNT ALONE (0.74.3, P2-3): taken as asked, and said so to whoever reads the
3036
+ // log - if 9 does not refund by itself (questions #51), nothing else would show it.
3037
+ if (said?.by === 'amount') {
3038
+ counts.refundAssumed += 1;
3039
+ log(`[otaless] CHECK: ${forTheLog(r.id)} at ${r.group} cancelled with refundAmount ${back ? `${back.currency} ${back.minor}` : String(out?.refundAmount)} and no refund status or words - taken as its refund asked, 9.1 not sent (questions #51)`);
3040
+ }
3041
+ }
3042
+ else {
3043
+ // ⚠️ ONE CANCEL, AND THE MONEY ASKED BACK IN IT (0.74.2, robin 11-17 through 总线程:
3044
+ // 「申请退款和取消有什么区别,应该留一个取消就好了」): 9 did not say the refund was taken, so
3045
+ // 9.1 is asked now, all of it, as the refund tool did. ⚠️ CANCELLED STAYS CANCELLED:
3046
+ // a 9.1 that fails is said as that - a person takes it up - never "not cancelled".
3047
+ // ⚠️ "NOT SENT" IS WRITTEN FIRST (0.74.3, P3): a restart in 9.1's round trip leaves
3048
+ // a refund still being asked, and the service says so when it starts again.
3049
+ r.refund = { status: 'not-sent', asking: true };
3050
+ put(r);
3051
+ r.refund = await askRefund(r, acct, who, why);
3052
+ }
3053
+ }
2921
3054
  put(r);
2922
3055
  return toBooking(r);
2923
3056
  };
3057
+ /**
3058
+ * 9.1 for an order just cancelled (0.74.2): what the group did with it - `requested` /
3059
+ * `refunded`; `not-sent`, refused; `unsure` (0.74.3, 总线程's review of 0.74.2, P3), not
3060
+ * answered or answered with nothing, so whether it was sent is not known. The last two are
3061
+ * logged in the backend's own words for a person to take up. Nothing thrown.
3062
+ */
3063
+ const askRefund = async (r, acct, who, why) => {
3064
+ const g = r.group;
3065
+ const person = (state, how, words) => {
3066
+ if (state === 'not-sent')
3067
+ counts.refundNotSent += 1;
3068
+ else
3069
+ counts.refundUnsure += 1;
3070
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${g} was cancelled, and its refund (9.1) ${how}: ${forTheLog(words)} - ${state === 'unsure' ? 'whether it was sent is not known; check with the group' : 'nothing goes back until a person takes it up'}`);
3071
+ return { status: state };
3072
+ };
3073
+ try {
3074
+ const out = await withSession(acct, who, (cred) => client.refund({
3075
+ unifiedOrderId: r.id,
3076
+ ...(r.code ? { booking: r.code } : {}),
3077
+ providerCode: r.group,
3078
+ ...(why ? { reason: why } : {}),
3079
+ }, cred, deps.callerOf(who)));
3080
+ const outcome = refundOutcome(out);
3081
+ const said = String(out?.message ?? out?.status ?? 'no words');
3082
+ if (outcome === 'not-sent')
3083
+ return person('not-sent', 'was refused', said);
3084
+ if (outcome === 'unsure')
3085
+ return person('unsure', 'was answered with nothing', said);
3086
+ const back = moneyOf(out?.refundAmount, out?.refundCurrency);
3087
+ return {
3088
+ status: outcome === 'refunded' ? 'refunded' : 'requested',
3089
+ ...(back && back.minor > 0 ? { amount: back } : {}),
3090
+ };
3091
+ }
3092
+ catch (err) {
3093
+ const words = err instanceof OtalessError
3094
+ ? `${err.backendCode ?? err.status ?? ''} ${String(err.answer?.msg ?? err.message)}`
3095
+ : err instanceof Error
3096
+ ? err.message
3097
+ : String(err);
3098
+ // ⚠️ NOT ANSWERED IS NOT REFUSED (0.74.3, P3): a 9.1 that timed out may have been taken.
3099
+ return unanswered(err)
3100
+ ? person('unsure', 'was not answered', words)
3101
+ : person('not-sent', 'failed', words);
3102
+ }
3103
+ };
2924
3104
  // ── refund: a preview, then the refund it previewed (9.1; 总线程's review of 0.63.0, B) ──
2925
3105
  /** Refund previews waiting to be sent, by their `preview`. */
2926
3106
  const refundPreviews = new Map();