@curless/hotel-storefront 0.68.1 → 0.68.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.
@@ -89,6 +89,15 @@ const DEAD_ROW_STATUSES = new Set([
89
89
  'EXPIRED',
90
90
  'CLOSED',
91
91
  ]);
92
+ /**
93
+ * What an order row (12) says of an order that still stands - the document's
94
+ * own names. ⚠️ ONLY THESE (总线程's review of 0.68.2, P3): a refusal's order
95
+ * listed as anything else - REJECTED, SOLD_OUT, a name not yet seen - is taken
96
+ * as one that holds nothing, and the log says what it was. (`couldBe` keeps
97
+ * `DEAD_ROW_STATUSES`: there, a name not known holds the stay back - the
98
+ * careful way round.)
99
+ */
100
+ const LIVE_ROW_STATUSES = new Set(['PENDING_PAYMENT', 'CONFIRMED', 'PAID']);
92
101
  /** The longest terms shown word for word - longer is refused rather than cut. */
93
102
  export const TERMS_MAX = 50_000;
94
103
  /** A guest's reason for cancelling, as the group's staff are sent it. */
@@ -953,6 +962,7 @@ export const createOtalessBookings = (deps) => {
953
962
  terms: 'terms not agreed by the guest',
954
963
  confirm: 'not confirmed by the guest',
955
964
  'confirm-unsent': 'its confirmation cannot be sent back yet',
965
+ refused: 'refused when it was ordered',
956
966
  };
957
967
  /** Thrown where there is no sign-in at the order's group to cancel it with. */
958
968
  class NotSignedIn extends Error {
@@ -1004,6 +1014,7 @@ export const createOtalessBookings = (deps) => {
1004
1014
  terms: `${g} asked for terms of its own to be agreed to when it was ordered, which cannot be shown here`,
1005
1015
  confirm: `${g} made the order asking for it to be confirmed, with nothing here the guest could confirm as they were shown it`,
1006
1016
  'confirm-unsent': `${g} asked for the order to be confirmed first, and that confirmation cannot be sent back from here yet`,
1017
+ refused: `${g} turned the booking down, yet made the order its answer named`,
1007
1018
  })[why];
1008
1019
  /** An order made that is not the one asked for: cancelled at once, and said as it came out. */
1009
1020
  const letGo = async (a, r, why) => {
@@ -1080,6 +1091,103 @@ export const createOtalessBookings = (deps) => {
1080
1091
  for (const r of [...orders.values()])
1081
1092
  if (r.held && !r.released)
1082
1093
  holdFor(r);
1094
+ /**
1095
+ * ⚠️ AN ORDER 8 NAMES THAT IS ONE OF OURS ALREADY IS NOT TOUCHED (总线程's
1096
+ * review of 0.68.2, P2): a refusal - "already booked", say - may name the
1097
+ * guest's earlier order, paid perhaps, or another account's linked to the
1098
+ * same membership. Kept here by its id, by a refusal naming it, or by its
1099
+ * group's own number: never cancelled, overwritten or claimed from this
1100
+ * booking - logged, and said as a refusal (`untouched`).
1101
+ */
1102
+ const knownNamed = (group, id, code) => orders.has(id) ||
1103
+ [...orders.values()].some((r) => r.refusedOrder?.id === id || (!!code && r.group === group && r.code === code));
1104
+ const untouched = (a, id, how) => {
1105
+ const guest = [a.guest.name, a.guest.email, a.guest.phone];
1106
+ 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`);
1107
+ return {
1108
+ failed: failedStay(a, `${deps.groupOf(a.group).name} did not take the booking, so nothing was booked or charged.`),
1109
+ };
1110
+ };
1111
+ /** The order a refusal of 8 named - `refusedOrder` - if it named one. */
1112
+ const refusalOf = (a, data, said) => {
1113
+ if (!data || typeof data !== 'object')
1114
+ return undefined;
1115
+ const w = data;
1116
+ const id = text(w.unifiedOrderId);
1117
+ if (!id)
1118
+ return undefined;
1119
+ const code = text(w.originalOrderId);
1120
+ return {
1121
+ id,
1122
+ ...(code ? { code } : {}),
1123
+ said: forTheLog(String(said ?? w.errorMessage ?? w.status ?? '?'), [
1124
+ a.guest.name,
1125
+ a.guest.email,
1126
+ a.guest.phone,
1127
+ ]),
1128
+ };
1129
+ };
1130
+ /**
1131
+ * ⚠️ A REFUSAL THAT NAMES AN ORDER IS NOT TAKEN AS NOTHING (总线程 11-12, on
1132
+ * 0.68.0's review): 8 refused - sold out, say, not a step - and its answer
1133
+ * named an order. Kept as not known first (`refusedOrder`), and looked for in
1134
+ * the group's list (12) at once - never ordered again:
1135
+ * - listed, live: that order, as 8 had answered it - cancelled by 9 with
1136
+ * `proceedWithoutGroupConfirm` off; checked as any order with it on (`settle`);
1137
+ * - listed as failed or closed, or not listed: the refusal stands, said as it
1138
+ * came; the stay is not held back - the log says what 12 had;
1139
+ * - the list not read: as for 10014 - not known, the stay held back until it is.
1140
+ */
1141
+ const refusedWithOrder = async (a, ref, pairs) => {
1142
+ const g = deps.groupOf(a.group);
1143
+ if (knownNamed(a.group, ref.id, ref.code))
1144
+ return untouched(a, ref.id, 'refused');
1145
+ const r = recordOf(a, pairs, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1146
+ r.refusedOrder = ref;
1147
+ put(r);
1148
+ const guest = [a.guest.name, a.guest.email, a.guest.phone];
1149
+ const said = `booking at ${a.group} refused "${ref.said}" with order ${forTheLog(ref.id, guest)} in its answer`;
1150
+ let rows;
1151
+ try {
1152
+ rows = await withSession(a.acct, a.who, (cred) => client.orders({ providerCode: a.group }, cred, deps.callerOf(a.who)));
1153
+ }
1154
+ catch {
1155
+ rows = undefined;
1156
+ }
1157
+ if (!rows) {
1158
+ log(`[otaless] ${said} - its order list (12) not read: kept as not known, as for 10014`);
1159
+ return { booked: toBooking(r) };
1160
+ }
1161
+ orders.delete(r.id);
1162
+ save();
1163
+ const row = rows.find((x) => text(x.unifiedOrderId) === ref.id);
1164
+ const st = String(row?.status ?? '')
1165
+ .trim()
1166
+ .toUpperCase();
1167
+ if (!row || !LIVE_ROW_STATUSES.has(st)) {
1168
+ log(`[otaless] ${said}; its order list (12) has it ${row ? forTheLog(st, guest) : 'not at all'} - not booked`);
1169
+ return {
1170
+ failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
1171
+ };
1172
+ }
1173
+ log(`[otaless] ${said}; its order list (12) has it ${forTheLog(st || '?', guest)} - ${deps.proceedWithoutGroupConfirm ? 'checked as any order' : 'cancelled (proceedWithoutGroupConfirm off)'}`);
1174
+ const code = text(row.originalOrderId) ?? ref.code ?? '';
1175
+ if (!deps.proceedWithoutGroupConfirm) {
1176
+ const made = recordOf(a, pairs, ref.id, code, 'awaiting-payment');
1177
+ const charged = moneyOf(row.totalAmount, row.currency);
1178
+ if (charged)
1179
+ made.charged = charged;
1180
+ return letGo(a, made, 'refused');
1181
+ }
1182
+ return settle(a, {
1183
+ unifiedOrderId: ref.id,
1184
+ ...(code ? { originalOrderId: code } : {}),
1185
+ ...(row.status !== undefined ? { status: row.status } : {}),
1186
+ ...(row.checkoutUrl !== undefined ? { checkoutUrl: row.checkoutUrl } : {}),
1187
+ ...(row.totalAmount !== undefined ? { totalAmount: row.totalAmount } : {}),
1188
+ ...(row.currency !== undefined ? { currency: row.currency } : {}),
1189
+ }, pairs);
1190
+ };
1083
1191
  /** What 8 answered, settled: an order to hand on, one held for new terms, or none. */
1084
1192
  const settle = async (a, w, pairs) => {
1085
1193
  const g = deps.groupOf(a.group);
@@ -1092,6 +1200,9 @@ export const createOtalessBookings = (deps) => {
1092
1200
  if (!w || w.success === false || status === 'FAILED' || err) {
1093
1201
  if (err)
1094
1202
  log(`[otaless] booking at ${a.group} refused: "${forTheLog(err, [a.guest.name, a.guest.email, a.guest.phone])}"`);
1203
+ const ref = w ? refusalOf(a, w, err ?? status) : undefined;
1204
+ if (ref)
1205
+ return refusedWithOrder(a, ref, pairs);
1095
1206
  return {
1096
1207
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
1097
1208
  };
@@ -1108,6 +1219,8 @@ export const createOtalessBookings = (deps) => {
1108
1219
  return { booked: toBooking(r) };
1109
1220
  }
1110
1221
  const code = text(w.originalOrderId) ?? '';
1222
+ if (knownNamed(a.group, id, code))
1223
+ return untouched(a, id, 'answered');
1111
1224
  const page = text(w.checkoutUrl ?? undefined);
1112
1225
  // ⚠️ "CONFIRMED" WITH A PAYMENT PAGE IS WAITING FOR ITS PAYMENT (questions
1113
1226
  // #53, #61 (1) - 8's own example): paid is what 15 says, never this status.
@@ -1238,6 +1351,8 @@ export const createOtalessBookings = (deps) => {
1238
1351
  let fromBook;
1239
1352
  /** The step 8 was sent once more for, with `termsAccepted` - see `again`. */
1240
1353
  let autoAccepted;
1354
+ /** A refusal for a step naming an order we keep already (`knownNamed`) - see `madeIn`. */
1355
+ let namedKnown;
1241
1356
  /** This stay as it was asked, and - once sent again - as agreed for the guest. */
1242
1357
  const as = () => (autoAccepted ? { ...a, autoAccepted } : a);
1243
1358
  let w;
@@ -1311,10 +1426,17 @@ export const createOtalessBookings = (deps) => {
1311
1426
  const id = text(order.unifiedOrderId);
1312
1427
  if (!id)
1313
1428
  return undefined;
1429
+ // One of ours already: not this booking's - a refusal, not sent again (P2).
1430
+ if (knownNamed(a.group, id, text(order.originalOrderId))) {
1431
+ namedKnown = { err, id };
1432
+ return undefined;
1433
+ }
1314
1434
  const status = String(order.status ?? '')
1315
1435
  .trim()
1316
1436
  .toUpperCase();
1317
- log(`[otaless] booking at ${a.group}: 8 refused "${forTheLog(String(msg))}" with order ${forTheLog(id)} in its answer - kept as that order, not sent again`);
1437
+ // The guest's own details never on the log (总线程's re-review of 0.68.1, 2).
1438
+ const guest = [a.guest.name, a.guest.email, a.guest.phone];
1439
+ 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`);
1318
1440
  return {
1319
1441
  ...order,
1320
1442
  status: /TERMS/.test(status) || stepAsked(status) ? order.status : String(msg),
@@ -1345,6 +1467,8 @@ export const createOtalessBookings = (deps) => {
1345
1467
  booked = await send(body);
1346
1468
  }
1347
1469
  catch (err) {
1470
+ if (err === namedKnown?.err)
1471
+ throw err;
1348
1472
  const step = err instanceof OtalessError
1349
1473
  ? unshown(stepAsked(err.answer?.msg), err.answer?.data)
1350
1474
  : undefined;
@@ -1392,10 +1516,17 @@ export const createOtalessBookings = (deps) => {
1392
1516
  put(r);
1393
1517
  return { booked: toBooking(r) };
1394
1518
  }
1519
+ if (namedKnown && err === namedKnown.err)
1520
+ return untouched(as(), namedKnown.id, 'refused');
1395
1521
  // ⚠️ 8 REFUSED FOR TERMS (robin 11-11 18:30, Ascott: code 500 "terms_required").
1396
1522
  const step = err === fromBook && err instanceof OtalessError ? stepAsked(err.answer?.msg) : undefined;
1397
1523
  if (step)
1398
1524
  return groupAsked(as(), step, err instanceof OtalessError ? err.answer?.data : undefined, pairs ?? { orig: a.total }, err instanceof OtalessError ? err.answer?.msg : undefined);
1525
+ const ref = err === fromBook && err instanceof OtalessError
1526
+ ? refusalOf(as(), err.answer?.data, err.answer?.msg)
1527
+ : undefined;
1528
+ if (ref)
1529
+ return refusedWithOrder(as(), ref, pairs ?? { orig: a.total });
1399
1530
  if (err instanceof OtalessError)
1400
1531
  return {
1401
1532
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
@@ -1909,22 +2040,53 @@ export const createOtalessBookings = (deps) => {
1909
2040
  const hotelId = text(row.unifiedHotelId) ?? text(row.hotelId);
1910
2041
  const roomId = text(row.unifiedRoomId) ?? text(row.roomId);
1911
2042
  const dates = String(row.dates ?? '');
2043
+ // A refusal that named this order (`refusedOrder`): by that id alone.
1912
2044
  const waiting = mine.filter((o) => o.status === 'unconfirmed' &&
1913
2045
  o.id.startsWith('pending:') &&
1914
2046
  !o.released &&
1915
2047
  o.owner === acct.owner &&
1916
2048
  o.group === group &&
1917
- !!hotelId &&
1918
- !!roomId &&
1919
- o.hotelId === hotelId &&
1920
- o.roomId === roomId &&
1921
- dates.includes(o.stay.checkIn) &&
1922
- dates.includes(o.stay.checkOut));
2049
+ (o.refusedOrder
2050
+ ? o.refusedOrder.id === id
2051
+ : !!hotelId &&
2052
+ !!roomId &&
2053
+ o.hotelId === hotelId &&
2054
+ o.roomId === roomId &&
2055
+ dates.includes(o.stay.checkIn) &&
2056
+ dates.includes(o.stay.checkOut)));
1923
2057
  if (waiting.length !== 1)
1924
2058
  continue;
1925
2059
  const o = waiting[0];
1926
2060
  if (!o)
1927
2061
  continue;
2062
+ if (o.refusedOrder) {
2063
+ const st = String(row.status ?? '')
2064
+ .trim()
2065
+ .toUpperCase();
2066
+ const said = `an order 8 refused yet named (${forTheLog(id, [o.guest.name])}) is ${forTheLog(st || '?', [o.guest.name])} in the order list (12) at ${group}`;
2067
+ const dead = !LIVE_ROW_STATUSES.has(st);
2068
+ if (dead || !deps.proceedWithoutGroupConfirm) {
2069
+ orders.delete(o.id);
2070
+ o.id = id;
2071
+ o.code = text(row.originalOrderId) ?? o.refusedOrder.code ?? '';
2072
+ const charged = moneyOf(row.totalAmount, row.currency);
2073
+ if (charged)
2074
+ o.charged = charged;
2075
+ known.add(id);
2076
+ if (dead) {
2077
+ o.status = statusOfRow(st).status ?? (st === 'EXPIRED' ? 'expired' : 'failed');
2078
+ put(o);
2079
+ log(`[otaless] ${said} - not booked`);
2080
+ }
2081
+ else {
2082
+ o.status = 'awaiting-payment';
2083
+ log(`[otaless] ${said} - cancelled (proceedWithoutGroupConfirm off)`);
2084
+ await release(acct, who, o, 'refused');
2085
+ }
2086
+ continue;
2087
+ }
2088
+ // On: claimed below, as any order 8 did not answer for - its amount and page checked.
2089
+ }
1928
2090
  const charged = moneyOf(row.totalAmount, row.currency);
1929
2091
  // ⚠️ AT ANOTHER AMOUNT, NOT CLAIMED: the record keeps its own id and stays
1930
2092
  // unconfirmed - for a person.
@@ -1960,6 +2122,18 @@ export const createOtalessBookings = (deps) => {
1960
2122
  known.add(id);
1961
2123
  put(o);
1962
2124
  }
2125
+ // A refusal's order not in the list at all: the refusal stands - it holds nothing back.
2126
+ for (const o of mine)
2127
+ if (o.refusedOrder &&
2128
+ o.status === 'unconfirmed' &&
2129
+ !o.released &&
2130
+ o.owner === acct.owner &&
2131
+ o.group === group &&
2132
+ !rows.some((row) => text(row.unifiedOrderId) === o.refusedOrder?.id)) {
2133
+ o.status = 'failed';
2134
+ put(o);
2135
+ log(`[otaless] an order 8 refused yet named (${forTheLog(o.refusedOrder.id, [o.guest.name])}) is not in the order list (12) at ${group} - not booked`);
2136
+ }
1963
2137
  return rows;
1964
2138
  };
1965
2139
  /** The guest's orders brought up to date, each by their sign-in at its group (`signed`). */