@curless/hotel-storefront 0.68.0 → 0.68.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.
@@ -953,6 +953,7 @@ export const createOtalessBookings = (deps) => {
953
953
  terms: 'terms not agreed by the guest',
954
954
  confirm: 'not confirmed by the guest',
955
955
  'confirm-unsent': 'its confirmation cannot be sent back yet',
956
+ refused: 'refused when it was ordered',
956
957
  };
957
958
  /** Thrown where there is no sign-in at the order's group to cancel it with. */
958
959
  class NotSignedIn extends Error {
@@ -1004,6 +1005,7 @@ export const createOtalessBookings = (deps) => {
1004
1005
  terms: `${g} asked for terms of its own to be agreed to when it was ordered, which cannot be shown here`,
1005
1006
  confirm: `${g} made the order asking for it to be confirmed, with nothing here the guest could confirm as they were shown it`,
1006
1007
  'confirm-unsent': `${g} asked for the order to be confirmed first, and that confirmation cannot be sent back from here yet`,
1008
+ refused: `${g} turned the booking down, yet made the order its answer named`,
1007
1009
  })[why];
1008
1010
  /** An order made that is not the one asked for: cancelled at once, and said as it came out. */
1009
1011
  const letGo = async (a, r, why) => {
@@ -1080,6 +1082,84 @@ export const createOtalessBookings = (deps) => {
1080
1082
  for (const r of [...orders.values()])
1081
1083
  if (r.held && !r.released)
1082
1084
  holdFor(r);
1085
+ /** The order a refusal of 8 named - `refusedOrder` - if it named one. */
1086
+ const refusalOf = (a, data, said) => {
1087
+ if (!data || typeof data !== 'object')
1088
+ return undefined;
1089
+ const w = data;
1090
+ const id = text(w.unifiedOrderId);
1091
+ if (!id)
1092
+ return undefined;
1093
+ const code = text(w.originalOrderId);
1094
+ return {
1095
+ id,
1096
+ ...(code ? { code } : {}),
1097
+ said: forTheLog(String(said ?? w.errorMessage ?? w.status ?? '?'), [
1098
+ a.guest.name,
1099
+ a.guest.email,
1100
+ a.guest.phone,
1101
+ ]),
1102
+ };
1103
+ };
1104
+ /**
1105
+ * ⚠️ A REFUSAL THAT NAMES AN ORDER IS NOT TAKEN AS NOTHING (总线程 11-12, on
1106
+ * 0.68.0's review): 8 refused - sold out, say, not a step - and its answer
1107
+ * named an order. Kept as not known first (`refusedOrder`), and looked for in
1108
+ * the group's list (12) at once - never ordered again:
1109
+ * - listed, live: that order, as 8 had answered it - cancelled by 9 with
1110
+ * `proceedWithoutGroupConfirm` off; checked as any order with it on (`settle`);
1111
+ * - listed as failed or closed, or not listed: the refusal stands, said as it
1112
+ * came; the stay is not held back - the log says what 12 had;
1113
+ * - the list not read: as for 10014 - not known, the stay held back until it is.
1114
+ */
1115
+ const refusedWithOrder = async (a, ref, pairs) => {
1116
+ const g = deps.groupOf(a.group);
1117
+ const r = recordOf(a, pairs, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1118
+ r.refusedOrder = ref;
1119
+ put(r);
1120
+ const guest = [a.guest.name, a.guest.email, a.guest.phone];
1121
+ const said = `booking at ${a.group} refused "${ref.said}" with order ${forTheLog(ref.id, guest)} in its answer`;
1122
+ let rows;
1123
+ try {
1124
+ rows = await withSession(a.acct, a.who, (cred) => client.orders({ providerCode: a.group }, cred, deps.callerOf(a.who)));
1125
+ }
1126
+ catch {
1127
+ rows = undefined;
1128
+ }
1129
+ if (!rows) {
1130
+ log(`[otaless] ${said} - its order list (12) not read: kept as not known, as for 10014`);
1131
+ return { booked: toBooking(r) };
1132
+ }
1133
+ orders.delete(r.id);
1134
+ save();
1135
+ const row = rows.find((x) => text(x.unifiedOrderId) === ref.id);
1136
+ const st = String(row?.status ?? '')
1137
+ .trim()
1138
+ .toUpperCase();
1139
+ if (!row || DEAD_ROW_STATUSES.has(st)) {
1140
+ log(`[otaless] ${said}; its order list (12) has it ${row ? forTheLog(st, guest) : 'not at all'} - not booked`);
1141
+ return {
1142
+ failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
1143
+ };
1144
+ }
1145
+ log(`[otaless] ${said}; its order list (12) has it ${forTheLog(st || '?', guest)} - ${deps.proceedWithoutGroupConfirm ? 'checked as any order' : 'cancelled (proceedWithoutGroupConfirm off)'}`);
1146
+ const code = text(row.originalOrderId) ?? ref.code ?? '';
1147
+ if (!deps.proceedWithoutGroupConfirm) {
1148
+ const made = recordOf(a, pairs, ref.id, code, 'awaiting-payment');
1149
+ const charged = moneyOf(row.totalAmount, row.currency);
1150
+ if (charged)
1151
+ made.charged = charged;
1152
+ return letGo(a, made, 'refused');
1153
+ }
1154
+ return settle(a, {
1155
+ unifiedOrderId: ref.id,
1156
+ ...(code ? { originalOrderId: code } : {}),
1157
+ ...(row.status !== undefined ? { status: row.status } : {}),
1158
+ ...(row.checkoutUrl !== undefined ? { checkoutUrl: row.checkoutUrl } : {}),
1159
+ ...(row.totalAmount !== undefined ? { totalAmount: row.totalAmount } : {}),
1160
+ ...(row.currency !== undefined ? { currency: row.currency } : {}),
1161
+ }, pairs);
1162
+ };
1083
1163
  /** What 8 answered, settled: an order to hand on, one held for new terms, or none. */
1084
1164
  const settle = async (a, w, pairs) => {
1085
1165
  const g = deps.groupOf(a.group);
@@ -1092,6 +1172,9 @@ export const createOtalessBookings = (deps) => {
1092
1172
  if (!w || w.success === false || status === 'FAILED' || err) {
1093
1173
  if (err)
1094
1174
  log(`[otaless] booking at ${a.group} refused: "${forTheLog(err, [a.guest.name, a.guest.email, a.guest.phone])}"`);
1175
+ const ref = w ? refusalOf(a, w, err ?? status) : undefined;
1176
+ if (ref)
1177
+ return refusedWithOrder(a, ref, pairs);
1095
1178
  return {
1096
1179
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
1097
1180
  };
@@ -1286,10 +1369,42 @@ export const createOtalessBookings = (deps) => {
1286
1369
  return await client.book(b, { token: cred.token, ...(session ? { session } : {}) }, deps.callerOf(a.who));
1287
1370
  }
1288
1371
  catch (err) {
1372
+ // ⚠️ A REFUSAL THAT CARRIES ITS ORDER IS THAT ORDER (总线程's review of
1373
+ // 0.68.0, P2): 8 refusing for a step - code 500 "terms_required" - with
1374
+ // a `unifiedOrderId` in its answer has made one. Never sent again: kept,
1375
+ // and checked as any order a step was asked for with (`settle` - the
1376
+ // amount, the guests, the page; cancelled by 9 with the switch off).
1377
+ const made = madeIn(err);
1378
+ if (made)
1379
+ return { data: made };
1289
1380
  fromBook = err;
1290
1381
  throw err;
1291
1382
  }
1292
1383
  };
1384
+ /** The order a refusal for a step carries, as 8's answer of one asking that step. */
1385
+ const madeIn = (err) => {
1386
+ if (!(err instanceof OtalessError))
1387
+ return undefined;
1388
+ const msg = err.answer?.msg;
1389
+ const step = stepAsked(msg);
1390
+ const data = err.answer?.data;
1391
+ if (!step || !data || typeof data !== 'object')
1392
+ return undefined;
1393
+ const { success: _refused, errorMessage: _why, ...order } = data;
1394
+ const id = text(order.unifiedOrderId);
1395
+ if (!id)
1396
+ return undefined;
1397
+ const status = String(order.status ?? '')
1398
+ .trim()
1399
+ .toUpperCase();
1400
+ // The guest's own details never on the log (总线程's re-review of 0.68.1, 2).
1401
+ const guest = [a.guest.name, a.guest.email, a.guest.phone];
1402
+ 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`);
1403
+ return {
1404
+ ...order,
1405
+ status: /TERMS/.test(status) || stepAsked(status) ? order.status : String(msg),
1406
+ };
1407
+ };
1293
1408
  /**
1294
1409
  * ⚠️ AGREED FOR THE GUEST, ONCE (robin 11-12 「流程不用卡」, through 总线程 -
1295
1410
  * `proceedWithoutGroupConfirm`, the dev backend's deployment alone; off
@@ -1356,7 +1471,9 @@ export const createOtalessBookings = (deps) => {
1356
1471
  // repeat of one that went in a moment ago (10014) - may have made it.
1357
1472
  // Kept to look for in the group's order list (12), said as not known.
1358
1473
  if (err === fromBook && (unanswered(err) || isCode(err, REPEAT_SUBMIT))) {
1359
- const r = recordOf(a, pairs ?? { orig: a.total }, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1474
+ // `as()`: the second 8 - agreed for the guest - is what may have made it
1475
+ // (总线程's review of 0.68.0, P3); its booking says so like any other.
1476
+ const r = recordOf(as(), pairs ?? { orig: a.total }, `pending:${randomBytes(8).toString('hex')}`, '', 'unconfirmed');
1360
1477
  put(r);
1361
1478
  return { booked: toBooking(r) };
1362
1479
  }
@@ -1364,6 +1481,11 @@ export const createOtalessBookings = (deps) => {
1364
1481
  const step = err === fromBook && err instanceof OtalessError ? stepAsked(err.answer?.msg) : undefined;
1365
1482
  if (step)
1366
1483
  return groupAsked(as(), step, err instanceof OtalessError ? err.answer?.data : undefined, pairs ?? { orig: a.total }, err instanceof OtalessError ? err.answer?.msg : undefined);
1484
+ const ref = err === fromBook && err instanceof OtalessError
1485
+ ? refusalOf(as(), err.answer?.data, err.answer?.msg)
1486
+ : undefined;
1487
+ if (ref)
1488
+ return refusedWithOrder(as(), ref, pairs ?? { orig: a.total });
1367
1489
  if (err instanceof OtalessError)
1368
1490
  return {
1369
1491
  failed: failedStay(a, `${g.name} did not take the booking, so nothing was booked or charged.`),
@@ -1877,22 +1999,53 @@ export const createOtalessBookings = (deps) => {
1877
1999
  const hotelId = text(row.unifiedHotelId) ?? text(row.hotelId);
1878
2000
  const roomId = text(row.unifiedRoomId) ?? text(row.roomId);
1879
2001
  const dates = String(row.dates ?? '');
2002
+ // A refusal that named this order (`refusedOrder`): by that id alone.
1880
2003
  const waiting = mine.filter((o) => o.status === 'unconfirmed' &&
1881
2004
  o.id.startsWith('pending:') &&
1882
2005
  !o.released &&
1883
2006
  o.owner === acct.owner &&
1884
2007
  o.group === group &&
1885
- !!hotelId &&
1886
- !!roomId &&
1887
- o.hotelId === hotelId &&
1888
- o.roomId === roomId &&
1889
- dates.includes(o.stay.checkIn) &&
1890
- dates.includes(o.stay.checkOut));
2008
+ (o.refusedOrder
2009
+ ? o.refusedOrder.id === id
2010
+ : !!hotelId &&
2011
+ !!roomId &&
2012
+ o.hotelId === hotelId &&
2013
+ o.roomId === roomId &&
2014
+ dates.includes(o.stay.checkIn) &&
2015
+ dates.includes(o.stay.checkOut)));
1891
2016
  if (waiting.length !== 1)
1892
2017
  continue;
1893
2018
  const o = waiting[0];
1894
2019
  if (!o)
1895
2020
  continue;
2021
+ if (o.refusedOrder) {
2022
+ const st = String(row.status ?? '')
2023
+ .trim()
2024
+ .toUpperCase();
2025
+ 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}`;
2026
+ const dead = DEAD_ROW_STATUSES.has(st);
2027
+ if (dead || !deps.proceedWithoutGroupConfirm) {
2028
+ orders.delete(o.id);
2029
+ o.id = id;
2030
+ o.code = text(row.originalOrderId) ?? o.refusedOrder.code ?? '';
2031
+ const charged = moneyOf(row.totalAmount, row.currency);
2032
+ if (charged)
2033
+ o.charged = charged;
2034
+ known.add(id);
2035
+ if (dead) {
2036
+ o.status = statusOfRow(st).status ?? (st === 'EXPIRED' ? 'expired' : 'failed');
2037
+ put(o);
2038
+ log(`[otaless] ${said} - not booked`);
2039
+ }
2040
+ else {
2041
+ o.status = 'awaiting-payment';
2042
+ log(`[otaless] ${said} - cancelled (proceedWithoutGroupConfirm off)`);
2043
+ await release(acct, who, o, 'refused');
2044
+ }
2045
+ continue;
2046
+ }
2047
+ // On: claimed below, as any order 8 did not answer for - its amount and page checked.
2048
+ }
1896
2049
  const charged = moneyOf(row.totalAmount, row.currency);
1897
2050
  // ⚠️ AT ANOTHER AMOUNT, NOT CLAIMED: the record keeps its own id and stays
1898
2051
  // unconfirmed - for a person.
@@ -1928,6 +2081,18 @@ export const createOtalessBookings = (deps) => {
1928
2081
  known.add(id);
1929
2082
  put(o);
1930
2083
  }
2084
+ // A refusal's order not in the list at all: the refusal stands - it holds nothing back.
2085
+ for (const o of mine)
2086
+ if (o.refusedOrder &&
2087
+ o.status === 'unconfirmed' &&
2088
+ !o.released &&
2089
+ o.owner === acct.owner &&
2090
+ o.group === group &&
2091
+ !rows.some((row) => text(row.unifiedOrderId) === o.refusedOrder?.id)) {
2092
+ o.status = 'failed';
2093
+ put(o);
2094
+ 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`);
2095
+ }
1931
2096
  return rows;
1932
2097
  };
1933
2098
  /** The guest's orders brought up to date, each by their sign-in at its group (`signed`). */