@curless/hotel-storefront 0.66.0 → 0.67.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.
Files changed (78) hide show
  1. package/dist/account/fake-ota.d.ts.map +1 -1
  2. package/dist/account/fake-ota.js +2 -0
  3. package/dist/account/fake-ota.js.map +1 -1
  4. package/dist/account/index.d.ts +16 -0
  5. package/dist/account/index.d.ts.map +1 -1
  6. package/dist/account/index.js +55 -8
  7. package/dist/account/index.js.map +1 -1
  8. package/dist/account/ota-member.d.ts +2 -0
  9. package/dist/account/ota-member.d.ts.map +1 -1
  10. package/dist/account/ota-member.js +2 -0
  11. package/dist/account/ota-member.js.map +1 -1
  12. package/dist/account/pages.d.ts +11 -0
  13. package/dist/account/pages.d.ts.map +1 -1
  14. package/dist/account/pages.js +51 -7
  15. package/dist/account/pages.js.map +1 -1
  16. package/dist/account/store.d.ts +38 -1
  17. package/dist/account/store.d.ts.map +1 -1
  18. package/dist/account/store.js +32 -1
  19. package/dist/account/store.js.map +1 -1
  20. package/dist/card.generated.d.ts +1 -1
  21. package/dist/card.generated.js +2 -2
  22. package/dist/card.generated.js.map +1 -1
  23. package/dist/config.d.ts +10 -0
  24. package/dist/config.d.ts.map +1 -1
  25. package/dist/config.js.map +1 -1
  26. package/dist/otaless/backend.d.ts +2 -0
  27. package/dist/otaless/backend.d.ts.map +1 -1
  28. package/dist/otaless/backend.js +41 -5
  29. package/dist/otaless/backend.js.map +1 -1
  30. package/dist/otaless/booking.d.ts +77 -4
  31. package/dist/otaless/booking.d.ts.map +1 -1
  32. package/dist/otaless/booking.js +355 -43
  33. package/dist/otaless/booking.js.map +1 -1
  34. package/dist/otaless/client.d.ts +83 -7
  35. package/dist/otaless/client.d.ts.map +1 -1
  36. package/dist/otaless/client.js +117 -13
  37. package/dist/otaless/client.js.map +1 -1
  38. package/dist/otaless/fake.d.ts +39 -2
  39. package/dist/otaless/fake.d.ts.map +1 -1
  40. package/dist/otaless/fake.js +88 -7
  41. package/dist/otaless/fake.js.map +1 -1
  42. package/dist/otaless/make.d.ts +2 -0
  43. package/dist/otaless/make.d.ts.map +1 -1
  44. package/dist/otaless/make.js +29 -3
  45. package/dist/otaless/make.js.map +1 -1
  46. package/dist/otaless/places.d.ts +14 -0
  47. package/dist/otaless/places.d.ts.map +1 -1
  48. package/dist/otaless/places.js +108 -1
  49. package/dist/otaless/places.js.map +1 -1
  50. package/dist/pictures.d.ts +19 -0
  51. package/dist/pictures.d.ts.map +1 -0
  52. package/dist/pictures.js +74 -0
  53. package/dist/pictures.js.map +1 -0
  54. package/dist/server.d.ts +15 -2
  55. package/dist/server.d.ts.map +1 -1
  56. package/dist/server.js +216 -37
  57. package/dist/server.js.map +1 -1
  58. package/dist/sessions.d.ts +23 -0
  59. package/dist/sessions.d.ts.map +1 -1
  60. package/dist/sessions.js +39 -1
  61. package/dist/sessions.js.map +1 -1
  62. package/dist/supplier.js +4 -0
  63. package/dist/supplier.js.map +1 -1
  64. package/dist/surface.d.ts +24 -1
  65. package/dist/surface.d.ts.map +1 -1
  66. package/dist/surface.js +31 -4
  67. package/dist/surface.js.map +1 -1
  68. package/dist/tools.d.ts +24 -0
  69. package/dist/tools.d.ts.map +1 -1
  70. package/dist/tools.js +153 -19
  71. package/dist/tools.js.map +1 -1
  72. package/dist/trip-ids.d.ts +13 -1
  73. package/dist/trip-ids.d.ts.map +1 -1
  74. package/dist/trip-ids.js +30 -3
  75. package/dist/trip-ids.js.map +1 -1
  76. package/dist/types.d.ts +46 -1
  77. package/dist/types.d.ts.map +1 -1
  78. package/package.json +1 -1
@@ -58,6 +58,13 @@ export const STATUS_EVERY_MS = 10_000;
58
58
  * the document's checkout holds a room unpaid.
59
59
  */
60
60
  export const UNCONFIRMED_GIVE_UP_MS = 2 * 60 * 60_000;
61
+ /**
62
+ * An order a person was to see to (`needsPerson`), still not settled this long
63
+ * after: no longer holds the stay back - the guest is told first, as it is
64
+ * (总线程's review of 0.66.0, 6). The same two hours as an order 8 did not
65
+ * answer for: any hold on an unpaid room is long gone by then.
66
+ */
67
+ export const NEEDS_PERSON_GIVE_UP_MS = 2 * 60 * 60_000;
61
68
  /**
62
69
  * An order waiting for its payment with no time to pay by from 13: taken as
63
70
  * payable this long after it was made, or after the payment status (15) last
@@ -98,9 +105,10 @@ const CHECKED_PER_LOOK = 5;
98
105
  * re-review of 0.63.1, 720f5e4f; again 11-11, B): that is our own hosted
99
106
  * checkout, which takes any merchant and any amount, and has no `/pay/cashier`
100
107
  * behind it today - though 8's example names it. A page there is not handed
101
- * on: the order is cancelled and its host and path logged, to ask the backend
102
- * about. Should it ever be listed, it is as `mcp.curless.ai/pay/cashier?order={order}`
103
- * - that page for that order alone (`payPageOf`).
108
+ * on: the order is cancelled and its host logged, to ask the backend about.
109
+ * Should it ever be listed, it is as `mcp.curless.ai/pay/cashier?order={order}`
110
+ * - that page for that order alone (`payPageOf`). A page on a host listed
111
+ * alone must still name the order (总线程's review of 0.66.0, 2).
104
112
  */
105
113
  export const DEFAULT_CHECKOUT_HOSTS = ['pay.discoverasr.com'];
106
114
  /** How long a refund's preview can be sent back (总线程's review of 0.63.0, B). */
@@ -190,6 +198,12 @@ export const checkoutHostsProblem = (hosts) => {
190
198
  * takes any path on it (总线程's review of 0.63.0, 8). The page handed on is
191
199
  * the one checked: its path as compared.
192
200
  *
201
+ * ⚠️ AND ONLY A PAGE NAMING THIS ORDER (总线程's review of 0.66.0, 2): through an
202
+ * entry without `{order}`, the order's `unifiedOrderId` or `originalOrderId` must
203
+ * be in the page - a whole path segment, or a whole query value. Neither there,
204
+ * or no order to hold it to: nothing. (`pay.discoverasr.com` until its pages'
205
+ * real form is known - then an exact entry naming `{order}`.)
206
+ *
193
207
  * ⚠️ AN ENTRY NAMING THE ORDER (`host/pay/cashier?order={order}`, 总线程
194
208
  * 11-11, B) TAKES ONE PAGE: that path exactly, and a query of that one
195
209
  * parameter, once, naming `order` - this very order's `unifiedOrderId` - and
@@ -217,39 +231,186 @@ export const payPageOf = (raw, hosts, order) => {
217
231
  if (e.host !== host || !e.under.every((seg, i) => segs[i] === seg))
218
232
  continue;
219
233
  if (e.order === undefined) {
234
+ if (!namesOrder(u, segs, order))
235
+ continue;
220
236
  u.pathname = `/${segs.join('/')}${segs.length && u.pathname.endsWith('/') ? '/' : ''}`;
221
237
  return { url: u.toString(), host };
222
238
  }
223
- if (!order || segs.length !== e.under.length || u.hash)
239
+ if (!order?.id || segs.length !== e.under.length || u.hash)
224
240
  continue;
225
241
  const keys = [...u.searchParams.keys()];
226
- if (keys.length !== 1 || keys[0] !== e.order || u.searchParams.get(e.order) !== order)
242
+ if (keys.length !== 1 || keys[0] !== e.order || u.searchParams.get(e.order) !== order.id)
227
243
  continue;
228
244
  return {
229
- url: `https://${host}/${e.under.join('/')}?${e.order}=${encodeURIComponent(order)}`,
245
+ url: `https://${host}/${e.under.join('/')}?${e.order}=${encodeURIComponent(order.id)}`,
230
246
  host,
231
247
  };
232
248
  }
233
249
  return undefined;
234
250
  };
235
251
  /**
236
- * The host and path of a page not handed on, for the log - never its query or
237
- * fragment (they may carry a payment token): what 总线程 asks the backend about.
252
+ * Whether a page names the order: its `unifiedOrderId` or `originalOrderId` as
253
+ * a whole path segment (as written, or decoded) or a whole query value - never
254
+ * a part of one, never in the fragment (总线程's review of 0.66.0, 2).
255
+ */
256
+ const namesOrder = (u, segs, order) => {
257
+ const ids = [order?.id, order?.code].filter((x) => !!x);
258
+ if (!ids.length)
259
+ return false;
260
+ const decoded = (seg) => {
261
+ try {
262
+ return decodeURIComponent(seg);
263
+ }
264
+ catch {
265
+ return seg;
266
+ }
267
+ };
268
+ return (segs.some((seg) => ids.includes(seg) || ids.includes(decoded(seg))) ||
269
+ [...u.searchParams.values()].some((v) => ids.includes(v)));
270
+ };
271
+ /** Whether a page is on a host the list names - whatever its path. */
272
+ const onListedHost = (raw, hosts) => {
273
+ try {
274
+ const host = new URL(String(raw)).hostname.toLowerCase();
275
+ return hosts.some((h) => checkoutEntryOf(h).host === host);
276
+ }
277
+ catch {
278
+ return false;
279
+ }
280
+ };
281
+ /**
282
+ * The host of a page not handed on, for the log - never its path, query or
283
+ * fragment: some payment pages carry their token in the path (总线程's review
284
+ * of 0.66.0, 4).
285
+ */
286
+ const hostForLog = (raw) => {
287
+ try {
288
+ return forTheLog(new URL(String(raw)).host) || '?';
289
+ }
290
+ catch {
291
+ return 'not a URL';
292
+ }
293
+ };
294
+ /** A path segment written on the log as it is: a plain lower-case word - anything else is its length. */
295
+ const PATH_WORD = /^[a-z_-]{1,20}$/;
296
+ /** A query parameter's name written as it is: a plain word, either case. */
297
+ const KEY_WORD = /^[A-Za-z_-]{1,20}$/;
298
+ /** At most this many path segments, and as many parameters, on one shape. */
299
+ const SHAPE_PARTS = 12;
300
+ /**
301
+ * The form of a payment page not handed on, for the log - NO VALUE OF IT
302
+ * (总线程 11-11 late): each path segment a plain word as it is, or its length;
303
+ * each parameter's name (a plain word, or its length) and its value's length;
304
+ * and where this order's `unifiedOrderId` / `originalOrderId` is in it - or
305
+ * that it is not. From the first real page, the exact `{order}` entry can be
306
+ * written; no payment token reaches the log.
238
307
  */
239
- const pageForLog = (raw) => {
308
+ export const pageShape = (raw, order) => {
309
+ let u;
240
310
  try {
241
- const u = new URL(String(raw));
242
- return forTheLog(`${u.host}${u.pathname}`) || '?';
311
+ u = new URL(String(raw));
243
312
  }
244
313
  catch {
245
314
  return 'not a URL';
246
315
  }
316
+ const segs = pathSegments(u.pathname) ?? u.pathname.split('/').filter(Boolean);
317
+ const pairs = [...u.searchParams.entries()];
318
+ const size = (x) => `<${x.length} chars>`;
319
+ const key = (k) => (KEY_WORD.test(k) ? k : size(k));
320
+ const more = (n) => (n > SHAPE_PARTS ? ` (+${n - SHAPE_PARTS} more)` : '');
321
+ const decoded = (x) => {
322
+ try {
323
+ return decodeURIComponent(x);
324
+ }
325
+ catch {
326
+ return x;
327
+ }
328
+ };
329
+ const path = `/${segs
330
+ .slice(0, SHAPE_PARTS)
331
+ .map((x) => (PATH_WORD.test(x) ? x : size(x)))
332
+ .join('/')}${more(segs.length)}`;
333
+ const query = pairs.length
334
+ ? ` ? ${pairs
335
+ .slice(0, SHAPE_PARTS)
336
+ .map(([k, v]) => `${key(k)}=${size(v)}`)
337
+ .join(' & ')}${more(pairs.length)}`
338
+ : '';
339
+ const hash = u.hash.length > 1 ? ` # ${size(u.hash.slice(1))}` : '';
340
+ const where = [];
341
+ for (const [what, id] of [
342
+ ['unifiedOrderId', order?.id],
343
+ ['originalOrderId', order?.code],
344
+ ]) {
345
+ if (!id)
346
+ continue;
347
+ segs.forEach((x, i) => {
348
+ const d = decoded(x);
349
+ if (x === id || d === id)
350
+ where.push(`${what} is path segment ${i + 1}`);
351
+ else if (x.includes(id) || d.includes(id))
352
+ where.push(`${what} is inside path segment ${i + 1}`);
353
+ });
354
+ for (const [k, v] of pairs) {
355
+ if (v === id)
356
+ where.push(`${what} is the value of ${key(k)}`);
357
+ else if (v.includes(id))
358
+ where.push(`${what} is inside the value of ${key(k)}`);
359
+ if (k.includes(id))
360
+ where.push(`${what} is inside a parameter's name`);
361
+ }
362
+ if (decoded(u.hash.slice(1)).includes(id))
363
+ where.push(`${what} is in the fragment`);
364
+ }
365
+ return `${path}${query}${hash} - ${where.length ? where.join(', ') : 'this order is not in it'}`;
247
366
  };
248
367
  /**
249
368
  * Two terms the same but for spacing and punctuation (总线程 11-11, D): a
250
369
  * group's answer formatted a little differently is not new terms to agree to.
251
370
  */
252
371
  export const termsKey = (s) => s.normalize('NFKC').replace(/[\s\p{P}]/gu, '');
372
+ /** Which step a refusal of 8 - or its status - asks for, or none. */
373
+ export const stepAsked = (said) => {
374
+ const s = String(said ?? '');
375
+ if (/\bterms[ _-]?required\b/i.test(s))
376
+ return 'terms';
377
+ if (/\bconfirm(?:ation)?[ _-]?required\b/i.test(s))
378
+ return 'confirm';
379
+ return undefined;
380
+ };
381
+ /**
382
+ * What to agree to or confirm, and the token to do it with, as 8 would send
383
+ * them back with `terms_required` / `confirm_required` - both, or nothing.
384
+ * ⚠️ THE NAMES ARE THE ONES ASKED FOR IN QUESTIONS #66 AND #68
385
+ * (`termsText` / `termsToken`, `confirmText` / `confirmToken`, or written with
386
+ * `_`), not yet in 8's document: until the backend answers, an answer naming
387
+ * them otherwise is a refusal, as it is today. An amount or nights beside them
388
+ * are read by 8's own names (`totalAmount`, `currency`, `checkIn`, `checkOut`).
389
+ */
390
+ export const groupStepOf = (step, data) => {
391
+ if (!data || typeof data !== 'object')
392
+ return undefined;
393
+ const d = data;
394
+ const said = step === 'terms' ? (d.termsText ?? d.terms_text) : (d.confirmText ?? d.confirm_text);
395
+ const token = step === 'terms' ? (d.termsToken ?? d.terms_token) : (d.confirmToken ?? d.confirm_token);
396
+ if (typeof said !== 'string' || typeof token !== 'string')
397
+ return undefined;
398
+ const t = said.trim();
399
+ const k = token.trim();
400
+ if (!t || t.length > TERMS_MAX || !/^[\x21-\x7e]{1,512}$/.test(k))
401
+ return undefined;
402
+ const amount = moneyOf(d.totalAmount, d.currency);
403
+ const named = (d.totalAmount ?? d.currency) !== undefined && (d.totalAmount ?? d.currency) !== null;
404
+ const checkIn = text(d.checkIn);
405
+ const checkOut = text(d.checkOut);
406
+ return {
407
+ text: t,
408
+ token: k,
409
+ ...(amount ? { amount } : named ? { amountUnread: true } : {}),
410
+ ...(checkIn ? { checkIn } : {}),
411
+ ...(checkOut ? { checkOut } : {}),
412
+ };
413
+ };
253
414
  /**
254
415
  * The statuses a cancellation (9 `booking/cancel`; 14 `checkout/cancel` before 11-11) is
255
416
  * taken as DONE by - exactly these, case aside (总线程's review of 0.63.0, 4):
@@ -554,7 +715,10 @@ export const createOtalessBookings = (deps) => {
554
715
  };
555
716
  // Beside the type, for the words (0.63.2): why it is not known - and (11-11,
556
717
  // D) an order whose payment page waits for terms the group answered it with.
557
- return Object.assign(booking, r.unverifiable ? { unverifiable: true } : {}, r.needsPerson ? { needsPerson: r.needsPerson } : {}, r.held ? { termsChanged: true, termsUntil: r.held.until } : {});
718
+ return Object.assign(booking, r.unverifiable ? { unverifiable: true } : {}, r.needsPerson ? { needsPerson: r.needsPerson } : {}, r.held ? { termsChanged: true, termsUntil: r.held.until } : {},
719
+ // With a person past its time and the guest told so: no longer holds the stay
720
+ // back (总线程's review of 0.66.0, 6) - the words must not say it does.
721
+ r.needsPerson && r.rebookable ? { rebookable: true } : {});
558
722
  };
559
723
  const failedStay = (a, why, code) => ({
560
724
  hotelId: a.hotel.id,
@@ -611,6 +775,7 @@ export const createOtalessBookings = (deps) => {
611
775
  source: p.source,
612
776
  more: true,
613
777
  ...(p.changed ? { changed: true } : {}),
778
+ ...(p.askedByGroup ? { askedByGroup: p.askedByGroup } : {}),
614
779
  ...(p.bookingId ? { bookingId: p.bookingId } : {}),
615
780
  ...(p.confirmationCode ? { confirmationCode: p.confirmationCode } : {}),
616
781
  until: p.until,
@@ -654,6 +819,73 @@ export const createOtalessBookings = (deps) => {
654
819
  key: a.acct.key,
655
820
  ask: { roomId: a.roomId, guest: a.guest },
656
821
  });
822
+ /**
823
+ * 8 asked for a second step (`terms_required`, questions #66;
824
+ * `confirm_required`, #68). ⚠️ NEVER AGREED OR CONFIRMED FOR THE GUEST (#52):
825
+ * with what to agree to or confirm and a token - and, where it names an
826
+ * amount or nights, the price and nights the guest was shown - it is the
827
+ * guest's to read and do themselves, nothing ordered; done, 8 is priced again
828
+ * and sent once more with the token. Without both - or asked again after the
829
+ * guest did it - nothing is booked, and the log says which it was.
830
+ */
831
+ const groupAsked = (a, step, data, pairs) => {
832
+ const g = deps.groupOf(a.group);
833
+ const what = `${step}_required`;
834
+ const code = (step === 'terms' ? 'group-terms' : 'group-confirm');
835
+ const asked = groupStepOf(step, data);
836
+ if (asked && !a.groupStep) {
837
+ const amountOff = asked.amountUnread ||
838
+ (asked.amount && !(same(asked.amount, pairs.orig) || same(asked.amount, pairs.usd)));
839
+ const nightsOff = (asked.checkIn !== undefined && asked.checkIn !== a.stay.checkIn) ||
840
+ (asked.checkOut !== undefined && asked.checkOut !== a.stay.checkOut);
841
+ if (amountOff || nightsOff) {
842
+ log(`[otaless] booking at ${a.group} answered ${what} for ${amountOff ? 'another amount' : 'other nights'} than the guest was shown - not shown, not booked`);
843
+ return {
844
+ failed: failedStay(a, `${g.name} asked for ${step === 'terms' ? 'its terms to be agreed to' : 'the booking to be confirmed'} at ${amountOff ? 'another amount' : 'other nights'} than the guest was shown, so nothing was booked or charged.`, code),
845
+ };
846
+ }
847
+ log(`[otaless] booking at ${a.group} answered ${what} with what to ${step === 'terms' ? 'agree to' : 'confirm'} - shown to the guest, nothing ordered`);
848
+ return {
849
+ terms: remember({
850
+ ref: randomBytes(18).toString('base64url'),
851
+ group: a.group,
852
+ ...groupWords(a.group),
853
+ hotelId: a.hotel.id,
854
+ hotelName: a.hotel.name,
855
+ ...(a.hotel.names?.name ? { hotelNames: a.hotel.names.name } : {}),
856
+ roomName: a.roomName,
857
+ stay: a.stay,
858
+ rooms: a.rooms,
859
+ total: a.total,
860
+ text: asked.text,
861
+ source: 'booking',
862
+ more: true,
863
+ askedByGroup: step,
864
+ until: iso(now() + TERMS_MS),
865
+ owner: a.acct.owner,
866
+ key: a.acct.key,
867
+ ask: {
868
+ roomId: a.roomId,
869
+ guest: a.guest,
870
+ roomTerms: a.agreed,
871
+ token: asked.token,
872
+ step,
873
+ },
874
+ }),
875
+ };
876
+ }
877
+ log(a.groupStep
878
+ ? `[otaless] booking at ${a.group} answered ${what} again, after the guest ${step === 'terms' ? 'agreed to its terms' : 'confirmed it'} - not booked`
879
+ : `[otaless] booking at ${a.group} answered ${what} with no ${step === 'terms' ? 'terms' : 'details'} and token to show the guest - not booked (questions #${step === 'terms' ? 66 : 68})`);
880
+ const again = step === 'terms'
881
+ ? `${g.name} asked for its terms to be agreed to again after the guest agreed to them, so nothing was booked or charged.`
882
+ : `${g.name} asked for the booking to be confirmed again after the guest confirmed it, so nothing was booked or charged.`;
883
+ const none = step === 'terms'
884
+ ? `${g.name} asked for terms of its own to be agreed to, which it did not send to be shown here, so nothing was booked or charged.`
885
+ : `${g.name} asked for the booking to be confirmed in a second step, and did not send what to confirm to be shown here, so nothing was booked or charged.`;
886
+ // New codes for the card (酒店平台's next version): until then, these words.
887
+ return { failed: failedStay(a, a.groupStep ? again : none, code) };
888
+ };
657
889
  /**
658
890
  * The terms an order is held back for (总线程 11-11, D): those kept - or,
659
891
  * gone from memory (a restart), the order's own kept again until its time.
@@ -661,6 +893,9 @@ export const createOtalessBookings = (deps) => {
661
893
  const termsOfHeld = (r, acct) => {
662
894
  if (!r.held || r.released || !r.terms || now() >= Date.parse(r.held.until))
663
895
  return undefined;
896
+ // Seen in this conversation: its time running out is cancelled with this sign-in.
897
+ if (acct.owner === r.owner)
898
+ heldBy.set(r.id, acct.key);
664
899
  for (const p of pending.values())
665
900
  if (p.orderId === r.id && p.owner === acct.owner && now() <= Date.parse(p.until))
666
901
  return view(p);
@@ -696,20 +931,29 @@ export const createOtalessBookings = (deps) => {
696
931
  address: 'payment page not accepted',
697
932
  terms: 'terms not agreed by the guest',
698
933
  };
934
+ /** Thrown where there is no sign-in at the order's group to cancel it with. */
935
+ class NotSignedIn extends Error {
936
+ name = 'not signed in there';
937
+ }
699
938
  /**
700
939
  * Cancels an order we will not hand on, at once - by 9, the only cancel there
701
940
  * is now. Its payment page goes first, whatever comes back. ⚠️ ONE THAT DOES
702
- * NOT CANCEL NEEDS A PERSON: logged as such, kept, listed - never paid.
941
+ * NOT CANCEL NEEDS A PERSON: logged as such, kept, listed - never paid. So
942
+ * does one with no sign-in to cancel it with (`acct` none - 总线程's review of
943
+ * 0.66.0, 1).
703
944
  */
704
945
  const release = async (acct, who, r, why) => {
705
946
  r.released = why;
706
947
  r.held = undefined;
948
+ heldBy.delete(r.id);
707
949
  payLinks.delete(r.id);
708
950
  for (const [ref, p] of pending)
709
951
  if (p.orderId === r.id)
710
952
  pending.delete(ref);
711
953
  counts.released += 1;
712
954
  try {
955
+ if (!acct)
956
+ throw new NotSignedIn();
713
957
  const done = await withSession(acct, who, (cred) => client.cancelBooking({ unifiedOrderId: r.id, providerCode: r.group, reason: RELEASE_REASON[why] }, cred, deps.callerOf(who)));
714
958
  // Not said to be done - refused, or not known: a person, as any failure here.
715
959
  if (cancelOutcome(done) !== 'done')
@@ -722,6 +966,7 @@ export const createOtalessBookings = (deps) => {
722
966
  catch (err) {
723
967
  r.releaseFailed = true;
724
968
  r.needsPerson = 'release';
969
+ r.needsPersonAt = iso();
725
970
  counts.releaseFailed += 1;
726
971
  log(`[otaless] NEEDS A PERSON: order ${forTheLog(r.id)} at ${r.group} (${why}) could not be cancelled: ${err instanceof Error ? err.name : 'error'}`);
727
972
  put(r);
@@ -757,29 +1002,53 @@ export const createOtalessBookings = (deps) => {
757
1002
  const t = setTimeout(fn, ms);
758
1003
  t.unref?.();
759
1004
  });
760
- /** A held order's time ran out: cancelled - if the guest is signed in at its group to do it with. */
761
- const lapse = async (id, key) => {
1005
+ /**
1006
+ * The conversation each held order was last seen in, by order id - in memory
1007
+ * only: whose sign-in its time running out is cancelled with.
1008
+ */
1009
+ const heldBy = new Map();
1010
+ /** A held order whose time ran out - or is past it already (not a number): due. */
1011
+ const heldOver = (r) => !(now() < Date.parse(r.held?.until ?? ''));
1012
+ /**
1013
+ * A held order's time ran out: cancelled with the guest's sign-in at its
1014
+ * group. ⚠️ NOT LEFT STANDING (总线程's review of 0.66.0, 1): not signed in
1015
+ * there now - or not known in whose conversation, after a restart - it
1016
+ * needs a person, said as such on the log, as any order that did not cancel.
1017
+ */
1018
+ const lapse = async (id) => {
762
1019
  const r = orders.get(id);
763
- if (!r?.held || r.released || now() < Date.parse(r.held.until))
764
- return;
765
- const who = { sessionId: key };
766
- const acct = accountFor(who, r.group);
767
- if (!acct || acct.owner !== r.owner) {
768
- log(`[otaless] order ${forTheLog(id)} at ${r.group}: its terms were not agreed in time, and the guest is not signed in there to cancel it with - cancelled when they next look`);
1020
+ if (!r?.held || r.released || !heldOver(r))
769
1021
  return;
770
- }
771
- await release(acct, who, r, 'terms');
1022
+ const key = heldBy.get(id);
1023
+ const who = { sessionId: key ?? '' };
1024
+ const acct = key ? accountFor(who, r.group) : undefined;
1025
+ await release(acct && acct.owner === r.owner ? acct : undefined, who, r, 'terms');
772
1026
  };
773
- /** This guest's held orders whose time ran out - the timer may not have run (a restart): cancelled now. */
1027
+ /** A held order's time, kept: `lapse` just after it - for whichever conversation saw it last. */
1028
+ const holdFor = (r, key) => {
1029
+ if (key)
1030
+ heldBy.set(r.id, key);
1031
+ const left = Date.parse(r.held?.until ?? '') - now();
1032
+ later(Number.isFinite(left) && left > 0 ? left + 1_000 : 0, () => {
1033
+ lapse(r.id).catch(() => undefined);
1034
+ });
1035
+ };
1036
+ /** This guest's held orders whose time ran out - before their timer: cancelled now. */
774
1037
  const sweep = async (who) => {
775
1038
  for (const r of [...orders.values()]) {
776
- if (!r.held || r.released || now() < Date.parse(r.held.until))
1039
+ if (!r.held || r.released || !heldOver(r))
777
1040
  continue;
778
1041
  const acct = accountFor(who, r.group);
779
1042
  if (acct && acct.owner === r.owner)
780
1043
  await release(acct, who, r, 'terms');
781
1044
  }
782
1045
  };
1046
+ // ⚠️ HELD ORDERS KEPT ACROSS A RESTART (总线程's review of 0.66.0, 1): their
1047
+ // timers went with the process. Each gets its own again - one whose time ran
1048
+ // out while it was down, at once - so none is left standing at its group.
1049
+ for (const r of [...orders.values()])
1050
+ if (r.held && !r.released)
1051
+ holdFor(r);
783
1052
  /** What 8 answered, settled: an order to hand on, one held for new terms, or none. */
784
1053
  const settle = async (a, w, pairs) => {
785
1054
  const g = deps.groupOf(a.group);
@@ -797,13 +1066,10 @@ export const createOtalessBookings = (deps) => {
797
1066
  };
798
1067
  }
799
1068
  if (!id) {
800
- // Terms of its own to agree to, and no order: 8 has nothing to send them back with.
801
- if (/TERMS/.test(status)) {
802
- log(`[otaless] booking at ${a.group} asked for terms (${forTheLog(status)}) - not booked`);
803
- return {
804
- failed: failedStay(a, `${g.name} asked for terms of its own to be agreed to, which cannot be shown here, so nothing was booked or charged.`),
805
- };
806
- }
1069
+ // Terms of its own to agree to, and no order (questions #66).
1070
+ const step = /TERMS/.test(status) ? 'terms' : stepAsked(status);
1071
+ if (step)
1072
+ return groupAsked(a, step, w, pairs);
807
1073
  // Something may be held, under a number we were not told: kept to look for (12).
808
1074
  const r = recordOf(a, pairs, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
809
1075
  put(r);
@@ -836,10 +1102,15 @@ export const createOtalessBookings = (deps) => {
836
1102
  }
837
1103
  // ── the payment page (总线程 11-10, rule 5; 11-11, B) ──
838
1104
  if (page) {
839
- const ok = payPageOf(page, deps.checkoutHosts, id);
1105
+ const ok = payPageOf(page, deps.checkoutHosts, { id, code });
840
1106
  if (!ok) {
841
- // ⚠️ ITS HOST AND PATH ON THE LOG (总线程 11-11, B) - never its query.
842
- log(`[otaless] booking at ${a.group} sent a payment page on ${pageForLog(page)}, not on the list or not this order's - cancelled`);
1107
+ // ⚠️ ITS HOST ALONE ON THE LOG (总线程's review of 0.66.0, 4) - never its
1108
+ // path or query: either may carry a payment token.
1109
+ log(`[otaless] booking at ${a.group} sent a payment page on ${hostForLog(page)}, not on the list or not this order's - cancelled`);
1110
+ // On a host listed: its shape, no value (总线程 11-11 late) - to write
1111
+ // the exact `{order}` entry from the first real one.
1112
+ if (onListedHost(page, deps.checkoutHosts))
1113
+ log(`[otaless] that page's shape: ${pageShape(page, { id, code })}`);
843
1114
  return letGo(a, r, 'address');
844
1115
  }
845
1116
  r.payHost = ok.host;
@@ -855,7 +1126,9 @@ export const createOtalessBookings = (deps) => {
855
1126
  log(`[otaless] booking at ${a.group} came with terms too long to show - cancelled`);
856
1127
  return letGo(a, r, 'terms');
857
1128
  }
858
- if (said && termsKey(said) !== termsKey(a.agreed)) {
1129
+ if (said &&
1130
+ termsKey(said) !== termsKey(a.agreed) &&
1131
+ !(a.groupStep?.step === 'terms' && termsKey(said) === termsKey(a.groupStep.text))) {
859
1132
  // ⚠️ NOT THE TERMS AGREED TO: the order stands, its payment page is held
860
1133
  // back until the guest agrees to these too - not agreed, or not in time,
861
1134
  // it is cancelled.
@@ -863,9 +1136,7 @@ export const createOtalessBookings = (deps) => {
863
1136
  r.held = { until: iso(now() + TERMS_MS) };
864
1137
  put(r);
865
1138
  log(`[otaless] booking ${forTheLog(id)} at ${a.group} came with other terms than the guest agreed to - its payment page held back`);
866
- later(TERMS_MS + 1_000, () => {
867
- lapse(id, a.acct.key).catch(() => undefined);
868
- });
1139
+ holdFor(r, a.acct.key);
869
1140
  const t = termsOfHeld(r, a.acct);
870
1141
  return t ? { terms: t } : { booked: toBooking(r) };
871
1142
  }
@@ -927,6 +1198,13 @@ export const createOtalessBookings = (deps) => {
927
1198
  checkIn: a.stay.checkIn,
928
1199
  checkOut: a.stay.checkOut,
929
1200
  roomCount: a.rooms,
1201
+ // Only after the guest agreed to the terms 8 asked for (questions #66).
1202
+ ...(a.groupStep?.step === 'terms'
1203
+ ? { termsToken: a.groupStep.token, termsAccepted: true }
1204
+ : {}),
1205
+ ...(a.groupStep?.step === 'confirm'
1206
+ ? { confirmToken: a.groupStep.token, confirmed: true }
1207
+ : {}),
930
1208
  };
931
1209
  let booked;
932
1210
  try {
@@ -961,6 +1239,10 @@ export const createOtalessBookings = (deps) => {
961
1239
  put(r);
962
1240
  return { booked: toBooking(r) };
963
1241
  }
1242
+ // ⚠️ 8 REFUSED FOR TERMS (robin 11-11 18:30, Ascott: code 500 "terms_required").
1243
+ const step = err === fromBook && err instanceof OtalessError ? stepAsked(err.answer?.msg) : undefined;
1244
+ if (step)
1245
+ return groupAsked(a, step, err instanceof OtalessError ? err.answer?.data : undefined, pairs ?? { orig: a.total });
964
1246
  if (err instanceof OtalessError)
965
1247
  return {
966
1248
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
@@ -999,7 +1281,25 @@ export const createOtalessBookings = (deps) => {
999
1281
  o.stay.checkOut === stay.checkOut &&
1000
1282
  o.rooms === rooms;
1001
1283
  // A person's: said paid at another amount, or cancelled at once and not undone.
1002
- const person = [...orders.values()].find((o) => sameStay(o) && o.needsPerson);
1284
+ const people = [...orders.values()].filter((o) => sameStay(o) && o.needsPerson && !o.rebookable);
1285
+ // ⚠️ THE WAY OUT (总线程's review of 0.66.0, 6), as for an order that could
1286
+ // not be verified: with a person this long and still not settled, the stay
1287
+ // is no longer held back. The guest is told first, as it is - it may stand
1288
+ // at the group, check there; asked again, a new booking is made.
1289
+ if (people.length &&
1290
+ people.every((o) => now() - Date.parse(o.needsPersonAt ?? o.createdAt) >= NEEDS_PERSON_GIVE_UP_MS)) {
1291
+ for (const o of people) {
1292
+ o.rebookable = true;
1293
+ put(o);
1294
+ }
1295
+ log(`[otaless] ${people.length} order(s) at ${group} still with a person after ${NEEDS_PERSON_GIVE_UP_MS / 3_600_000}h - the stay is no longer held back`);
1296
+ return {
1297
+ failed: failedStay(label, `An earlier booking of this stay is still with a person to sort out with ${name}, and it may stand there: the guest should check with ${name} before booking it again. Nothing new was booked this time - asked again, a new booking is made.`,
1298
+ // New for the card (酒店平台's next version): until then, these words.
1299
+ 'earlier-needs-person-overdue'),
1300
+ };
1301
+ }
1302
+ const person = people[0];
1003
1303
  if (person)
1004
1304
  return {
1005
1305
  failed: failedStay(label, person.needsPerson === 'release'
@@ -1246,7 +1546,11 @@ export const createOtalessBookings = (deps) => {
1246
1546
  roomName: p.roomName,
1247
1547
  stay: p.stay,
1248
1548
  rooms: p.rooms,
1249
- agreed: p.text,
1549
+ // A step 8 asked for (questions #66, #68): the room's terms were agreed first; this now.
1550
+ agreed: ask.token ? (ask.roomTerms ?? '') : p.text,
1551
+ ...(ask.token
1552
+ ? { groupStep: { step: ask.step ?? 'terms', text: p.text, token: ask.token } }
1553
+ : {}),
1250
1554
  guest: ask.guest,
1251
1555
  total: p.total,
1252
1556
  });
@@ -1317,6 +1621,7 @@ export const createOtalessBookings = (deps) => {
1317
1621
  if (paid) {
1318
1622
  if (amount && !asked(amount, r)) {
1319
1623
  r.needsPerson = 'paid-amount';
1624
+ r.needsPersonAt = iso();
1320
1625
  payLinks.delete(r.id);
1321
1626
  put(r);
1322
1627
  log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${r.group} paid ${amount.currency} ${amount.minor}, not what was asked - not taken as paid`);
@@ -1384,6 +1689,7 @@ export const createOtalessBookings = (deps) => {
1384
1689
  const amount = moneyOf(row.totalAmount, row.currency);
1385
1690
  if (!amount || !asked(amount, r)) {
1386
1691
  r.needsPerson = 'paid-amount';
1692
+ r.needsPersonAt = iso();
1387
1693
  payLinks.delete(r.id);
1388
1694
  put(r);
1389
1695
  log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${group} is listed PAID at ${amount ? `${amount.currency} ${amount.minor}` : 'no amount'}, not what was asked - not taken as paid`);
@@ -1406,7 +1712,10 @@ export const createOtalessBookings = (deps) => {
1406
1712
  };
1407
1713
  if (r.status === 'awaiting-payment' && !payLinks.has(r.id)) {
1408
1714
  // Held to this order's own page where the list says so (11-11, B).
1409
- const ok = payPageOf(text(row.checkoutUrl ?? undefined), deps.checkoutHosts, r.id);
1715
+ const ok = payPageOf(text(row.checkoutUrl ?? undefined), deps.checkoutHosts, {
1716
+ id: r.id,
1717
+ code: r.code,
1718
+ });
1410
1719
  if (ok)
1411
1720
  payLinks.set(r.id, ok.url);
1412
1721
  }
@@ -1448,7 +1757,10 @@ export const createOtalessBookings = (deps) => {
1448
1757
  continue;
1449
1758
  }
1450
1759
  const s = statusOfRow(row.status);
1451
- const ok = payPageOf(text(row.checkoutUrl ?? undefined), deps.checkoutHosts, id);
1760
+ const ok = payPageOf(text(row.checkoutUrl ?? undefined), deps.checkoutHosts, {
1761
+ id,
1762
+ code: text(row.originalOrderId),
1763
+ });
1452
1764
  orders.delete(o.id);
1453
1765
  o.id = id;
1454
1766
  o.code = text(row.originalOrderId) ?? '';