@curless/hotel-storefront 0.80.0 → 0.80.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.
@@ -708,6 +708,22 @@ export const refundOutcome = (out) => {
708
708
  return 'unsure';
709
709
  return 'sent';
710
710
  };
711
+ /**
712
+ * 9 refusing a PAID order with `paid_use_refund` (0.80.1, 11-20 - robin's video: AT-F2519E, paid
713
+ * and confirmed, answered HTTP 500 "paid_use_refund", nothing changed at the backend): it cancels a
714
+ * paid order only through its refund (9.1). Read from the refusal's own `msg` - or its `data`,
715
+ * as a string or an object's code / message - in any case; nothing else is it.
716
+ */
717
+ export const paidUseRefundSaid = (err) => {
718
+ if (!(err instanceof OtalessError) || err instanceof OtalessBusy)
719
+ return false;
720
+ const a = err.answer;
721
+ const data = a?.data;
722
+ const fields = data && typeof data === 'object'
723
+ ? ['code', 'errorCode', 'error', 'message', 'msg'].map((k) => data[k])
724
+ : [data];
725
+ return [a?.msg, ...fields].some((w) => typeof w === 'string' && /(^|[^a-z])paid_use_refund([^a-z]|$)/i.test(w));
726
+ };
711
727
  /**
712
728
  * The backend refusing because the guest's membership of that group is not linked - or its
713
729
  * channel not signed in - in its own words (0.74.0, robin 11-17: 「未绑定 / 未登录渠道」). Its
@@ -786,6 +802,12 @@ export const createOtalessBookings = (deps) => {
786
802
  refundUnsure: 0,
787
803
  /** Cancelled, and 9's `refundAmount` alone taken as its refund asked - 9.1 not sent (0.74.3, #51). */
788
804
  refundAssumed: 0,
805
+ /** A paid order 9 answered `paid_use_refund` for, and 9.1 asked instead (0.80.1). */
806
+ cancelByRefund: 0,
807
+ /** …and 9.1 not taken: not cancelled, nothing refunded (0.80.1). */
808
+ cancelByRefundRefused: 0,
809
+ /** …or not answered: whether it was taken is not known - a person checks (0.80.1). */
810
+ cancelByRefundUnsure: 0,
789
811
  };
790
812
  const keyOf = (who) => who.tripOwner ?? who.sessionId;
791
813
  const accountFor = (who, group) => {
@@ -1652,6 +1674,31 @@ export const createOtalessBookings = (deps) => {
1652
1674
  .slice(0, 10)
1653
1675
  .map((id) => forTheLog(id))
1654
1676
  .join(', ')}${older.length > 10 ? ', …' : ''}) - each was logged when it happened, as one for a person; once a person has taken one up, write refund.handledAt on it in otaless-orders.json (service stopped)`);
1677
+ // ⚠️ A PAID ORDER CANCELLED THROUGH ITS REFUND, NOT KNOWN (0.80.1): `asking` kept is a 9.1 a
1678
+ // restart cut short - nobody knows whether it was sent: `unsure`, as one not answered. Each is
1679
+ // said line by line for `REFUND_SAID_DAYS`, then counted in one line, as the refunds above.
1680
+ const olderCancels = [];
1681
+ for (const r of [...orders.values()]) {
1682
+ const c = r.refundCancel;
1683
+ if (!c)
1684
+ continue;
1685
+ const cut = c.state === 'asking';
1686
+ if (cut) {
1687
+ r.refundCancel = { state: 'unsure', at: c.at };
1688
+ save();
1689
+ }
1690
+ const at = Date.parse(c.at);
1691
+ if (!cut && Number.isFinite(at) && at < saidSince) {
1692
+ olderCancels.push(r.id);
1693
+ continue;
1694
+ }
1695
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${r.group} is paid, and the refund (9.1) that cancels it ${cut ? 'was being asked when the service stopped' : 'was not answered'} - whether it was taken, and the booking cancelled with it, is not known; check with the group, then (service stopped) delete refundCancel on it in otaless-orders.json and write what the group said`);
1696
+ }
1697
+ if (olderCancels.length)
1698
+ log(`[otaless] ${olderCancels.length} paid ${olderCancels.length === 1 ? 'order' : 'orders'} asked to be cancelled through ${olderCancels.length === 1 ? 'its refund' : 'their refunds'} more than ${REFUND_SAID_DAYS} days ago still not known (${olderCancels
1699
+ .slice(0, 10)
1700
+ .map((id) => forTheLog(id))
1701
+ .join(', ')}${olderCancels.length > 10 ? ', …' : ''}) - each was logged when it happened, as one for a person`);
1655
1702
  // ⚠️ PAID AT ANOTHER AMOUNT, KEPT FROM BEFORE 0.75.1 AS NOT PAID (the review's P1-b): its group
1656
1703
  // said it was paid. Taken as paid now, as a PAID row is (`lookAtOrders`) - confirmed unless it
1657
1704
  // was cancelled - so it is said as paid and its cancel asks the money back. What was paid was
@@ -2971,6 +3018,26 @@ export const createOtalessBookings = (deps) => {
2971
3018
  status: 'refunded',
2972
3019
  ...(r.refund?.amount ? { amount: r.refund.amount } : {}),
2973
3020
  };
3021
+ // ⚠️ A CANCEL THROUGH ITS REFUND NOT KNOWN (0.80.1, `refundCancel`), SETTLED BY THE LIST: a
3022
+ // row being refunded (REFUND_REQUESTED, …) is that refund taken - cancelled, its refund
3023
+ // submitted; one cancelled without a word of its money is cancelled, and whether the
3024
+ // refund reached the group is still not known (`unsure` - said so, and a person's).
3025
+ if (r.refundCancel && r.refundCancel.state !== 'asking') {
3026
+ const v = String(row.status ?? '')
3027
+ .trim()
3028
+ .toUpperCase();
3029
+ if (!s.refunded && REFUND_STATUSES.has(v)) {
3030
+ r.status = 'cancelled';
3031
+ r.refund = { status: 'requested' };
3032
+ }
3033
+ if (r.status === 'cancelled') {
3034
+ r.cancelledAt ??= iso();
3035
+ r.refund ??= { status: 'unsure' };
3036
+ r.refundCancel = undefined;
3037
+ payLinks.delete(r.id);
3038
+ log(`[otaless] ${forTheLog(r.id)} at ${group}: listed ${forTheLog(v) || 'cancelled'} - its cancel through its refund (9.1), not known till now, taken as ${r.refund.status === 'unsure' ? 'cancelled, the refund still not known' : `cancelled, the refund ${r.refund.status}`}`);
3039
+ }
3040
+ }
2974
3041
  if (r.status === 'awaiting-payment' && !payLinks.has(r.id)) {
2975
3042
  // Held to this order's own page where the list says so (11-11, B).
2976
3043
  const ok = payPageOf(text(row.checkoutUrl ?? undefined), deps.checkoutHosts, {
@@ -3128,7 +3195,10 @@ export const createOtalessBookings = (deps) => {
3128
3195
  const refresh = async (who, mine, signed) => {
3129
3196
  const newest = [...mine].sort((a, b) => b.createdAt.localeCompare(a.createdAt));
3130
3197
  const groups = new Set(newest
3131
- .filter((r) => r.status === 'unconfirmed' || (r.status === 'awaiting-payment' && !payLinks.has(r.id)))
3198
+ .filter((r) => r.status === 'unconfirmed' ||
3199
+ (r.status === 'awaiting-payment' && !payLinks.has(r.id)) ||
3200
+ // 0.80.1: a cancel through its refund not known - the list may say what became of it.
3201
+ r.refundCancel?.state === 'unsure')
3132
3202
  .map((r) => r.group));
3133
3203
  for (const g of groups) {
3134
3204
  const acct = signed.get(g);
@@ -3203,6 +3273,17 @@ export const createOtalessBookings = (deps) => {
3203
3273
  cancellable: false,
3204
3274
  reason: 'unconfirmed',
3205
3275
  };
3276
+ // ⚠️ ITS CANCEL THROUGH ITS REFUND IS NOT KNOWN (0.80.1): nothing more is offered - a second
3277
+ // 9.1 could be a second refund. A person is checking with the group.
3278
+ if (r.refundCancel)
3279
+ return {
3280
+ ...base,
3281
+ fee: money(0, amount.currency),
3282
+ refund: money(0, amount.currency),
3283
+ rule: `${g} has not said yet whether it took the refund request that cancels this paid booking - a person here is checking with ${g}, and nothing more can be asked for it until then.`,
3284
+ cancellable: false,
3285
+ reason: 'unconfirmed',
3286
+ };
3206
3287
  if (r.status === 'awaiting-payment' && !r.paid)
3207
3288
  return {
3208
3289
  ...base,
@@ -3279,10 +3360,23 @@ export const createOtalessBookings = (deps) => {
3279
3360
  const g = deps.groupOf(r.group).name;
3280
3361
  if (r.status === 'unconfirmed')
3281
3362
  throw refusal('unconfirmed', `${g} has not said yet whether it took this booking, so it cannot be cancelled yet - look at the bookings again in a few minutes.`);
3363
+ // ⚠️ A CANCEL THROUGH ITS REFUND NOT KNOWN YET (0.80.1): nothing is sent again - not 9, whose
3364
+ // "done" would ask 9.1 a second time, not 9.1. A person is checking with the group.
3365
+ if (r.refundCancel)
3366
+ throw refusal('cancel-unknown', `${g} has not said yet whether it took the refund request that cancels this paid booking, so it is not known yet whether it is cancelled - a person here is checking with ${g}, and nothing more is sent for it until then.`);
3282
3367
  const unpaid = r.status === 'awaiting-payment' && !r.paid;
3283
3368
  const why = cleanReason(reason);
3284
3369
  // 9, paid or not: 14 is not called any more (questions #56).
3285
- const out = await withSession(acct, who, (cred) => client.cancelBooking({ unifiedOrderId: r.id, providerCode: r.group, ...(why ? { reason: why } : {}) }, cred, deps.callerOf(who)));
3370
+ let out;
3371
+ try {
3372
+ out = await withSession(acct, who, (cred) => client.cancelBooking({ unifiedOrderId: r.id, providerCode: r.group, ...(why ? { reason: why } : {}) }, cred, deps.callerOf(who)));
3373
+ }
3374
+ catch (err) {
3375
+ // ⚠️ "PAID - USE THE REFUND" (0.80.1, 11-20): 9 cancels no paid order; its refund does.
3376
+ if (!paidUseRefundSaid(err))
3377
+ throw err;
3378
+ return cancelByRefund(r, acct, who, why);
3379
+ }
3286
3380
  const said = cancelOutcome(out);
3287
3381
  // ⚠️ NOTHING SAID IS NOT DONE (总线程's review of 0.63.0, 4): our record
3288
3382
  // stays as it was, and the guest is told it is not known yet.
@@ -3337,15 +3431,23 @@ export const createOtalessBookings = (deps) => {
3337
3431
  * logged in the backend's own words for a person to take up. Nothing thrown.
3338
3432
  */
3339
3433
  const askRefund = async (r, acct, who, why) => {
3340
- const g = r.group;
3341
- const person = (state, how, words) => {
3342
- if (state === 'not-sent')
3343
- counts.refundNotSent += 1;
3344
- else
3345
- counts.refundUnsure += 1;
3346
- log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${g} was cancelled, and its refund (9.1) ${how}: ${forTheLog(words)} - ${state === 'unsure' ? 'whether it was sent is not known; check with the group' : 'nothing goes back until a person takes it up'}`);
3347
- return { status: state };
3348
- };
3434
+ const got = await sendRefund(r, acct, who, why);
3435
+ if (got.state === 'requested' || got.state === 'refunded')
3436
+ return { status: got.state, ...(got.amount ? { amount: got.amount } : {}) };
3437
+ if (got.state === 'not-sent')
3438
+ counts.refundNotSent += 1;
3439
+ else
3440
+ counts.refundUnsure += 1;
3441
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${r.group} was cancelled, and its refund (9.1) ${got.how}: ${forTheLog(got.words)} - ${got.state === 'unsure' ? 'whether it was sent is not known; check with the group' : 'nothing goes back until a person takes it up'}`);
3442
+ return { status: got.state };
3443
+ };
3444
+ /**
3445
+ * 9.1 for one of our paid orders, all of it - and what its answer says: `requested` /
3446
+ * `refunded`, taken; `not-sent`, refused (with what was thrown, if anything was); `unsure`, not
3447
+ * answered or answered with nothing (0.74.3). Nothing logged, nothing thrown: each caller says
3448
+ * it its own way (`askRefund`, `cancelByRefund`).
3449
+ */
3450
+ const sendRefund = async (r, acct, who, why) => {
3349
3451
  try {
3350
3452
  const out = await withSession(acct, who, (cred) => client.refund({
3351
3453
  unifiedOrderId: r.id,
@@ -3356,14 +3458,14 @@ export const createOtalessBookings = (deps) => {
3356
3458
  const outcome = refundOutcome(out);
3357
3459
  const said = String(out?.message ?? out?.status ?? 'no words');
3358
3460
  if (outcome === 'not-sent')
3359
- return person('not-sent', 'was refused', said);
3461
+ return { state: 'not-sent', how: 'was refused', words: said };
3360
3462
  if (outcome === 'unsure')
3361
- return person('unsure', 'was answered with nothing', said);
3463
+ return { state: 'unsure', how: 'was answered with nothing', words: said };
3362
3464
  const back = moneyOf(out?.refundAmount, out?.refundCurrency);
3363
- return {
3364
- status: outcome === 'refunded' ? 'refunded' : 'requested',
3365
- ...(back && back.minor > 0 ? { amount: back } : {}),
3366
- };
3465
+ const amount = back && back.minor > 0 ? { amount: back } : {};
3466
+ return outcome === 'refunded'
3467
+ ? { state: 'refunded', ...amount }
3468
+ : { state: 'requested', ...amount };
3367
3469
  }
3368
3470
  catch (err) {
3369
3471
  const words = err instanceof OtalessError
@@ -3373,9 +3475,71 @@ export const createOtalessBookings = (deps) => {
3373
3475
  : String(err);
3374
3476
  // ⚠️ NOT ANSWERED IS NOT REFUSED (0.74.3, P3): a 9.1 that timed out may have been taken.
3375
3477
  return unanswered(err)
3376
- ? person('unsure', 'was not answered', words)
3377
- : person('not-sent', 'failed', words);
3478
+ ? { state: 'unsure', how: 'was not answered', words }
3479
+ : { state: 'not-sent', how: 'failed', words, err };
3480
+ }
3481
+ };
3482
+ /**
3483
+ * ⚠️ A PAID ORDER 9 ANSWERED `paid_use_refund` (0.80.1, 11-20 - robin's video: "Could not cancel
3484
+ * that booking."): the backend cancels a paid order only through its refund, so the cancel asks
3485
+ * 9.1 - as `cancel_<p>_booking` says it does, and as its preview said it would - and says what
3486
+ * 9.1 answered, never more:
3487
+ * - taken (`requested` / `refunded`): cancelled, its refund submitted to the group, which reviews
3488
+ * it - said as the cancel that asked 9.1 after 9 says it (`groupReviews`);
3489
+ * - refused: NOT cancelled - 9 changed nothing, and 9.1 nothing either: "it stands as it was,
3490
+ * nothing was refunded" (`not-cancelled`), logged NEEDS A PERSON with the backend's words;
3491
+ * - not answered: whether it was taken - the refund, and the cancellation with it - is not known
3492
+ * (`cancel-unknown`, never "cancelled", never "not cancelled"): `refundCancel` kept, nothing
3493
+ * more sent for this order, NEEDS A PERSON.
3494
+ * ⚠️ ONLY AN ORDER SEEN PAID HERE: one this side has not seen paid is not refunded on 9's word -
3495
+ * the guest was shown "nothing has been charged"; it stands, and is looked at again.
3496
+ * ⚠️ "ASKING" IS WRITTEN FIRST, as for 9.1 after 9 (0.74.3): a restart in its round trip leaves
3497
+ * one nobody knows the fate of, and the service says so when it starts.
3498
+ */
3499
+ const cancelByRefund = async (r, acct, who, why) => {
3500
+ const g = deps.groupOf(r.group).name;
3501
+ if (!r.paid) {
3502
+ log(`[otaless] CHECK: ${forTheLog(r.id)} at ${r.group}: 9 answered paid_use_refund for an order not seen paid here - nothing more sent, nothing changed; its payment is looked at again`);
3503
+ throw refusal('not-cancelled', `${g} did not cancel it - it stands as it was. ${g} says this booking has been paid, which is not seen here yet: look at the booking again in a moment, and cancel it from there - a paid booking is cancelled with a refund of what was paid.`);
3504
+ }
3505
+ if (!deps.refundsOn) {
3506
+ log(`[otaless] ${forTheLog(r.id)} at ${r.group}: 9 answered paid_use_refund and refunds are off here - not cancelled, nothing sent`);
3507
+ throw refusal('not-cancelled', `${g} did not cancel it - it stands as it was: ${g} cancels a paid booking only with a refund of it, and no refund is asked from here.`);
3508
+ }
3509
+ counts.cancelByRefund += 1;
3510
+ r.refundCancel = { state: 'asking', at: iso() };
3511
+ put(r);
3512
+ const got = await sendRefund(r, acct, who, why);
3513
+ if (got.state === 'requested' || got.state === 'refunded') {
3514
+ r.refundCancel = undefined;
3515
+ r.status = 'cancelled';
3516
+ r.cancelledAt = iso();
3517
+ payLinks.delete(r.id);
3518
+ r.held = undefined;
3519
+ for (const [k, p] of pending)
3520
+ if (p.orderId === r.id)
3521
+ pending.delete(k);
3522
+ r.refund = { status: got.state, ...(got.amount ? { amount: got.amount } : {}) };
3523
+ put(r);
3524
+ log(`[otaless] ${forTheLog(r.id)} at ${r.group}: 9 answered paid_use_refund - cancelled through its refund (9.1), ${got.state}`);
3525
+ return toBooking(r);
3378
3526
  }
3527
+ if (got.state === 'not-sent') {
3528
+ r.refundCancel = undefined;
3529
+ put(r);
3530
+ // Never sent at all - a limit of ours, a sign-in to do again: said as those, as 9's are.
3531
+ const code = got.err?.code;
3532
+ if (got.err instanceof OtalessBusy || (got.err instanceof Error && typeof code === 'string'))
3533
+ throw got.err;
3534
+ counts.cancelByRefundRefused += 1;
3535
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${r.group} is paid and the guest asked to cancel it; 9 answered paid_use_refund, and the refund (9.1) that cancels it ${got.how}: ${forTheLog(got.words)} - not cancelled, nothing refunded`);
3536
+ throw refusal('not-cancelled', `${g} did not cancel it - it stands as it was, and nothing was refunded: ${g} cancels a paid booking only through a refund of it, and it did not take the refund request.`);
3537
+ }
3538
+ r.refundCancel = { state: 'unsure', at: r.refundCancel?.at ?? iso() };
3539
+ put(r);
3540
+ counts.cancelByRefundUnsure += 1;
3541
+ log(`[otaless] NEEDS A PERSON: ${forTheLog(r.id)} at ${r.group} is paid; 9 answered paid_use_refund, and the refund (9.1) that cancels it ${got.how}: ${forTheLog(got.words)} - whether it was taken, and the booking cancelled with it, is not known; nothing more is sent for it - check with the group, then (service stopped) delete refundCancel on it in otaless-orders.json and write what the group said`);
3542
+ throw refusal('cancel-unknown', `${g} did not say in time whether it took the refund request that cancels this paid booking, so it is not known yet whether it is cancelled or whether its refund was submitted - nothing is changed here, a person here is checking with ${g}, and nothing more is sent for it until then.`);
3379
3543
  };
3380
3544
  // ── refund: a preview, then the refund it previewed (9.1; 总线程's review of 0.63.0, B) ──
3381
3545
  /** Refund previews waiting to be sent, by their `preview`. */
@@ -3389,7 +3553,8 @@ export const createOtalessBookings = (deps) => {
3389
3553
  if (!own)
3390
3554
  return null;
3391
3555
  const { r } = own;
3392
- if (r.refund)
3556
+ // 0.80.1: a refund that cancels it, not known yet (`refundCancel`), is one already asked.
3557
+ if (r.refund || r.refundCancel)
3393
3558
  throw refusal('refunded', 'A refund was already asked for this booking.');
3394
3559
  refundable(r);
3395
3560
  for (const [k, v] of refundPreviews)
@@ -3423,6 +3588,9 @@ export const createOtalessBookings = (deps) => {
3423
3588
  const { r, acct } = own;
3424
3589
  if (r.refund)
3425
3590
  return toBooking(r);
3591
+ // 0.80.1: never a second 9.1 beside one not known yet.
3592
+ if (r.refundCancel)
3593
+ throw refusal('refunded', 'A refund was already asked for this booking.');
3426
3594
  const g = deps.groupOf(r.group).name;
3427
3595
  refundable(r);
3428
3596
  // ⚠️ THE PREVIEW OF THIS VERY BOOKING, FOR THIS GUEST, AT THIS AMOUNT, ONCE