@fleetbridge/client 0.0.0-stage → 1.4.0
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/README.md +308 -2
- package/index.d.ts +1691 -0
- package/index.js +2078 -0
- package/package.json +20 -4
package/index.js
ADDED
|
@@ -0,0 +1,2078 @@
|
|
|
1
|
+
// src/transport.ts
|
|
2
|
+
var PUBLIC_ACTIONS = [
|
|
3
|
+
"public_branches",
|
|
4
|
+
"search_sources",
|
|
5
|
+
"cache_version",
|
|
6
|
+
"search_availability",
|
|
7
|
+
"get_estimation",
|
|
8
|
+
"get_extras",
|
|
9
|
+
"get_one_way_fee",
|
|
10
|
+
"supplier_terms",
|
|
11
|
+
"booking_request",
|
|
12
|
+
"request_quote",
|
|
13
|
+
"lookup_quote",
|
|
14
|
+
"confirm_quote",
|
|
15
|
+
"lookup_booking",
|
|
16
|
+
"cancel_my_booking",
|
|
17
|
+
"amend_my_booking",
|
|
18
|
+
"payment_methods",
|
|
19
|
+
"start_payment",
|
|
20
|
+
"verify_payment",
|
|
21
|
+
"claim_coupon",
|
|
22
|
+
"my_coupons"
|
|
23
|
+
];
|
|
24
|
+
var COUPON_CHANNELS = ["web", "app", "whatsapp"];
|
|
25
|
+
function isCouponChannel(value) {
|
|
26
|
+
return typeof value === "string" && COUPON_CHANNELS.includes(value);
|
|
27
|
+
}
|
|
28
|
+
var CHANNEL_HEADER = "x-fleetbridge-channel";
|
|
29
|
+
var GENERIC_ERROR = "Something went wrong. Please try again.";
|
|
30
|
+
var RATE_LIMITED = "Too many requests just now. Please try again in an hour.";
|
|
31
|
+
var UNREACHABLE = "Could not reach the site. Check your connection and try again.";
|
|
32
|
+
function shortAction(action) {
|
|
33
|
+
return action.startsWith("fleetbridge_") ? action.slice("fleetbridge_".length) : action;
|
|
34
|
+
}
|
|
35
|
+
async function readEnvelope(response, action) {
|
|
36
|
+
const raw = await response.text();
|
|
37
|
+
let payload;
|
|
38
|
+
try {
|
|
39
|
+
payload = JSON.parse(raw);
|
|
40
|
+
} catch {
|
|
41
|
+
return { success: false, error: response.status === 429 ? RATE_LIMITED : GENERIC_ERROR, status: response.status };
|
|
42
|
+
}
|
|
43
|
+
if (!response.ok || !payload.success) {
|
|
44
|
+
const status = response.status;
|
|
45
|
+
if (response.status === 429 && !payload.data?.message) return { success: false, error: RATE_LIMITED, status };
|
|
46
|
+
return {
|
|
47
|
+
success: false,
|
|
48
|
+
error: payload.data?.message ?? GENERIC_ERROR,
|
|
49
|
+
status,
|
|
50
|
+
data: payload.data
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return { success: true, data: payload.data };
|
|
54
|
+
}
|
|
55
|
+
function formBody(fields) {
|
|
56
|
+
const body = new FormData();
|
|
57
|
+
for (const [key, value] of Object.entries(fields)) body.append(key, value);
|
|
58
|
+
return body;
|
|
59
|
+
}
|
|
60
|
+
function wordpressTransport(options) {
|
|
61
|
+
return {
|
|
62
|
+
async post(action, fields = {}) {
|
|
63
|
+
const name = shortAction(action);
|
|
64
|
+
if (!options.ajaxUrl || !options.nonce) {
|
|
65
|
+
return { success: false, error: "This site isn't connected to FleetBridge yet." };
|
|
66
|
+
}
|
|
67
|
+
const body = formBody({ ...fields, action: `fleetbridge_${name}`, nonce: options.nonce });
|
|
68
|
+
try {
|
|
69
|
+
const response = await fetch(options.ajaxUrl, { method: "POST", credentials: "same-origin", body });
|
|
70
|
+
return await readEnvelope(response, name);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
console.error(`[FleetBridge] ${name} failed`, err);
|
|
73
|
+
return { success: false, error: UNREACHABLE, status: 0 };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
var TIMED_OUT = "The site took too long to answer. Check your connection and try again.";
|
|
79
|
+
function httpTransport(options) {
|
|
80
|
+
const headers = { ...options.headers ?? {} };
|
|
81
|
+
if (options.channel) headers[CHANNEL_HEADER] = options.channel;
|
|
82
|
+
return {
|
|
83
|
+
async post(action, fields = {}) {
|
|
84
|
+
const name = shortAction(action);
|
|
85
|
+
const body = formBody({ ...fields, action: name });
|
|
86
|
+
const limit = typeof options.timeoutMs === "function" ? options.timeoutMs(name) : options.timeoutMs;
|
|
87
|
+
const controller = limit && limit > 0 && typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
88
|
+
let timedOut = false;
|
|
89
|
+
const timer = controller ? setTimeout(() => {
|
|
90
|
+
timedOut = true;
|
|
91
|
+
controller.abort();
|
|
92
|
+
}, limit) : null;
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(options.endpoint, {
|
|
95
|
+
method: "POST",
|
|
96
|
+
credentials: options.credentials ?? "same-origin",
|
|
97
|
+
headers,
|
|
98
|
+
body,
|
|
99
|
+
...controller ? { signal: controller.signal } : {}
|
|
100
|
+
});
|
|
101
|
+
return await readEnvelope(response, name);
|
|
102
|
+
} catch (err) {
|
|
103
|
+
if (timedOut) return { success: false, error: TIMED_OUT, status: 0 };
|
|
104
|
+
console.error(`[FleetBridge] ${name} failed`, err);
|
|
105
|
+
return { success: false, error: UNREACHABLE, status: 0 };
|
|
106
|
+
} finally {
|
|
107
|
+
if (timer) clearTimeout(timer);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
var current = null;
|
|
113
|
+
function setTransport(transport) {
|
|
114
|
+
current = transport;
|
|
115
|
+
}
|
|
116
|
+
function getTransport() {
|
|
117
|
+
if (current) return current;
|
|
118
|
+
return {
|
|
119
|
+
async post() {
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: "This site isn't connected to FleetBridge yet. Call setTransport() (or wrap the FleetBridge components in <FleetBridgeProvider transport={\u2026}>)."
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/catalogue/extras.ts
|
|
129
|
+
var POPULAR_CODES = /* @__PURE__ */ new Set([
|
|
130
|
+
"WWC",
|
|
131
|
+
"T&W",
|
|
132
|
+
"TWC2",
|
|
133
|
+
"ADR",
|
|
134
|
+
"FADR",
|
|
135
|
+
"CSS",
|
|
136
|
+
"NAV",
|
|
137
|
+
// Navigation System / GPS
|
|
138
|
+
"GPS",
|
|
139
|
+
"DRIVR",
|
|
140
|
+
"AD",
|
|
141
|
+
// CarPro Add Driver (Thrifty Extras API)
|
|
142
|
+
"BS",
|
|
143
|
+
// CarPro Baby Seat
|
|
144
|
+
"YDS",
|
|
145
|
+
// Young Driver
|
|
146
|
+
"GAT",
|
|
147
|
+
// Glass and tyre waiver (legacy Thermeon catalogue)
|
|
148
|
+
"CHILD",
|
|
149
|
+
"INFAN"
|
|
150
|
+
]);
|
|
151
|
+
var POPULAR_NAME = /wheel\s*&?\s*windscreen|tire\s*and\s*windscreen|additional\s*driver|add\s*driver|baby\s*seat|young\s*driver|3[- ]?in[- ]?1\s*child\s*seat|\bgps\b|navigat|wifi|wi-?fi|hotspot|internet/i;
|
|
152
|
+
function isPopularExtra(code, name) {
|
|
153
|
+
if (POPULAR_CODES.has(code.toUpperCase())) return true;
|
|
154
|
+
return POPULAR_NAME.test(name);
|
|
155
|
+
}
|
|
156
|
+
function normalizeServiceType(raw) {
|
|
157
|
+
const value = (raw ?? "").trim().toUpperCase().replace(/[_-]+/g, " ");
|
|
158
|
+
if (value === "PER UNIT" || value === "UNIT") return "Per Unit";
|
|
159
|
+
if (value === "PER DAY" || value === "DAY" || value === "DAILY") return "Per Day";
|
|
160
|
+
if (value === "PER TRIP" || value === "TRIP") return "Per Trip";
|
|
161
|
+
if (value === "PER KILOMETER" || value === "PER KILOMETRE" || value === "PER KM") {
|
|
162
|
+
return "Per Kilometer";
|
|
163
|
+
}
|
|
164
|
+
if (value.includes("WAIVER")) return "WAIVERS & EXTRAS";
|
|
165
|
+
if (value === "PER RENTAL" || value === "RENTAL" || !value) return "Per Rental";
|
|
166
|
+
return "Per Rental";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/lib/search.ts
|
|
170
|
+
var SAME_AS_PICKUP = "same";
|
|
171
|
+
var DEFAULT_PICKUP_LOCATION = "OR Tambo International Airport";
|
|
172
|
+
var DRIVER_AGE_BANDS = [
|
|
173
|
+
{ label: "25\u201370" },
|
|
174
|
+
{ label: "21\u201324", age: 21 },
|
|
175
|
+
{ label: "18\u201320", age: 18 },
|
|
176
|
+
{ label: "71+", age: 71 }
|
|
177
|
+
];
|
|
178
|
+
var DEFAULT_CRITERIA = {
|
|
179
|
+
pickupLocation: DEFAULT_PICKUP_LOCATION,
|
|
180
|
+
dropoffLocation: SAME_AS_PICKUP,
|
|
181
|
+
pickupDate: "",
|
|
182
|
+
dropoffDate: "",
|
|
183
|
+
pickupTime: "10:00",
|
|
184
|
+
dropoffTime: "10:00",
|
|
185
|
+
coupon: "",
|
|
186
|
+
residency: "ZA"
|
|
187
|
+
};
|
|
188
|
+
var TIME_SLOTS = Array.from({ length: 33 }, (_, index) => {
|
|
189
|
+
const minutes = 6 * 60 + index * 30;
|
|
190
|
+
return `${String(Math.floor(minutes / 60)).padStart(2, "0")}:${minutes % 60 === 0 ? "00" : "30"}`;
|
|
191
|
+
});
|
|
192
|
+
function criteriaToQuery(criteria) {
|
|
193
|
+
const params = new URLSearchParams();
|
|
194
|
+
if (criteria.pickupLocation) params.set("location", criteria.pickupLocation);
|
|
195
|
+
if (criteria.dropoffLocation !== SAME_AS_PICKUP) params.set("dropoff", criteria.dropoffLocation);
|
|
196
|
+
if (criteria.pickupDate) {
|
|
197
|
+
params.set("pickup", criteria.pickupDate);
|
|
198
|
+
params.set("pickupTime", criteria.pickupTime);
|
|
199
|
+
}
|
|
200
|
+
if (criteria.dropoffDate) {
|
|
201
|
+
params.set("return", criteria.dropoffDate);
|
|
202
|
+
params.set("returnTime", criteria.dropoffTime);
|
|
203
|
+
}
|
|
204
|
+
if (criteria.coupon.trim()) params.set("coupon", criteria.coupon.trim().toUpperCase());
|
|
205
|
+
if (criteria.residency !== DEFAULT_CRITERIA.residency) params.set("residency", criteria.residency);
|
|
206
|
+
if (criteria.supplier) params.set("supplier", criteria.supplier);
|
|
207
|
+
if (criteria.driverAge) params.set("age", String(criteria.driverAge));
|
|
208
|
+
return params.toString();
|
|
209
|
+
}
|
|
210
|
+
function criteriaFromParams(search) {
|
|
211
|
+
const params = new URLSearchParams(search);
|
|
212
|
+
const get = (key, fallback = "") => params.get(key) || fallback;
|
|
213
|
+
return {
|
|
214
|
+
pickupLocation: get("location", DEFAULT_CRITERIA.pickupLocation),
|
|
215
|
+
dropoffLocation: get("dropoff", SAME_AS_PICKUP),
|
|
216
|
+
pickupDate: get("pickup"),
|
|
217
|
+
dropoffDate: get("return"),
|
|
218
|
+
pickupTime: get("pickupTime", DEFAULT_CRITERIA.pickupTime),
|
|
219
|
+
dropoffTime: get("returnTime", DEFAULT_CRITERIA.dropoffTime),
|
|
220
|
+
coupon: get("coupon"),
|
|
221
|
+
residency: get("residency") === "INT" ? "INT" : "ZA",
|
|
222
|
+
...params.get("supplier") ? { supplier: params.get("supplier") ?? void 0 } : {},
|
|
223
|
+
...ageFrom(params.get("age")) ? { driverAge: ageFrom(params.get("age")) } : {}
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function ageFrom(value) {
|
|
227
|
+
const age = Number(value);
|
|
228
|
+
return Number.isInteger(age) && age >= 16 && age <= 99 ? age : void 0;
|
|
229
|
+
}
|
|
230
|
+
function rentalDays(criteria) {
|
|
231
|
+
const { pickupDate, dropoffDate } = criteria;
|
|
232
|
+
if (!pickupDate || !dropoffDate) return 0;
|
|
233
|
+
const start = Date.parse(`${pickupDate}T00:00:00`);
|
|
234
|
+
const end = Date.parse(`${dropoffDate}T00:00:00`);
|
|
235
|
+
if (Number.isNaN(start) || Number.isNaN(end) || end < start) return 0;
|
|
236
|
+
return Math.max(1, Math.round((end - start) / 864e5));
|
|
237
|
+
}
|
|
238
|
+
function formatDateLabel(iso) {
|
|
239
|
+
if (!iso) return "";
|
|
240
|
+
const [year, month, day] = iso.split("-").map(Number);
|
|
241
|
+
if (!year || !month || !day) return iso;
|
|
242
|
+
return new Date(year, month - 1, day).toLocaleDateString(void 0, {
|
|
243
|
+
day: "numeric",
|
|
244
|
+
month: "short"
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
var ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
248
|
+
var CLOCK = /^(\d{1,2}):(\d{2})$/;
|
|
249
|
+
function isRealDate(value) {
|
|
250
|
+
const match = ISO_DATE.exec(value);
|
|
251
|
+
if (!match) return false;
|
|
252
|
+
const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
253
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
254
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
255
|
+
}
|
|
256
|
+
function isClock(value) {
|
|
257
|
+
const match = CLOCK.exec(value);
|
|
258
|
+
return Boolean(match) && Number(match[1]) <= 23 && Number(match[2]) <= 59;
|
|
259
|
+
}
|
|
260
|
+
function hasValidTrip(criteria) {
|
|
261
|
+
const { pickupDate, dropoffDate, pickupTime, dropoffTime } = criteria;
|
|
262
|
+
if (!isRealDate(pickupDate) || !isRealDate(dropoffDate)) return false;
|
|
263
|
+
if (!isClock(pickupTime) || !isClock(dropoffTime)) return false;
|
|
264
|
+
return `${dropoffDate}T${dropoffTime.padStart(5, "0")}` > `${pickupDate}T${pickupTime.padStart(5, "0")}`;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// src/lib/queries/availability.queries.ts
|
|
268
|
+
function toSearchParams(criteria) {
|
|
269
|
+
const returnLocation = criteria.dropoffLocation === SAME_AS_PICKUP ? criteria.pickupLocation : criteria.dropoffLocation;
|
|
270
|
+
return {
|
|
271
|
+
pickupLocation: criteria.pickupLocation,
|
|
272
|
+
returnLocation,
|
|
273
|
+
pickupDate: criteria.pickupDate,
|
|
274
|
+
pickupTime: criteria.pickupTime,
|
|
275
|
+
returnDate: criteria.dropoffDate,
|
|
276
|
+
returnTime: criteria.dropoffTime,
|
|
277
|
+
promoCode: criteria.coupon,
|
|
278
|
+
// FormData is string-only - ActionController.php converts this back to a
|
|
279
|
+
// real boolean before it reaches AvailabilityParams.isInternational.
|
|
280
|
+
isInternational: criteria.residency === "INT" ? "true" : "false",
|
|
281
|
+
...criteria.supplier ? { supplier: criteria.supplier } : {},
|
|
282
|
+
...criteria.driverAge ? { driverAge: String(criteria.driverAge) } : {}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function availabilityQueryKey(criteria) {
|
|
286
|
+
return ["availability", criteria];
|
|
287
|
+
}
|
|
288
|
+
async function post(action, fields) {
|
|
289
|
+
const result = await getTransport().post(action, fields);
|
|
290
|
+
if (!result.success) {
|
|
291
|
+
const data = result.data;
|
|
292
|
+
throw Object.assign(new Error(result.error || "Search failed."), { sources: data?.sources ?? [], ...data?.couponNotice ? { couponNotice: data.couponNotice } : {} });
|
|
293
|
+
}
|
|
294
|
+
return result.data;
|
|
295
|
+
}
|
|
296
|
+
async function searchAvailability(criteria, onProgress) {
|
|
297
|
+
const params = toSearchParams(criteria);
|
|
298
|
+
if (criteria.supplier) return post("search_availability", params);
|
|
299
|
+
let listed;
|
|
300
|
+
try {
|
|
301
|
+
const data = await post("search_sources", {});
|
|
302
|
+
listed = (data.sources ?? []).map((source) => ({ ...source, status: "pending", pending: true }));
|
|
303
|
+
} catch {
|
|
304
|
+
return post("search_availability", params);
|
|
305
|
+
}
|
|
306
|
+
if (listed.length === 0) return post("search_availability", params);
|
|
307
|
+
const progress = new Map(listed.map((source) => [source.source, source]));
|
|
308
|
+
const offers = [];
|
|
309
|
+
let couponNotice;
|
|
310
|
+
const report = () => onProgress?.([...progress.values()], [...offers]);
|
|
311
|
+
report();
|
|
312
|
+
const settle = (source, answered) => {
|
|
313
|
+
const own = answered.find((item) => item.source === source.source) ?? answered[0];
|
|
314
|
+
progress.set(source.source, {
|
|
315
|
+
...own ?? { source: source.source, status: "error" },
|
|
316
|
+
source: source.source,
|
|
317
|
+
supplierName: source.supplierName || own?.supplierName,
|
|
318
|
+
logoUrl: source.logoUrl || own?.logoUrl,
|
|
319
|
+
pending: false
|
|
320
|
+
});
|
|
321
|
+
report();
|
|
322
|
+
};
|
|
323
|
+
await Promise.all(
|
|
324
|
+
listed.map(async (source) => {
|
|
325
|
+
try {
|
|
326
|
+
const data = await post("search_availability", { ...params, supplier: source.source });
|
|
327
|
+
offers.push(...data.offers ?? []);
|
|
328
|
+
couponNotice ??= data.couponNotice;
|
|
329
|
+
settle(source, data.sources ?? []);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
const sources2 = error.sources ?? [];
|
|
332
|
+
couponNotice ??= error.couponNotice;
|
|
333
|
+
settle(source, sources2.length ? sources2 : [{ source: source.source, status: "error", customerMessage: void 0 }]);
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
);
|
|
337
|
+
const sources = [...progress.values()].map(({ pending: _pending, ...source }) => source);
|
|
338
|
+
if (offers.length === 0) {
|
|
339
|
+
const reason = sources.map((source) => source.customerMessage).find(Boolean);
|
|
340
|
+
throw Object.assign(new Error(reason ?? "No cars for those dates and places."), { sources, ...couponNotice ? { couponNotice } : {} });
|
|
341
|
+
}
|
|
342
|
+
return { offers, sources, ...couponNotice ? { couponNotice } : {} };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/lib/offers.ts
|
|
346
|
+
function sourceNotices(result) {
|
|
347
|
+
return Array.from(new Set((result.sources ?? []).map((source) => source.customerMessage?.trim() ?? "").filter(Boolean)));
|
|
348
|
+
}
|
|
349
|
+
function isOffersResult(value) {
|
|
350
|
+
return typeof value === "object" && value !== null && Array.isArray(value.offers);
|
|
351
|
+
}
|
|
352
|
+
function supplierName(value, fallback = "") {
|
|
353
|
+
if (!value) return fallback;
|
|
354
|
+
if (typeof value === "string") return value;
|
|
355
|
+
return value.name ?? fallback;
|
|
356
|
+
}
|
|
357
|
+
function hasIncludedInsurance(rate) {
|
|
358
|
+
return (rate.options ?? []).some((option) => {
|
|
359
|
+
const code = String(option["Code"] ?? option["code"] ?? "");
|
|
360
|
+
const inclusive = Boolean(option["Inclusive"] ?? option["inclusive"]);
|
|
361
|
+
const mandatory = Boolean(option["Mandatory"] ?? option["mandatory"]);
|
|
362
|
+
return (code === "CDW" || code === "SLI") && inclusive && mandatory;
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
function toQuoteOptions(raw) {
|
|
366
|
+
if (!raw?.length) return void 0;
|
|
367
|
+
const mapped = raw.map((option) => {
|
|
368
|
+
const code = String(option["Code"] ?? option["code"] ?? "");
|
|
369
|
+
if (!code) return null;
|
|
370
|
+
return {
|
|
371
|
+
code,
|
|
372
|
+
name: String(option["Name"] ?? option["name"] ?? "") || void 0,
|
|
373
|
+
rate: Number(option["Rate"] ?? option["rate"] ?? 0),
|
|
374
|
+
inclusive: Boolean(option["Inclusive"] ?? option["inclusive"]),
|
|
375
|
+
mandatory: Boolean(option["Mandatory"] ?? option["mandatory"])
|
|
376
|
+
};
|
|
377
|
+
}).filter((option) => option !== null);
|
|
378
|
+
return mapped.length ? mapped : void 0;
|
|
379
|
+
}
|
|
380
|
+
function splitMakeModel(model) {
|
|
381
|
+
const trimmed = model.trim();
|
|
382
|
+
const space = trimmed.indexOf(" ");
|
|
383
|
+
if (space <= 0) return { make: "", model: trimmed };
|
|
384
|
+
return { make: trimmed.slice(0, space), model: trimmed.slice(space + 1) };
|
|
385
|
+
}
|
|
386
|
+
function normalizeTransmission(value) {
|
|
387
|
+
if (!value) return void 0;
|
|
388
|
+
const raw = value.trim().toUpperCase();
|
|
389
|
+
if (raw === "A" || raw === "A/T" || raw === "AT" || raw === "AUTOMATIC") return "Automatic";
|
|
390
|
+
if (raw === "M" || raw === "M/T" || raw === "MT" || raw === "MANUAL") return "Manual";
|
|
391
|
+
if (raw === "R") return "Manual";
|
|
392
|
+
return value;
|
|
393
|
+
}
|
|
394
|
+
function toPackage(rate, discountCodes) {
|
|
395
|
+
const unlimitedMileage = Boolean(rate.unlimitedMileage);
|
|
396
|
+
const kmPerDay = rate.kmPerDay ?? 0;
|
|
397
|
+
const rentalDays2 = rate.rentalDays ?? 1;
|
|
398
|
+
const superRate = rate.superRate;
|
|
399
|
+
const dailyRate = rate.dailyRate ?? rate.standardRate;
|
|
400
|
+
const hasSuperCover = typeof superRate === "number" && superRate > 0 && superRate !== rate.standardRate;
|
|
401
|
+
const discount = rate.discount && rate.discount > 0 ? rate.discount : void 0;
|
|
402
|
+
return {
|
|
403
|
+
cdpCode: rate.cdpCode || "",
|
|
404
|
+
isDiscounted: Boolean(rate.cdpCode) && discountCodes.includes(rate.cdpCode ?? ""),
|
|
405
|
+
kmSelectable: rate.kmSelectable !== false,
|
|
406
|
+
cdpKmAllowance: rate.cdpKmAllowance ?? null,
|
|
407
|
+
dailyRate,
|
|
408
|
+
totalRate: rate.standardRate,
|
|
409
|
+
wasDailyRate: discount ? dailyRate + discount / rentalDays2 : void 0,
|
|
410
|
+
wasTotalRate: discount ? rate.standardRate + discount : void 0,
|
|
411
|
+
kmPerDay: unlimitedMileage ? 0 : kmPerDay,
|
|
412
|
+
unlimitedMileage,
|
|
413
|
+
deposit: rate.superDeposit ?? 0,
|
|
414
|
+
liability: rate.liability ?? 0,
|
|
415
|
+
excessPerKm: rate.standardPricePerKm ?? rate.standardExcessRatePerKm ?? 0,
|
|
416
|
+
dropOffFee: rate.dropOffFee ?? 0,
|
|
417
|
+
rentalDays: rentalDays2,
|
|
418
|
+
// Stringified rather than passed through: the API types these as
|
|
419
|
+
// `string | number` (Thermeon rate IDs are numeric, WheelSys's are not)
|
|
420
|
+
// and everything downstream puts them straight into a form field.
|
|
421
|
+
rateNo: rate.rateNo != null ? String(rate.rateNo) : "",
|
|
422
|
+
rateSrNo: rate.rateSrNo != null ? String(rate.rateSrNo) : "",
|
|
423
|
+
quoteRef: rate.quoteRef || void 0,
|
|
424
|
+
options: toQuoteOptions(rate.options),
|
|
425
|
+
ratePackageLabel: "Standard",
|
|
426
|
+
superCover: hasSuperCover ? {
|
|
427
|
+
label: "SuperCover",
|
|
428
|
+
dailyRate: superRate / rentalDays2,
|
|
429
|
+
totalRate: superRate,
|
|
430
|
+
liability: rate.superExcess,
|
|
431
|
+
excessPerKm: rate.superPricePerKm ?? rate.superExcessRatePerKm,
|
|
432
|
+
deposit: rate.superCoverDeposit
|
|
433
|
+
} : void 0
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function toMetadata(listing, offer) {
|
|
437
|
+
const meta = listing.metadata;
|
|
438
|
+
if (!meta && !offer.metadata) return void 0;
|
|
439
|
+
return {
|
|
440
|
+
supplierId: meta?.supplierId || offer.metadata?.supplierId || listing.supplier?.id || "",
|
|
441
|
+
origin: Array.isArray(meta?.origin) ? meta.origin : [],
|
|
442
|
+
adapter: Array.isArray(meta?.adapter) ? meta.adapter : []
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function listedCategories(listing) {
|
|
446
|
+
const fromList = (listing.categories ?? []).map((item) => String(item).trim()).filter(Boolean);
|
|
447
|
+
if (fromList.length) return fromList;
|
|
448
|
+
const single = listing.category?.trim();
|
|
449
|
+
return single ? [single] : [];
|
|
450
|
+
}
|
|
451
|
+
function toCar(offer, listing, options) {
|
|
452
|
+
if (!listing.rates.length) return null;
|
|
453
|
+
const discountCodes = offer.discountCodes ?? [];
|
|
454
|
+
const km = (p) => p.unlimitedMileage ? Infinity : Number(p.kmPerDay) || 0;
|
|
455
|
+
const priced = listing.rates.map((rate2) => ({ rate: rate2, pkg: toPackage(rate2, discountCodes) })).sort((a, b) => (a.pkg.totalRate || Infinity) - (b.pkg.totalRate || Infinity) || km(b.pkg) - km(a.pkg));
|
|
456
|
+
const packages = priced.map((p) => p.pkg);
|
|
457
|
+
const rate = priced[0].rate;
|
|
458
|
+
const selected = packages[0];
|
|
459
|
+
const offerSupplier = supplierName(offer.supplier);
|
|
460
|
+
const listingSupplier = listing.supplier;
|
|
461
|
+
const name = listingSupplier?.name || offerSupplier;
|
|
462
|
+
const branchCode = listing.pickUpBranch || offer.pickBranch || "";
|
|
463
|
+
const locationLabel = options.pickupLabel || branchCode;
|
|
464
|
+
const { make, model } = splitMakeModel(listing.model);
|
|
465
|
+
return {
|
|
466
|
+
id: `${listingSupplier?.id || offerSupplier || "offer"}-${listing.carGroup}-${listing.code ?? listing.model}`.toLowerCase().replace(/\s+/g, "-"),
|
|
467
|
+
make,
|
|
468
|
+
model,
|
|
469
|
+
trim: listing.code || listing.carGroup,
|
|
470
|
+
// Never fall back category → carGroup (that duplicated the group badge as a "category").
|
|
471
|
+
category: listing.category?.trim() || listedCategories(listing)[0] || "",
|
|
472
|
+
categories: listedCategories(listing),
|
|
473
|
+
seats: listing.seat ?? 0,
|
|
474
|
+
doors: listing.door ?? 0,
|
|
475
|
+
dailyRate: selected.dailyRate,
|
|
476
|
+
totalRate: selected.totalRate,
|
|
477
|
+
wasDailyRate: selected.wasDailyRate,
|
|
478
|
+
wasTotalRate: selected.wasTotalRate,
|
|
479
|
+
deposit: selected.deposit,
|
|
480
|
+
liability: selected.liability,
|
|
481
|
+
excessPerKm: selected.excessPerKm,
|
|
482
|
+
dropOffFee: selected.dropOffFee,
|
|
483
|
+
currency: "ZAR",
|
|
484
|
+
kmPerDay: selected.kmPerDay,
|
|
485
|
+
image: listing.carImage || "",
|
|
486
|
+
featured: false,
|
|
487
|
+
location: { hub: locationLabel, city: locationLabel },
|
|
488
|
+
insuranceIncluded: hasIncludedInsurance(rate),
|
|
489
|
+
unlimitedMileage: selected.unlimitedMileage,
|
|
490
|
+
available: offer.available,
|
|
491
|
+
supplier: {
|
|
492
|
+
id: listingSupplier?.id,
|
|
493
|
+
name,
|
|
494
|
+
logoUrl: listingSupplier?.logoUrl
|
|
495
|
+
},
|
|
496
|
+
code: listing.code,
|
|
497
|
+
cdpCode: selected.cdpCode || void 0,
|
|
498
|
+
isDiscounted: selected.isDiscounted,
|
|
499
|
+
rateNo: selected.rateNo || void 0,
|
|
500
|
+
rateSrNo: selected.rateSrNo || void 0,
|
|
501
|
+
quoteRef: selected.quoteRef,
|
|
502
|
+
ratePackageLabel: selected.ratePackageLabel,
|
|
503
|
+
carGroup: listing.carGroup,
|
|
504
|
+
luggage: listing.luggage,
|
|
505
|
+
pickUpBranch: listing.pickUpBranch || offer.pickBranch || void 0,
|
|
506
|
+
returnBranch: listing.returnBranch || offer.returnBranch || void 0,
|
|
507
|
+
rentalDays: selected.rentalDays,
|
|
508
|
+
packages,
|
|
509
|
+
quoteIds: Array.isArray(listing.quoteId) ? listing.quoteId : listing.quoteId ? [listing.quoteId] : void 0,
|
|
510
|
+
metadata: toMetadata(listing, offer),
|
|
511
|
+
transmission: normalizeTransmission(listing.transmission),
|
|
512
|
+
fuelType: listing.fuel || void 0
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function mapOffersToCars(offers, options = {}) {
|
|
516
|
+
const cars = [];
|
|
517
|
+
for (const offer of offers) {
|
|
518
|
+
if (!offer.available || !offer.fleets) continue;
|
|
519
|
+
for (const listing of offer.fleets) {
|
|
520
|
+
const car = toCar(offer, listing, options);
|
|
521
|
+
if (car) cars.push(car);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return cars;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// src/lib/packages.ts
|
|
528
|
+
function packageKey(pkg) {
|
|
529
|
+
const mileage = pkg.unlimitedMileage ? "u" : String(pkg.kmPerDay);
|
|
530
|
+
return `${mileage}:${pkg.totalRate}:${pkg.cdpCode}:${pkg.rateSrNo ?? ""}`;
|
|
531
|
+
}
|
|
532
|
+
function withPackage(car, pkg, tier = "standard") {
|
|
533
|
+
const useSuper = tier === "super" && pkg.superCover;
|
|
534
|
+
return {
|
|
535
|
+
...car,
|
|
536
|
+
dailyRate: useSuper ? pkg.superCover.dailyRate : pkg.dailyRate,
|
|
537
|
+
totalRate: useSuper ? pkg.superCover.totalRate : pkg.totalRate,
|
|
538
|
+
// SuperCover is a different, undiscounted tier - the "was" price only
|
|
539
|
+
// applies to the standard tier's own discount.
|
|
540
|
+
wasDailyRate: useSuper ? void 0 : pkg.wasDailyRate,
|
|
541
|
+
wasTotalRate: useSuper ? void 0 : pkg.wasTotalRate,
|
|
542
|
+
// What was actually quoted/booked, not a fixed default - see this
|
|
543
|
+
// field's own docblock and booking.queries.ts's ratePackageLabel.
|
|
544
|
+
ratePackageLabel: useSuper ? pkg.superCover.label : pkg.ratePackageLabel,
|
|
545
|
+
deposit: useSuper && pkg.superCover?.deposit != null ? pkg.superCover.deposit : pkg.deposit,
|
|
546
|
+
liability: useSuper && pkg.superCover?.liability != null ? pkg.superCover.liability : pkg.liability,
|
|
547
|
+
excessPerKm: useSuper && pkg.superCover?.excessPerKm != null ? pkg.superCover.excessPerKm : pkg.excessPerKm,
|
|
548
|
+
dropOffFee: pkg.dropOffFee,
|
|
549
|
+
kmPerDay: pkg.kmPerDay,
|
|
550
|
+
unlimitedMileage: pkg.unlimitedMileage,
|
|
551
|
+
rentalDays: pkg.rentalDays,
|
|
552
|
+
cdpCode: pkg.cdpCode || car.cdpCode,
|
|
553
|
+
isDiscounted: pkg.cdpCode ? pkg.isDiscounted : car.isDiscounted,
|
|
554
|
+
// The booking is made against these (see Car.rateNo) - switching km
|
|
555
|
+
// package has to switch the rate that gets booked, not just the price
|
|
556
|
+
// on screen.
|
|
557
|
+
rateNo: pkg.rateNo || car.rateNo,
|
|
558
|
+
rateSrNo: pkg.rateSrNo || car.rateSrNo,
|
|
559
|
+
quoteRef: pkg.quoteRef || car.quoteRef
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// src/lib/queries/base.queries.ts
|
|
564
|
+
var BaseQuery = class {
|
|
565
|
+
/**
|
|
566
|
+
* Sends `action` + `fields` through the transport, which normalizes
|
|
567
|
+
* every possible outcome - unconfigured site, network failure, non-2xx
|
|
568
|
+
* response, `{success: false}` - into one `QueryResult`, so subclasses
|
|
569
|
+
* never branch on `response.ok` vs `payload.success` vs a thrown error
|
|
570
|
+
* differently from each other.
|
|
571
|
+
*/
|
|
572
|
+
post(action, fields = {}) {
|
|
573
|
+
return getTransport().post(action, fields);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
// src/lib/queries/branches.queries.ts
|
|
578
|
+
var BranchQueries = class extends BaseQuery {
|
|
579
|
+
/** Every place, ordered by region then name. A failure keeps the site's message and status. */
|
|
580
|
+
async list() {
|
|
581
|
+
const result = await this.post("public_branches");
|
|
582
|
+
if (!result.success) return { success: false, error: result.error, status: result.status };
|
|
583
|
+
return { success: true, data: Array.isArray(result.data.locations) ? result.data.locations : [] };
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
var branchQueries = new BranchQueries();
|
|
587
|
+
var branchesQueryKey = ["branches"];
|
|
588
|
+
|
|
589
|
+
// src/lib/queries/sources.queries.ts
|
|
590
|
+
var SourceQueries = class extends BaseQuery {
|
|
591
|
+
/** The live list, in the site's order. A failure keeps the site's message and status. */
|
|
592
|
+
async list() {
|
|
593
|
+
const result = await this.post("search_sources");
|
|
594
|
+
if (!result.success) return { success: false, error: result.error, status: result.status };
|
|
595
|
+
const sources = Array.isArray(result.data.sources) ? result.data.sources : [];
|
|
596
|
+
return { success: true, data: sources.filter((s) => s && typeof s.source === "string" && s.source !== "") };
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
var sourceQueries = new SourceQueries();
|
|
600
|
+
var sourcesQueryKey = ["sources"];
|
|
601
|
+
|
|
602
|
+
// src/lib/queries/cache.queries.ts
|
|
603
|
+
var CacheQueries = class extends BaseQuery {
|
|
604
|
+
/** The current cache version. A failure (offline, old site without the action) keeps the site's message and status. */
|
|
605
|
+
async version() {
|
|
606
|
+
const result = await this.post("cache_version");
|
|
607
|
+
if (!result.success) return { success: false, error: result.error, status: result.status };
|
|
608
|
+
const version = typeof result.data?.version === "string" ? result.data.version.trim() : "";
|
|
609
|
+
if (version === "") return { success: false, error: "The site did not send a cache version." };
|
|
610
|
+
return { success: true, data: { version, generatedAt: String(result.data.generatedAt ?? "") } };
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
var cacheQueries = new CacheQueries();
|
|
614
|
+
var cacheVersionQueryKey = ["cache-version"];
|
|
615
|
+
|
|
616
|
+
// src/lib/display-name.ts
|
|
617
|
+
var ACRONYMS = /* @__PURE__ */ new Set([
|
|
618
|
+
"ABS",
|
|
619
|
+
"AC",
|
|
620
|
+
"ADR",
|
|
621
|
+
"ALI",
|
|
622
|
+
"AWD",
|
|
623
|
+
"CDW",
|
|
624
|
+
"DVD",
|
|
625
|
+
"EFT",
|
|
626
|
+
"EV",
|
|
627
|
+
"GPS",
|
|
628
|
+
"ID",
|
|
629
|
+
"LCD",
|
|
630
|
+
"LDW",
|
|
631
|
+
"MPV",
|
|
632
|
+
"PAI",
|
|
633
|
+
"PAE",
|
|
634
|
+
"SCDW",
|
|
635
|
+
"SLI",
|
|
636
|
+
"SMS",
|
|
637
|
+
"SUV",
|
|
638
|
+
"TLW",
|
|
639
|
+
"TP",
|
|
640
|
+
"TW",
|
|
641
|
+
"UK",
|
|
642
|
+
"USA",
|
|
643
|
+
"USB",
|
|
644
|
+
"VAT",
|
|
645
|
+
"VW",
|
|
646
|
+
"WWC",
|
|
647
|
+
"BMW",
|
|
648
|
+
"GTI",
|
|
649
|
+
"XL"
|
|
650
|
+
]);
|
|
651
|
+
var SPECIAL = {
|
|
652
|
+
MIFI: "MiFi",
|
|
653
|
+
WIFI: "Wi-Fi",
|
|
654
|
+
"4X4": "4x4",
|
|
655
|
+
"4X2": "4x2",
|
|
656
|
+
KM: "km"
|
|
657
|
+
};
|
|
658
|
+
var MINOR = /* @__PURE__ */ new Set(["a", "an", "and", "as", "at", "by", "for", "in", "of", "on", "or", "per", "similar", "the", "to", "with", "x"]);
|
|
659
|
+
function displayName(value) {
|
|
660
|
+
if (!value) return "";
|
|
661
|
+
if (/[a-z]/.test(value) || !/[A-Z]/.test(value)) return value;
|
|
662
|
+
let position = 0;
|
|
663
|
+
return value.replace(/[A-Z0-9]+(?:'[A-Z]+)?/g, (word) => {
|
|
664
|
+
const first = position === 0;
|
|
665
|
+
position += 1;
|
|
666
|
+
if (SPECIAL[word]) return SPECIAL[word];
|
|
667
|
+
if (ACRONYMS.has(word)) return word;
|
|
668
|
+
if (/\d/.test(word)) return word;
|
|
669
|
+
const lower = word.toLowerCase();
|
|
670
|
+
if (!first && MINOR.has(lower)) return lower;
|
|
671
|
+
if (word.length <= 3 && !/[AEIOUY]/.test(word)) return word;
|
|
672
|
+
return lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
function carDisplayName(car) {
|
|
676
|
+
return displayName([car.make, car.model].filter(Boolean).join(" ").trim());
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// src/lib/catalogue.ts
|
|
680
|
+
var SORT_OPTIONS = [
|
|
681
|
+
{ value: "recommended", label: "Recommended" },
|
|
682
|
+
{ value: "price-asc", label: "Price: low to high" },
|
|
683
|
+
{ value: "price-desc", label: "Price: high to low" },
|
|
684
|
+
{ value: "rating-desc", label: "Top rated" },
|
|
685
|
+
{ value: "year-desc", label: "Newest first" }
|
|
686
|
+
];
|
|
687
|
+
var TRANSMISSIONS = ["Manual", "Automatic"];
|
|
688
|
+
var FUEL_TYPES = ["Petrol", "Diesel", "Electric", "Hybrid"];
|
|
689
|
+
var CONDITIONS = ["New", "Used"];
|
|
690
|
+
var DEFAULT_PRICE_BOUNDS = [0, 1e4];
|
|
691
|
+
var DEFAULT_YEAR_BOUNDS = [2015, (/* @__PURE__ */ new Date()).getFullYear() + 1];
|
|
692
|
+
var DEFAULT_SEAT_OPTIONS = [2, 4, 5, 7];
|
|
693
|
+
var DEFAULT_FILTERS = {
|
|
694
|
+
query: "",
|
|
695
|
+
makes: [],
|
|
696
|
+
models: [],
|
|
697
|
+
categories: [],
|
|
698
|
+
fuels: [],
|
|
699
|
+
suppliers: [],
|
|
700
|
+
transmission: "any",
|
|
701
|
+
condition: "any",
|
|
702
|
+
seats: null,
|
|
703
|
+
luggage: null,
|
|
704
|
+
priceRange: DEFAULT_PRICE_BOUNDS,
|
|
705
|
+
yearRange: DEFAULT_YEAR_BOUNDS,
|
|
706
|
+
location: "",
|
|
707
|
+
availableOnly: false,
|
|
708
|
+
instantBook: false,
|
|
709
|
+
insuranceIncluded: false,
|
|
710
|
+
mileage: null,
|
|
711
|
+
sort: "recommended"
|
|
712
|
+
};
|
|
713
|
+
function uniqueSorted(values) {
|
|
714
|
+
return Array.from(new Set(values.filter(Boolean))).sort((a, b) => a.localeCompare(b));
|
|
715
|
+
}
|
|
716
|
+
function seatOptionsFromCars(cars) {
|
|
717
|
+
const seats = Array.from(new Set(cars.map((c) => c.seats).filter((n) => n > 0))).sort(
|
|
718
|
+
(a, b) => a - b
|
|
719
|
+
);
|
|
720
|
+
return seats.length ? seats : DEFAULT_SEAT_OPTIONS;
|
|
721
|
+
}
|
|
722
|
+
function luggageOptionsFromCars(cars) {
|
|
723
|
+
const values = Array.from(
|
|
724
|
+
new Set(cars.map((c) => c.luggage ?? 0).filter((n) => n > 0))
|
|
725
|
+
).sort((a, b) => a - b);
|
|
726
|
+
return values.length ? values : [2, 3, 4, 5, 6];
|
|
727
|
+
}
|
|
728
|
+
function mileageOptionsFromCars(cars) {
|
|
729
|
+
const allowances = Array.from(
|
|
730
|
+
new Set(cars.filter((c) => !c.unlimitedMileage && c.kmPerDay > 0).map((c) => c.kmPerDay))
|
|
731
|
+
).sort((a, b) => a - b);
|
|
732
|
+
const options = [...allowances];
|
|
733
|
+
if (cars.some((c) => c.unlimitedMileage)) options.push("unlimited");
|
|
734
|
+
return options;
|
|
735
|
+
}
|
|
736
|
+
function meetsMileage(car, mileage) {
|
|
737
|
+
if (mileage === null) return true;
|
|
738
|
+
const options = car.packages?.length ? car.packages : [car];
|
|
739
|
+
return options.some((o) => o.unlimitedMileage || mileage !== "unlimited" && o.kmPerDay >= mileage);
|
|
740
|
+
}
|
|
741
|
+
function mileageLabel(mileage) {
|
|
742
|
+
return mileage === "unlimited" ? "Unlimited" : `${mileage}+ km`;
|
|
743
|
+
}
|
|
744
|
+
function priceBoundsFromCars(cars) {
|
|
745
|
+
if (!cars.length) return DEFAULT_PRICE_BOUNDS;
|
|
746
|
+
const rates = cars.map((c) => c.dailyRate);
|
|
747
|
+
const min = Math.floor(Math.min(...rates) / 100) * 100;
|
|
748
|
+
const max = Math.ceil(Math.max(...rates) / 100) * 100;
|
|
749
|
+
return [min, Math.max(min + 100, max)];
|
|
750
|
+
}
|
|
751
|
+
function yearBoundsFromCars(cars) {
|
|
752
|
+
const years = cars.map((c) => c.year).filter((year) => year !== void 0);
|
|
753
|
+
if (!years.length) return null;
|
|
754
|
+
return [Math.min(...years), Math.max(...years)];
|
|
755
|
+
}
|
|
756
|
+
function formatZAR(amount) {
|
|
757
|
+
return `R${amount.toLocaleString("en-ZA", {
|
|
758
|
+
minimumFractionDigits: 2,
|
|
759
|
+
maximumFractionDigits: 2
|
|
760
|
+
})}`;
|
|
761
|
+
}
|
|
762
|
+
function pluralize(count, word) {
|
|
763
|
+
return count === 1 ? word : `${word}s`;
|
|
764
|
+
}
|
|
765
|
+
function matchesQuery(car, query) {
|
|
766
|
+
const haystack = [
|
|
767
|
+
car.make,
|
|
768
|
+
car.model,
|
|
769
|
+
car.trim,
|
|
770
|
+
car.category,
|
|
771
|
+
car.location.city,
|
|
772
|
+
car.location.hub,
|
|
773
|
+
car.supplier.name
|
|
774
|
+
].join(" ").toLowerCase();
|
|
775
|
+
return query.toLowerCase().split(/\s+/).filter(Boolean).every((term) => haystack.includes(term));
|
|
776
|
+
}
|
|
777
|
+
function filterCars(cars, filters) {
|
|
778
|
+
return cars.filter((car) => {
|
|
779
|
+
if (filters.query && !matchesQuery(car, filters.query)) return false;
|
|
780
|
+
if (filters.makes.length && !filters.makes.includes(car.make || car.model)) return false;
|
|
781
|
+
if (filters.models.length && !filters.models.includes(car.model)) return false;
|
|
782
|
+
if (filters.categories.length && !filters.categories.includes(car.category)) return false;
|
|
783
|
+
if (filters.suppliers.length && !filters.suppliers.includes(car.supplier.name)) return false;
|
|
784
|
+
if (filters.fuels.length && !(car.fuelType && filters.fuels.includes(car.fuelType))) return false;
|
|
785
|
+
if (filters.transmission !== "any" && car.transmission !== filters.transmission) return false;
|
|
786
|
+
if (filters.condition !== "any" && car.condition !== filters.condition) return false;
|
|
787
|
+
if (filters.seats !== null && car.seats < filters.seats) return false;
|
|
788
|
+
if (filters.luggage !== null && (car.luggage ?? 0) < filters.luggage) return false;
|
|
789
|
+
if (Number.isFinite(filters.priceRange[1]) && (car.dailyRate < filters.priceRange[0] || car.dailyRate > filters.priceRange[1])) {
|
|
790
|
+
return false;
|
|
791
|
+
}
|
|
792
|
+
if (car.year !== void 0 && (car.year < filters.yearRange[0] || car.year > filters.yearRange[1])) {
|
|
793
|
+
return false;
|
|
794
|
+
}
|
|
795
|
+
if (filters.location && car.location.city !== filters.location) return false;
|
|
796
|
+
if (filters.availableOnly && !car.available) return false;
|
|
797
|
+
if (filters.instantBook && !car.instantBook) return false;
|
|
798
|
+
if (filters.insuranceIncluded && !car.insuranceIncluded) return false;
|
|
799
|
+
if (!meetsMileage(car, filters.mileage)) return false;
|
|
800
|
+
return true;
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
function sortCars(cars, sort) {
|
|
804
|
+
const sorted = [...cars];
|
|
805
|
+
switch (sort) {
|
|
806
|
+
case "price-asc":
|
|
807
|
+
return sorted.sort((a, b) => a.dailyRate - b.dailyRate);
|
|
808
|
+
case "price-desc":
|
|
809
|
+
return sorted.sort((a, b) => b.dailyRate - a.dailyRate);
|
|
810
|
+
case "rating-desc":
|
|
811
|
+
return sorted.sort(
|
|
812
|
+
(a, b) => (b.rating ?? 0) - (a.rating ?? 0) || (b.reviewCount ?? 0) - (a.reviewCount ?? 0)
|
|
813
|
+
);
|
|
814
|
+
case "year-desc":
|
|
815
|
+
return sorted.sort((a, b) => (b.year ?? 0) - (a.year ?? 0));
|
|
816
|
+
case "recommended":
|
|
817
|
+
default:
|
|
818
|
+
return sorted.sort(
|
|
819
|
+
(a, b) => Number(b.featured) - Number(a.featured) || Number(b.available) - Number(a.available) || a.dailyRate - b.dailyRate
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
var FACET_VALUE = {
|
|
824
|
+
categories: (car) => car.category,
|
|
825
|
+
makes: (car) => car.make || car.model,
|
|
826
|
+
fuels: (car) => car.fuelType ?? "",
|
|
827
|
+
seats: (car) => String(car.seats),
|
|
828
|
+
transmission: (car) => car.transmission ?? "",
|
|
829
|
+
condition: (car) => car.condition ?? ""
|
|
830
|
+
};
|
|
831
|
+
var FACET_CLEARED = {
|
|
832
|
+
categories: { categories: [] },
|
|
833
|
+
makes: { makes: [] },
|
|
834
|
+
fuels: { fuels: [] },
|
|
835
|
+
seats: { seats: null },
|
|
836
|
+
transmission: { transmission: "any" },
|
|
837
|
+
condition: { condition: "any" }
|
|
838
|
+
};
|
|
839
|
+
function facetCounts(cars, filters, key) {
|
|
840
|
+
const pool = filterCars(cars, { ...filters, ...FACET_CLEARED[key] });
|
|
841
|
+
const counts = {};
|
|
842
|
+
pool.forEach((car) => {
|
|
843
|
+
const value = FACET_VALUE[key](car);
|
|
844
|
+
if (!value) return;
|
|
845
|
+
counts[value] = (counts[value] ?? 0) + 1;
|
|
846
|
+
});
|
|
847
|
+
if (key === "seats") {
|
|
848
|
+
const options = seatOptionsFromCars(cars);
|
|
849
|
+
const cumulative = {};
|
|
850
|
+
options.forEach((seats) => {
|
|
851
|
+
cumulative[String(seats)] = pool.filter((car) => car.seats >= seats).length;
|
|
852
|
+
});
|
|
853
|
+
return cumulative;
|
|
854
|
+
}
|
|
855
|
+
return counts;
|
|
856
|
+
}
|
|
857
|
+
function locationCounts(cars, filters) {
|
|
858
|
+
const pool = filterCars(cars, { ...filters, location: "" });
|
|
859
|
+
const counts = {};
|
|
860
|
+
pool.forEach((car) => {
|
|
861
|
+
if (!car.location.city) return;
|
|
862
|
+
counts[car.location.city] = (counts[car.location.city] ?? 0) + 1;
|
|
863
|
+
});
|
|
864
|
+
return counts;
|
|
865
|
+
}
|
|
866
|
+
function activeChips(filters, bounds = {
|
|
867
|
+
price: DEFAULT_PRICE_BOUNDS,
|
|
868
|
+
year: DEFAULT_YEAR_BOUNDS
|
|
869
|
+
}) {
|
|
870
|
+
const chips = [];
|
|
871
|
+
if (filters.query) {
|
|
872
|
+
chips.push({ id: "query", label: `"${filters.query}"`, clear: { query: "" } });
|
|
873
|
+
}
|
|
874
|
+
if (filters.location) {
|
|
875
|
+
chips.push({ id: "location", label: filters.location, clear: { location: "" } });
|
|
876
|
+
}
|
|
877
|
+
filters.makes.forEach(
|
|
878
|
+
(make) => chips.push({
|
|
879
|
+
id: `make:${make}`,
|
|
880
|
+
label: make,
|
|
881
|
+
clear: { makes: filters.makes.filter((m) => m !== make) }
|
|
882
|
+
})
|
|
883
|
+
);
|
|
884
|
+
filters.models.forEach(
|
|
885
|
+
(model) => chips.push({
|
|
886
|
+
id: `model:${model}`,
|
|
887
|
+
label: model,
|
|
888
|
+
clear: { models: filters.models.filter((m) => m !== model) }
|
|
889
|
+
})
|
|
890
|
+
);
|
|
891
|
+
filters.categories.forEach(
|
|
892
|
+
(category) => chips.push({
|
|
893
|
+
id: `category:${category}`,
|
|
894
|
+
label: category,
|
|
895
|
+
clear: { categories: filters.categories.filter((c) => c !== category) }
|
|
896
|
+
})
|
|
897
|
+
);
|
|
898
|
+
filters.suppliers.forEach(
|
|
899
|
+
(supplier) => chips.push({
|
|
900
|
+
id: `supplier:${supplier}`,
|
|
901
|
+
label: supplier,
|
|
902
|
+
clear: { suppliers: filters.suppliers.filter((s) => s !== supplier) }
|
|
903
|
+
})
|
|
904
|
+
);
|
|
905
|
+
filters.fuels.forEach(
|
|
906
|
+
(fuel) => chips.push({
|
|
907
|
+
id: `fuel:${fuel}`,
|
|
908
|
+
label: fuel,
|
|
909
|
+
clear: { fuels: filters.fuels.filter((f) => f !== fuel) }
|
|
910
|
+
})
|
|
911
|
+
);
|
|
912
|
+
if (filters.transmission !== "any") {
|
|
913
|
+
chips.push({
|
|
914
|
+
id: "transmission",
|
|
915
|
+
label: filters.transmission,
|
|
916
|
+
clear: { transmission: "any" }
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
if (filters.condition !== "any") {
|
|
920
|
+
chips.push({ id: "condition", label: filters.condition, clear: { condition: "any" } });
|
|
921
|
+
}
|
|
922
|
+
if (filters.seats !== null) {
|
|
923
|
+
chips.push({ id: "seats", label: `${filters.seats}+ seats`, clear: { seats: null } });
|
|
924
|
+
}
|
|
925
|
+
if (filters.luggage !== null) {
|
|
926
|
+
chips.push({ id: "luggage", label: `${filters.luggage}+ luggage`, clear: { luggage: null } });
|
|
927
|
+
}
|
|
928
|
+
if (filters.priceRange[0] !== bounds.price[0] || filters.priceRange[1] !== bounds.price[1]) {
|
|
929
|
+
chips.push({
|
|
930
|
+
id: "price",
|
|
931
|
+
label: `${formatZAR(filters.priceRange[0])} \u2013 ${formatZAR(filters.priceRange[1])}`,
|
|
932
|
+
clear: { priceRange: bounds.price }
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
if (bounds.year && (filters.yearRange[0] !== bounds.year[0] || filters.yearRange[1] !== bounds.year[1])) {
|
|
936
|
+
chips.push({
|
|
937
|
+
id: "year",
|
|
938
|
+
label: `${filters.yearRange[0]} \u2013 ${filters.yearRange[1]}`,
|
|
939
|
+
clear: { yearRange: bounds.year }
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
const toggles = [
|
|
943
|
+
["availableOnly", "Available now"],
|
|
944
|
+
["instantBook", "Instant booking"],
|
|
945
|
+
["insuranceIncluded", "Insurance included"]
|
|
946
|
+
];
|
|
947
|
+
toggles.forEach(([key, label]) => {
|
|
948
|
+
if (filters[key]) chips.push({ id: key, label, clear: { [key]: false } });
|
|
949
|
+
});
|
|
950
|
+
if (filters.mileage !== null) {
|
|
951
|
+
chips.push({ id: "mileage", label: `${mileageLabel(filters.mileage)} per day`, clear: { mileage: null } });
|
|
952
|
+
}
|
|
953
|
+
return chips;
|
|
954
|
+
}
|
|
955
|
+
function priceHistogram(cars, bounds = DEFAULT_PRICE_BOUNDS, buckets = 16) {
|
|
956
|
+
const [min, max] = bounds;
|
|
957
|
+
const span = max - min || 1;
|
|
958
|
+
const counts = new Array(buckets).fill(0);
|
|
959
|
+
cars.forEach((car) => {
|
|
960
|
+
const index = Math.min(buckets - 1, Math.floor((car.dailyRate - min) / span * buckets));
|
|
961
|
+
counts[index] += 1;
|
|
962
|
+
});
|
|
963
|
+
const peak = Math.max(...counts, 1);
|
|
964
|
+
return counts.map((count) => count / peak);
|
|
965
|
+
}
|
|
966
|
+
function filtersFromSearchParams(search) {
|
|
967
|
+
const params = new URLSearchParams(search);
|
|
968
|
+
const patch = {};
|
|
969
|
+
const make = params.get("make");
|
|
970
|
+
if (make) patch.makes = [make];
|
|
971
|
+
const model = params.get("model");
|
|
972
|
+
if (model) patch.models = [model];
|
|
973
|
+
const category = params.get("category");
|
|
974
|
+
if (category) patch.categories = [category];
|
|
975
|
+
const fuel = params.get("fuel");
|
|
976
|
+
if (fuel && FUEL_TYPES.some((type) => type === fuel)) patch.fuels = [fuel];
|
|
977
|
+
const sort = params.get("sort");
|
|
978
|
+
if (sort && SORT_OPTIONS.some((option) => option.value === sort)) patch.sort = sort;
|
|
979
|
+
return patch;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// src/lib/queries/extras.queries.ts
|
|
983
|
+
var SUPPLIER_GROUPS = {
|
|
984
|
+
WAI: "Waivers",
|
|
985
|
+
ADC: "Additional services",
|
|
986
|
+
ADH: "Additional services",
|
|
987
|
+
EXS: "Extended services"
|
|
988
|
+
};
|
|
989
|
+
function supplierGroup(raw) {
|
|
990
|
+
return raw ? SUPPLIER_GROUPS[raw.trim().toUpperCase()] : void 0;
|
|
991
|
+
}
|
|
992
|
+
function extrasQueryKey(car) {
|
|
993
|
+
const categories = (car.categories?.length ? car.categories : car.category ? [car.category] : []).join(",");
|
|
994
|
+
return ["extras", car.id, categories];
|
|
995
|
+
}
|
|
996
|
+
var ExtrasQueries = class extends BaseQuery {
|
|
997
|
+
/**
|
|
998
|
+
* The adapter response has no icon field, so the closest keyword in the
|
|
999
|
+
* name/type stands in for one of the fixed set the wizard already renders.
|
|
1000
|
+
* Order matters: more specific terms are checked before the generic ones
|
|
1001
|
+
* they could also match (e.g. "child" before "driver").
|
|
1002
|
+
*/
|
|
1003
|
+
inferIcon(name, type) {
|
|
1004
|
+
const haystack = `${name} ${type ?? ""}`.toLowerCase();
|
|
1005
|
+
if (/child|baby|infant|isofix|booster/.test(haystack)) return "child";
|
|
1006
|
+
if (/tyre|tire|glass|windscreen/.test(haystack)) return "tyre";
|
|
1007
|
+
if (/waiver|excess|insur|cdw|damage|theft|liab|protect/.test(haystack)) return "shield";
|
|
1008
|
+
if (/driver/.test(haystack)) return "driver";
|
|
1009
|
+
if (/gps|navigat/.test(haystack)) return "gps";
|
|
1010
|
+
if (/wi-?fi|hotspot|internet|mifi|router|\bdata\b|\bsim\b/.test(haystack)) return "wifi";
|
|
1011
|
+
if (/deliver|collect/.test(haystack)) return "delivery";
|
|
1012
|
+
return "document";
|
|
1013
|
+
}
|
|
1014
|
+
/** Quote Options for the CDP package the user picked (falls back to first). */
|
|
1015
|
+
quoteOptions(car) {
|
|
1016
|
+
const packages = car.packages ?? [];
|
|
1017
|
+
if (!packages.length) return [];
|
|
1018
|
+
const selected = packages.find((pkg) => pkg.cdpCode && pkg.cdpCode === car.cdpCode) ?? packages[0];
|
|
1019
|
+
return selected.options ?? [];
|
|
1020
|
+
}
|
|
1021
|
+
/**
|
|
1022
|
+
* Build an Extra with the right charge field for its serviceType.
|
|
1023
|
+
* Only "Per Day" multiplies by rental days — everything else is once-off
|
|
1024
|
+
* × quantity, matching carhire BookingSummary (`price * quantity`).
|
|
1025
|
+
*/
|
|
1026
|
+
toExtra(input) {
|
|
1027
|
+
const { code, name, description, serviceType, price, maxQuantity } = input;
|
|
1028
|
+
const base = {
|
|
1029
|
+
code,
|
|
1030
|
+
name,
|
|
1031
|
+
serviceType,
|
|
1032
|
+
description: description?.trim() || void 0,
|
|
1033
|
+
maxQuantity: Math.max(1, maxQuantity),
|
|
1034
|
+
icon: this.inferIcon(name, serviceType),
|
|
1035
|
+
recommended: isPopularExtra(code, name)
|
|
1036
|
+
};
|
|
1037
|
+
if (serviceType === "Per Day") {
|
|
1038
|
+
return { ...base, perDay: price };
|
|
1039
|
+
}
|
|
1040
|
+
return { ...base, onceOff: price };
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Carhire `getSaniExtras`: keep catalogue rows that also appear on the
|
|
1044
|
+
* quote as optional (not inclusive) paid extras, priced in ZAR. The
|
|
1045
|
+
* catalogue supplies name/description/serviceType; the quote's Options
|
|
1046
|
+
* supply the actual per-vehicle-group price the plain catalogue can't.
|
|
1047
|
+
*/
|
|
1048
|
+
mergeSaniCatalogue(catalogue, quoteOptions) {
|
|
1049
|
+
const mapRow = (item, price) => {
|
|
1050
|
+
const serviceType = normalizeServiceType(item.serviceType);
|
|
1051
|
+
const maxQuantity = serviceType === "Per Unit" ? 99 : 1;
|
|
1052
|
+
return this.toExtra({
|
|
1053
|
+
code: item.code,
|
|
1054
|
+
name: item.name,
|
|
1055
|
+
description: item.description,
|
|
1056
|
+
serviceType,
|
|
1057
|
+
price,
|
|
1058
|
+
maxQuantity
|
|
1059
|
+
});
|
|
1060
|
+
};
|
|
1061
|
+
if (!quoteOptions.length) {
|
|
1062
|
+
return catalogue.map(
|
|
1063
|
+
(item) => mapRow(item, item.isFree ? 0 : item.price ?? 0)
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
const priced = [];
|
|
1067
|
+
for (const item of catalogue) {
|
|
1068
|
+
const found = quoteOptions.find(
|
|
1069
|
+
(opt) => opt.code === item.code && !opt.inclusive && Number(opt.rate) > 0
|
|
1070
|
+
);
|
|
1071
|
+
if (!found) continue;
|
|
1072
|
+
priced.push(
|
|
1073
|
+
mapRow(
|
|
1074
|
+
{ ...item, name: found.name || item.name },
|
|
1075
|
+
Number((found.rate / 100).toFixed(2))
|
|
1076
|
+
)
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
return priced;
|
|
1080
|
+
}
|
|
1081
|
+
mapCatalogueRow(item) {
|
|
1082
|
+
const serviceType = normalizeServiceType(item.serviceType);
|
|
1083
|
+
const maxQuantity = serviceType === "Per Unit" ? 99 : 1;
|
|
1084
|
+
return this.toExtra({
|
|
1085
|
+
code: item.code,
|
|
1086
|
+
name: item.name,
|
|
1087
|
+
description: item.description,
|
|
1088
|
+
serviceType,
|
|
1089
|
+
price: item.isFree ? 0 : item.price ?? 0,
|
|
1090
|
+
maxQuantity
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Bluu-specific overrides applied to every extra from its live catalogue:
|
|
1095
|
+
*
|
|
1096
|
+
* - `priceUnknown: true` on all of them - GetAdditionalProductDetails
|
|
1097
|
+
* never carries a real price (see BluuAdapter.parseExtrasResponse's
|
|
1098
|
+
* docblock), so every entry starts at a placeholder 0 until
|
|
1099
|
+
* BookingWizard's setQuantity live-quotes it. The UI shows "priced once
|
|
1100
|
+
* selected" instead of a misleading R0,00 for these (see ExtraCard).
|
|
1101
|
+
* - Additional Driver Waiver gets `serviceType: 'Per Unit'` / a real
|
|
1102
|
+
* `maxQuantity` - Bluu's own extras endpoint has no field distinguishing
|
|
1103
|
+
* "one of these" from "one per driver", so it maps to a single on/off
|
|
1104
|
+
* toggle by default (see mapCatalogueRow). It's confirmed genuinely
|
|
1105
|
+
* per-driver: GetQuote needs the count in a separate top-level
|
|
1106
|
+
* AdditionalDrivers field (BCR Interface Spec 2.16, GetQuote request,
|
|
1107
|
+
* p.25-26; see BluuAdapter.getEstimation), which BookingWizard derives
|
|
1108
|
+
* from this extra's selected quantity. Matched by name (no stable code
|
|
1109
|
+
* is documented) the same way isPopularExtra() matches known extras
|
|
1110
|
+
* elsewhere.
|
|
1111
|
+
*/
|
|
1112
|
+
applyBluuGrouping(extras) {
|
|
1113
|
+
return extras.map((extra) => ({
|
|
1114
|
+
.../driver/i.test(extra.name) ? { ...extra, serviceType: "Per Unit", maxQuantity: 99 } : extra,
|
|
1115
|
+
priceUnknown: true
|
|
1116
|
+
}));
|
|
1117
|
+
}
|
|
1118
|
+
/** The car's add-ons, or null when there are none to show or they could not be loaded. See extrasResult() to tell those two apart. */
|
|
1119
|
+
async extras(car) {
|
|
1120
|
+
const result = await this.extrasResult(car);
|
|
1121
|
+
return result.ok ? result.extras : null;
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Like extras(), but a failure (the site could not be reached, or refused)
|
|
1125
|
+
* comes back as `{ ok: false }` instead of looking like "no add-ons".
|
|
1126
|
+
* `extras` is null when the car has none.
|
|
1127
|
+
*/
|
|
1128
|
+
async extrasResult(car) {
|
|
1129
|
+
const identifier = car.supplier.id;
|
|
1130
|
+
if (!identifier) return { ok: true, extras: null };
|
|
1131
|
+
const result = await this.post("get_extras", {
|
|
1132
|
+
identifier,
|
|
1133
|
+
category: (car.categories?.length ? car.categories : car.category ? [car.category] : []).join(",")
|
|
1134
|
+
});
|
|
1135
|
+
if (!result.success) return { ok: false, error: result.error, status: result.status };
|
|
1136
|
+
return { ok: true, extras: this.mapExtras(car, identifier, result.data.extras) };
|
|
1137
|
+
}
|
|
1138
|
+
mapExtras(car, identifier, raw) {
|
|
1139
|
+
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
1140
|
+
const catalogue = raw.filter(
|
|
1141
|
+
(item) => Boolean(item.code && item.name)
|
|
1142
|
+
);
|
|
1143
|
+
if (!catalogue.length) return null;
|
|
1144
|
+
let mapped = identifier === "sanisixt" ? this.mergeSaniCatalogue(catalogue, this.quoteOptions(car)) : catalogue.map((item) => this.mapCatalogueRow(item));
|
|
1145
|
+
if (identifier === "bluu") mapped = this.applyBluuGrouping(mapped);
|
|
1146
|
+
const groups = new Map(catalogue.map((item) => [item.code, supplierGroup(item.serviceType)]));
|
|
1147
|
+
mapped = mapped.map((extra) => {
|
|
1148
|
+
const group = groups.get(extra.code);
|
|
1149
|
+
return group ? { ...extra, group } : extra;
|
|
1150
|
+
});
|
|
1151
|
+
return mapped.length > 0 ? mapped : null;
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
var extrasQueries = new ExtrasQueries();
|
|
1155
|
+
|
|
1156
|
+
// src/lib/queries/estimation.queries.ts
|
|
1157
|
+
function bluuDepositQueryKey(car, criteria) {
|
|
1158
|
+
return [
|
|
1159
|
+
"bluu-deposit",
|
|
1160
|
+
car.id,
|
|
1161
|
+
criteria.pickupDate,
|
|
1162
|
+
criteria.pickupTime,
|
|
1163
|
+
criteria.dropoffDate,
|
|
1164
|
+
criteria.dropoffTime
|
|
1165
|
+
];
|
|
1166
|
+
}
|
|
1167
|
+
function estimationQueryKey(car, criteria) {
|
|
1168
|
+
return [
|
|
1169
|
+
"estimation",
|
|
1170
|
+
car.id,
|
|
1171
|
+
criteria.pickupDate,
|
|
1172
|
+
criteria.pickupTime,
|
|
1173
|
+
criteria.dropoffDate,
|
|
1174
|
+
criteria.dropoffTime
|
|
1175
|
+
];
|
|
1176
|
+
}
|
|
1177
|
+
var EstimationQueries = class extends BaseQuery {
|
|
1178
|
+
async quote(car, criteria, extras) {
|
|
1179
|
+
const identifier = car.supplier.id;
|
|
1180
|
+
if (!identifier || !car.pickUpBranch || !car.carGroup) return null;
|
|
1181
|
+
const result = await this.post("get_estimation", {
|
|
1182
|
+
identifier,
|
|
1183
|
+
pickupLocation: car.pickUpBranch,
|
|
1184
|
+
returnLocation: car.returnBranch || car.pickUpBranch,
|
|
1185
|
+
pickupDate: criteria.pickupDate,
|
|
1186
|
+
pickupTime: criteria.pickupTime,
|
|
1187
|
+
returnDate: criteria.dropoffDate,
|
|
1188
|
+
returnTime: criteria.dropoffTime,
|
|
1189
|
+
vehicleGroup: car.carGroup,
|
|
1190
|
+
// The rate the car was offered under (Bluu needs it to quote; see Car.rateNo).
|
|
1191
|
+
rateId: car.rateNo ?? "",
|
|
1192
|
+
extras: JSON.stringify(extras)
|
|
1193
|
+
});
|
|
1194
|
+
if (!result.success) {
|
|
1195
|
+
return { totalPrice: null, deposit: null, dropOffFee: null, breakdown: {}, quoteRef: null, error: result.error };
|
|
1196
|
+
}
|
|
1197
|
+
return {
|
|
1198
|
+
totalPrice: result.data.totalPrice ?? null,
|
|
1199
|
+
deposit: result.data.deposit ?? null,
|
|
1200
|
+
dropOffFee: result.data.dropOffFee ?? null,
|
|
1201
|
+
breakdown: result.data.breakdown ?? {},
|
|
1202
|
+
quoteRef: result.data.quoteRef ?? null
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
};
|
|
1206
|
+
var estimationQueries = new EstimationQueries();
|
|
1207
|
+
|
|
1208
|
+
// src/lib/queries/one-way.queries.ts
|
|
1209
|
+
function oneWayFeeQueryKey(car, criteria) {
|
|
1210
|
+
return [
|
|
1211
|
+
"bluu-one-way-fee",
|
|
1212
|
+
car.id,
|
|
1213
|
+
criteria.pickupDate,
|
|
1214
|
+
criteria.pickupTime,
|
|
1215
|
+
criteria.dropoffDate,
|
|
1216
|
+
criteria.dropoffTime
|
|
1217
|
+
];
|
|
1218
|
+
}
|
|
1219
|
+
var OneWayFeeQueries = class extends BaseQuery {
|
|
1220
|
+
async check(car, criteria) {
|
|
1221
|
+
const identifier = car.supplier.id;
|
|
1222
|
+
if (!identifier || !car.pickUpBranch || !car.returnBranch) return null;
|
|
1223
|
+
const result = await this.post("get_one_way_fee", {
|
|
1224
|
+
identifier,
|
|
1225
|
+
pickupLocation: car.pickUpBranch,
|
|
1226
|
+
returnLocation: car.returnBranch,
|
|
1227
|
+
pickupDate: criteria.pickupDate,
|
|
1228
|
+
pickupTime: criteria.pickupTime
|
|
1229
|
+
});
|
|
1230
|
+
if (!result.success) {
|
|
1231
|
+
return { fee: null, allowed: null, error: result.error };
|
|
1232
|
+
}
|
|
1233
|
+
return {
|
|
1234
|
+
fee: result.data.fee ?? null,
|
|
1235
|
+
allowed: result.data.allowed ?? null
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
var oneWayFeeQueries = new OneWayFeeQueries();
|
|
1240
|
+
|
|
1241
|
+
// src/lib/booking.ts
|
|
1242
|
+
var TITLES = ["Mr", "Ms", "Mrs", "Dr", "Prof"];
|
|
1243
|
+
var EMPTY_DRIVER = {
|
|
1244
|
+
title: "Mr",
|
|
1245
|
+
firstName: "",
|
|
1246
|
+
lastName: "",
|
|
1247
|
+
email: "",
|
|
1248
|
+
phone: "",
|
|
1249
|
+
licenceNumber: "",
|
|
1250
|
+
notes: "",
|
|
1251
|
+
country: "South Africa",
|
|
1252
|
+
idNumber: "",
|
|
1253
|
+
crossingBorder: false,
|
|
1254
|
+
streetAddress: "",
|
|
1255
|
+
city: "",
|
|
1256
|
+
suburb: "",
|
|
1257
|
+
province: "",
|
|
1258
|
+
postalCode: "",
|
|
1259
|
+
additionalEmail: ""
|
|
1260
|
+
};
|
|
1261
|
+
var DRIVER_LABELS = {
|
|
1262
|
+
firstName: "first name",
|
|
1263
|
+
lastName: "last name",
|
|
1264
|
+
email: "email address",
|
|
1265
|
+
phone: "phone number",
|
|
1266
|
+
additionalEmail: "second email address",
|
|
1267
|
+
idNumber: "ID or passport number",
|
|
1268
|
+
notes: "notes"
|
|
1269
|
+
};
|
|
1270
|
+
function describeDriverErrors(errors) {
|
|
1271
|
+
const labels = Object.keys(errors).map((field) => DRIVER_LABELS[field] ?? String(field)).filter((label, index, all) => all.indexOf(label) === index);
|
|
1272
|
+
if (labels.length === 0) return "";
|
|
1273
|
+
if (labels.length === 1) return labels[0];
|
|
1274
|
+
return `${labels.slice(0, -1).join(", ")} and ${labels[labels.length - 1]}`;
|
|
1275
|
+
}
|
|
1276
|
+
function isValidSouthAfricanId(value) {
|
|
1277
|
+
if (!/^\d{13}$/.test(value)) return false;
|
|
1278
|
+
const month = Number(value.slice(2, 4));
|
|
1279
|
+
const day = Number(value.slice(4, 6));
|
|
1280
|
+
if (month < 1 || month > 12 || day < 1 || day > 31) return false;
|
|
1281
|
+
const digits = value.slice(0, 12).split("").map(Number);
|
|
1282
|
+
const oddSum = digits.filter((_, index) => index % 2 === 0).reduce((sum, digit) => sum + digit, 0);
|
|
1283
|
+
const evenDigits = digits.filter((_, index) => index % 2 === 1).join("");
|
|
1284
|
+
const doubledEven = String(Number(evenDigits) * 2);
|
|
1285
|
+
const evenSum = doubledEven.split("").reduce((sum, digit) => sum + Number(digit), 0);
|
|
1286
|
+
const checkDigit = (10 - (oddSum + evenSum) % 10) % 10;
|
|
1287
|
+
return checkDigit === Number(value[12]);
|
|
1288
|
+
}
|
|
1289
|
+
function isPlausiblePhoneNumber(value) {
|
|
1290
|
+
const digits = value.replace(/\D/g, "");
|
|
1291
|
+
if (digits.length < 7 || digits.length > 15) return false;
|
|
1292
|
+
if (/^(\d)\1+$/.test(digits)) return false;
|
|
1293
|
+
if (digits === "0123456789".slice(0, digits.length)) return false;
|
|
1294
|
+
if (digits === "9876543210".slice(0, digits.length)) return false;
|
|
1295
|
+
return true;
|
|
1296
|
+
}
|
|
1297
|
+
function validateDriver(driver) {
|
|
1298
|
+
const errors = {};
|
|
1299
|
+
if (driver.firstName.trim().length < 2) errors.firstName = "Enter a first name";
|
|
1300
|
+
if (driver.lastName.trim().length < 2) errors.lastName = "Enter a last name";
|
|
1301
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(driver.email.trim())) {
|
|
1302
|
+
errors.email = "Enter a valid email address";
|
|
1303
|
+
}
|
|
1304
|
+
if (!isPlausiblePhoneNumber(driver.phone)) {
|
|
1305
|
+
errors.phone = "Enter a valid phone number";
|
|
1306
|
+
}
|
|
1307
|
+
if (driver.additionalEmail.trim() && !/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(driver.additionalEmail.trim())) {
|
|
1308
|
+
errors.additionalEmail = "Enter a valid email address";
|
|
1309
|
+
}
|
|
1310
|
+
if (driver.country === "South Africa" && driver.idNumber.trim() && !isValidSouthAfricanId(driver.idNumber.trim())) {
|
|
1311
|
+
errors.idNumber = "Enter a valid 13-digit South African ID number";
|
|
1312
|
+
}
|
|
1313
|
+
if (driver.notes.length > 500) errors.notes = "Keep notes under 500 characters";
|
|
1314
|
+
return errors;
|
|
1315
|
+
}
|
|
1316
|
+
function quoteFor(car, criteria, days, selection, extras, liveDeposit, liveDropOffFee) {
|
|
1317
|
+
const rentalDays2 = Math.max(1, days);
|
|
1318
|
+
const vehicleLabel = carDisplayName(car);
|
|
1319
|
+
const vehicleAmount = car.totalRate;
|
|
1320
|
+
const lines = [
|
|
1321
|
+
{
|
|
1322
|
+
id: "vehicle",
|
|
1323
|
+
label: vehicleLabel,
|
|
1324
|
+
detail: `${formatZAR(car.dailyRate)} \xD7 ${rentalDays2} ${rentalDays2 === 1 ? "day" : "days"}`,
|
|
1325
|
+
amount: vehicleAmount
|
|
1326
|
+
}
|
|
1327
|
+
];
|
|
1328
|
+
extras.forEach((extra) => {
|
|
1329
|
+
const quantity = selection[extra.code] ?? 0;
|
|
1330
|
+
if (quantity < 1) return;
|
|
1331
|
+
const unit = extraUnitPrice(extra, rentalDays2);
|
|
1332
|
+
const label = quantity > 1 ? `${displayName(extra.name)} \xD7 ${quantity}` : displayName(extra.name);
|
|
1333
|
+
let detail;
|
|
1334
|
+
if (extra.perDay) {
|
|
1335
|
+
detail = `${formatZAR(extra.perDay)}/day \xD7 ${rentalDays2} ${rentalDays2 === 1 ? "day" : "days"}`;
|
|
1336
|
+
} else if (extra.serviceType === "Per Unit" && quantity > 1) {
|
|
1337
|
+
detail = `${formatZAR(extra.onceOff ?? 0)} \xD7 ${quantity} \xB7 ${extra.serviceType}`;
|
|
1338
|
+
} else {
|
|
1339
|
+
detail = extra.serviceType;
|
|
1340
|
+
}
|
|
1341
|
+
lines.push({
|
|
1342
|
+
id: extra.code,
|
|
1343
|
+
label,
|
|
1344
|
+
detail,
|
|
1345
|
+
amount: unit * quantity
|
|
1346
|
+
});
|
|
1347
|
+
});
|
|
1348
|
+
const extrasTotal = lines.reduce((sum, line) => sum + line.amount, 0) - lines[0].amount;
|
|
1349
|
+
const notes = [];
|
|
1350
|
+
const deposit = liveDeposit ?? car.deposit;
|
|
1351
|
+
const excess = car.liability > 0 ? car.liability : 0;
|
|
1352
|
+
if (car.insuranceIncluded) {
|
|
1353
|
+
notes.push({ label: "Insurance", value: "Included" });
|
|
1354
|
+
}
|
|
1355
|
+
notes.push({
|
|
1356
|
+
label: "Mileage",
|
|
1357
|
+
value: car.unlimitedMileage ? "Unlimited" : `${car.kmPerDay} km/day`
|
|
1358
|
+
});
|
|
1359
|
+
const oneWay = criteria.dropoffLocation !== SAME_AS_PICKUP && criteria.dropoffLocation !== criteria.pickupLocation;
|
|
1360
|
+
if (oneWay) {
|
|
1361
|
+
const dropOffFee = liveDropOffFee ?? (car.dropOffFee > 0 ? car.dropOffFee : null);
|
|
1362
|
+
if (dropOffFee != null) {
|
|
1363
|
+
lines.push({ id: "one-way-fee", label: "One-way drop-off", amount: dropOffFee });
|
|
1364
|
+
} else {
|
|
1365
|
+
notes.push({ label: "One-way drop-off", value: "Fee confirmed by supplier" });
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
if (criteria.coupon) {
|
|
1369
|
+
notes.push({ label: `Coupon ${criteria.coupon}`, value: "Applied at confirmation" });
|
|
1370
|
+
}
|
|
1371
|
+
const total = lines.reduce((sum, line) => sum + line.amount, 0);
|
|
1372
|
+
return { days: rentalDays2, lines, notes, extrasTotal, total, deposit, excess };
|
|
1373
|
+
}
|
|
1374
|
+
function extraUnitPrice(extra, days) {
|
|
1375
|
+
return extra.perDay ? extra.perDay * Math.max(1, days) : extra.onceOff ?? 0;
|
|
1376
|
+
}
|
|
1377
|
+
function isOneWay(car) {
|
|
1378
|
+
return Boolean(car.returnBranch) && car.returnBranch !== car.pickUpBranch;
|
|
1379
|
+
}
|
|
1380
|
+
function selectedExtras(selection, extras) {
|
|
1381
|
+
return extras.filter((extra) => (selection[extra.code] ?? 0) > 0).map((extra) => ({
|
|
1382
|
+
code: extra.code,
|
|
1383
|
+
quantity: selection[extra.code]
|
|
1384
|
+
}));
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
// src/lib/countries.ts
|
|
1388
|
+
var COUNTRIES = [
|
|
1389
|
+
{ name: "South Africa", code: "ZA", dialCode: "+27", flag: "\u{1F1FF}\u{1F1E6}" },
|
|
1390
|
+
{ name: "Afghanistan", code: "AF", dialCode: "+93", flag: "\u{1F1E6}\u{1F1EB}" },
|
|
1391
|
+
{ name: "Albania", code: "AL", dialCode: "+355", flag: "\u{1F1E6}\u{1F1F1}" },
|
|
1392
|
+
{ name: "Algeria", code: "DZ", dialCode: "+213", flag: "\u{1F1E9}\u{1F1FF}" },
|
|
1393
|
+
{ name: "Andorra", code: "AD", dialCode: "+376", flag: "\u{1F1E6}\u{1F1E9}" },
|
|
1394
|
+
{ name: "Angola", code: "AO", dialCode: "+244", flag: "\u{1F1E6}\u{1F1F4}" },
|
|
1395
|
+
{ name: "Antigua and Barbuda", code: "AG", dialCode: "+1268", flag: "\u{1F1E6}\u{1F1EC}" },
|
|
1396
|
+
{ name: "Argentina", code: "AR", dialCode: "+54", flag: "\u{1F1E6}\u{1F1F7}" },
|
|
1397
|
+
{ name: "Armenia", code: "AM", dialCode: "+374", flag: "\u{1F1E6}\u{1F1F2}" },
|
|
1398
|
+
{ name: "Australia", code: "AU", dialCode: "+61", flag: "\u{1F1E6}\u{1F1FA}" },
|
|
1399
|
+
{ name: "Austria", code: "AT", dialCode: "+43", flag: "\u{1F1E6}\u{1F1F9}" },
|
|
1400
|
+
{ name: "Azerbaijan", code: "AZ", dialCode: "+994", flag: "\u{1F1E6}\u{1F1FF}" },
|
|
1401
|
+
{ name: "Bahamas", code: "BS", dialCode: "+1242", flag: "\u{1F1E7}\u{1F1F8}" },
|
|
1402
|
+
{ name: "Bahrain", code: "BH", dialCode: "+973", flag: "\u{1F1E7}\u{1F1ED}" },
|
|
1403
|
+
{ name: "Bangladesh", code: "BD", dialCode: "+880", flag: "\u{1F1E7}\u{1F1E9}" },
|
|
1404
|
+
{ name: "Barbados", code: "BB", dialCode: "+1246", flag: "\u{1F1E7}\u{1F1E7}" },
|
|
1405
|
+
{ name: "Belarus", code: "BY", dialCode: "+375", flag: "\u{1F1E7}\u{1F1FE}" },
|
|
1406
|
+
{ name: "Belgium", code: "BE", dialCode: "+32", flag: "\u{1F1E7}\u{1F1EA}" },
|
|
1407
|
+
{ name: "Belize", code: "BZ", dialCode: "+501", flag: "\u{1F1E7}\u{1F1FF}" },
|
|
1408
|
+
{ name: "Benin", code: "BJ", dialCode: "+229", flag: "\u{1F1E7}\u{1F1EF}" },
|
|
1409
|
+
{ name: "Bhutan", code: "BT", dialCode: "+975", flag: "\u{1F1E7}\u{1F1F9}" },
|
|
1410
|
+
{ name: "Bolivia", code: "BO", dialCode: "+591", flag: "\u{1F1E7}\u{1F1F4}" },
|
|
1411
|
+
{ name: "Bosnia and Herzegovina", code: "BA", dialCode: "+387", flag: "\u{1F1E7}\u{1F1E6}" },
|
|
1412
|
+
{ name: "Botswana", code: "BW", dialCode: "+267", flag: "\u{1F1E7}\u{1F1FC}" },
|
|
1413
|
+
{ name: "Brazil", code: "BR", dialCode: "+55", flag: "\u{1F1E7}\u{1F1F7}" },
|
|
1414
|
+
{ name: "Brunei", code: "BN", dialCode: "+673", flag: "\u{1F1E7}\u{1F1F3}" },
|
|
1415
|
+
{ name: "Bulgaria", code: "BG", dialCode: "+359", flag: "\u{1F1E7}\u{1F1EC}" },
|
|
1416
|
+
{ name: "Burkina Faso", code: "BF", dialCode: "+226", flag: "\u{1F1E7}\u{1F1EB}" },
|
|
1417
|
+
{ name: "Burundi", code: "BI", dialCode: "+257", flag: "\u{1F1E7}\u{1F1EE}" },
|
|
1418
|
+
{ name: "Cambodia", code: "KH", dialCode: "+855", flag: "\u{1F1F0}\u{1F1ED}" },
|
|
1419
|
+
{ name: "Cameroon", code: "CM", dialCode: "+237", flag: "\u{1F1E8}\u{1F1F2}" },
|
|
1420
|
+
{ name: "Canada", code: "CA", dialCode: "+1", flag: "\u{1F1E8}\u{1F1E6}" },
|
|
1421
|
+
{ name: "Cape Verde", code: "CV", dialCode: "+238", flag: "\u{1F1E8}\u{1F1FB}" },
|
|
1422
|
+
{ name: "Central African Republic", code: "CF", dialCode: "+236", flag: "\u{1F1E8}\u{1F1EB}" },
|
|
1423
|
+
{ name: "Chad", code: "TD", dialCode: "+235", flag: "\u{1F1F9}\u{1F1E9}" },
|
|
1424
|
+
{ name: "Chile", code: "CL", dialCode: "+56", flag: "\u{1F1E8}\u{1F1F1}" },
|
|
1425
|
+
{ name: "China", code: "CN", dialCode: "+86", flag: "\u{1F1E8}\u{1F1F3}" },
|
|
1426
|
+
{ name: "Colombia", code: "CO", dialCode: "+57", flag: "\u{1F1E8}\u{1F1F4}" },
|
|
1427
|
+
{ name: "Comoros", code: "KM", dialCode: "+269", flag: "\u{1F1F0}\u{1F1F2}" },
|
|
1428
|
+
{ name: "Congo", code: "CG", dialCode: "+242", flag: "\u{1F1E8}\u{1F1EC}" },
|
|
1429
|
+
{ name: "Costa Rica", code: "CR", dialCode: "+506", flag: "\u{1F1E8}\u{1F1F7}" },
|
|
1430
|
+
{ name: "Croatia", code: "HR", dialCode: "+385", flag: "\u{1F1ED}\u{1F1F7}" },
|
|
1431
|
+
{ name: "Cuba", code: "CU", dialCode: "+53", flag: "\u{1F1E8}\u{1F1FA}" },
|
|
1432
|
+
{ name: "Cyprus", code: "CY", dialCode: "+357", flag: "\u{1F1E8}\u{1F1FE}" },
|
|
1433
|
+
{ name: "Czech Republic", code: "CZ", dialCode: "+420", flag: "\u{1F1E8}\u{1F1FF}" },
|
|
1434
|
+
{ name: "Denmark", code: "DK", dialCode: "+45", flag: "\u{1F1E9}\u{1F1F0}" },
|
|
1435
|
+
{ name: "Djibouti", code: "DJ", dialCode: "+253", flag: "\u{1F1E9}\u{1F1EF}" },
|
|
1436
|
+
{ name: "Dominica", code: "DM", dialCode: "+1767", flag: "\u{1F1E9}\u{1F1F2}" },
|
|
1437
|
+
{ name: "Dominican Republic", code: "DO", dialCode: "+1849", flag: "\u{1F1E9}\u{1F1F4}" },
|
|
1438
|
+
{ name: "Ecuador", code: "EC", dialCode: "+593", flag: "\u{1F1EA}\u{1F1E8}" },
|
|
1439
|
+
{ name: "Egypt", code: "EG", dialCode: "+20", flag: "\u{1F1EA}\u{1F1EC}" },
|
|
1440
|
+
{ name: "El Salvador", code: "SV", dialCode: "+503", flag: "\u{1F1F8}\u{1F1FB}" },
|
|
1441
|
+
{ name: "Equatorial Guinea", code: "GQ", dialCode: "+240", flag: "\u{1F1EC}\u{1F1F6}" },
|
|
1442
|
+
{ name: "Eritrea", code: "ER", dialCode: "+291", flag: "\u{1F1EA}\u{1F1F7}" },
|
|
1443
|
+
{ name: "Estonia", code: "EE", dialCode: "+372", flag: "\u{1F1EA}\u{1F1EA}" },
|
|
1444
|
+
{ name: "Ethiopia", code: "ET", dialCode: "+251", flag: "\u{1F1EA}\u{1F1F9}" },
|
|
1445
|
+
{ name: "Fiji", code: "FJ", dialCode: "+679", flag: "\u{1F1EB}\u{1F1EF}" },
|
|
1446
|
+
{ name: "Finland", code: "FI", dialCode: "+358", flag: "\u{1F1EB}\u{1F1EE}" },
|
|
1447
|
+
{ name: "France", code: "FR", dialCode: "+33", flag: "\u{1F1EB}\u{1F1F7}" },
|
|
1448
|
+
{ name: "Gabon", code: "GA", dialCode: "+241", flag: "\u{1F1EC}\u{1F1E6}" },
|
|
1449
|
+
{ name: "Gambia", code: "GM", dialCode: "+220", flag: "\u{1F1EC}\u{1F1F2}" },
|
|
1450
|
+
{ name: "Georgia", code: "GE", dialCode: "+995", flag: "\u{1F1EC}\u{1F1EA}" },
|
|
1451
|
+
{ name: "Germany", code: "DE", dialCode: "+49", flag: "\u{1F1E9}\u{1F1EA}" },
|
|
1452
|
+
{ name: "Ghana", code: "GH", dialCode: "+233", flag: "\u{1F1EC}\u{1F1ED}" },
|
|
1453
|
+
{ name: "Greece", code: "GR", dialCode: "+30", flag: "\u{1F1EC}\u{1F1F7}" },
|
|
1454
|
+
{ name: "Grenada", code: "GD", dialCode: "+1473", flag: "\u{1F1EC}\u{1F1E9}" },
|
|
1455
|
+
{ name: "Guatemala", code: "GT", dialCode: "+502", flag: "\u{1F1EC}\u{1F1F9}" },
|
|
1456
|
+
{ name: "Guinea", code: "GN", dialCode: "+224", flag: "\u{1F1EC}\u{1F1F3}" },
|
|
1457
|
+
{ name: "Guinea-Bissau", code: "GW", dialCode: "+245", flag: "\u{1F1EC}\u{1F1FC}" },
|
|
1458
|
+
{ name: "Guyana", code: "GY", dialCode: "+592", flag: "\u{1F1EC}\u{1F1FE}" },
|
|
1459
|
+
{ name: "Haiti", code: "HT", dialCode: "+509", flag: "\u{1F1ED}\u{1F1F9}" },
|
|
1460
|
+
{ name: "Honduras", code: "HN", dialCode: "+504", flag: "\u{1F1ED}\u{1F1F3}" },
|
|
1461
|
+
{ name: "Hong Kong", code: "HK", dialCode: "+852", flag: "\u{1F1ED}\u{1F1F0}" },
|
|
1462
|
+
{ name: "Hungary", code: "HU", dialCode: "+36", flag: "\u{1F1ED}\u{1F1FA}" },
|
|
1463
|
+
{ name: "Iceland", code: "IS", dialCode: "+354", flag: "\u{1F1EE}\u{1F1F8}" },
|
|
1464
|
+
{ name: "India", code: "IN", dialCode: "+91", flag: "\u{1F1EE}\u{1F1F3}" },
|
|
1465
|
+
{ name: "Indonesia", code: "ID", dialCode: "+62", flag: "\u{1F1EE}\u{1F1E9}" },
|
|
1466
|
+
{ name: "Iran", code: "IR", dialCode: "+98", flag: "\u{1F1EE}\u{1F1F7}" },
|
|
1467
|
+
{ name: "Iraq", code: "IQ", dialCode: "+964", flag: "\u{1F1EE}\u{1F1F6}" },
|
|
1468
|
+
{ name: "Ireland", code: "IE", dialCode: "+353", flag: "\u{1F1EE}\u{1F1EA}" },
|
|
1469
|
+
{ name: "Israel", code: "IL", dialCode: "+972", flag: "\u{1F1EE}\u{1F1F1}" },
|
|
1470
|
+
{ name: "Italy", code: "IT", dialCode: "+39", flag: "\u{1F1EE}\u{1F1F9}" },
|
|
1471
|
+
{ name: "Jamaica", code: "JM", dialCode: "+1876", flag: "\u{1F1EF}\u{1F1F2}" },
|
|
1472
|
+
{ name: "Japan", code: "JP", dialCode: "+81", flag: "\u{1F1EF}\u{1F1F5}" },
|
|
1473
|
+
{ name: "Jordan", code: "JO", dialCode: "+962", flag: "\u{1F1EF}\u{1F1F4}" },
|
|
1474
|
+
{ name: "Kazakhstan", code: "KZ", dialCode: "+7", flag: "\u{1F1F0}\u{1F1FF}" },
|
|
1475
|
+
{ name: "Kenya", code: "KE", dialCode: "+254", flag: "\u{1F1F0}\u{1F1EA}" },
|
|
1476
|
+
{ name: "Kiribati", code: "KI", dialCode: "+686", flag: "\u{1F1F0}\u{1F1EE}" },
|
|
1477
|
+
{ name: "Kuwait", code: "KW", dialCode: "+965", flag: "\u{1F1F0}\u{1F1FC}" },
|
|
1478
|
+
{ name: "Kyrgyzstan", code: "KG", dialCode: "+996", flag: "\u{1F1F0}\u{1F1EC}" },
|
|
1479
|
+
{ name: "Laos", code: "LA", dialCode: "+856", flag: "\u{1F1F1}\u{1F1E6}" },
|
|
1480
|
+
{ name: "Latvia", code: "LV", dialCode: "+371", flag: "\u{1F1F1}\u{1F1FB}" },
|
|
1481
|
+
{ name: "Lebanon", code: "LB", dialCode: "+961", flag: "\u{1F1F1}\u{1F1E7}" },
|
|
1482
|
+
{ name: "Lesotho", code: "LS", dialCode: "+266", flag: "\u{1F1F1}\u{1F1F8}" },
|
|
1483
|
+
{ name: "Liberia", code: "LR", dialCode: "+231", flag: "\u{1F1F1}\u{1F1F7}" },
|
|
1484
|
+
{ name: "Libya", code: "LY", dialCode: "+218", flag: "\u{1F1F1}\u{1F1FE}" },
|
|
1485
|
+
{ name: "Liechtenstein", code: "LI", dialCode: "+423", flag: "\u{1F1F1}\u{1F1EE}" },
|
|
1486
|
+
{ name: "Lithuania", code: "LT", dialCode: "+370", flag: "\u{1F1F1}\u{1F1F9}" },
|
|
1487
|
+
{ name: "Luxembourg", code: "LU", dialCode: "+352", flag: "\u{1F1F1}\u{1F1FA}" },
|
|
1488
|
+
{ name: "Macau", code: "MO", dialCode: "+853", flag: "\u{1F1F2}\u{1F1F4}" },
|
|
1489
|
+
{ name: "Madagascar", code: "MG", dialCode: "+261", flag: "\u{1F1F2}\u{1F1EC}" },
|
|
1490
|
+
{ name: "Malawi", code: "MW", dialCode: "+265", flag: "\u{1F1F2}\u{1F1FC}" },
|
|
1491
|
+
{ name: "Malaysia", code: "MY", dialCode: "+60", flag: "\u{1F1F2}\u{1F1FE}" },
|
|
1492
|
+
{ name: "Maldives", code: "MV", dialCode: "+960", flag: "\u{1F1F2}\u{1F1FB}" },
|
|
1493
|
+
{ name: "Mali", code: "ML", dialCode: "+223", flag: "\u{1F1F2}\u{1F1F1}" },
|
|
1494
|
+
{ name: "Malta", code: "MT", dialCode: "+356", flag: "\u{1F1F2}\u{1F1F9}" },
|
|
1495
|
+
{ name: "Marshall Islands", code: "MH", dialCode: "+692", flag: "\u{1F1F2}\u{1F1ED}" },
|
|
1496
|
+
{ name: "Mauritania", code: "MR", dialCode: "+222", flag: "\u{1F1F2}\u{1F1F7}" },
|
|
1497
|
+
{ name: "Mauritius", code: "MU", dialCode: "+230", flag: "\u{1F1F2}\u{1F1FA}" },
|
|
1498
|
+
{ name: "Mexico", code: "MX", dialCode: "+52", flag: "\u{1F1F2}\u{1F1FD}" },
|
|
1499
|
+
{ name: "Micronesia", code: "FM", dialCode: "+691", flag: "\u{1F1EB}\u{1F1F2}" },
|
|
1500
|
+
{ name: "Moldova", code: "MD", dialCode: "+373", flag: "\u{1F1F2}\u{1F1E9}" },
|
|
1501
|
+
{ name: "Monaco", code: "MC", dialCode: "+377", flag: "\u{1F1F2}\u{1F1E8}" },
|
|
1502
|
+
{ name: "Mongolia", code: "MN", dialCode: "+976", flag: "\u{1F1F2}\u{1F1F3}" },
|
|
1503
|
+
{ name: "Montenegro", code: "ME", dialCode: "+382", flag: "\u{1F1F2}\u{1F1EA}" },
|
|
1504
|
+
{ name: "Morocco", code: "MA", dialCode: "+212", flag: "\u{1F1F2}\u{1F1E6}" },
|
|
1505
|
+
{ name: "Mozambique", code: "MZ", dialCode: "+258", flag: "\u{1F1F2}\u{1F1FF}" },
|
|
1506
|
+
{ name: "Myanmar", code: "MM", dialCode: "+95", flag: "\u{1F1F2}\u{1F1F2}" },
|
|
1507
|
+
{ name: "Namibia", code: "NA", dialCode: "+264", flag: "\u{1F1F3}\u{1F1E6}" },
|
|
1508
|
+
{ name: "Nauru", code: "NR", dialCode: "+674", flag: "\u{1F1F3}\u{1F1F7}" },
|
|
1509
|
+
{ name: "Nepal", code: "NP", dialCode: "+977", flag: "\u{1F1F3}\u{1F1F5}" },
|
|
1510
|
+
{ name: "Netherlands", code: "NL", dialCode: "+31", flag: "\u{1F1F3}\u{1F1F1}" },
|
|
1511
|
+
{ name: "New Zealand", code: "NZ", dialCode: "+64", flag: "\u{1F1F3}\u{1F1FF}" },
|
|
1512
|
+
{ name: "Nicaragua", code: "NI", dialCode: "+505", flag: "\u{1F1F3}\u{1F1EE}" },
|
|
1513
|
+
{ name: "Niger", code: "NE", dialCode: "+227", flag: "\u{1F1F3}\u{1F1EA}" },
|
|
1514
|
+
{ name: "Nigeria", code: "NG", dialCode: "+234", flag: "\u{1F1F3}\u{1F1EC}" },
|
|
1515
|
+
{ name: "North Korea", code: "KP", dialCode: "+850", flag: "\u{1F1F0}\u{1F1F5}" },
|
|
1516
|
+
{ name: "North Macedonia", code: "MK", dialCode: "+389", flag: "\u{1F1F2}\u{1F1F0}" },
|
|
1517
|
+
{ name: "Norway", code: "NO", dialCode: "+47", flag: "\u{1F1F3}\u{1F1F4}" },
|
|
1518
|
+
{ name: "Oman", code: "OM", dialCode: "+968", flag: "\u{1F1F4}\u{1F1F2}" },
|
|
1519
|
+
{ name: "Pakistan", code: "PK", dialCode: "+92", flag: "\u{1F1F5}\u{1F1F0}" },
|
|
1520
|
+
{ name: "Palau", code: "PW", dialCode: "+680", flag: "\u{1F1F5}\u{1F1FC}" },
|
|
1521
|
+
{ name: "Palestine", code: "PS", dialCode: "+970", flag: "\u{1F1F5}\u{1F1F8}" },
|
|
1522
|
+
{ name: "Panama", code: "PA", dialCode: "+507", flag: "\u{1F1F5}\u{1F1E6}" },
|
|
1523
|
+
{ name: "Papua New Guinea", code: "PG", dialCode: "+675", flag: "\u{1F1F5}\u{1F1EC}" },
|
|
1524
|
+
{ name: "Paraguay", code: "PY", dialCode: "+595", flag: "\u{1F1F5}\u{1F1FE}" },
|
|
1525
|
+
{ name: "Peru", code: "PE", dialCode: "+51", flag: "\u{1F1F5}\u{1F1EA}" },
|
|
1526
|
+
{ name: "Philippines", code: "PH", dialCode: "+63", flag: "\u{1F1F5}\u{1F1ED}" },
|
|
1527
|
+
{ name: "Poland", code: "PL", dialCode: "+48", flag: "\u{1F1F5}\u{1F1F1}" },
|
|
1528
|
+
{ name: "Portugal", code: "PT", dialCode: "+351", flag: "\u{1F1F5}\u{1F1F9}" },
|
|
1529
|
+
{ name: "Qatar", code: "QA", dialCode: "+974", flag: "\u{1F1F6}\u{1F1E6}" },
|
|
1530
|
+
{ name: "Romania", code: "RO", dialCode: "+40", flag: "\u{1F1F7}\u{1F1F4}" },
|
|
1531
|
+
{ name: "Russia", code: "RU", dialCode: "+7", flag: "\u{1F1F7}\u{1F1FA}" },
|
|
1532
|
+
{ name: "Rwanda", code: "RW", dialCode: "+250", flag: "\u{1F1F7}\u{1F1FC}" },
|
|
1533
|
+
{ name: "Saint Kitts and Nevis", code: "KN", dialCode: "+1869", flag: "\u{1F1F0}\u{1F1F3}" },
|
|
1534
|
+
{ name: "Saint Lucia", code: "LC", dialCode: "+1758", flag: "\u{1F1F1}\u{1F1E8}" },
|
|
1535
|
+
{ name: "Saint Vincent", code: "VC", dialCode: "+1784", flag: "\u{1F1FB}\u{1F1E8}" },
|
|
1536
|
+
{ name: "Samoa", code: "WS", dialCode: "+685", flag: "\u{1F1FC}\u{1F1F8}" },
|
|
1537
|
+
{ name: "San Marino", code: "SM", dialCode: "+378", flag: "\u{1F1F8}\u{1F1F2}" },
|
|
1538
|
+
{ name: "Sao Tome and Principe", code: "ST", dialCode: "+239", flag: "\u{1F1F8}\u{1F1F9}" },
|
|
1539
|
+
{ name: "Saudi Arabia", code: "SA", dialCode: "+966", flag: "\u{1F1F8}\u{1F1E6}" },
|
|
1540
|
+
{ name: "Senegal", code: "SN", dialCode: "+221", flag: "\u{1F1F8}\u{1F1F3}" },
|
|
1541
|
+
{ name: "Serbia", code: "RS", dialCode: "+381", flag: "\u{1F1F7}\u{1F1F8}" },
|
|
1542
|
+
{ name: "Seychelles", code: "SC", dialCode: "+248", flag: "\u{1F1F8}\u{1F1E8}" },
|
|
1543
|
+
{ name: "Sierra Leone", code: "SL", dialCode: "+232", flag: "\u{1F1F8}\u{1F1F1}" },
|
|
1544
|
+
{ name: "Singapore", code: "SG", dialCode: "+65", flag: "\u{1F1F8}\u{1F1EC}" },
|
|
1545
|
+
{ name: "Slovakia", code: "SK", dialCode: "+421", flag: "\u{1F1F8}\u{1F1F0}" },
|
|
1546
|
+
{ name: "Slovenia", code: "SI", dialCode: "+386", flag: "\u{1F1F8}\u{1F1EE}" },
|
|
1547
|
+
{ name: "Solomon Islands", code: "SB", dialCode: "+677", flag: "\u{1F1F8}\u{1F1E7}" },
|
|
1548
|
+
{ name: "Somalia", code: "SO", dialCode: "+252", flag: "\u{1F1F8}\u{1F1F4}" },
|
|
1549
|
+
{ name: "South Korea", code: "KR", dialCode: "+82", flag: "\u{1F1F0}\u{1F1F7}" },
|
|
1550
|
+
{ name: "South Sudan", code: "SS", dialCode: "+211", flag: "\u{1F1F8}\u{1F1F8}" },
|
|
1551
|
+
{ name: "Spain", code: "ES", dialCode: "+34", flag: "\u{1F1EA}\u{1F1F8}" },
|
|
1552
|
+
{ name: "Sri Lanka", code: "LK", dialCode: "+94", flag: "\u{1F1F1}\u{1F1F0}" },
|
|
1553
|
+
{ name: "Sudan", code: "SD", dialCode: "+249", flag: "\u{1F1F8}\u{1F1E9}" },
|
|
1554
|
+
{ name: "Suriname", code: "SR", dialCode: "+597", flag: "\u{1F1F8}\u{1F1F7}" },
|
|
1555
|
+
{ name: "Sweden", code: "SE", dialCode: "+46", flag: "\u{1F1F8}\u{1F1EA}" },
|
|
1556
|
+
{ name: "Switzerland", code: "CH", dialCode: "+41", flag: "\u{1F1E8}\u{1F1ED}" },
|
|
1557
|
+
{ name: "Syria", code: "SY", dialCode: "+963", flag: "\u{1F1F8}\u{1F1FE}" },
|
|
1558
|
+
{ name: "Taiwan", code: "TW", dialCode: "+886", flag: "\u{1F1F9}\u{1F1FC}" },
|
|
1559
|
+
{ name: "Tajikistan", code: "TJ", dialCode: "+992", flag: "\u{1F1F9}\u{1F1EF}" },
|
|
1560
|
+
{ name: "Tanzania", code: "TZ", dialCode: "+255", flag: "\u{1F1F9}\u{1F1FF}" },
|
|
1561
|
+
{ name: "Thailand", code: "TH", dialCode: "+66", flag: "\u{1F1F9}\u{1F1ED}" },
|
|
1562
|
+
{ name: "Timor-Leste", code: "TL", dialCode: "+670", flag: "\u{1F1F9}\u{1F1F1}" },
|
|
1563
|
+
{ name: "Togo", code: "TG", dialCode: "+228", flag: "\u{1F1F9}\u{1F1EC}" },
|
|
1564
|
+
{ name: "Tonga", code: "TO", dialCode: "+676", flag: "\u{1F1F9}\u{1F1F4}" },
|
|
1565
|
+
{ name: "Trinidad and Tobago", code: "TT", dialCode: "+1868", flag: "\u{1F1F9}\u{1F1F9}" },
|
|
1566
|
+
{ name: "Tunisia", code: "TN", dialCode: "+216", flag: "\u{1F1F9}\u{1F1F3}" },
|
|
1567
|
+
{ name: "Turkey", code: "TR", dialCode: "+90", flag: "\u{1F1F9}\u{1F1F7}" },
|
|
1568
|
+
{ name: "Turkmenistan", code: "TM", dialCode: "+993", flag: "\u{1F1F9}\u{1F1F2}" },
|
|
1569
|
+
{ name: "Tuvalu", code: "TV", dialCode: "+688", flag: "\u{1F1F9}\u{1F1FB}" },
|
|
1570
|
+
{ name: "Uganda", code: "UG", dialCode: "+256", flag: "\u{1F1FA}\u{1F1EC}" },
|
|
1571
|
+
{ name: "Ukraine", code: "UA", dialCode: "+380", flag: "\u{1F1FA}\u{1F1E6}" },
|
|
1572
|
+
{ name: "United Arab Emirates", code: "AE", dialCode: "+971", flag: "\u{1F1E6}\u{1F1EA}" },
|
|
1573
|
+
{ name: "United Kingdom", code: "GB", dialCode: "+44", flag: "\u{1F1EC}\u{1F1E7}" },
|
|
1574
|
+
{ name: "United States", code: "US", dialCode: "+1", flag: "\u{1F1FA}\u{1F1F8}" },
|
|
1575
|
+
{ name: "Uruguay", code: "UY", dialCode: "+598", flag: "\u{1F1FA}\u{1F1FE}" },
|
|
1576
|
+
{ name: "Uzbekistan", code: "UZ", dialCode: "+998", flag: "\u{1F1FA}\u{1F1FF}" },
|
|
1577
|
+
{ name: "Vanuatu", code: "VU", dialCode: "+678", flag: "\u{1F1FB}\u{1F1FA}" },
|
|
1578
|
+
{ name: "Vatican City", code: "VA", dialCode: "+379", flag: "\u{1F1FB}\u{1F1E6}" },
|
|
1579
|
+
{ name: "Venezuela", code: "VE", dialCode: "+58", flag: "\u{1F1FB}\u{1F1EA}" },
|
|
1580
|
+
{ name: "Vietnam", code: "VN", dialCode: "+84", flag: "\u{1F1FB}\u{1F1F3}" },
|
|
1581
|
+
{ name: "Yemen", code: "YE", dialCode: "+967", flag: "\u{1F1FE}\u{1F1EA}" },
|
|
1582
|
+
{ name: "Zambia", code: "ZM", dialCode: "+260", flag: "\u{1F1FF}\u{1F1F2}" },
|
|
1583
|
+
{ name: "Zimbabwe", code: "ZW", dialCode: "+263", flag: "\u{1F1FF}\u{1F1FC}" }
|
|
1584
|
+
];
|
|
1585
|
+
function countryByName(name) {
|
|
1586
|
+
return COUNTRIES.find((country) => country.name === name);
|
|
1587
|
+
}
|
|
1588
|
+
var COUNTRY_GROUPS = [
|
|
1589
|
+
{
|
|
1590
|
+
options: COUNTRIES.map((country) => ({
|
|
1591
|
+
value: country.name,
|
|
1592
|
+
label: `${country.flag} ${country.name}`
|
|
1593
|
+
}))
|
|
1594
|
+
}
|
|
1595
|
+
];
|
|
1596
|
+
|
|
1597
|
+
// src/lib/queries/booking.queries.ts
|
|
1598
|
+
function bookingRequestFields(request, options = {}) {
|
|
1599
|
+
const { car, criteria, driver, selection, extras, days, dropOffFee, deposit } = request;
|
|
1600
|
+
const chosen = selectedExtras(selection, extras);
|
|
1601
|
+
const pickup = criteria.pickupLocation;
|
|
1602
|
+
const dropoffName = criteria.dropoffLocation === SAME_AS_PICKUP ? pickup : criteria.dropoffLocation;
|
|
1603
|
+
const country = countryByName(driver.country);
|
|
1604
|
+
const rentalDays2 = Math.max(1, days);
|
|
1605
|
+
const extraFields = chosen.map((extra) => {
|
|
1606
|
+
const catalog = extras.find((candidate) => candidate.code === extra.code);
|
|
1607
|
+
const unitPrice = catalog ? extraUnitPrice(catalog, rentalDays2) : 0;
|
|
1608
|
+
return {
|
|
1609
|
+
code: extra.code,
|
|
1610
|
+
name: catalog?.name ?? extra.code,
|
|
1611
|
+
quantity: extra.quantity,
|
|
1612
|
+
unitPrice
|
|
1613
|
+
};
|
|
1614
|
+
});
|
|
1615
|
+
return {
|
|
1616
|
+
// Kept separate from `extras` below rather than folded into it as a
|
|
1617
|
+
// synthetic line - that field is also what becomes the real
|
|
1618
|
+
// supplier reservation/quote-refresh request (see PHP's
|
|
1619
|
+
// ajax_submit_booking/ajax_amend_my_booking), and a fake product code
|
|
1620
|
+
// sent to the supplier would either get rejected outright or silently
|
|
1621
|
+
// book something that doesn't exist. The PHP handlers append this as
|
|
1622
|
+
// its own BookingExtras row for display only, after building the
|
|
1623
|
+
// supplier-facing params from the untouched `extras` list.
|
|
1624
|
+
dropOffFee: dropOffFee != null && dropOffFee > 0 ? String(dropOffFee) : "",
|
|
1625
|
+
termsId: request.terms?.id ?? "",
|
|
1626
|
+
termsVersion: request.terms ? String(request.terms.version) : "",
|
|
1627
|
+
// `quoteFor`'s refundable card hold - same BookingRate-only treatment
|
|
1628
|
+
// as dropOffFee above.
|
|
1629
|
+
deposit: deposit != null && deposit > 0 ? String(deposit) : "",
|
|
1630
|
+
// The vehicle rental line only (quoteFor's own 'vehicle' line, i.e.
|
|
1631
|
+
// car.totalRate) - BookingRate.amount, kept apart from dailyRate below
|
|
1632
|
+
// (a per-day figure) so both survive on the priced record.
|
|
1633
|
+
vehicleAmount: String(car.totalRate),
|
|
1634
|
+
// Whichever tier CarCard's coverage toggle actually priced this
|
|
1635
|
+
// booking at (see Car.ratePackageLabel / withPackage's `tier` param) -
|
|
1636
|
+
// 'Standard' unless the supplier quoted a genuine second tier (e.g.
|
|
1637
|
+
// Europcar's SuperCover) and the customer selected it.
|
|
1638
|
+
ratePackageLabel: car.ratePackageLabel || "Standard",
|
|
1639
|
+
// ── Supplier + listing identity, all straight from the search ──────
|
|
1640
|
+
// `supplier.id` is the adapter identifier ("bluu", "sanisixt", …) -
|
|
1641
|
+
// the AccountSupplier's own id never reaches the browser, and the
|
|
1642
|
+
// handler resolves the connection from this.
|
|
1643
|
+
identifier: car.supplier.id ?? "",
|
|
1644
|
+
// Branch CODES, already resolved per supplier at search time. Names
|
|
1645
|
+
// go along separately, for the record and the notification email.
|
|
1646
|
+
pickupCode: car.pickUpBranch ?? "",
|
|
1647
|
+
dropoffCode: car.returnBranch ?? car.pickUpBranch ?? "",
|
|
1648
|
+
vehicleGroup: car.carGroup ?? "",
|
|
1649
|
+
// The chosen km package's rate and quote references, so the booking
|
|
1650
|
+
// is made at the rate that was shown (see Car.rateNo).
|
|
1651
|
+
rateId: car.rateNo ?? "",
|
|
1652
|
+
// The price quote of the chosen package. The rate's own number
|
|
1653
|
+
// (rateSrNo) is never a quote: SaniSixt answered it with "Price Quote
|
|
1654
|
+
// Not Found or has Expired".
|
|
1655
|
+
quoteRef: car.quoteRef ?? car.quoteIds?.[0] ?? "",
|
|
1656
|
+
cdpCode: car.cdpCode ?? "",
|
|
1657
|
+
carId: car.id,
|
|
1658
|
+
carLabel: `${car.make} ${car.model} ${car.trim}`.trim(),
|
|
1659
|
+
supplier: car.supplier.name,
|
|
1660
|
+
dailyRate: String(car.dailyRate),
|
|
1661
|
+
currency: car.currency,
|
|
1662
|
+
days: String(request.days),
|
|
1663
|
+
estimatedTotal: String(request.total),
|
|
1664
|
+
// Vehicle snapshot - captured now because none of it survives once
|
|
1665
|
+
// the search that produced it is gone (see booking.prisma's
|
|
1666
|
+
// docblock on Booking's vehicle-snapshot columns).
|
|
1667
|
+
carImage: car.image ?? "",
|
|
1668
|
+
supplierLogo: car.supplier.logoUrl ?? "",
|
|
1669
|
+
seats: String(car.seats || ""),
|
|
1670
|
+
doors: String(car.doors || ""),
|
|
1671
|
+
transmission: car.transmission ?? "",
|
|
1672
|
+
fuelType: car.fuelType ?? "",
|
|
1673
|
+
insuranceIncluded: car.insuranceIncluded ? "1" : "0",
|
|
1674
|
+
// SpecGrid / BookingSummary km option (see QuoteController and
|
|
1675
|
+
// Booking::to_email_notification) - same "unlimited wins" convention
|
|
1676
|
+
// as VehicleCard's own mileage spec.
|
|
1677
|
+
unlimitedMileage: car.unlimitedMileage ? "1" : "0",
|
|
1678
|
+
kmPerDay: String(car.kmPerDay || ""),
|
|
1679
|
+
// Machine-readable for the reservation, plus a human-readable copy so
|
|
1680
|
+
// the recorded booking and the notification email are legible on
|
|
1681
|
+
// their own.
|
|
1682
|
+
extras: JSON.stringify(extraFields),
|
|
1683
|
+
extrasLabel: extraFields.map((extra) => extra.quantity > 1 ? `${extra.name} \xD7 ${extra.quantity}` : extra.name).join(", "),
|
|
1684
|
+
pickupLocation: pickup,
|
|
1685
|
+
dropoffLocation: dropoffName,
|
|
1686
|
+
pickupDate: criteria.pickupDate,
|
|
1687
|
+
pickupTime: criteria.pickupTime,
|
|
1688
|
+
returnDate: criteria.dropoffDate,
|
|
1689
|
+
returnTime: criteria.dropoffTime,
|
|
1690
|
+
coupon: criteria.coupon,
|
|
1691
|
+
// Booking with the same residency the rates were searched at, so the
|
|
1692
|
+
// CDP code the handler picks matches the one they were quoted under.
|
|
1693
|
+
residency: criteria.residency,
|
|
1694
|
+
title: driver.title,
|
|
1695
|
+
firstName: driver.firstName.trim(),
|
|
1696
|
+
lastName: driver.lastName.trim(),
|
|
1697
|
+
email: driver.email.trim(),
|
|
1698
|
+
phone: driver.phone.trim(),
|
|
1699
|
+
licenceNumber: driver.licenceNumber.trim(),
|
|
1700
|
+
country: driver.country.trim(),
|
|
1701
|
+
countryCode: country?.code ?? "",
|
|
1702
|
+
// The phone's own dial code when the driver picked one, else their country's.
|
|
1703
|
+
dialCode: driver.dialCode?.trim() || country?.dialCode || "",
|
|
1704
|
+
idNumber: driver.idNumber.trim(),
|
|
1705
|
+
crossingBorder: driver.crossingBorder ? "1" : "0",
|
|
1706
|
+
streetAddress: driver.streetAddress.trim(),
|
|
1707
|
+
city: driver.city.trim(),
|
|
1708
|
+
suburb: driver.suburb.trim(),
|
|
1709
|
+
province: driver.province.trim(),
|
|
1710
|
+
postalCode: driver.postalCode.trim(),
|
|
1711
|
+
additionalEmail: driver.additionalEmail.trim(),
|
|
1712
|
+
notes: driver.notes.trim(),
|
|
1713
|
+
// How the customer pays (the site's own cars only), and the page to come back to after paying online.
|
|
1714
|
+
paymentMethodId: request.payment?.methodId ?? "",
|
|
1715
|
+
paymentPurpose: request.payment?.purpose ?? "FULL",
|
|
1716
|
+
returnUrl: request.payment ? request.payment.returnUrl || options.paymentReturnUrl?.() || "" : ""
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
function notify(hook, request) {
|
|
1720
|
+
if (!hook) return;
|
|
1721
|
+
try {
|
|
1722
|
+
hook(request);
|
|
1723
|
+
} catch (error) {
|
|
1724
|
+
console.error("[FleetBridge] booking hook failed", error);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
var BookingQueries = class extends BaseQuery {
|
|
1728
|
+
constructor(hooks = {}) {
|
|
1729
|
+
super();
|
|
1730
|
+
this.hooks = hooks;
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* Posts the booking to the host site. Calls `onBookingRequested` first,
|
|
1734
|
+
* either way (@fleetbridge/ui turns it into the
|
|
1735
|
+
* `fleetbridge:booking-requested` window event, so a theme that would
|
|
1736
|
+
* rather handle bookings itself can listen instead).
|
|
1737
|
+
*/
|
|
1738
|
+
async submitBooking(request) {
|
|
1739
|
+
notify(this.hooks.onBookingRequested, request);
|
|
1740
|
+
const result = await this.post(
|
|
1741
|
+
"booking_request",
|
|
1742
|
+
bookingRequestFields(request, this.hooks)
|
|
1743
|
+
);
|
|
1744
|
+
if (!result.success) {
|
|
1745
|
+
return { ok: false, error: result.error, bookingPin: result.data?.bookingPin };
|
|
1746
|
+
}
|
|
1747
|
+
if (result.data.status === "PAYMENT_PENDING" && result.data.paymentToken && result.data.reference) {
|
|
1748
|
+
return {
|
|
1749
|
+
ok: true,
|
|
1750
|
+
reference: result.data.reference,
|
|
1751
|
+
reservationId: "",
|
|
1752
|
+
bookingPin: result.data.bookingPin ?? "",
|
|
1753
|
+
supplier: result.data.supplier ?? request.car.supplier.name,
|
|
1754
|
+
payment: { token: result.data.paymentToken, redirectUrl: result.data.redirectUrl, error: result.data.paymentError }
|
|
1755
|
+
};
|
|
1756
|
+
}
|
|
1757
|
+
if (!result.data.reference || !result.data.reservationId) {
|
|
1758
|
+
return { ok: false, error: "Something went wrong confirming your booking. Please contact us before trying again." };
|
|
1759
|
+
}
|
|
1760
|
+
return {
|
|
1761
|
+
ok: true,
|
|
1762
|
+
reference: result.data.reference,
|
|
1763
|
+
reservationId: result.data.reservationId,
|
|
1764
|
+
bookingPin: result.data.bookingPin ?? "",
|
|
1765
|
+
supplier: result.data.supplier ?? request.car.supplier.name,
|
|
1766
|
+
customerId: result.data.customerId || void 0
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
/**
|
|
1770
|
+
* Posts a "Request a quote" - same fields as `submitBooking()` (it's the
|
|
1771
|
+
* same wizard, just ending here instead of at "Confirm booking"), but
|
|
1772
|
+
* WordPress records it as a Quote and emails it rather than calling the
|
|
1773
|
+
* supplier - see Tenant\Quote's own docblock. Calls `onQuoteRequested`
|
|
1774
|
+
* first (in @fleetbridge/ui, the `fleetbridge:quote-requested` window
|
|
1775
|
+
* event, the quote-mode counterpart of `fleetbridge:booking-requested`).
|
|
1776
|
+
*/
|
|
1777
|
+
async requestQuote(request) {
|
|
1778
|
+
notify(this.hooks.onQuoteRequested, request);
|
|
1779
|
+
const result = await this.post(
|
|
1780
|
+
"request_quote",
|
|
1781
|
+
bookingRequestFields(request, this.hooks)
|
|
1782
|
+
);
|
|
1783
|
+
if (!result.success) {
|
|
1784
|
+
return { ok: false, error: result.error };
|
|
1785
|
+
}
|
|
1786
|
+
return { ok: true, message: result.data.message };
|
|
1787
|
+
}
|
|
1788
|
+
/** The terms version this supplier's customers agree to now. Null when none is published, or when they could not be loaded (see supplierTermsResult()). */
|
|
1789
|
+
async supplierTerms(identifier) {
|
|
1790
|
+
const result = await this.supplierTermsResult(identifier);
|
|
1791
|
+
return result.ok ? result.terms : null;
|
|
1792
|
+
}
|
|
1793
|
+
/** Like supplierTerms(), but a failure comes back as `{ ok: false }` instead of looking like "no terms". */
|
|
1794
|
+
async supplierTermsResult(identifier) {
|
|
1795
|
+
const result = await this.post(
|
|
1796
|
+
"supplier_terms",
|
|
1797
|
+
{ identifier }
|
|
1798
|
+
);
|
|
1799
|
+
if (!result.success) return { ok: false, error: result.error, status: result.status };
|
|
1800
|
+
const terms = result.data.terms;
|
|
1801
|
+
if (!terms?.id || !terms.version || !terms.html) return { ok: true, terms: null };
|
|
1802
|
+
return { ok: true, terms: { id: terms.id, version: terms.version, html: terms.html } };
|
|
1803
|
+
}
|
|
1804
|
+
};
|
|
1805
|
+
function createBookingQueries(hooks = {}) {
|
|
1806
|
+
return new BookingQueries(hooks);
|
|
1807
|
+
}
|
|
1808
|
+
var bookingQueries = createBookingQueries();
|
|
1809
|
+
|
|
1810
|
+
// src/lib/queries/booking-management.queries.ts
|
|
1811
|
+
var BookingManagementQueries = class extends BaseQuery {
|
|
1812
|
+
/** `options.paymentReturnUrl` feeds amend()'s booking fields (see bookingRequestFields). */
|
|
1813
|
+
constructor(options = {}) {
|
|
1814
|
+
super();
|
|
1815
|
+
this.options = options;
|
|
1816
|
+
}
|
|
1817
|
+
async lookup(reservationNumber, bookingPin) {
|
|
1818
|
+
return this.post("lookup_booking", {
|
|
1819
|
+
reservationNumber: reservationNumber.trim(),
|
|
1820
|
+
booking_pin: bookingPin.trim()
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
async cancel(reservationNumber, bookingPin, reason) {
|
|
1824
|
+
return this.post("cancel_my_booking", {
|
|
1825
|
+
reservationNumber: reservationNumber.trim(),
|
|
1826
|
+
booking_pin: bookingPin.trim(),
|
|
1827
|
+
reason: reason.trim()
|
|
1828
|
+
});
|
|
1829
|
+
}
|
|
1830
|
+
/**
|
|
1831
|
+
* Load a pending quote for review. The email link calls this first; Book
|
|
1832
|
+
* on that screen then calls `confirmQuote()`.
|
|
1833
|
+
*/
|
|
1834
|
+
async lookupQuote(token) {
|
|
1835
|
+
return this.post("lookup_quote", { token: token.trim() });
|
|
1836
|
+
}
|
|
1837
|
+
/**
|
|
1838
|
+
* Turns a pending Quote into a real reservation after the customer has
|
|
1839
|
+
* reviewed it and clicked Book. The caller still needs its own `lookup()`
|
|
1840
|
+
* afterwards for the full `ManagedBooking` shape the rest of this drawer
|
|
1841
|
+
* expects.
|
|
1842
|
+
*/
|
|
1843
|
+
async confirmQuote(token) {
|
|
1844
|
+
return this.post("confirm_quote", { token: token.trim() });
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Re-search submit: same payload as a new booking, plus the reservation
|
|
1848
|
+
* number + PIN that prove ownership. If the supplier doesn't support
|
|
1849
|
+
* amending online (AdapterCapabilities.amendBooking), this fails with a
|
|
1850
|
+
* plain message rather than a structured code.
|
|
1851
|
+
*/
|
|
1852
|
+
async amend(reservationNumber, bookingPin, request) {
|
|
1853
|
+
const result = await this.post("amend_my_booking", {
|
|
1854
|
+
reservationNumber: reservationNumber.trim(),
|
|
1855
|
+
booking_pin: bookingPin.trim(),
|
|
1856
|
+
...bookingRequestFields(request, this.options)
|
|
1857
|
+
});
|
|
1858
|
+
if (!result.success) {
|
|
1859
|
+
return { ok: false, error: result.error };
|
|
1860
|
+
}
|
|
1861
|
+
return {
|
|
1862
|
+
ok: true,
|
|
1863
|
+
reference: "",
|
|
1864
|
+
reservationId: result.data.booking.supplierRef ?? reservationNumber,
|
|
1865
|
+
bookingPin,
|
|
1866
|
+
supplier: result.data.booking.supplierName ?? request.car.supplier.name,
|
|
1867
|
+
booking: result.data.booking
|
|
1868
|
+
};
|
|
1869
|
+
}
|
|
1870
|
+
};
|
|
1871
|
+
function createBookingManagementQueries(options = {}) {
|
|
1872
|
+
return new BookingManagementQueries(options);
|
|
1873
|
+
}
|
|
1874
|
+
var bookingManagementQueries = createBookingManagementQueries();
|
|
1875
|
+
|
|
1876
|
+
// src/lib/queries/payment.queries.ts
|
|
1877
|
+
var PAYMENT_RETURN_PARAM = "fb_payment";
|
|
1878
|
+
var PAYMENT_KIND_LABEL = {
|
|
1879
|
+
PAYSTACK: "Card or EFT with Paystack",
|
|
1880
|
+
STITCH: "Pay by bank with Stitch",
|
|
1881
|
+
PAYFAST: "Card or EFT with PayFast",
|
|
1882
|
+
PAY_AT_BRANCH: "Pay at the branch"
|
|
1883
|
+
};
|
|
1884
|
+
var PaymentQueries = class extends BaseQuery {
|
|
1885
|
+
async methods(branchCode) {
|
|
1886
|
+
if (!branchCode) return [];
|
|
1887
|
+
const result = await this.post("payment_methods", { branchCode });
|
|
1888
|
+
return result.success ? result.data.methods ?? [] : [];
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Opens the payment page again for a booking that waits for payment.
|
|
1892
|
+
* `returnUrl` is where the payment page sends the customer back to, with
|
|
1893
|
+
* `?fb_payment=<token>` (PAYMENT_RETURN_PARAM) added.
|
|
1894
|
+
*/
|
|
1895
|
+
async start(paymentToken, methodId, purpose, returnUrl) {
|
|
1896
|
+
const result = await this.post("start_payment", {
|
|
1897
|
+
paymentToken,
|
|
1898
|
+
paymentMethodId: methodId,
|
|
1899
|
+
paymentPurpose: purpose,
|
|
1900
|
+
returnUrl
|
|
1901
|
+
});
|
|
1902
|
+
if (!result.success || !result.data.redirectUrl) return { ok: false, error: result.success ? "We could not open the payment page. Please try again." : result.error };
|
|
1903
|
+
return { ok: true, redirectUrl: result.data.redirectUrl };
|
|
1904
|
+
}
|
|
1905
|
+
async verify(paymentToken) {
|
|
1906
|
+
const result = await this.post("verify_payment", { paymentToken });
|
|
1907
|
+
return result.success ? { ok: true, check: result.data } : { ok: false, error: result.error };
|
|
1908
|
+
}
|
|
1909
|
+
};
|
|
1910
|
+
var paymentQueries = new PaymentQueries();
|
|
1911
|
+
|
|
1912
|
+
// src/lib/queries/coupon.queries.ts
|
|
1913
|
+
var CouponQueries = class extends BaseQuery {
|
|
1914
|
+
/**
|
|
1915
|
+
* Claims the coupon with this code for the signed-in customer.
|
|
1916
|
+
* `claimType` says why they get it, free text (for example "appDownload").
|
|
1917
|
+
*/
|
|
1918
|
+
async claim(code, claimType) {
|
|
1919
|
+
const fields = { code: code.trim() };
|
|
1920
|
+
if (claimType) fields.claimType = claimType;
|
|
1921
|
+
const result = await this.post("claim_coupon", fields);
|
|
1922
|
+
if (!result.success) return { ok: false, error: result.error, code: result.data?.code, status: result.status };
|
|
1923
|
+
return { ok: true, coupon: result.data.coupon };
|
|
1924
|
+
}
|
|
1925
|
+
/** The signed-in customer's claimed coupons, newest first. */
|
|
1926
|
+
async mine() {
|
|
1927
|
+
const result = await this.post("my_coupons");
|
|
1928
|
+
if (!result.success) return { ok: false, error: result.error, status: result.status };
|
|
1929
|
+
return { ok: true, coupons: Array.isArray(result.data.coupons) ? result.data.coupons : [] };
|
|
1930
|
+
}
|
|
1931
|
+
};
|
|
1932
|
+
var couponQueries = new CouponQueries();
|
|
1933
|
+
var myCouponsQueryKey = ["coupons", "mine"];
|
|
1934
|
+
var time = (iso) => {
|
|
1935
|
+
if (!iso) return null;
|
|
1936
|
+
const ms = new Date(iso).getTime();
|
|
1937
|
+
return Number.isNaN(ms) ? null : ms;
|
|
1938
|
+
};
|
|
1939
|
+
function couponUsable(coupon, channel, now = /* @__PURE__ */ new Date()) {
|
|
1940
|
+
if (!coupon.isActive) return false;
|
|
1941
|
+
const at = now.getTime();
|
|
1942
|
+
const start = time(coupon.startDate);
|
|
1943
|
+
const end = time(coupon.endDate);
|
|
1944
|
+
if (start !== null && at < start) return false;
|
|
1945
|
+
if (end !== null && at > end) return false;
|
|
1946
|
+
if (coupon.usageLimit !== null && coupon.usageLimit !== void 0 && coupon.usageCount >= coupon.usageLimit) return false;
|
|
1947
|
+
if (coupon.remainingForCustomer === 0) return false;
|
|
1948
|
+
if (channel && coupon.channels.length > 0 && !coupon.channels.includes(channel)) return false;
|
|
1949
|
+
return true;
|
|
1950
|
+
}
|
|
1951
|
+
var CHANNEL_NAMES = { web: "Website", app: "App", whatsapp: "WhatsApp" };
|
|
1952
|
+
function channelsLabel(channels) {
|
|
1953
|
+
const names = [...new Set(channels)].map((channel) => CHANNEL_NAMES[channel] ?? channel);
|
|
1954
|
+
if (names.length === 0) return "Everywhere";
|
|
1955
|
+
if (names.length === 1) return `${names[0]} only`;
|
|
1956
|
+
const rest = names.slice(1).map((name) => name === "App" || name === "Website" ? name.toLowerCase() : name);
|
|
1957
|
+
return `${names[0]}${rest.length > 1 ? `, ${rest.slice(0, -1).join(", ")}` : ""} and ${rest[rest.length - 1]}`;
|
|
1958
|
+
}
|
|
1959
|
+
function discountLabel(coupon) {
|
|
1960
|
+
if (coupon.discountType === "PERCENTAGE") return `${Number(coupon.discountValue)}% off`;
|
|
1961
|
+
return `${formatZAR(Number(coupon.discountValue))} off`;
|
|
1962
|
+
}
|
|
1963
|
+
function rands(amount) {
|
|
1964
|
+
return Number.isInteger(amount) ? `R${amount.toLocaleString("en-ZA")}` : formatZAR(amount);
|
|
1965
|
+
}
|
|
1966
|
+
function couponLabel(coupon) {
|
|
1967
|
+
const value = Number(coupon.discountValue) || 0;
|
|
1968
|
+
const ownFleet = coupon.supplierIdentifier === "own-fleet";
|
|
1969
|
+
if (coupon.unlocksSpecialRate && !(ownFleet && value > 0)) {
|
|
1970
|
+
return coupon.supplierName ? `Special rate with ${coupon.supplierName}` : "Special rate";
|
|
1971
|
+
}
|
|
1972
|
+
if (coupon.discountType === "PERCENTAGE") return `${value}% off`;
|
|
1973
|
+
return `${rands(value)} off`;
|
|
1974
|
+
}
|
|
1975
|
+
export {
|
|
1976
|
+
BaseQuery,
|
|
1977
|
+
BookingManagementQueries,
|
|
1978
|
+
BookingQueries,
|
|
1979
|
+
BranchQueries,
|
|
1980
|
+
CHANNEL_HEADER,
|
|
1981
|
+
CONDITIONS,
|
|
1982
|
+
COUNTRIES,
|
|
1983
|
+
COUNTRY_GROUPS,
|
|
1984
|
+
COUPON_CHANNELS,
|
|
1985
|
+
CacheQueries,
|
|
1986
|
+
CouponQueries,
|
|
1987
|
+
DEFAULT_CRITERIA,
|
|
1988
|
+
DEFAULT_FILTERS,
|
|
1989
|
+
DEFAULT_PICKUP_LOCATION,
|
|
1990
|
+
DEFAULT_PRICE_BOUNDS,
|
|
1991
|
+
DEFAULT_SEAT_OPTIONS,
|
|
1992
|
+
DEFAULT_YEAR_BOUNDS,
|
|
1993
|
+
DRIVER_AGE_BANDS,
|
|
1994
|
+
EMPTY_DRIVER,
|
|
1995
|
+
FUEL_TYPES,
|
|
1996
|
+
PAYMENT_KIND_LABEL,
|
|
1997
|
+
PAYMENT_RETURN_PARAM,
|
|
1998
|
+
PUBLIC_ACTIONS,
|
|
1999
|
+
PaymentQueries,
|
|
2000
|
+
SAME_AS_PICKUP,
|
|
2001
|
+
SORT_OPTIONS,
|
|
2002
|
+
SUPPLIER_GROUPS,
|
|
2003
|
+
SourceQueries,
|
|
2004
|
+
TIME_SLOTS,
|
|
2005
|
+
TITLES,
|
|
2006
|
+
TRANSMISSIONS,
|
|
2007
|
+
activeChips,
|
|
2008
|
+
availabilityQueryKey,
|
|
2009
|
+
bluuDepositQueryKey,
|
|
2010
|
+
bookingManagementQueries,
|
|
2011
|
+
bookingQueries,
|
|
2012
|
+
bookingRequestFields,
|
|
2013
|
+
branchQueries,
|
|
2014
|
+
branchesQueryKey,
|
|
2015
|
+
cacheQueries,
|
|
2016
|
+
cacheVersionQueryKey,
|
|
2017
|
+
carDisplayName,
|
|
2018
|
+
channelsLabel,
|
|
2019
|
+
countryByName,
|
|
2020
|
+
couponLabel,
|
|
2021
|
+
couponQueries,
|
|
2022
|
+
couponUsable,
|
|
2023
|
+
createBookingManagementQueries,
|
|
2024
|
+
createBookingQueries,
|
|
2025
|
+
criteriaFromParams,
|
|
2026
|
+
criteriaToQuery,
|
|
2027
|
+
describeDriverErrors,
|
|
2028
|
+
discountLabel,
|
|
2029
|
+
displayName,
|
|
2030
|
+
estimationQueries,
|
|
2031
|
+
estimationQueryKey,
|
|
2032
|
+
extraUnitPrice,
|
|
2033
|
+
extrasQueries,
|
|
2034
|
+
extrasQueryKey,
|
|
2035
|
+
facetCounts,
|
|
2036
|
+
filterCars,
|
|
2037
|
+
filtersFromSearchParams,
|
|
2038
|
+
formatDateLabel,
|
|
2039
|
+
formatZAR,
|
|
2040
|
+
getTransport,
|
|
2041
|
+
hasValidTrip,
|
|
2042
|
+
httpTransport,
|
|
2043
|
+
isCouponChannel,
|
|
2044
|
+
isOffersResult,
|
|
2045
|
+
isOneWay,
|
|
2046
|
+
isPopularExtra,
|
|
2047
|
+
locationCounts,
|
|
2048
|
+
luggageOptionsFromCars,
|
|
2049
|
+
mapOffersToCars,
|
|
2050
|
+
mileageLabel,
|
|
2051
|
+
mileageOptionsFromCars,
|
|
2052
|
+
myCouponsQueryKey,
|
|
2053
|
+
normalizeServiceType,
|
|
2054
|
+
normalizeTransmission,
|
|
2055
|
+
oneWayFeeQueries,
|
|
2056
|
+
oneWayFeeQueryKey,
|
|
2057
|
+
packageKey,
|
|
2058
|
+
paymentQueries,
|
|
2059
|
+
pluralize,
|
|
2060
|
+
priceBoundsFromCars,
|
|
2061
|
+
priceHistogram,
|
|
2062
|
+
quoteFor,
|
|
2063
|
+
rentalDays,
|
|
2064
|
+
searchAvailability,
|
|
2065
|
+
seatOptionsFromCars,
|
|
2066
|
+
selectedExtras,
|
|
2067
|
+
setTransport,
|
|
2068
|
+
shortAction,
|
|
2069
|
+
sortCars,
|
|
2070
|
+
sourceNotices,
|
|
2071
|
+
sourceQueries,
|
|
2072
|
+
sourcesQueryKey,
|
|
2073
|
+
uniqueSorted,
|
|
2074
|
+
validateDriver,
|
|
2075
|
+
withPackage,
|
|
2076
|
+
wordpressTransport,
|
|
2077
|
+
yearBoundsFromCars
|
|
2078
|
+
};
|