@curless/hotel-storefront 0.81.0 → 0.82.1
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/account/addon-pages.d.ts +22 -0
- package/dist/account/addon-pages.d.ts.map +1 -0
- package/dist/account/addon-pages.js +282 -0
- package/dist/account/addon-pages.js.map +1 -0
- package/dist/account/index.d.ts +40 -0
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/index.js +444 -46
- package/dist/account/index.js.map +1 -1
- package/dist/account/ota-member.d.ts +15 -0
- package/dist/account/ota-member.d.ts.map +1 -1
- package/dist/account/ota-member.js +29 -5
- package/dist/account/ota-member.js.map +1 -1
- package/dist/account/pages.d.ts +28 -3
- package/dist/account/pages.d.ts.map +1 -1
- package/dist/account/pages.js +99 -35
- package/dist/account/pages.js.map +1 -1
- package/dist/account/requests.d.ts +11 -1
- package/dist/account/requests.d.ts.map +1 -1
- package/dist/account/requests.js +2 -1
- package/dist/account/requests.js.map +1 -1
- package/dist/backend.d.ts +7 -0
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js.map +1 -1
- package/dist/card.generated.d.ts +1 -1
- package/dist/card.generated.js +2 -2
- package/dist/card.generated.js.map +1 -1
- package/dist/config.d.ts +4 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +12 -5
- package/dist/config.js.map +1 -1
- package/dist/legal.d.ts.map +1 -1
- package/dist/legal.js +10 -3
- package/dist/legal.js.map +1 -1
- package/dist/otaless/addons-answers.d.ts +17 -0
- package/dist/otaless/addons-answers.d.ts.map +1 -0
- package/dist/otaless/addons-answers.js +170 -0
- package/dist/otaless/addons-answers.js.map +1 -0
- package/dist/otaless/addons.d.ts +218 -0
- package/dist/otaless/addons.d.ts.map +1 -0
- package/dist/otaless/addons.js +758 -0
- package/dist/otaless/addons.js.map +1 -0
- package/dist/otaless/booking.d.ts +64 -0
- package/dist/otaless/booking.d.ts.map +1 -1
- package/dist/otaless/booking.js +156 -0
- package/dist/otaless/booking.js.map +1 -1
- package/dist/otaless/client.d.ts +2 -0
- package/dist/otaless/client.d.ts.map +1 -1
- package/dist/otaless/client.js.map +1 -1
- package/dist/otaless/fake.d.ts +22 -0
- package/dist/otaless/fake.d.ts.map +1 -1
- package/dist/otaless/fake.js +40 -7
- package/dist/otaless/fake.js.map +1 -1
- package/dist/otaless/make.d.ts +4 -3
- package/dist/otaless/make.d.ts.map +1 -1
- package/dist/otaless/make.js +21 -7
- package/dist/otaless/make.js.map +1 -1
- package/dist/otaless/shop-answers.d.ts +19 -6
- package/dist/otaless/shop-answers.d.ts.map +1 -1
- package/dist/otaless/shop-answers.js +60 -26
- package/dist/otaless/shop-answers.js.map +1 -1
- package/dist/otaless/shop-fake.d.ts +31 -1
- package/dist/otaless/shop-fake.d.ts.map +1 -1
- package/dist/otaless/shop-fake.js +79 -3
- package/dist/otaless/shop-fake.js.map +1 -1
- package/dist/otaless/shop-service.d.ts +12 -6
- package/dist/otaless/shop-service.d.ts.map +1 -1
- package/dist/otaless/shop-service.js +15 -7
- package/dist/otaless/shop-service.js.map +1 -1
- package/dist/otaless/shop.d.ts +18 -9
- package/dist/otaless/shop.d.ts.map +1 -1
- package/dist/otaless/shop.js +95 -17
- package/dist/otaless/shop.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +49 -5
- package/dist/server.js.map +1 -1
- package/dist/surface.js +2 -2
- package/dist/surface.js.map +1 -1
- package/dist/tools.d.ts +132 -13
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +268 -97
- package/dist/tools.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { WindowCounter } from '../limits.js';
|
|
3
|
+
import { minorFactor, toMinor } from '../money.js';
|
|
4
|
+
// ADD-ONS AFTER A HOTEL BOOKING (0.82.0, docs/otaless-hotels.md §17.14; the contract with the card:
|
|
5
|
+
// scratchpad phase2/contract-addons.md). Two things can be added to a booking of THIS account:
|
|
6
|
+
// - the hotel's own services (10 `/addons`, 11 `/addons/book`): booked for a total we recompute
|
|
7
|
+
// from 10's price and hold the guest's confirmed total to;
|
|
8
|
+
// - goods from the Otaless shop (`/addon/buy`): the backend charges a card on the account, or gives
|
|
9
|
+
// a payment link when there is none.
|
|
10
|
+
//
|
|
11
|
+
// ⚠️ NOTHING HERE IS A TOOL THE MODEL CAN CALL TO SPEND MONEY. Every write below is reached from
|
|
12
|
+
// (a) the card's own tool (app-only, a `press` made per confirm screen), or
|
|
13
|
+
// (b) the one-time confirm page `/account?addon=<code>` (account/index.ts), whose ticket - made by
|
|
14
|
+
// the model's tool as a PREPARATION - is single use, ten minutes, bound to the account.
|
|
15
|
+
// A press/ticket is the guest's yes to exactly the values on the screen: another value is `used`,
|
|
16
|
+
// the same values again after an answer are not sent again.
|
|
17
|
+
//
|
|
18
|
+
// ⚠️ ONE CONFIRMATION, ONE SEND. 11 and `/buy` have no idempotency key here (#105): each is sent at
|
|
19
|
+
// most once per press, never retried, and an answer not known is `unknown` - never resent.
|
|
20
|
+
// ⚠️ NEVER "PAID" FOR A HOTEL ADD-ON: the backend does not say how it collects (`done`).
|
|
21
|
+
// ⚠️ NEVER A REFUND OR A CANCEL OF OUR OWN FOR A CHARGE THAT DIFFERS (decision D): it is kept, said as
|
|
22
|
+
// it is, and handed to a person (`needsPerson`).
|
|
23
|
+
// ⚠️ NOTHING PERSONAL IN A LOG LINE: no name, email, flight number, card, token. Codes of orders and
|
|
24
|
+
// amounts only.
|
|
25
|
+
/**
|
|
26
|
+
* ⚠️ WHAT THE GUEST IS TOLD BEFORE A SHOP PURCHASE (card, page and the model's words say the same): the
|
|
27
|
+
* backend charges a card on the account, or gives a payment link when there is none. NEVER NAMES A
|
|
28
|
+
* CARD (总线程 11-23, 1): none of the backend's documented answers - `/addon/info`, `/addon/buy` - gives
|
|
29
|
+
* the charged card's brand or last four, so none is shown or guessed (the card's contract has
|
|
30
|
+
* `addonsView.shop.chargeCard?` for the day one does). Member discounts only lower the total.
|
|
31
|
+
*/
|
|
32
|
+
export const SHOP_CONFIRM_TEXT = {
|
|
33
|
+
zh: '确认后,会直接从你账号里绑定的卡付款;没有可用的卡就给你付款链接。会员折扣以后台为准,只会更少。',
|
|
34
|
+
en: 'When you confirm, the backend charges a card linked to your account; if there is none, you get a payment link. Member discounts are applied by the backend and can only lower the total.',
|
|
35
|
+
};
|
|
36
|
+
/** The card's `press` - or a page ticket's code: made once per confirm screen, crypto random. */
|
|
37
|
+
export const ADDON_PRESS = /^[A-Za-z0-9_-]{16,64}$/;
|
|
38
|
+
/** How long a press is remembered. */
|
|
39
|
+
export const ADDON_PRESS_MS = 15 * 60_000;
|
|
40
|
+
/** How long a prepared confirm (a page ticket) lives. */
|
|
41
|
+
export const ADDON_TICKET_MS = 10 * 60_000;
|
|
42
|
+
/** Add-on writes one account may make in ten minutes, on top of every other limit. */
|
|
43
|
+
export const ADDON_WRITES_PER_TEN_MINUTES = 10;
|
|
44
|
+
const MAX_PRESSES = 20_000;
|
|
45
|
+
const MAX_TICKETS = 2_000;
|
|
46
|
+
const TICKETS_PER_CONVERSATION = 10;
|
|
47
|
+
const ITEMS_MAX = 24;
|
|
48
|
+
/** How long a hotel add-on whose 11 ended `unknown` is not sent again for the same booking (总线程 11-23). */
|
|
49
|
+
export const ADDON_CHECKING_MS = 10 * 60_000;
|
|
50
|
+
const keyOf = (who) => who.tripOwner ?? who.sessionId;
|
|
51
|
+
const hashOf = (s) => createHash('sha256').update(s).digest('base64url').slice(0, 16);
|
|
52
|
+
/** A shop price is always whole cents (shop.ts `centsOf`): `1850` -> `"18.50"`, whatever the currency. */
|
|
53
|
+
export const centsStr = (cents) => (cents / 100).toFixed(2);
|
|
54
|
+
/** `"18.50"` -> 1850: plain, at most two decimals, above zero - else undefined. Integer maths, never a float. */
|
|
55
|
+
export const centsOfStr = (v) => {
|
|
56
|
+
if (typeof v !== 'string' || !/^\d{1,7}(\.\d{1,2})?$/.test(v.trim()))
|
|
57
|
+
return undefined;
|
|
58
|
+
const [whole = '0', frac = ''] = v.trim().split('.');
|
|
59
|
+
const n = Number(whole) * 100 + Number(frac.padEnd(2, '0'));
|
|
60
|
+
return Number.isSafeInteger(n) && n > 0 ? n : undefined;
|
|
61
|
+
};
|
|
62
|
+
/** `45000, 'CNY'` -> `"450.00"`. */
|
|
63
|
+
export const amountOf = (minor, currency) => (minor / minorFactor(currency)).toFixed(2);
|
|
64
|
+
/** `"450.00"` -> minor units; anything that is not a plain amount of at most two decimals: undefined. */
|
|
65
|
+
export const minorOf = (v, currency) => {
|
|
66
|
+
if (typeof v !== 'string' || !/^\d{1,9}(\.\d{1,2})?$/.test(v.trim()))
|
|
67
|
+
return undefined;
|
|
68
|
+
const n = toMinor(Number(v.trim()), currency);
|
|
69
|
+
return Number.isSafeInteger(n) && n > 0 ? n : undefined;
|
|
70
|
+
};
|
|
71
|
+
/** What a hotel add-on asks the guest for: a transfer's details, else a date and a quantity. */
|
|
72
|
+
export const fieldsOf = (category) => category === 'transfer'
|
|
73
|
+
? ['direction', 'flightNo', 'pickupTime', 'date', 'partySize']
|
|
74
|
+
: ['date', 'quantity'];
|
|
75
|
+
/**
|
|
76
|
+
* ⚠️ WHAT THE TOTAL IS MULTIPLIED BY - the one rule the card and the server share: a line that asks
|
|
77
|
+
* for a `quantity` is price × quantity; a per-person line (unit has guest, person or pax - the card's
|
|
78
|
+
* rule, 总线程 11-23) that asks for the party is price × party;
|
|
79
|
+
* everything else is the price once (a transfer is per trip, whatever the party). A per-day unit is
|
|
80
|
+
* NOT multiplied by days: we do not know the days the backend will count - if its total comes back
|
|
81
|
+
* higher, it is kept, said so, and a person looks (`needsPerson`).
|
|
82
|
+
*/
|
|
83
|
+
export const multiplierOf = (line, fields, p) => fields.includes('quantity')
|
|
84
|
+
? (p.quantity ?? 1)
|
|
85
|
+
: fields.includes('partySize') && /guest|person|pax/i.test(line.unit ?? '')
|
|
86
|
+
? (p.partySize ?? 1)
|
|
87
|
+
: 1;
|
|
88
|
+
const refused = (action, reason) => ({ action, outcome: 'refused', reason });
|
|
89
|
+
export const createOtalessAddons = (o) => {
|
|
90
|
+
const { booking, shop, now } = o;
|
|
91
|
+
const log = o.log ?? ((line) => console.error(line));
|
|
92
|
+
const hotelOn = o.hotelOn && !!booking;
|
|
93
|
+
const shopOn = !!shop;
|
|
94
|
+
const presses = new Map();
|
|
95
|
+
const writes = new WindowCounter(10 * 60_000, now);
|
|
96
|
+
const prepared = new Map();
|
|
97
|
+
/**
|
|
98
|
+
* ⚠️ 11 ENDED `unknown` FOR (ACCOUNT, BOOKING, ITEM): no new write for it for ten minutes - it may have been
|
|
99
|
+
* made - unless a fresh 10 read says it is `booked` (then "add another" is the guest's, on a new confirm).
|
|
100
|
+
*/
|
|
101
|
+
const unknownAt = new Map();
|
|
102
|
+
const checkingKey = (account, code, id) => `${account}|${code}|${id}`;
|
|
103
|
+
/** True while the last request for this item is not known and 10 does not show it booked. */
|
|
104
|
+
const stillChecking = (account, code, line) => {
|
|
105
|
+
const k = checkingKey(account, code, line.id);
|
|
106
|
+
const at = unknownAt.get(k);
|
|
107
|
+
if (at === undefined)
|
|
108
|
+
return false;
|
|
109
|
+
if (now() - at >= ADDON_CHECKING_MS || line.booked) {
|
|
110
|
+
unknownAt.delete(k);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
/** Presses being sent now: a second one of the same is `busy`, never a second send. */
|
|
116
|
+
const inFlight = new Set();
|
|
117
|
+
const iso = (t) => new Date(t).toISOString();
|
|
118
|
+
const tag = (key) => hashOf(key).slice(0, 8);
|
|
119
|
+
// ── presses ────────────────────────────────────────────────────────────────────────────────
|
|
120
|
+
/** A press: new, a replay with the same values, or used with others. Nothing is claimed when full. */
|
|
121
|
+
const claim = (account, press, sig) => {
|
|
122
|
+
const k = `${account}|${press}`;
|
|
123
|
+
const t = now();
|
|
124
|
+
const had = presses.get(k);
|
|
125
|
+
if (had && t - had.at < ADDON_PRESS_MS)
|
|
126
|
+
return had.sig === sig ? { state: 'replay', rec: had } : 'used';
|
|
127
|
+
if (presses.size >= MAX_PRESSES) {
|
|
128
|
+
for (const [x, p] of presses)
|
|
129
|
+
if (t - p.at >= ADDON_PRESS_MS)
|
|
130
|
+
presses.delete(x);
|
|
131
|
+
if (presses.size >= MAX_PRESSES)
|
|
132
|
+
return 'full';
|
|
133
|
+
}
|
|
134
|
+
const rec = { sig, at: t };
|
|
135
|
+
presses.set(k, rec);
|
|
136
|
+
return { state: 'new', rec };
|
|
137
|
+
};
|
|
138
|
+
/** ⚠️ THE ACCOUNT'S OWN LIMIT, over every other: true (and not counted) while over it. */
|
|
139
|
+
const overWrites = (account) => {
|
|
140
|
+
if (writes.count(account) >= ADDON_WRITES_PER_TEN_MINUTES)
|
|
141
|
+
return true;
|
|
142
|
+
writes.add(account);
|
|
143
|
+
return false;
|
|
144
|
+
};
|
|
145
|
+
/** The press's end: remembered, so the same press again is not sent again. Refusals are not kept (a re-press may go). */
|
|
146
|
+
const ended = (rec, r) => {
|
|
147
|
+
if (rec &&
|
|
148
|
+
(r.outcome === 'done' ||
|
|
149
|
+
r.outcome === 'paid' ||
|
|
150
|
+
r.outcome === 'awaiting-payment' ||
|
|
151
|
+
r.outcome === 'unknown'))
|
|
152
|
+
rec.result = r;
|
|
153
|
+
return r;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* The press gate shared by both writes. Returns a result to answer with, or the press record to
|
|
157
|
+
* go on with. A replay of an answer already given is that answer again - never a second send; a
|
|
158
|
+
* replay of one still in flight is `busy`.
|
|
159
|
+
*/
|
|
160
|
+
const gate = (action, account, press, sig) => {
|
|
161
|
+
if (!account)
|
|
162
|
+
return { no: refused(action, 'unavailable') };
|
|
163
|
+
if (!ADDON_PRESS.test(press))
|
|
164
|
+
return { no: refused(action, 'bad-input') };
|
|
165
|
+
const c = claim(account, press, sig);
|
|
166
|
+
if (c === 'used')
|
|
167
|
+
return { no: refused(action, 'used') };
|
|
168
|
+
if (c === 'full')
|
|
169
|
+
return { no: refused(action, 'busy') };
|
|
170
|
+
if (c.state === 'replay') {
|
|
171
|
+
if (c.rec.result) {
|
|
172
|
+
log(`[addons] ${action}: the same press again after an answer - not sent again`);
|
|
173
|
+
return { no: c.rec.result };
|
|
174
|
+
}
|
|
175
|
+
// Either in flight, or refused before (a refused press may go again - it left no result).
|
|
176
|
+
if (c.rec.sig === sig && inFlight.has(`${account}|${press}`))
|
|
177
|
+
return { no: refused(action, 'busy') };
|
|
178
|
+
}
|
|
179
|
+
if (overWrites(account)) {
|
|
180
|
+
log(`[addons] ${action} refused: too many writes in ten minutes`);
|
|
181
|
+
return { no: refused(action, 'busy') };
|
|
182
|
+
}
|
|
183
|
+
inFlight.add(`${account}|${press}`);
|
|
184
|
+
return { rec: c.rec };
|
|
185
|
+
};
|
|
186
|
+
const release = (account, press) => {
|
|
187
|
+
if (account)
|
|
188
|
+
inFlight.delete(`${account}|${press}`);
|
|
189
|
+
};
|
|
190
|
+
// ── the booking ────────────────────────────────────────────────────────────────────────────
|
|
191
|
+
/** A booking of this account that is live - from the booking layer's own record check. */
|
|
192
|
+
const ownLive = async (who, code) => {
|
|
193
|
+
const b = await o.findBooking(code, who);
|
|
194
|
+
if (!b)
|
|
195
|
+
return { ok: false, why: 'not-yours' };
|
|
196
|
+
if (b.status !== 'confirmed' && b.status !== 'awaiting-payment')
|
|
197
|
+
return { ok: false, why: 'not-live' };
|
|
198
|
+
const today = iso(now()).slice(0, 10);
|
|
199
|
+
if (!b.stay?.checkOut || b.stay.checkOut < today)
|
|
200
|
+
return { ok: false, why: 'not-live' };
|
|
201
|
+
return {
|
|
202
|
+
ok: true,
|
|
203
|
+
booking: {
|
|
204
|
+
code: b.confirmationCode,
|
|
205
|
+
hotelName: b.hotelName,
|
|
206
|
+
checkIn: b.stay.checkIn,
|
|
207
|
+
checkOut: b.stay.checkOut,
|
|
208
|
+
},
|
|
209
|
+
...(b.hotelNames ? { hotelNames: b.hotelNames } : {}),
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
// ── reading ────────────────────────────────────────────────────────────────────────────────
|
|
213
|
+
/**
|
|
214
|
+
* Whether the shop wants its own sign-in for this account: one read of the orders for an id that
|
|
215
|
+
* is no order (the nil UUID) - no payment re-check of any real order. `shop-sign-in` is the shop's
|
|
216
|
+
* own `sign_in_required`; any other answer (not found, busy, unreadable) is not that.
|
|
217
|
+
*/
|
|
218
|
+
const needsShopSignIn = async (who, lang) => !!shop &&
|
|
219
|
+
(await shop.mine(who, '00000000-0000-0000-0000-000000000000', lang).catch(() => false)) ===
|
|
220
|
+
'shop-sign-in';
|
|
221
|
+
const hotelItem = (l) => ({
|
|
222
|
+
id: l.id,
|
|
223
|
+
name: l.name,
|
|
224
|
+
...(l.description ? { description: l.description } : {}),
|
|
225
|
+
...(l.category ? { category: l.category } : {}),
|
|
226
|
+
...(l.unit ? { unit: l.unit } : {}),
|
|
227
|
+
price: amountOf(l.priceMinor, l.currency),
|
|
228
|
+
currency: l.currency,
|
|
229
|
+
booked: l.booked,
|
|
230
|
+
fields: fieldsOf(l.category),
|
|
231
|
+
});
|
|
232
|
+
const shopItem = (i) => ({
|
|
233
|
+
id: i.id,
|
|
234
|
+
name: i.name,
|
|
235
|
+
...(i.description ? { description: i.description } : {}),
|
|
236
|
+
price: centsStr(i.priceCents),
|
|
237
|
+
currency: i.currency,
|
|
238
|
+
available: i.available,
|
|
239
|
+
options: i.options,
|
|
240
|
+
...(i.imageUrl ? { imageUrl: i.imageUrl } : {}),
|
|
241
|
+
});
|
|
242
|
+
const view = async (who, code, lang) => {
|
|
243
|
+
const ref = typeof code === 'string' ? code.trim() : '';
|
|
244
|
+
if (!ref || ref.length > 120)
|
|
245
|
+
return { ok: false, why: 'not-yours' };
|
|
246
|
+
// The booking first: nothing of the hotel's or the shop's is read for one that is not this
|
|
247
|
+
// account's, live. (With add-ons on, 10's own read carries it - and says the same two things.)
|
|
248
|
+
const own = await ownLive(who, ref);
|
|
249
|
+
if (!own.ok)
|
|
250
|
+
return own;
|
|
251
|
+
let hotel;
|
|
252
|
+
if (hotelOn && booking) {
|
|
253
|
+
const r = await booking.addonsOf(own.booking.code, who);
|
|
254
|
+
if (!r.ok)
|
|
255
|
+
return { ok: false, why: r.why === 'off' ? 'unavailable' : r.why };
|
|
256
|
+
hotel =
|
|
257
|
+
r.lines === 'unavailable'
|
|
258
|
+
? { state: 'unavailable', items: [] }
|
|
259
|
+
: { state: r.lines.length ? 'ready' : 'none', items: r.lines.map(hotelItem) };
|
|
260
|
+
}
|
|
261
|
+
let shopPart;
|
|
262
|
+
if (shopOn && shop) {
|
|
263
|
+
const items = await shop.items({ lang, limit: ITEMS_MAX });
|
|
264
|
+
// The catalogue is anonymous; the account's shop sign-in is told by an orders read.
|
|
265
|
+
const needs = await needsShopSignIn(who, lang);
|
|
266
|
+
shopPart = {
|
|
267
|
+
state: needs ? 'needs-shop-sign-in' : items.ok ? 'ready' : 'unavailable',
|
|
268
|
+
...(needs && o.linkUrl ? { linkUrl: o.linkUrl } : {}),
|
|
269
|
+
buy: shop.buyOpen,
|
|
270
|
+
items: items.ok ? items.items.map(shopItem) : [],
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
ok: true,
|
|
275
|
+
view: {
|
|
276
|
+
booking: own.booking,
|
|
277
|
+
...(hotel ? { hotel } : {}),
|
|
278
|
+
...(shopPart ? { shop: shopPart } : {}),
|
|
279
|
+
},
|
|
280
|
+
...(own.hotelNames ? { hotelNames: own.hotelNames } : {}),
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
// ── the hotel's own add-on (10, 11) ────────────────────────────────────────────────────────
|
|
284
|
+
/**
|
|
285
|
+
* ⚠️ THE GUEST'S FIELDS, HELD TO THE ITEM'S OWN LIST (the card's contract, 总线程 11-23): EVERY field
|
|
286
|
+
* the item lists is required, none it does not list is allowed; pickupTime HH:MM, date YYYY-MM-DD
|
|
287
|
+
* within the stay (check-in to check-out), quantity and partySize whole numbers 1 to 10.
|
|
288
|
+
* `defaults` (the model's tool alone, whose schema says "Default 1"): a missing quantity or party is 1.
|
|
289
|
+
*/
|
|
290
|
+
const validHotel = (line, a, stay, defaults) => {
|
|
291
|
+
const fields = fieldsOf(line.category);
|
|
292
|
+
const given = {
|
|
293
|
+
direction: a.direction,
|
|
294
|
+
flightNo: a.flightNo,
|
|
295
|
+
pickupTime: a.pickupTime,
|
|
296
|
+
date: a.date,
|
|
297
|
+
quantity: a.quantity ?? (defaults && fields.includes('quantity') ? 1 : undefined),
|
|
298
|
+
partySize: a.partySize ?? (defaults && fields.includes('partySize') ? 1 : undefined),
|
|
299
|
+
};
|
|
300
|
+
for (const f of Object.keys(given)) {
|
|
301
|
+
if (given[f] !== undefined && !fields.includes(f))
|
|
302
|
+
return { ok: false };
|
|
303
|
+
if (given[f] === undefined && fields.includes(f))
|
|
304
|
+
return { ok: false };
|
|
305
|
+
}
|
|
306
|
+
if (a.direction !== undefined && !['arrival', 'departure', 'round_trip'].includes(a.direction))
|
|
307
|
+
return { ok: false };
|
|
308
|
+
if (a.flightNo !== undefined && !/^[A-Z0-9]{2,8}$/i.test(a.flightNo))
|
|
309
|
+
return { ok: false };
|
|
310
|
+
if (a.pickupTime !== undefined && !/^([01]\d|2[0-3]):[0-5]\d$/.test(a.pickupTime))
|
|
311
|
+
return { ok: false };
|
|
312
|
+
if (a.date !== undefined &&
|
|
313
|
+
!(/^\d{4}-\d{2}-\d{2}$/.test(a.date) &&
|
|
314
|
+
!Number.isNaN(Date.parse(a.date)) &&
|
|
315
|
+
a.date >= stay.checkIn.slice(0, 10) &&
|
|
316
|
+
a.date <= stay.checkOut.slice(0, 10)))
|
|
317
|
+
return { ok: false };
|
|
318
|
+
for (const n of [given.quantity, given.partySize])
|
|
319
|
+
if (n !== undefined && !(Number.isInteger(n) && n >= 1 && n <= 10))
|
|
320
|
+
return { ok: false };
|
|
321
|
+
return {
|
|
322
|
+
ok: true,
|
|
323
|
+
fields,
|
|
324
|
+
quantity: given.quantity ?? 1,
|
|
325
|
+
partySize: given.partySize ?? 1,
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
const hotelSig = (a) => [
|
|
329
|
+
'hotel',
|
|
330
|
+
a.booking,
|
|
331
|
+
a.catalogId,
|
|
332
|
+
a.direction ?? '',
|
|
333
|
+
a.flightNo?.toUpperCase() ?? '',
|
|
334
|
+
a.pickupTime ?? '',
|
|
335
|
+
a.date ?? '',
|
|
336
|
+
a.quantity ?? '',
|
|
337
|
+
a.partySize ?? '',
|
|
338
|
+
a.shownTotal,
|
|
339
|
+
a.currency,
|
|
340
|
+
].join('|');
|
|
341
|
+
/** Everything up to the send, fresh: the item, its fields, the total - or the answer to give instead. */
|
|
342
|
+
const hotelFresh = async (who, a, defaults = false) => {
|
|
343
|
+
const action = 'book_hotel_addon';
|
|
344
|
+
if (!hotelOn || !booking)
|
|
345
|
+
return { ok: false, result: refused(action, 'off') };
|
|
346
|
+
const r = await booking.addonsOf(a.booking, who);
|
|
347
|
+
if (!r.ok)
|
|
348
|
+
return { ok: false, result: refused(action, r.why === 'off' ? 'off' : r.why) };
|
|
349
|
+
if (r.lines === 'unavailable')
|
|
350
|
+
return { ok: false, result: refused(action, 'unavailable') };
|
|
351
|
+
const line = r.lines.find((l) => l.id === a.catalogId);
|
|
352
|
+
// ⚠️ GONE SINCE THE GUEST LOOKED: nothing is sent - the screen is redrawn. (One already added may be
|
|
353
|
+
// added again - "add another" - but only through its own confirm and press, 总线程 11-23.)
|
|
354
|
+
if (!line)
|
|
355
|
+
return { ok: false, result: { action, outcome: 'changed' } };
|
|
356
|
+
const v = validHotel(line, a, r.booking, defaults);
|
|
357
|
+
if (!v.ok)
|
|
358
|
+
return { ok: false, result: refused(action, 'bad-input') };
|
|
359
|
+
const mult = multiplierOf(line, v.fields, { quantity: v.quantity, partySize: v.partySize });
|
|
360
|
+
const totalMinor = line.priceMinor * mult;
|
|
361
|
+
return {
|
|
362
|
+
ok: true,
|
|
363
|
+
code: r.booking.code,
|
|
364
|
+
line,
|
|
365
|
+
totalMinor,
|
|
366
|
+
quantity: v.quantity,
|
|
367
|
+
partySize: v.partySize,
|
|
368
|
+
fields: v.fields,
|
|
369
|
+
hotelName: r.booking.hotelName,
|
|
370
|
+
...(r.booking.hotelNames ? { hotelNames: r.booking.hotelNames } : {}),
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
const bookHotelWith = async (who, ask) => {
|
|
374
|
+
const action = 'book_hotel_addon';
|
|
375
|
+
if (!hotelOn || !booking)
|
|
376
|
+
return refused(action, 'off');
|
|
377
|
+
const shownMinor = minorOf(ask.shownTotal, ask.currency);
|
|
378
|
+
if (shownMinor === undefined ||
|
|
379
|
+
!/^[A-Z]{3}$/.test(ask.currency) ||
|
|
380
|
+
typeof ask.catalogId !== 'string' ||
|
|
381
|
+
!ask.catalogId ||
|
|
382
|
+
ask.catalogId.length > 200)
|
|
383
|
+
return refused(action, 'bad-input');
|
|
384
|
+
const account = o.accountOf(who);
|
|
385
|
+
const g = gate(action, account, ask.press, hotelSig(ask));
|
|
386
|
+
if ('no' in g)
|
|
387
|
+
return g.no;
|
|
388
|
+
try {
|
|
389
|
+
// ⚠️ FRESH, AT THE MOMENT OF THE PRESS: 10 is read again; the total is recomputed from its
|
|
390
|
+
// price and held to what the screen showed - to the minor unit and the currency.
|
|
391
|
+
const f = await hotelFresh(who, ask);
|
|
392
|
+
if (!f.ok)
|
|
393
|
+
return ended(undefined, f.result);
|
|
394
|
+
if (account && stillChecking(account, f.code, f.line))
|
|
395
|
+
return refused(action, 'checking');
|
|
396
|
+
if (f.line.currency !== ask.currency || f.totalMinor !== shownMinor)
|
|
397
|
+
return { action, outcome: 'changed' };
|
|
398
|
+
const sent = await booking.bookAddonOnce(f.code, {
|
|
399
|
+
catalogId: f.line.id,
|
|
400
|
+
...(ask.direction ? { direction: ask.direction } : {}),
|
|
401
|
+
...(ask.flightNo ? { flightNo: ask.flightNo } : {}),
|
|
402
|
+
...(ask.pickupTime ? { pickupTime: ask.pickupTime } : {}),
|
|
403
|
+
...(ask.date ? { date: ask.date } : {}),
|
|
404
|
+
...(f.fields.includes('quantity') ? { quantity: f.quantity } : {}),
|
|
405
|
+
...(f.fields.includes('partySize') ? { partySize: f.partySize } : {}),
|
|
406
|
+
}, who);
|
|
407
|
+
log(`[addons] hotel add-on ${f.line.id} on order of ${tag(keyOf(who))}: ${sent.outcome}${sent.outcome === 'refused' ? ` (${sent.why})` : ''}`);
|
|
408
|
+
if (sent.outcome === 'refused')
|
|
409
|
+
return ended(g.rec, refused(action, sent.why === 'off' ? 'off' : sent.why));
|
|
410
|
+
if (sent.outcome === 'unknown') {
|
|
411
|
+
if (account)
|
|
412
|
+
unknownAt.set(checkingKey(account, f.code, f.line.id), now());
|
|
413
|
+
return ended(g.rec, { action, outcome: 'unknown' });
|
|
414
|
+
}
|
|
415
|
+
// Sent. `CONFIRMED` is a done; any other status is not known to be one.
|
|
416
|
+
const answered = sent.totalMinor !== undefined && sent.currency !== undefined;
|
|
417
|
+
const over = answered && (sent.currency !== ask.currency || sent.totalMinor > shownMinor);
|
|
418
|
+
if (over)
|
|
419
|
+
log(`[addons] NEEDS A PERSON: hotel add-on ${sent.code} was totalled ${sent.currency} ${amountOf(sent.totalMinor, sent.currency)} against ${ask.currency} ${ask.shownTotal} confirmed`);
|
|
420
|
+
if (!sent.confirmed) {
|
|
421
|
+
if (account)
|
|
422
|
+
unknownAt.set(checkingKey(account, f.code, f.line.id), now());
|
|
423
|
+
return ended(g.rec, {
|
|
424
|
+
action,
|
|
425
|
+
outcome: 'unknown',
|
|
426
|
+
addonCode: sent.code,
|
|
427
|
+
...(answered
|
|
428
|
+
? {
|
|
429
|
+
total: amountOf(sent.totalMinor, sent.currency),
|
|
430
|
+
currency: sent.currency,
|
|
431
|
+
}
|
|
432
|
+
: {}),
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
return ended(g.rec, {
|
|
436
|
+
action,
|
|
437
|
+
outcome: 'done',
|
|
438
|
+
addonCode: sent.code,
|
|
439
|
+
...(answered
|
|
440
|
+
? {
|
|
441
|
+
total: amountOf(sent.totalMinor, sent.currency),
|
|
442
|
+
currency: sent.currency,
|
|
443
|
+
}
|
|
444
|
+
: {}),
|
|
445
|
+
...(over ? { needsPerson: true } : {}),
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
catch {
|
|
449
|
+
// Nothing threw before the send that could have made it; anything that did is not a sent request.
|
|
450
|
+
return refused(action, 'unavailable');
|
|
451
|
+
}
|
|
452
|
+
finally {
|
|
453
|
+
release(account, ask.press);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
// ── the shop purchase (/addon/buy) ─────────────────────────────────────────────────────────
|
|
457
|
+
const shopSig = (a) => ['shop', a.booking, a.item, a.option ?? '', a.quantity, a.shownUnit, a.currency].join('|');
|
|
458
|
+
const fromBuy = (r) => {
|
|
459
|
+
const action = 'buy_shop_item';
|
|
460
|
+
switch (r.outcome) {
|
|
461
|
+
case 'off':
|
|
462
|
+
return refused(action, 'off');
|
|
463
|
+
case 'busy':
|
|
464
|
+
return refused(action, 'busy');
|
|
465
|
+
case 'not-signed-in':
|
|
466
|
+
case 'session-expired':
|
|
467
|
+
return refused(action, 'unavailable');
|
|
468
|
+
case 'shop-sign-in':
|
|
469
|
+
return refused(action, 'needs-shop-sign-in');
|
|
470
|
+
case 'booking-refused':
|
|
471
|
+
return refused(action, r.why === 'not-found' ? 'not-yours' : 'not-live');
|
|
472
|
+
case 'refused':
|
|
473
|
+
return refused(action, r.why === 'quantity' ||
|
|
474
|
+
r.why === 'confirmation-code' ||
|
|
475
|
+
r.why === 'shown' ||
|
|
476
|
+
r.why === 'option'
|
|
477
|
+
? 'bad-input'
|
|
478
|
+
: r.why === 'in-progress' || r.why === 'checking'
|
|
479
|
+
? 'busy'
|
|
480
|
+
: r.why === 'backend'
|
|
481
|
+
? 'declined'
|
|
482
|
+
: 'unavailable');
|
|
483
|
+
case 'price-changed':
|
|
484
|
+
return { action, outcome: 'changed' };
|
|
485
|
+
case 'awaiting-payment':
|
|
486
|
+
return {
|
|
487
|
+
action,
|
|
488
|
+
outcome: 'awaiting-payment',
|
|
489
|
+
total: centsStr(r.order.totalCents),
|
|
490
|
+
currency: r.order.currency,
|
|
491
|
+
...(r.order.id ? { orderId: r.order.id } : {}),
|
|
492
|
+
checkoutUrl: r.checkoutUrl,
|
|
493
|
+
};
|
|
494
|
+
case 'wrong-amount':
|
|
495
|
+
case 'bad-link':
|
|
496
|
+
return {
|
|
497
|
+
action,
|
|
498
|
+
outcome: 'refused',
|
|
499
|
+
reason: 'declined',
|
|
500
|
+
...(r.needsPerson ? { needsPerson: true } : {}),
|
|
501
|
+
held: r.outcome === 'bad-link' ? 'link' : 'amount',
|
|
502
|
+
...(r.orderId ? { orderId: r.orderId } : {}),
|
|
503
|
+
};
|
|
504
|
+
case 'paid':
|
|
505
|
+
return {
|
|
506
|
+
action,
|
|
507
|
+
outcome: 'paid',
|
|
508
|
+
...(r.totalCents !== undefined && r.currency
|
|
509
|
+
? { total: centsStr(r.totalCents), currency: r.currency }
|
|
510
|
+
: {}),
|
|
511
|
+
...(r.order.id ? { orderId: r.order.id } : {}),
|
|
512
|
+
...(r.needsPerson ? { needsPerson: true } : {}),
|
|
513
|
+
};
|
|
514
|
+
case 'unknown':
|
|
515
|
+
return {
|
|
516
|
+
action,
|
|
517
|
+
outcome: 'unknown',
|
|
518
|
+
...(r.needsPerson ? { needsPerson: true } : {}),
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
const buyShopWith = async (who, ask, lang) => {
|
|
523
|
+
const action = 'buy_shop_item';
|
|
524
|
+
if (!shop || !shop.buyOpen)
|
|
525
|
+
return refused(action, 'off');
|
|
526
|
+
const unit = centsOfStr(ask.shownUnit);
|
|
527
|
+
if (unit === undefined ||
|
|
528
|
+
!/^[A-Z]{3}$/.test(ask.currency) ||
|
|
529
|
+
!Number.isInteger(ask.quantity) ||
|
|
530
|
+
ask.quantity < 1 ||
|
|
531
|
+
ask.quantity > 10 ||
|
|
532
|
+
typeof ask.item !== 'string' ||
|
|
533
|
+
!ask.item ||
|
|
534
|
+
ask.item.length > 200 ||
|
|
535
|
+
(ask.option !== undefined && (typeof ask.option !== 'string' || ask.option.length > 200)))
|
|
536
|
+
return refused(action, 'bad-input');
|
|
537
|
+
const account = o.accountOf(who);
|
|
538
|
+
const g = gate(action, account, ask.press, shopSig(ask));
|
|
539
|
+
if ('no' in g)
|
|
540
|
+
return g.no;
|
|
541
|
+
try {
|
|
542
|
+
// ⚠️ ONE SEND: shop.buy re-reads the item (price, stock, option) and the booking, and is itself
|
|
543
|
+
// held to once per item; its answer `unknown` is never sent again.
|
|
544
|
+
const r = await shop.buy(who, {
|
|
545
|
+
itemId: ask.item,
|
|
546
|
+
...(ask.option ? { option: ask.option } : {}),
|
|
547
|
+
quantity: ask.quantity,
|
|
548
|
+
booking: ask.booking,
|
|
549
|
+
unitCents: unit,
|
|
550
|
+
currency: ask.currency,
|
|
551
|
+
lang,
|
|
552
|
+
});
|
|
553
|
+
const out = fromBuy(r);
|
|
554
|
+
log(`[addons] shop buy on ${tag(keyOf(who))}: ${out.outcome}${out.reason ? ` (${out.reason})` : ''}${out.needsPerson ? ' NEEDS A PERSON' : ''}`);
|
|
555
|
+
return ended(g.rec, out);
|
|
556
|
+
}
|
|
557
|
+
catch {
|
|
558
|
+
return refused(action, 'unavailable');
|
|
559
|
+
}
|
|
560
|
+
finally {
|
|
561
|
+
release(account, ask.press);
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
// ── preparing a confirm for the page (the model's tools) ───────────────────────────────────
|
|
565
|
+
const store = (p) => {
|
|
566
|
+
const t = now();
|
|
567
|
+
if (prepared.size >= MAX_TICKETS)
|
|
568
|
+
for (const [k, v] of prepared)
|
|
569
|
+
if (v.until <= t)
|
|
570
|
+
prepared.delete(k);
|
|
571
|
+
const mine = [...prepared].filter(([, v]) => v.key === p.key);
|
|
572
|
+
if (mine.length >= TICKETS_PER_CONVERSATION)
|
|
573
|
+
for (const [k] of mine.slice(0, mine.length - TICKETS_PER_CONVERSATION + 1))
|
|
574
|
+
prepared.delete(k);
|
|
575
|
+
if (prepared.size >= MAX_TICKETS)
|
|
576
|
+
return '';
|
|
577
|
+
const ref = randomBytes(24).toString('base64url');
|
|
578
|
+
prepared.set(ref, p);
|
|
579
|
+
return ref;
|
|
580
|
+
};
|
|
581
|
+
const prepareHotel = async (who, ask) => {
|
|
582
|
+
const action = 'book_hotel_addon';
|
|
583
|
+
const account = o.accountOf(who);
|
|
584
|
+
if (!account)
|
|
585
|
+
return { ok: false, result: refused(action, 'unavailable') };
|
|
586
|
+
const base = { ...ask, shownTotal: '0.00', currency: 'XXX' };
|
|
587
|
+
const f = await hotelFresh(who, base, true);
|
|
588
|
+
if (!f.ok)
|
|
589
|
+
return { ok: false, result: f.result };
|
|
590
|
+
// The last request for this item is not known yet: no page is made for it either.
|
|
591
|
+
if (stillChecking(account, f.code, f.line))
|
|
592
|
+
return { ok: false, result: refused(action, 'checking') };
|
|
593
|
+
const shownTotal = amountOf(f.totalMinor, f.line.currency);
|
|
594
|
+
const full = {
|
|
595
|
+
...ask,
|
|
596
|
+
booking: f.code,
|
|
597
|
+
catalogId: f.line.id,
|
|
598
|
+
// The defaults the model's tool allows, made explicit: the confirm sends what the page showed.
|
|
599
|
+
...(f.fields.includes('quantity') ? { quantity: f.quantity } : {}),
|
|
600
|
+
...(f.fields.includes('partySize') ? { partySize: f.partySize } : {}),
|
|
601
|
+
shownTotal,
|
|
602
|
+
currency: f.line.currency,
|
|
603
|
+
};
|
|
604
|
+
const until = now() + ADDON_TICKET_MS;
|
|
605
|
+
const confirm = {
|
|
606
|
+
kind: 'hotel',
|
|
607
|
+
until: iso(until),
|
|
608
|
+
booking: {
|
|
609
|
+
code: f.code,
|
|
610
|
+
hotelName: f.hotelName,
|
|
611
|
+
...(f.hotelNames ? { hotelNames: f.hotelNames } : {}),
|
|
612
|
+
},
|
|
613
|
+
name: f.line.name,
|
|
614
|
+
unit: amountOf(f.line.priceMinor, f.line.currency),
|
|
615
|
+
currency: f.line.currency,
|
|
616
|
+
quantity: f.fields.includes('quantity')
|
|
617
|
+
? f.quantity
|
|
618
|
+
: f.fields.includes('partySize') && /guest|person|pax/i.test(f.line.unit ?? '')
|
|
619
|
+
? f.partySize
|
|
620
|
+
: 1,
|
|
621
|
+
total: shownTotal,
|
|
622
|
+
details: {
|
|
623
|
+
...(ask.direction ? { direction: ask.direction } : {}),
|
|
624
|
+
...(ask.flightNo ? { flightNo: ask.flightNo.toUpperCase() } : {}),
|
|
625
|
+
...(ask.pickupTime ? { pickupTime: ask.pickupTime } : {}),
|
|
626
|
+
...(ask.date ? { date: ask.date } : {}),
|
|
627
|
+
...(f.fields.includes('partySize') && ask.partySize ? { partySize: ask.partySize } : {}),
|
|
628
|
+
},
|
|
629
|
+
};
|
|
630
|
+
const ref = store({ kind: 'hotel', key: keyOf(who), account, ask: full, confirm, until });
|
|
631
|
+
if (!ref)
|
|
632
|
+
return { ok: false, result: refused(action, 'busy') };
|
|
633
|
+
return { ok: true, ref, confirm };
|
|
634
|
+
};
|
|
635
|
+
const prepareShop = async (who, ask, lang) => {
|
|
636
|
+
const action = 'buy_shop_item';
|
|
637
|
+
if (!shop || !shop.buyOpen)
|
|
638
|
+
return { ok: false, result: refused(action, 'off') };
|
|
639
|
+
const account = o.accountOf(who);
|
|
640
|
+
if (!account)
|
|
641
|
+
return { ok: false, result: refused(action, 'unavailable') };
|
|
642
|
+
const unit = centsOfStr(ask.unitPrice);
|
|
643
|
+
if (unit === undefined ||
|
|
644
|
+
!/^[A-Z]{3}$/.test(ask.currency) ||
|
|
645
|
+
!Number.isInteger(ask.quantity) ||
|
|
646
|
+
ask.quantity < 1 ||
|
|
647
|
+
ask.quantity > 10)
|
|
648
|
+
return { ok: false, result: refused(action, 'bad-input') };
|
|
649
|
+
// The booking: this account's, live - else no page is made for it.
|
|
650
|
+
const own = await ownLive(who, ask.booking);
|
|
651
|
+
if (!own.ok)
|
|
652
|
+
return { ok: false, result: refused(action, own.why) };
|
|
653
|
+
const it = await shop.item(ask.item, lang);
|
|
654
|
+
if (!it.ok)
|
|
655
|
+
return {
|
|
656
|
+
ok: false,
|
|
657
|
+
result: refused(action, it.why === 'shop-sign-in'
|
|
658
|
+
? 'needs-shop-sign-in'
|
|
659
|
+
: it.why === 'busy'
|
|
660
|
+
? 'busy'
|
|
661
|
+
: it.why === 'not-found'
|
|
662
|
+
? 'unavailable'
|
|
663
|
+
: 'unavailable'),
|
|
664
|
+
};
|
|
665
|
+
const item = it.item;
|
|
666
|
+
const option = ask.option?.trim() ?? '';
|
|
667
|
+
if (!item.available)
|
|
668
|
+
return { ok: false, result: refused(action, 'unavailable') };
|
|
669
|
+
if (item.options.length > 0 ? !item.options.includes(option) : option !== '')
|
|
670
|
+
return { ok: false, result: refused(action, 'bad-input') };
|
|
671
|
+
// ⚠️ THE PRICE THE MODEL SAID MUST BE THE SHOP'S NOW: else the guest is not shown a page for it.
|
|
672
|
+
if (item.priceCents !== unit || item.currency !== ask.currency)
|
|
673
|
+
return { ok: false, result: { action, outcome: 'changed' } };
|
|
674
|
+
// The shop wants its own sign-in: no page is made for a purchase that cannot be sent.
|
|
675
|
+
if (await needsShopSignIn(who, lang))
|
|
676
|
+
return { ok: false, result: refused(action, 'needs-shop-sign-in') };
|
|
677
|
+
const full = {
|
|
678
|
+
booking: own.booking.code,
|
|
679
|
+
item: item.id,
|
|
680
|
+
...(option ? { option } : {}),
|
|
681
|
+
quantity: ask.quantity,
|
|
682
|
+
shownUnit: centsStr(unit),
|
|
683
|
+
currency: item.currency,
|
|
684
|
+
};
|
|
685
|
+
const until = now() + ADDON_TICKET_MS;
|
|
686
|
+
const confirm = {
|
|
687
|
+
kind: 'shop',
|
|
688
|
+
until: iso(until),
|
|
689
|
+
booking: {
|
|
690
|
+
code: own.booking.code,
|
|
691
|
+
hotelName: own.booking.hotelName,
|
|
692
|
+
...(own.hotelNames ? { hotelNames: own.hotelNames } : {}),
|
|
693
|
+
},
|
|
694
|
+
name: item.name,
|
|
695
|
+
...(option ? { option } : {}),
|
|
696
|
+
unit: full.shownUnit,
|
|
697
|
+
currency: item.currency,
|
|
698
|
+
quantity: ask.quantity,
|
|
699
|
+
total: centsStr(unit * ask.quantity),
|
|
700
|
+
details: {},
|
|
701
|
+
};
|
|
702
|
+
const ref = store({
|
|
703
|
+
kind: 'shop',
|
|
704
|
+
key: keyOf(who),
|
|
705
|
+
account,
|
|
706
|
+
ask: full,
|
|
707
|
+
lang,
|
|
708
|
+
confirm,
|
|
709
|
+
until,
|
|
710
|
+
});
|
|
711
|
+
if (!ref)
|
|
712
|
+
return { ok: false, result: refused(action, 'busy') };
|
|
713
|
+
return { ok: true, ref, confirm };
|
|
714
|
+
};
|
|
715
|
+
const pending = (who, ref) => {
|
|
716
|
+
const p = prepared.get(ref);
|
|
717
|
+
if (!p || p.key !== keyOf(who) || p.until <= now())
|
|
718
|
+
return undefined;
|
|
719
|
+
return p.confirm;
|
|
720
|
+
};
|
|
721
|
+
const confirm = async (who, ref, lang) => {
|
|
722
|
+
const p = prepared.get(ref);
|
|
723
|
+
// ⚠️ ONE USE, WHATEVER COMES OF IT: the ticket is gone before anything is sent.
|
|
724
|
+
prepared.delete(ref);
|
|
725
|
+
if (!p || p.key !== keyOf(who) || p.until <= now())
|
|
726
|
+
return undefined;
|
|
727
|
+
// The ticket is for the account it was made for: a conversation that now says another is refused.
|
|
728
|
+
if (o.accountOf(who) !== p.account)
|
|
729
|
+
return undefined;
|
|
730
|
+
// The ticket's own code is the press.
|
|
731
|
+
const press = hashOf(`page:${ref}`) + hashOf(`page2:${ref}`);
|
|
732
|
+
const result = p.kind === 'hotel'
|
|
733
|
+
? await bookHotelWith(who, { ...p.ask, press })
|
|
734
|
+
: await buyShopWith(who, { ...p.ask, press }, p.lang ?? lang);
|
|
735
|
+
return { result, confirm: p.confirm };
|
|
736
|
+
};
|
|
737
|
+
return {
|
|
738
|
+
hotelOn,
|
|
739
|
+
shopOn,
|
|
740
|
+
buyOpen: !!shop?.buyOpen,
|
|
741
|
+
view,
|
|
742
|
+
canAdd: (b) => (b.status === 'confirmed' || b.status === 'awaiting-payment') &&
|
|
743
|
+
!!b.stay?.checkOut &&
|
|
744
|
+
b.stay.checkOut >= iso(now()).slice(0, 10),
|
|
745
|
+
bookHotel: bookHotelWith,
|
|
746
|
+
buyShop: buyShopWith,
|
|
747
|
+
prepareHotel,
|
|
748
|
+
prepareShop,
|
|
749
|
+
pending,
|
|
750
|
+
confirm,
|
|
751
|
+
discard: (who, ref) => {
|
|
752
|
+
const p = prepared.get(ref);
|
|
753
|
+
if (p && p.key === keyOf(who))
|
|
754
|
+
prepared.delete(ref);
|
|
755
|
+
},
|
|
756
|
+
};
|
|
757
|
+
};
|
|
758
|
+
//# sourceMappingURL=addons.js.map
|