@curless/hotel-storefront 0.84.3 → 0.84.5

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.
package/dist/tools.js CHANGED
@@ -4944,6 +4944,63 @@ const termsAnswer = (deps, ctx, key, terms) => {
4944
4944
  : {}),
4945
4945
  };
4946
4946
  };
4947
+ /**
4948
+ * ⚠️ THE BOOKINGS, THE LATEST ORDERED FIRST (0.84.5, `data.bookingsByTime`; robin 11-26: 「我的预订没有排序,应该最近订的在最上面」;
4949
+ * 0.84.4 had them in parts by state, which he did not see as an order): the card's own order, in words - ONE list,
4950
+ * the booking made last on top, each line headed with when it was made (UTC, to the minute) and carrying its state
4951
+ * and hotel (and its Chinese name) as ever, with one line of how many are in each state. A guest asking for one
4952
+ * hotel's booking is answered from these lines. A booking whose time is not known or not to be trusted (none, not a
4953
+ * date, before 2020, more than a day ahead of now) is listed after the others, the later check-in first, and says
4954
+ * "booking time not known" - never a time we made up. "Stayed" is a check-out before today in UTC - a day out at
4955
+ * most at either end of the world.
4956
+ */
4957
+ export const bookingsInTime = (list, line, now) => {
4958
+ const today = new Date(now).toISOString().slice(0, 10);
4959
+ const kind = (b) => b.status === 'cancelled' || b.status === 'expired' || b.status === 'failed'
4960
+ ? 'off'
4961
+ : b.stay?.checkOut && b.stay.checkOut < today
4962
+ ? 'past'
4963
+ : b.status === 'awaiting-payment'
4964
+ ? 'pay'
4965
+ : 'up';
4966
+ const madeAt = (b) => {
4967
+ const t = typeof b.createdAt === 'string' ? Date.parse(b.createdAt) : Number.NaN;
4968
+ return Number.isFinite(t) && t >= Date.UTC(2020, 0, 1) && t <= now + 86_400_000
4969
+ ? t
4970
+ : Number.NaN;
4971
+ };
4972
+ const ci = (b) => String(b.stay?.checkIn ?? '');
4973
+ const sorted = [...list].sort((a, b) => {
4974
+ const ta = madeAt(a);
4975
+ const tb = madeAt(b);
4976
+ if (Number.isNaN(ta) !== Number.isNaN(tb))
4977
+ return Number.isNaN(ta) ? 1 : -1;
4978
+ if (!Number.isNaN(ta) && ta !== tb)
4979
+ return tb - ta;
4980
+ return Number.isNaN(ta) ? ci(b).localeCompare(ci(a)) : 0;
4981
+ });
4982
+ const words = {
4983
+ pay: 'to pay',
4984
+ up: 'upcoming',
4985
+ past: 'stayed',
4986
+ off: 'cancelled, expired or not made',
4987
+ };
4988
+ const counts = ['pay', 'up', 'past', 'off']
4989
+ .map((k) => [k, list.filter((b) => kind(b) === k).length])
4990
+ .filter(([, n]) => n > 0)
4991
+ .map(([k, n]) => `${n} ${words[k]}`)
4992
+ .join(', ');
4993
+ const when = (b) => {
4994
+ const t = madeAt(b);
4995
+ return Number.isNaN(t)
4996
+ ? 'booking time not known'
4997
+ : `booked ${new Date(t).toISOString().slice(0, 16).replace('T', ' ')} UTC`;
4998
+ };
4999
+ return [
5000
+ `Bookings, latest booked first (${list.length}: ${counts}):`,
5001
+ ...sorted.map((b) => `[${when(b)}] ${line(b)}`),
5002
+ ].join('\n');
5003
+ };
4947
5004
  const bookingLine = (deps, b, noCards) => {
4948
5005
  const kinds = b.requests?.kinds ?? [];
4949
5006
  const held = noCards ? heldRoomsWords(b) : '';
@@ -9590,11 +9647,15 @@ const answerTool = async (deps, name, rawArgs, ctx) => {
9590
9647
  text: [
9591
9648
  missed ? missLine : '',
9592
9649
  bookings.length
9593
- ? bookings
9594
- .map((b) => [bookingLine(deps, b, ctx.noCards), shopPart?.under.get(b.confirmationCode) ?? '']
9595
- .filter(Boolean)
9596
- .join('\n'))
9597
- .join('\n')
9650
+ ? deps.bookingsByTime
9651
+ ? bookingsInTime(bookings, (b) => [bookingLine(deps, b, ctx.noCards), shopPart?.under.get(b.confirmationCode) ?? '']
9652
+ .filter(Boolean)
9653
+ .join('\n'), deps.trips.now())
9654
+ : bookings
9655
+ .map((b) => [bookingLine(deps, b, ctx.noCards), shopPart?.under.get(b.confirmationCode) ?? '']
9656
+ .filter(Boolean)
9657
+ .join('\n'))
9658
+ .join('\n')
9598
9659
  : code
9599
9660
  ? ''
9600
9661
  : // ⚠️ NOT "NO BOOKINGS" when a group did not answer: it may hold them.