@curless/hotel-storefront 0.72.0 → 0.72.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.
@@ -4,7 +4,7 @@ import { guestTooLong } from '../backend.js';
4
4
  import { nights as nightsOf } from '../dates.js';
5
5
  import { movedSince } from '../huazhu/backend.js';
6
6
  import { decimalsOf, minorFactor, money, toMinor } from '../money.js';
7
- import { CHANNEL_SESSION_EXPIRED, NOT_SIGNED_IN, OtalessBusy, OtalessError, REPEAT_SUBMIT, SESSION_ID, forTheLog, } from './client.js';
7
+ import { CHANNEL_SESSION_EXPIRED, NOT_SIGNED_IN, OtalessBusy, OtalessError, REPEAT_SUBMIT, SESSION_ID, forTheLog, isBookingTerms, priceTaken, } from './client.js';
8
8
  // Booking on the Otaless hotel backend: items 8-16 of its document of
9
9
  // 2026-11-10. Since 2026-11-11 the order is 8 `booking/create` - the backend's
10
10
  // own answer to questions #56: 「booking/create 就行,checkout 所有的 api 不需要」
@@ -17,6 +17,10 @@ import { CHANNEL_SESSION_EXPIRED, NOT_SIGNED_IN, OtalessBusy, OtalessError, REPE
17
17
  // group sent them with the room, word for word - are shown before anything
18
18
  // is ordered, and 8 is called only once the guest agreed on the card's own
19
19
  // button or the one-time page (`acceptTerms`), never on the model's word.
20
+ // A group whose `/site` row says `termsRequired` (the 11-16 document, 0.72.1)
21
+ // is asked for its own terms by 8 itself - `termsRequired: true`, which
22
+ // answers them and orders nothing - and those are what the guest agrees to;
23
+ // only then is 8 sent again, with `termsRequired: false`, to order it.
20
24
  // Terms the group answers the order with that are not the ones agreed hold
21
25
  // the order's payment page back until the guest agrees to them too; not
22
26
  // agreed, or not in `TERMS_MS`, the order is cancelled (9).
@@ -191,6 +195,8 @@ const CHECKED_PER_LOOK = 5;
191
195
  export const DEFAULT_CHECKOUT_HOSTS = ['pay.discoverasr.com'];
192
196
  /** How long a refund's preview can be sent back (总线程's review of 0.63.0, B). */
193
197
  export const REFUND_PREVIEW_MS = 10 * 60_000;
198
+ /** How long the 7 before the group's terms stands for the 7 before the order, on a 10014 (`pricedRecently`). */
199
+ export const PRICED_IN_MS = 30_000;
194
200
  const refusal = (code, message) => Object.assign(new Error(message), { code });
195
201
  const signInRefusal = (code, message, groups) => Object.assign(new Error(message), { code, groups: [...groups] });
196
202
  const num = (v) => {
@@ -508,6 +514,42 @@ export const pageShape = (raw, order) => {
508
514
  * group's answer formatted a little differently is not new terms to agree to.
509
515
  */
510
516
  export const termsKey = (s) => s.normalize('NFKC').replace(/[\s\p{P}]/gu, '');
517
+ /**
518
+ * 8's `termsPoints`, a point a line (0.72.2): a list - or a list written out as JSON text,
519
+ * as apidev sent Ascott's on 11-16 (「["Children: …","Cancellation policy: …",…]」) - is its
520
+ * points, each word for word; any other text is one point, as it came. Nothing translated,
521
+ * nothing dropped: a point that is not text is written as JSON.
522
+ */
523
+ export const pointsOf = (v) => {
524
+ let list = v;
525
+ if (typeof v === 'string') {
526
+ const t = v.trim();
527
+ if (!t)
528
+ return [];
529
+ if (!(t.startsWith('[') && t.endsWith(']')))
530
+ return [t];
531
+ try {
532
+ list = JSON.parse(t);
533
+ }
534
+ catch {
535
+ return [t];
536
+ }
537
+ if (!Array.isArray(list))
538
+ return [t];
539
+ }
540
+ if (!Array.isArray(list))
541
+ return [];
542
+ return list.flatMap((x) => {
543
+ const s = typeof x === 'string'
544
+ ? x.trim()
545
+ : x === null || x === undefined
546
+ ? ''
547
+ : typeof x === 'object'
548
+ ? JSON.stringify(x)
549
+ : String(x);
550
+ return s ? [s] : [];
551
+ });
552
+ };
511
553
  /** Which step a refusal of 8 - or its status - asks for, or none. */
512
554
  export const stepAsked = (said) => {
513
555
  const s = String(said ?? '');
@@ -635,6 +677,18 @@ export const createOtalessBookings = (deps) => {
635
677
  group,
636
678
  };
637
679
  };
680
+ /**
681
+ * Whether `group` asks for its own terms before it is booked - its `/site` row's
682
+ * `termsRequired` (0.72.1; client.ts keeps it by group). `undefined`: not said.
683
+ */
684
+ const groupTermsOf = async (group) => {
685
+ try {
686
+ return await client.termsRequired(group);
687
+ }
688
+ catch {
689
+ return undefined;
690
+ }
691
+ };
638
692
  const signInFirst = (groups) => signInRefusal('sign-in', `Nothing was booked: booking at ${groups.map((g) => deps.groupOf(g).name).join(' and ')} needs the guest signed in there through their ${deps.brand} account - ask them to sign in first.`, groups);
639
693
  /**
640
694
  * One of this guest's orders, by its id or the code the guest quotes, with
@@ -876,6 +930,13 @@ export const createOtalessBookings = (deps) => {
876
930
  // it separately; and an order made with no terms to agree to.
877
931
  r.stepUnsent ? { confirmUnseen: true } : {}, r.noTerms ? { noTermsGiven: true } : {}, r.autoAccepted ? { groupTermsAutoAccepted: true } : {});
878
932
  };
933
+ /**
934
+ * 8 asked for the group's own terms, nothing agreed by the guest yet (0.72.1): sent with
935
+ * `termsRequired: true`, it answers them and orders nothing - as the document has it.
936
+ * Not a room with no terms ordered at once (`noTerms`), nor one whose own terms the guest
937
+ * agreed to (`/site` not saying, `agreed`): those may order.
938
+ */
939
+ const asksTermsOnly = (a) => a.groupTerms === 'ask' && !a.agreed && !a.noTerms;
879
940
  const failedStay = (a, why, code) => ({
880
941
  hotelId: a.hotel.id,
881
942
  hotelName: a.hotel.name,
@@ -888,7 +949,6 @@ export const createOtalessBookings = (deps) => {
888
949
  const limited = (a, name) => failedStay(a, `Not sent to ${name}: too many requests just now - ask again in a minute.`, 'limited');
889
950
  const recordOf = (a, pairs, id, code, status) => ({
890
951
  ...(a.noTerms ? { noTerms: true } : {}),
891
- ...(a.autoAccepted ? { stepUnsent: a.autoAccepted, autoAccepted: true } : {}),
892
952
  id,
893
953
  code,
894
954
  group: a.group,
@@ -957,8 +1017,12 @@ export const createOtalessBookings = (deps) => {
957
1017
  const g = deps.groupOf(code);
958
1018
  return { groupName: g.name, ...(g.names ? { groupNames: g.names } : {}) };
959
1019
  };
960
- /** The terms of one stay, for the guest to agree to before it is ordered. */
961
- const termsBefore = (a) => remember({
1020
+ /**
1021
+ * The terms of one stay, for the guest to agree to before it is ordered. `askGroup`: its
1022
+ * group's `/site` did not say whether it asks for terms of its own - agreed, 8 is sent with
1023
+ * `termsRequired: true` (0.72.1).
1024
+ */
1025
+ const termsBefore = (a, askGroup = false) => remember({
962
1026
  ref: randomBytes(18).toString('base64url'),
963
1027
  group: a.group,
964
1028
  ...groupWords(a.group),
@@ -975,7 +1039,11 @@ export const createOtalessBookings = (deps) => {
975
1039
  until: iso(now() + TERMS_MS),
976
1040
  owner: a.acct.owner,
977
1041
  key: a.acct.key,
978
- ask: { roomId: a.roomId, guest: a.guest },
1042
+ ask: {
1043
+ roomId: a.roomId,
1044
+ guest: a.guest,
1045
+ ...(askGroup ? { groupTerms: 'ask' } : {}),
1046
+ },
979
1047
  });
980
1048
  /** Where what a group asks to agree to or confirm is not what the guest was shown - or nothing. */
981
1049
  const stepOff = (asked, a, pairs) => asked.amountUnread ||
@@ -994,12 +1062,12 @@ export const createOtalessBookings = (deps) => {
994
1062
  * and sent once more with the token. Without both - or asked again after the
995
1063
  * guest did it - nothing is booked, and the log says which it was.
996
1064
  */
997
- const groupAsked = (a, step, data, pairs, msg) => {
1065
+ const groupAsked = (a, step, data, pairs) => {
998
1066
  const g = deps.groupOf(a.group);
999
1067
  const what = `${step}_required`;
1000
1068
  const code = (step === 'terms' ? 'group-terms' : 'group-confirm');
1001
1069
  const asked = groupStepOf(step, data);
1002
- if (asked && !a.groupStep) {
1070
+ if (asked && !a.groupStep && a.groupTerms !== 'agreed') {
1003
1071
  const off = stepOff(asked, a, pairs);
1004
1072
  const amountOff = off === 'another amount';
1005
1073
  if (off) {
@@ -1038,11 +1106,11 @@ export const createOtalessBookings = (deps) => {
1038
1106
  }),
1039
1107
  };
1040
1108
  }
1041
- log(a.autoAccepted
1042
- ? `[otaless] booking at ${a.group}: still refused after a second try with termsAccepted - 8 answered "${forTheLog(msg ?? what, [a.guest.name, a.guest.email, a.guest.phone])}" - not booked`
1043
- : a.groupStep
1044
- ? `[otaless] booking at ${a.group} answered ${what} again, after the guest ${step === 'terms' ? 'agreed to its terms' : 'confirmed it'} - not booked`
1045
- : `[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})`);
1109
+ // After the guest agreed to the group's own terms - 8's (0.72.1) or a step's.
1110
+ const agreedAlready = !!a.groupStep || a.groupTerms === 'agreed';
1111
+ log(agreedAlready
1112
+ ? `[otaless] booking at ${a.group} answered ${what} again, after the guest ${step === 'terms' ? 'agreed to its terms' : 'confirmed it'} - not booked`
1113
+ : `[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})`);
1046
1114
  const again = step === 'terms'
1047
1115
  ? `${g.name} asked for its terms to be agreed to again after the guest agreed to them, so nothing was booked or charged.`
1048
1116
  : `${g.name} asked for the booking to be confirmed again after the guest confirmed it, so nothing was booked or charged.`;
@@ -1050,7 +1118,91 @@ export const createOtalessBookings = (deps) => {
1050
1118
  ? `${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.`
1051
1119
  : `${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.`;
1052
1120
  // New codes for the card (酒店平台's next version): until then, these words.
1053
- return { failed: failedStay(a, a.groupStep ? again : none, code) };
1121
+ return { failed: failedStay(a, agreedAlready ? again : none, code) };
1122
+ };
1123
+ /**
1124
+ * 8 answered the group's own terms, and no order (`WireBookingTerms` - the 11-16 document:
1125
+ * asked with `termsRequired: true` at a group whose `/site` says so; robin 11-16, questions
1126
+ * #66). ⚠️ NEVER AGREED FOR THE GUEST (#52): its `termsPoints` and `cancellation`, word
1127
+ * for word, are the guest's to read and agree to themselves - the card's button or the
1128
+ * one-time page; agreed, the room is priced again and 8 sent once more with
1129
+ * `termsRequired: false`, which orders it. Not agreed: nothing more is sent.
1130
+ * ⚠️ NOT HANDED ON: `next` (words for a model - "ask about add-ons before the yes"), the
1131
+ * `summary` (to the log alone, questions #75), `acceptArgs` and `confirmToken` (how they
1132
+ * go back is questions #79 - not kept). For another hotel or room than the one asked,
1133
+ * with nothing to show, too long to show whole, or answered again after the guest agreed:
1134
+ * nothing is shown and nothing booked.
1135
+ */
1136
+ const termsFrom8 = (a, w) => {
1137
+ const g = deps.groupOf(a.group);
1138
+ const guest = [a.guest.name, a.guest.email, a.guest.phone];
1139
+ const note = text(w.summary ?? undefined);
1140
+ if (note)
1141
+ log(`[otaless] booking at ${a.group}: the summary of its terms, kept to this log: "${summaryForLog(note, guest)}"`);
1142
+ const notBooked = (line, why, code) => {
1143
+ log(`[otaless] booking at ${a.group} answered its terms ${line} - not booked`);
1144
+ return { failed: failedStay(a, why, code) };
1145
+ };
1146
+ if (a.groupTerms === 'agreed')
1147
+ return notBooked('again, after the guest agreed to them (questions #79)', `${g.name} asked for its terms to be agreed to again after the guest agreed to them, so nothing was booked or charged.`);
1148
+ const tail = (id) => id.slice(id.lastIndexOf(':') + 1);
1149
+ const hotelSaid = text(w.unifiedHotelId);
1150
+ const roomSaid = text(w.unifiedRoomId);
1151
+ const groupSaid = text(w.providerCode)?.toLowerCase();
1152
+ if ((hotelSaid && tail(hotelSaid) !== tail(a.hotel.id)) ||
1153
+ (roomSaid && tail(roomSaid) !== tail(a.roomId)) ||
1154
+ (groupSaid && groupSaid !== a.group))
1155
+ return notBooked('for another hotel or room than the one asked', `${g.name} sent terms for another hotel or room than the one asked for, so nothing was booked or charged.`);
1156
+ // Word for word, each as the group wrote it - the same twice is shown once. Its points,
1157
+ // a list, one a line (0.72.2 - robin's Ascott run of 11-16 had them drawn as one string of
1158
+ // JSON, brackets and quotes and all): `pointsOf`.
1159
+ const points = pointsOf(w.termsPoints);
1160
+ const parts = [];
1161
+ for (const t of [
1162
+ points.length > 1 ? points.map((p) => `• ${p}`).join('\n') : (points[0] ?? ''),
1163
+ typeof w.cancellation === 'string' ? w.cancellation.trim() : '',
1164
+ ])
1165
+ if (t && !parts.some((p) => termsKey(p) === termsKey(t)))
1166
+ parts.push(t);
1167
+ const shown = parts.join('\n\n');
1168
+ if (!shown)
1169
+ return notBooked('with nothing to show', `${g.name} asked for its own terms to be agreed to and sent none to show here, so nothing was booked or charged.`, 'group-terms');
1170
+ if (shown.length > TERMS_MAX)
1171
+ return notBooked('too long to show whole', `${g.name}'s terms for this room are too long to show in full here, so nothing was booked or charged.`);
1172
+ const unsent = [
1173
+ w.acceptArgs !== undefined && w.acceptArgs !== null ? 'acceptArgs' : '',
1174
+ text(w.confirmToken ?? undefined) ? 'confirmToken' : '',
1175
+ ].filter(Boolean);
1176
+ log(`[otaless] booking at ${a.group} answered its terms (${[points.length ? `termsPoints${points.length > 1 || Array.isArray(w.termsPoints) ? ` - ${points.length} points` : ''}` : '', text(w.cancellation ?? undefined) ? 'cancellation' : ''].filter(Boolean).join(', ')}) - shown to the guest, nothing ordered${unsent.length ? `; ${unsent.join(', ')} not sent back (questions #79)` : ''}`);
1177
+ return {
1178
+ terms: remember({
1179
+ ref: randomBytes(18).toString('base64url'),
1180
+ group: a.group,
1181
+ ...groupWords(a.group),
1182
+ hotelId: a.hotel.id,
1183
+ hotelName: a.hotel.name,
1184
+ ...(a.hotel.names?.name ? { hotelNames: a.hotel.names.name } : {}),
1185
+ roomName: a.roomName,
1186
+ stay: a.stay,
1187
+ rooms: a.rooms,
1188
+ total: a.total,
1189
+ text: shown,
1190
+ source: 'booking',
1191
+ more: true,
1192
+ askedByGroup: 'terms',
1193
+ until: iso(now() + TERMS_MS),
1194
+ owner: a.acct.owner,
1195
+ key: a.acct.key,
1196
+ // Each point too, shown whole on a line of its own: an order whose terms are one of
1197
+ // them is one the guest agreed to (`agreedTo`).
1198
+ ask: {
1199
+ roomId: a.roomId,
1200
+ guest: a.guest,
1201
+ groupTerms: 'answered',
1202
+ parts: points.length > 1 ? [...parts, ...points] : parts,
1203
+ },
1204
+ }),
1205
+ };
1054
1206
  };
1055
1207
  /**
1056
1208
  * The terms an order is held back for (总线程 11-11, D): those kept - or,
@@ -1339,8 +1491,34 @@ export const createOtalessBookings = (deps) => {
1339
1491
  ...(row.currency !== undefined ? { currency: row.currency } : {}),
1340
1492
  }, pairs);
1341
1493
  };
1494
+ /**
1495
+ * An order's terms (8's `cancelPolicy`) are what the guest agreed to: the same words, but
1496
+ * for spacing and punctuation - or, agreed to as 8 answered the group's own terms (0.72.1),
1497
+ * one of the parts they were shown, WHOLE (its `cancellation` alone, say). ⚠️ NEVER A PIECE
1498
+ * OF ONE: "one night is retained" cut out of "after 18:00 the day before, one night is
1499
+ * retained" drops the condition the guest agreed to.
1500
+ */
1501
+ const agreedTo = (said, a) => {
1502
+ const k = termsKey(said);
1503
+ if (k === termsKey(a.agreed))
1504
+ return true;
1505
+ if (a.groupStep?.step === 'terms' && k === termsKey(a.groupStep.text))
1506
+ return true;
1507
+ return a.groupTerms === 'agreed' && (a.agreedParts ?? []).some((p) => termsKey(p) === k);
1508
+ };
1342
1509
  /** What 8 answered, settled: an order to hand on, one held for new terms, or none. */
1343
1510
  const settle = async (a, w, pairs) => {
1511
+ // ⚠️ THE GROUP'S TERMS, NOT AN ORDER (0.72.1): 8 asked with `termsRequired: true`
1512
+ // answers them with no order id - never an order "being looked into" (核对中) below.
1513
+ // ⚠️ ONLY AN 8 SENT WITH `termsRequired` (总线程's review of 0.72.1, P3-1): at a group
1514
+ // that asks for no terms (Atour, JI - `/site` false) every answer is an order's, and
1515
+ // one with no id is one being looked into - never "not booked", booked again.
1516
+ if (a.groupTerms && isBookingTerms(w))
1517
+ return termsFrom8(a, w);
1518
+ return settleOrder(a, w, pairs);
1519
+ };
1520
+ /** What 8 answered with - an order, a refusal or nothing - settled (`settle`). */
1521
+ const settleOrder = async (a, w, pairs) => {
1344
1522
  const g = deps.groupOf(a.group);
1345
1523
  // ⚠️ 8's `summary` IS THE BACKEND'S OWN NOTE - TO THE LOG ALONE (0.71.1, 总线程 11-13):
1346
1524
  // Atour's said "未检测到你名下可用的 VCC … 用 ?api_key=<你的 sk_test> 重连 … 会员:<name>
@@ -1369,7 +1547,7 @@ export const createOtalessBookings = (deps) => {
1369
1547
  // Terms of its own to agree to, and no order (questions #66).
1370
1548
  const step = /TERMS/.test(status) ? 'terms' : stepAsked(status);
1371
1549
  if (step)
1372
- return groupAsked(a, step, w, pairs, status);
1550
+ return groupAsked(a, step, w, pairs);
1373
1551
  // Something may be held, under a number we were not told: kept to look for (12).
1374
1552
  const r = recordOf(a, pairs, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1375
1553
  put(r);
@@ -1488,17 +1666,32 @@ export const createOtalessBookings = (deps) => {
1488
1666
  r.stepUnsent = step;
1489
1667
  log(`[otaless] booking ${forTheLog(id)} at ${a.group} answered ${step}_required with nothing to ${step === 'terms' ? 'agree to' : 'confirm'} and no token - handed on with its order, the checks passed (proceedWithoutGroupConfirm; questions #${q})`);
1490
1668
  }
1491
- if (said &&
1492
- termsKey(said) !== termsKey(a.agreed) &&
1493
- !(a.groupStep?.step === 'terms' && termsKey(said) === termsKey(a.groupStep.text))) {
1669
+ // ⚠️ ORDERED WHEN ITS TERMS WERE ASKED FOR, NOTHING AGREED YET (0.72.1): 8 sent with
1670
+ // `termsRequired: true` made the order - a group whose `/site` said it asks for terms
1671
+ // and then did not. The order's own terms - or the room's - are the guest's to agree to
1672
+ // before its page is handed on; with none to show, it is cancelled.
1673
+ // (A room with no terms ordered at once - `noTerms`, proceedWithoutGroupConfirm - is not.)
1674
+ const unagreed = asksTermsOnly(a);
1675
+ const toAgree = unagreed
1676
+ ? (said ?? text(a.roomTerms))
1677
+ : said && !agreedTo(said, a)
1678
+ ? said
1679
+ : undefined;
1680
+ if (unagreed && !toAgree) {
1681
+ log(`[otaless] booking ${forTheLog(id)} at ${a.group} was made when its terms were asked for, with no terms for the guest to agree to - cancelled (questions #79)`);
1682
+ return letGo(a, r, 'terms');
1683
+ }
1684
+ if (toAgree) {
1494
1685
  // ⚠️ NOT THE TERMS AGREED TO: the order stands, its payment page is held
1495
1686
  // back until the guest agrees to these too - not agreed, or not in time,
1496
1687
  // it is cancelled.
1497
- r.terms = said;
1688
+ r.terms = toAgree;
1498
1689
  r.termsNew = true;
1499
1690
  r.held = { until: iso(now() + TERMS_MS) };
1500
1691
  put(r);
1501
- log(`[otaless] booking ${forTheLog(id)} at ${a.group} came with other terms than the guest agreed to - its payment page held back`);
1692
+ log(unagreed
1693
+ ? `[otaless] booking ${forTheLog(id)} at ${a.group} was made when its terms were asked for (termsRequired) - its payment page held back for the guest to agree to its terms (questions #79)`
1694
+ : `[otaless] booking ${forTheLog(id)} at ${a.group} came with other terms than the guest agreed to - its payment page held back`);
1502
1695
  holdFor(r, a.acct.key);
1503
1696
  const t = termsOfHeld(r, a.acct);
1504
1697
  return t ? { terms: t } : { booked: toBooking(r) };
@@ -1508,6 +1701,30 @@ export const createOtalessBookings = (deps) => {
1508
1701
  put(r);
1509
1702
  return { booked: toBooking(r) };
1510
1703
  };
1704
+ /**
1705
+ * ⚠️ THE 7 BEFORE THE GROUP'S TERMS, FOR THE 7 BEFORE THE ORDER (0.72.1): a guest who
1706
+ * agrees within the backend's `@RepeatSubmit` window has the same 7 refused (10014) -
1707
+ * the same member's same room, nights, adults and rooms priced in their session a moment
1708
+ * ago is that price, as `recentQuote` is for the price shown (0.71.2). Only on a 10014,
1709
+ * only `PRICED_IN_MS`, by the sign-in's token - in memory alone.
1710
+ */
1711
+ const pricedIn = new Map();
1712
+ const pricedKey = (a, token) => `${hash(token)}|${a.hotel.id}|${a.roomId}|${a.stay.checkIn}|${a.stay.checkOut}|${a.stay.guests}|${a.rooms}`;
1713
+ const pricedJustNow = (a, token, data) => {
1714
+ const k = pricedKey(a, token);
1715
+ pricedIn.delete(k);
1716
+ pricedIn.set(k, { at: now(), data });
1717
+ while (pricedIn.size > PENDING_MAX) {
1718
+ const first = pricedIn.keys().next().value;
1719
+ if (first === undefined)
1720
+ break;
1721
+ pricedIn.delete(first);
1722
+ }
1723
+ };
1724
+ const pricedRecently = (a, token) => {
1725
+ const hit = pricedIn.get(pricedKey(a, token));
1726
+ return hit && now() - hit.at <= PRICED_IN_MS ? hit.data : undefined;
1727
+ };
1511
1728
  class Moved extends Error {
1512
1729
  }
1513
1730
  class NotPriced extends Error {
@@ -1531,12 +1748,8 @@ export const createOtalessBookings = (deps) => {
1531
1748
  let pairs;
1532
1749
  /** What 8 itself threw, if it did: only that may have made an order. */
1533
1750
  let fromBook;
1534
- /** The step 8 was sent once more for, with `termsAccepted` - see `again`. */
1535
- let autoAccepted;
1536
1751
  /** A refusal for a step naming an order we keep already (`knownNamed`) - see `madeIn`. */
1537
1752
  let namedKnown;
1538
- /** This stay as it was asked, and - once sent again - as agreed for the guest. */
1539
- const as = () => (autoAccepted ? { ...a, autoAccepted } : a);
1540
1753
  let w;
1541
1754
  try {
1542
1755
  w = await withSession(a.acct, a.who, async (cred) => {
@@ -1556,11 +1769,15 @@ export const createOtalessBookings = (deps) => {
1556
1769
  // asked once more after the pause, and a second 10014 is the group's "wait a moment".
1557
1770
  try {
1558
1771
  priced = await ask('once');
1772
+ pricedJustNow(a, cred.token, priced.data);
1559
1773
  }
1560
1774
  catch (err) {
1561
1775
  if (!isCode(err, REPEAT_SUBMIT))
1562
1776
  throw notPriced(err);
1563
- const shown = deps.recentQuote?.(a.hotel, a.roomId, a.stay, a.rooms, cred.token);
1777
+ // The 7 before the group's terms (0.72.1) a moment ago, in this same sign-in - the
1778
+ // guest agreed quickly - or the price the guest was shown.
1779
+ const shown = pricedRecently(a, cred.token) ??
1780
+ deps.recentQuote?.(a.hotel, a.roomId, a.stay, a.rooms, cred.token);
1564
1781
  if (shown) {
1565
1782
  priced = { data: shown };
1566
1783
  log(`[otaless] booking at ${a.group}: 7 answered 10014 - the same quote went in a moment ago; the price the guest was shown then, under the same sign-in, is the one booked at (questions #58)`);
@@ -1573,7 +1790,12 @@ export const createOtalessBookings = (deps) => {
1573
1790
  throw notPriced(again);
1574
1791
  }
1575
1792
  }
1576
- const orig = moneyOf(priced.data?.originalAmount, priced.data?.originalCurrency);
1793
+ // Its own currency - or, with none, its settlement's (`priceTaken`, questions #45):
1794
+ // the same as the price the guest was shown, so held to it the same.
1795
+ const p = priceTaken(priced.data);
1796
+ const orig = moneyOf(p.amount, p.currency);
1797
+ if (p.settled && orig)
1798
+ deps.pricedInSettlement?.(a.hotel.id);
1577
1799
  // ⚠️ THE PRICE THE GUEST SAW, TO THE CENT, OR 8 IS NOT CALLED.
1578
1800
  if (!orig || !same(orig, a.total))
1579
1801
  throw new Moved('moved');
@@ -1593,6 +1815,9 @@ export const createOtalessBookings = (deps) => {
1593
1815
  checkIn: a.stay.checkIn,
1594
1816
  checkOut: a.stay.checkOut,
1595
1817
  roomCount: a.rooms,
1818
+ // 8's own two steps (0.72.1): the group's terms asked for, or - agreed by the
1819
+ // guest themselves - the order. Not sent at a group that asks for no terms.
1820
+ ...(a.groupTerms ? { termsRequired: a.groupTerms === 'ask' } : {}),
1596
1821
  // Only after the guest agreed to the terms 8 asked for (questions #66).
1597
1822
  ...(a.groupStep?.step === 'terms'
1598
1823
  ? { termsToken: a.groupStep.token, termsAccepted: true }
@@ -1647,53 +1872,11 @@ export const createOtalessBookings = (deps) => {
1647
1872
  status: /TERMS/.test(status) || stepAsked(status) ? order.status : String(msg),
1648
1873
  };
1649
1874
  };
1650
- /**
1651
- * ⚠️ AGREED FOR THE GUEST, ONCE (robin 11-12 「流程不用卡」, through 总线程 -
1652
- * `proceedWithoutGroupConfirm`, the dev backend's deployment alone; off
1653
- * before real money): a step 8 asked for with nothing to show and no
1654
- * order - not one the guest already did - is sent once more, in this
1655
- * same session, with `termsAccepted` (item 13's names, borrowed).
1656
- */
1657
- const unshown = (step, data) => step && deps.proceedWithoutGroupConfirm && !a.groupStep && !groupStepOf(step, data)
1658
- ? step
1659
- : undefined;
1660
- const again = async (step) => {
1661
- autoAccepted = step;
1662
- fromBook = undefined;
1663
- log(`[otaless] booking at ${a.group}: 8 answered ${step}_required with nothing to show and no order - sent once more with termsAccepted (proceedWithoutGroupConfirm, robin 11-12; questions #${step === 'terms' ? 66 : 68})`);
1664
- return send({
1665
- ...body,
1666
- termsAccepted: true,
1667
- extraParams: { terms_accepted: true, confirmed: true },
1668
- });
1669
- };
1670
- let booked;
1671
- try {
1672
- booked = await send(body);
1673
- }
1674
- catch (err) {
1675
- if (err === namedKnown?.err)
1676
- throw err;
1677
- const step = err instanceof OtalessError
1678
- ? unshown(stepAsked(err.answer?.msg), err.answer?.data)
1679
- : undefined;
1680
- if (!step)
1681
- throw err;
1682
- booked = await again(step);
1683
- }
1684
- const first = booked.data;
1685
- const said = String(first?.status ?? '')
1686
- .trim()
1687
- .toUpperCase();
1688
- if (!autoAccepted &&
1689
- first &&
1690
- !text(first.unifiedOrderId) &&
1691
- first.success !== false &&
1692
- !text(first.errorMessage ?? undefined)) {
1693
- const step = unshown(/TERMS/.test(said) ? 'terms' : stepAsked(said), first);
1694
- if (step)
1695
- booked = await again(step);
1696
- }
1875
+ // ⚠️ NEVER SENT AGAIN FOR THE GUEST (0.72.1): the second 8 "agreed" with
1876
+ // `termsAccepted` - for a step asked with nothing to show - is gone. The backend took
1877
+ // no such field (11-12, questions #66), and a group that asks for terms now answers
1878
+ // them itself (`termsRequired`); a step asked with nothing to show is not booked.
1879
+ const booked = await send(body);
1697
1880
  const back = booked.session ?? priced.session;
1698
1881
  return { data: booked.data, ...(back ? { session: back } : {}) };
1699
1882
  });
@@ -1714,34 +1897,46 @@ export const createOtalessBookings = (deps) => {
1714
1897
  };
1715
1898
  if (err instanceof OtalessBusy)
1716
1899
  return { failed: limited(a, g.name) };
1900
+ // ⚠️ THE GROUP'S TERMS ASKED FOR, NOTHING AGREED - A 10014 IS NO ORDER (总线程's review
1901
+ // of 0.72.1, P2-1): 8 sent with `termsRequired: true` answers the terms and orders
1902
+ // nothing, so its repeat refused (10014) - Book pressed twice at Ascott, or asked again
1903
+ // by the model within the window - is a repeat of terms asked a moment ago, never an
1904
+ // order "being looked into": kept as one, it held the stay back for
1905
+ // UNCONFIRMED_GIVE_UP_MS once the guest agreed on the first card. Said as the group's
1906
+ // "wait a moment", nothing kept. (One that did not answer is kept as below all the
1907
+ // same: a `/site` out of date may have it order at once.)
1908
+ if (err === fromBook && isCode(err, REPEAT_SUBMIT) && asksTermsOnly(a)) {
1909
+ log(`[otaless] booking at ${a.group}: 8 asking for its terms answered 10014 - a repeat of terms asked a moment ago, which order nothing; not booked, nothing kept`);
1910
+ return {
1911
+ failed: failedStay(a, `${g.name} asked us to wait a moment before asking for its terms again, so nothing was booked or charged - the guest can press Book again in a few seconds.`, 'group-wait'),
1912
+ };
1913
+ }
1717
1914
  // ⚠️ NOT SENT AGAIN (§12.1): 8 itself not answering - or refusing it as a
1718
1915
  // repeat of one that went in a moment ago (10014) - may have made it.
1719
1916
  // Kept to look for in the group's order list (12), said as not known.
1720
1917
  if (err === fromBook && (unanswered(err) || isCode(err, REPEAT_SUBMIT))) {
1721
- // `as()`: the second 8 - agreed for the guest - is what may have made it
1722
- // (总线程's review of 0.68.0, P3); its booking says so like any other.
1723
- const r = recordOf(as(), pairs ?? { orig: a.total }, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1918
+ const r = recordOf(a, pairs ?? { orig: a.total }, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1724
1919
  put(r);
1725
1920
  return { booked: toBooking(r) };
1726
1921
  }
1727
1922
  if (namedKnown && err === namedKnown.err)
1728
- return untouched(as(), namedKnown.id, 'refused');
1923
+ return untouched(a, namedKnown.id, 'refused');
1729
1924
  // ⚠️ 8 REFUSED FOR TERMS (robin 11-11 18:30, Ascott: code 500 "terms_required").
1730
1925
  const step = err === fromBook && err instanceof OtalessError ? stepAsked(err.answer?.msg) : undefined;
1731
1926
  if (step)
1732
- return groupAsked(as(), step, err instanceof OtalessError ? err.answer?.data : undefined, pairs ?? { orig: a.total }, err instanceof OtalessError ? err.answer?.msg : undefined);
1927
+ return groupAsked(a, step, err instanceof OtalessError ? err.answer?.data : undefined, pairs ?? { orig: a.total });
1733
1928
  const ref = err === fromBook && err instanceof OtalessError
1734
- ? refusalOf(as(), err.answer?.data, err.answer?.msg)
1929
+ ? refusalOf(a, err.answer?.data, err.answer?.msg)
1735
1930
  : undefined;
1736
1931
  if (ref)
1737
- return refusedWithOrder(as(), ref, pairs ?? { orig: a.total });
1932
+ return refusedWithOrder(a, ref, pairs ?? { orig: a.total });
1738
1933
  if (err instanceof OtalessError)
1739
1934
  return {
1740
1935
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
1741
1936
  };
1742
1937
  throw err;
1743
1938
  }
1744
- return settle(as(), w, pairs ?? { orig: a.total });
1939
+ return settle(a, w, pairs ?? { orig: a.total });
1745
1940
  };
1746
1941
  /**
1747
1942
  * An order row that could be an order 8 did not answer for: of the group,
@@ -2002,12 +2197,41 @@ export const createOtalessBookings = (deps) => {
2002
2197
  guest: g0,
2003
2198
  total: line.total,
2004
2199
  };
2200
+ // ⚠️ WHETHER THE GROUP ASKS FOR ITS OWN TERMS: its `/site` row (the 11-16 document,
2201
+ // 0.72.1) - `true`: 8 itself is asked for them now (`termsRequired: true`, nothing is
2202
+ // ordered), and they - not the room's - are what the guest agrees to; `false`: the
2203
+ // room's terms, and one 8 orders it, as before; not said (not listed, a backend before
2204
+ // the document, `/site` not read since): the room's terms first, and 8 is then sent
2205
+ // with `termsRequired: true` all the same - a group that asks for terms answers them,
2206
+ // and any other orders it, as the document has it.
2207
+ const needsTerms = await groupTermsOf(group);
2208
+ if (needsTerms) {
2209
+ inFlight.add(lockKey);
2210
+ try {
2211
+ const out = await place({ ...ask, agreed: '', roomTerms: agreed, groupTerms: 'ask' });
2212
+ if (out.booked)
2213
+ booked.push(out.booked);
2214
+ if (out.failed)
2215
+ failed.push(out.failed);
2216
+ if (out.terms)
2217
+ terms.push(out.terms);
2218
+ }
2219
+ finally {
2220
+ inFlight.delete(lockKey);
2221
+ }
2222
+ continue;
2223
+ }
2224
+ const unsaid = needsTerms === undefined;
2005
2225
  // No terms for this room to agree to, and the deployment lets that go on
2006
2226
  // (robin 11-12 「条款没有也让走下一步」): ordered now - said on the booking.
2007
2227
  if (!agreed && deps.proceedWithoutGroupConfirm) {
2008
2228
  inFlight.add(lockKey);
2009
2229
  try {
2010
- const out = await place({ ...ask, noTerms: true });
2230
+ const out = await place({
2231
+ ...ask,
2232
+ noTerms: true,
2233
+ ...(unsaid ? { groupTerms: 'ask' } : {}),
2234
+ });
2011
2235
  if (out.booked)
2012
2236
  booked.push(out.booked);
2013
2237
  if (out.failed)
@@ -2021,7 +2245,7 @@ export const createOtalessBookings = (deps) => {
2021
2245
  continue;
2022
2246
  }
2023
2247
  // ⚠️ NOTHING IS ORDERED YET (§15.1): the terms first, for the guest to agree to.
2024
- terms.push(termsBefore(ask));
2248
+ terms.push(termsBefore(ask, unsaid));
2025
2249
  }
2026
2250
  return {
2027
2251
  booked,
@@ -2107,6 +2331,13 @@ export const createOtalessBookings = (deps) => {
2107
2331
  ...(ask.token
2108
2332
  ? { groupStep: { step: ask.step ?? 'terms', text: p.text, token: ask.token } }
2109
2333
  : {}),
2334
+ // 8's own two steps (0.72.1): the group's terms as 8 answered them, agreed - the
2335
+ // order now; or the room's, at a group whose `/site` did not say - 8 asks for them.
2336
+ ...(ask.groupTerms === 'answered'
2337
+ ? { groupTerms: 'agreed', ...(ask.parts ? { agreedParts: ask.parts } : {}) }
2338
+ : ask.groupTerms === 'ask'
2339
+ ? { groupTerms: 'ask' }
2340
+ : {}),
2110
2341
  guest: ask.guest,
2111
2342
  total: p.total,
2112
2343
  });