@curless/hotel-storefront 0.74.0 → 0.74.2

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,44 @@ 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, an amount going back above nothing, or its own words saying the
649
+ * refund was taken; undefined - none of these, so 9.1 is asked in the same cancel.
650
+ *
651
+ * ⚠️ WORDS THAT REFUSE A REFUND ARE NOT ONE: 「退款申请失败」 has 「退款申请」 in it.
652
+ */
653
+ export const cancelRefundSaid = (out) => {
654
+ const s = String(out?.status ?? '')
655
+ .trim()
656
+ .toUpperCase();
657
+ if (s === 'REFUNDED')
658
+ return 'refunded';
659
+ if (REFUND_STATUSES.has(s))
660
+ return 'requested';
661
+ const n = Number(out?.refundAmount);
662
+ if (Number.isFinite(n) && n > 0)
663
+ return 'requested';
664
+ const msg = String(out?.message ?? '');
665
+ if (/退款[^。.!!,,;;]{0,4}(失败|未|不|没有|无法|拒)|(无需|不予|不能|无法|未|没有?)(申请)?退款|\brefund(s|ed)?\b[^.,;]{0,20}\b(fail(ed)?|not|unable|denied|rejected|error)\b|\bno refund\b/i.test(msg))
666
+ return undefined;
667
+ 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))
668
+ return 'requested';
669
+ return undefined;
633
670
  };
634
671
  /**
635
672
  * The backend refusing because the guest's membership of that group is not linked - or its
@@ -685,6 +722,8 @@ export const createOtalessBookings = (deps) => {
685
722
  termsSent: 0,
686
723
  termsAgreed: 0,
687
724
  sessionRetried: 0,
725
+ /** Cancelled, and the refund the cancel asked (9.1) not taken - a person takes it up (0.74.2). */
726
+ refundNotSent: 0,
688
727
  };
689
728
  const keyOf = (who) => who.tripOwner ?? who.sessionId;
690
729
  const accountFor = (who, group) => {
@@ -793,6 +832,12 @@ export const createOtalessBookings = (deps) => {
793
832
  counts.initRefused += 1;
794
833
  log(`[otaless] initialize for ${group} with the member's token was refused (code ${err.backendCode}) - questions, 11-10 evening`);
795
834
  }
835
+ // 0.74.1 (总线程's review of 0.74.0, P3): the account's sign-in over is said as that -
836
+ // and the group not linked as that - never as the group not taking the booking.
837
+ if (isCode(err, NOT_SIGNED_IN))
838
+ throw signedOut(acct);
839
+ if (unlinkedSaid(err))
840
+ throw linkFirst(acct, 'initialize said so in its own words');
796
841
  throw err;
797
842
  }
798
843
  const id = text(sent.data?.mcpSessionId) ?? sent.session;
@@ -866,6 +911,9 @@ export const createOtalessBookings = (deps) => {
866
911
  */
867
912
  const signedOut = (acct) => {
868
913
  sessions.delete(sessionKey(acct));
914
+ // ⚠️ ENDED HERE TOO (0.74.1, 总线程's review of 0.74.0, P2-2): kept signed in with a token
915
+ // the backend refuses, the guest told to sign in again was answered "already signed in".
916
+ deps.auth?.signedOut?.(acct.key);
869
917
  return signInRefusal('signed-out', `The guest's sign-in to their ${deps.brand} account is over, so nothing was done - ask them to sign in again.`, []);
870
918
  };
871
919
  // ── an order, as a booking ────────────────────────────────────────────────
@@ -892,6 +940,10 @@ export const createOtalessBookings = (deps) => {
892
940
  ...(r.refund.amount && !same(r.refund.amount, amount)
893
941
  ? { refundAmount: r.refund.amount }
894
942
  : {}),
943
+ // 0.74.2: the cancel's 9.1 not taken - said as held up, a person seeing to it.
944
+ ...(r.refund.status === 'not-sent' ? { refundStuck: true } : {}),
945
+ // Asked of the group, which reviews it before it goes back (9.1's own document).
946
+ ...(r.refund.status !== 'refunded' ? { groupReviews: true } : {}),
895
947
  };
896
948
  if (r.paid)
897
949
  return { method: 'card', amount, status: 'paid' };
@@ -1148,7 +1200,7 @@ export const createOtalessBookings = (deps) => {
1148
1200
  */
1149
1201
  const termsFrom8 = (a, w) => {
1150
1202
  const g = deps.groupOf(a.group);
1151
- const guest = [a.guest.name, a.guest.email, a.guest.phone];
1203
+ const guest = [a.guest.name, a.guest.email, a.guest.phone, a.confirmToken];
1152
1204
  const note = text(w.summary ?? undefined);
1153
1205
  if (note)
1154
1206
  log(`[otaless] booking at ${a.group}: the summary of its terms, kept to this log: "${summaryForLog(note, guest)}"`);
@@ -1420,7 +1472,7 @@ export const createOtalessBookings = (deps) => {
1420
1472
  r.stay.checkIn === a.stay.checkIn &&
1421
1473
  r.stay.checkOut === a.stay.checkOut));
1422
1474
  const untouched = (a, id, how) => {
1423
- const guest = [a.guest.name, a.guest.email, a.guest.phone];
1475
+ const guest = [a.guest.name, a.guest.email, a.guest.phone, a.confirmToken];
1424
1476
  log(`[otaless] booking at ${a.group}: 8 ${how} naming order ${forTheLog(id, guest)}, one already kept here - not touched (not cancelled, overwritten or claimed); said as a refusal`);
1425
1477
  return {
1426
1478
  failed: failedStay(a, `${deps.groupOf(a.group).name} did not take the booking, so nothing was booked or charged.`),
@@ -1442,6 +1494,8 @@ export const createOtalessBookings = (deps) => {
1442
1494
  a.guest.name,
1443
1495
  a.guest.email,
1444
1496
  a.guest.phone,
1497
+ // ⚠️ AND STEP 3's TOKEN (0.74.1): this line goes to the log as it is.
1498
+ a.confirmToken,
1445
1499
  ]),
1446
1500
  };
1447
1501
  };
@@ -1463,7 +1517,7 @@ export const createOtalessBookings = (deps) => {
1463
1517
  const r = recordOf(a, pairs, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1464
1518
  r.refusedOrder = ref;
1465
1519
  put(r);
1466
- const guest = [a.guest.name, a.guest.email, a.guest.phone];
1520
+ const guest = [a.guest.name, a.guest.email, a.guest.phone, a.confirmToken];
1467
1521
  const said = `booking at ${a.group} refused "${ref.said}" with order ${forTheLog(ref.id, guest)} in its answer`;
1468
1522
  let rows;
1469
1523
  try {
@@ -1546,7 +1600,7 @@ export const createOtalessBookings = (deps) => {
1546
1600
  // number. Never shown to the guest, never in what the model reads (questions #75).
1547
1601
  const note = text(w?.summary ?? undefined);
1548
1602
  if (note)
1549
- log(`[otaless] booking at ${a.group}: 8's summary, kept to this log: "${summaryForLog(note, [a.guest.name, a.guest.email, a.guest.phone])}"`);
1603
+ log(`[otaless] booking at ${a.group}: 8's summary, kept to this log: "${summaryForLog(note, [a.guest.name, a.guest.email, a.guest.phone, a.confirmToken])}"`);
1550
1604
  const status = String(w?.status ?? '')
1551
1605
  .trim()
1552
1606
  .toUpperCase();
@@ -1555,7 +1609,7 @@ export const createOtalessBookings = (deps) => {
1555
1609
  const err = text(w?.errorMessage ?? undefined);
1556
1610
  if (!w || w.success === false || status === 'FAILED' || err) {
1557
1611
  if (err)
1558
- log(`[otaless] booking at ${a.group} refused: "${forTheLog(err, [a.guest.name, a.guest.email, a.guest.phone])}"`);
1612
+ log(`[otaless] booking at ${a.group} refused: "${forTheLog(err, [a.guest.name, a.guest.email, a.guest.phone, a.confirmToken])}"`);
1559
1613
  const ref = w ? refusalOf(a, w, err ?? status) : undefined;
1560
1614
  if (ref)
1561
1615
  return refusedWithOrder(a, ref, pairs);
@@ -1911,7 +1965,7 @@ export const createOtalessBookings = (deps) => {
1911
1965
  .trim()
1912
1966
  .toUpperCase();
1913
1967
  // The guest's own details never on the log (总线程's re-review of 0.68.1, 2).
1914
- const guest = [a.guest.name, a.guest.email, a.guest.phone];
1968
+ const guest = [a.guest.name, a.guest.email, a.guest.phone, a.confirmToken];
1915
1969
  log(`[otaless] booking at ${a.group}: 8 refused "${forTheLog(String(msg), guest)}" with order ${forTheLog(id, guest)} in its answer - kept as that order, not sent again`);
1916
1970
  return {
1917
1971
  ...order,
@@ -1976,12 +2030,14 @@ export const createOtalessBookings = (deps) => {
1976
2030
  : failedStay(a, `${g.name} did not price it again just now, so nothing was booked.`, 'unreachable'),
1977
2031
  };
1978
2032
  if (err instanceof OtalessBusy)
1979
- return { failed: limited(a, g.name) };
1980
- // ⚠️ THE BACKEND'S OWN WORDS FOR A GROUP NOT LINKED (0.74.0, robin 11-17): refused, so
1981
- // nothing was ordered - the guest links it and presses Book again. Any other refusal is
1982
- // "not booked" below, with the backend's words on the log (client.ts).
1983
- if (unlinkedSaid(err))
1984
- throw linkFirst(a.acct, 'its own words for a group not linked');
2033
+ return {
2034
+ failed:
2035
+ // ⚠️ AFTER THE GUEST'S YES, ITS TERMS ARE GONE (0.74.1, 总线程's review of 0.74.0,
2036
+ // P3): agreed once, they cannot be agreed again - Book again asks for new ones.
2037
+ a.groupTerms === 'agreed'
2038
+ ? failedStay(a, `Not sent to ${g.name}: too many requests just now, so nothing was booked - the guest presses Book again in a minute.`, 'limited')
2039
+ : limited(a, g.name),
2040
+ };
1985
2041
  // ⚠️ THE GROUP'S TERMS ASKED FOR, NOTHING AGREED - A 10014 IS NO ORDER (总线程's review
1986
2042
  // of 0.72.1, P2-1): 8 sent with `termsRequired: true` answers the terms and orders
1987
2043
  // nothing, so its repeat refused (10014) - Book pressed twice at Ascott, or asked again
@@ -2022,6 +2078,13 @@ export const createOtalessBookings = (deps) => {
2022
2078
  : undefined;
2023
2079
  if (ref)
2024
2080
  return refusedWithOrder(a, ref, pairs ?? { orig: a.total });
2081
+ // ⚠️ THE BACKEND'S OWN WORDS FOR A GROUP NOT LINKED (0.74.0, robin 11-17): refused, so
2082
+ // nothing was ordered - the guest links it and presses Book again. ⚠️ ONLY ONCE NO ORDER
2083
+ // IS NAMED IN THE REFUSAL (0.74.1, 总线程's review of 0.74.0, P2-1): one that names its
2084
+ // order was checked as that order just above - said as "not booked", it was booked twice.
2085
+ // Any other refusal is "not booked" below, with the backend's words on the log.
2086
+ if (unlinkedSaid(err))
2087
+ throw linkFirst(a.acct, 'its own words for a group not linked');
2025
2088
  if (err instanceof OtalessError)
2026
2089
  return {
2027
2090
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
@@ -2236,6 +2299,34 @@ export const createOtalessBookings = (deps) => {
2236
2299
  const booked = [];
2237
2300
  const terms = [];
2238
2301
  const reused = [];
2302
+ /**
2303
+ * ⚠️ ONE STAY REFUSED FOR A SIGN-IN OR A LINK IS THAT STAY'S ALONE (0.74.1, 总线程's review
2304
+ * of 0.74.0, P1): thrown, it took the stays booked before it out of the answer - the
2305
+ * guest read "nothing was booked", and Book again ordered them twice (an order CONFIRMED
2306
+ * with no payment page is not one `stayGuard` hands back). Kept as that stay's failure,
2307
+ * with its groups, beside what was booked; nothing booked at all, the refusal itself is
2308
+ * the answer, as before.
2309
+ */
2310
+ const stayRefusals = [];
2311
+ const placeStay = async (label, ask) => {
2312
+ try {
2313
+ return await place(ask);
2314
+ }
2315
+ catch (err) {
2316
+ const e = err;
2317
+ if (!(err instanceof Error) ||
2318
+ (e?.code !== 'sign-in' && e?.code !== 'signed-out' && e?.code !== 'session-expired') ||
2319
+ !Array.isArray(e.groups))
2320
+ throw err;
2321
+ stayRefusals.push(err);
2322
+ return {
2323
+ failed: {
2324
+ ...failedStay(label, err.message),
2325
+ ...(e.groups.length ? { groups: [...e.groups] } : {}),
2326
+ },
2327
+ };
2328
+ }
2329
+ };
2239
2330
  for (const line of q.lines) {
2240
2331
  if (line.kind !== 'room' || !line.stay || !line.roomRef)
2241
2332
  continue;
@@ -2300,7 +2391,12 @@ export const createOtalessBookings = (deps) => {
2300
2391
  if (needsTerms) {
2301
2392
  inFlight.add(lockKey);
2302
2393
  try {
2303
- const out = await place({ ...ask, agreed: '', roomTerms: agreed, groupTerms: 'ask' });
2394
+ const out = await placeStay(label, {
2395
+ ...ask,
2396
+ agreed: '',
2397
+ roomTerms: agreed,
2398
+ groupTerms: 'ask',
2399
+ });
2304
2400
  if (out.booked)
2305
2401
  booked.push(out.booked);
2306
2402
  if (out.failed)
@@ -2319,7 +2415,7 @@ export const createOtalessBookings = (deps) => {
2319
2415
  if (!agreed && deps.proceedWithoutGroupConfirm) {
2320
2416
  inFlight.add(lockKey);
2321
2417
  try {
2322
- const out = await place({
2418
+ const out = await placeStay(label, {
2323
2419
  ...ask,
2324
2420
  noTerms: true,
2325
2421
  ...(unsaid ? { groupTerms: 'ask' } : {}),
@@ -2339,6 +2435,13 @@ export const createOtalessBookings = (deps) => {
2339
2435
  // ⚠️ NOTHING IS ORDERED YET (§15.1): the terms first, for the guest to agree to.
2340
2436
  terms.push(termsBefore(ask, unsaid));
2341
2437
  }
2438
+ // Nothing booked, nothing waiting: the sign-in or link the backend asked for is the answer.
2439
+ // ⚠️ UNLESS ANOTHER STAY FAILED BESIDE IT (0.74.2, 总线程's review of 0.74.1, P3): thrown,
2440
+ // the refusal took that stay's failure out of the answer - an order 8 made and we could not
2441
+ // cancel (「取消没成功、需要人工、不要付款」), still in the bookings and in the way of booking it
2442
+ // again. Then every failure is the answer, the refused stay's with its groups.
2443
+ if (stayRefusals[0] && !booked.length && !terms.length && failed.length <= stayRefusals.length)
2444
+ throw stayRefusals[0];
2342
2445
  return {
2343
2446
  booked,
2344
2447
  failed,
@@ -2859,12 +2962,63 @@ export const createOtalessBookings = (deps) => {
2859
2962
  for (const [k, p] of pending)
2860
2963
  if (p.orderId === r.id)
2861
2964
  pending.delete(k);
2862
- const back = moneyOf(out?.refundAmount, out?.refundCurrency);
2863
- if (!unpaid && r.paid)
2864
- r.refund = { status: 'requested', ...(back ? { amount: back } : {}) };
2965
+ if (!unpaid && r.paid) {
2966
+ const said = cancelRefundSaid(out);
2967
+ const back = moneyOf(out?.refundAmount, out?.refundCurrency);
2968
+ if (said || !deps.refundsOn)
2969
+ // 9 said its money is going back (or is back) - never asked twice. Refunds off: what
2970
+ // 9 said, as it was before 0.74.2.
2971
+ r.refund = {
2972
+ status: said === 'refunded' ? 'refunded' : 'requested',
2973
+ ...(back && back.minor > 0 ? { amount: back } : {}),
2974
+ };
2975
+ else {
2976
+ // ⚠️ ONE CANCEL, AND THE MONEY ASKED BACK IN IT (0.74.2, robin 11-17 through 总线程:
2977
+ // 「申请退款和取消有什么区别,应该留一个取消就好了」): 9 did not say the refund was taken, so
2978
+ // 9.1 is asked now, all of it, as the refund tool did. ⚠️ CANCELLED STAYS CANCELLED:
2979
+ // a 9.1 that fails is said as that - a person takes it up - never "not cancelled".
2980
+ put(r);
2981
+ r.refund = await askRefund(r, acct, who, why);
2982
+ }
2983
+ }
2865
2984
  put(r);
2866
2985
  return toBooking(r);
2867
2986
  };
2987
+ /**
2988
+ * 9.1 for an order just cancelled (0.74.2): what the group did with it, or `not-sent` - its
2989
+ * refusal or silence logged in its own words for a person to take up. Nothing thrown.
2990
+ */
2991
+ const askRefund = async (r, acct, who, why) => {
2992
+ const g = r.group;
2993
+ try {
2994
+ const out = await withSession(acct, who, (cred) => client.refund({
2995
+ unifiedOrderId: r.id,
2996
+ ...(r.code ? { booking: r.code } : {}),
2997
+ providerCode: r.group,
2998
+ ...(why ? { reason: why } : {}),
2999
+ }, cred, deps.callerOf(who)));
3000
+ if (out?.success === false) {
3001
+ counts.refundNotSent += 1;
3002
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${g} was cancelled, and its refund (9.1) was refused: ${forTheLog(String(out?.message ?? out?.status ?? 'no words'))} - nothing goes back until a person takes it up`);
3003
+ return { status: 'not-sent' };
3004
+ }
3005
+ const back = moneyOf(out?.refundAmount, out?.refundCurrency);
3006
+ return {
3007
+ status: String(out?.status ?? '').toUpperCase() === 'REFUNDED' ? 'refunded' : 'requested',
3008
+ ...(back && back.minor > 0 ? { amount: back } : {}),
3009
+ };
3010
+ }
3011
+ catch (err) {
3012
+ counts.refundNotSent += 1;
3013
+ const words = err instanceof OtalessError
3014
+ ? `${err.backendCode ?? err.status ?? ''} ${String(err.answer?.msg ?? err.message)}`
3015
+ : err instanceof Error
3016
+ ? err.message
3017
+ : String(err);
3018
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${g} was cancelled, and its refund (9.1) ${unanswered(err) ? 'was not answered' : 'failed'}: ${forTheLog(words)} - nothing goes back until a person takes it up`);
3019
+ return { status: 'not-sent' };
3020
+ }
3021
+ };
2868
3022
  // ── refund: a preview, then the refund it previewed (9.1; 总线程's review of 0.63.0, B) ──
2869
3023
  /** Refund previews waiting to be sent, by their `preview`. */
2870
3024
  const refundPreviews = new Map();