@fleetbridge/ui 1.1.0 → 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 CHANGED
@@ -5,7 +5,7 @@ FleetBridge's customer UI: the search form, the results page (filters, car cards
5
5
  - **WordPress**: the FleetBridge plugin's shortcodes mount these components with `wordpressTransport` (admin-ajax).
6
6
  - **Next.js, Remix, Express, any custom code**: mount the same components with `httpTransport`, and serve the actions from your own route with `createPublicHandler` from `@fleetbridge/ui/server`, which runs them on [`@fleetbridge/sdk`](../../sdks/ts).
7
7
 
8
- It's built on `@fleetbridge/components` (shadcn primitives), which is bundled in.
8
+ It's built on `@fleetbridge/components` (shadcn primitives) and [`@fleetbridge/client`](../client) (the booking logic: transport, search, offers, extras, booking and payment calls), and both are bundled in. A native app, or any code without React, uses `@fleetbridge/client` on its own.
9
9
 
10
10
  ## Install
11
11
 
@@ -104,6 +104,45 @@ export default function SearchPage() {
104
104
 
105
105
  (`httpTransport` returns a plain object, so create it inside a client component if you prefer not to pass it across the server/client boundary.)
106
106
 
107
+ **3. Coupon channels, claims and per-customer limits (optional)**
108
+
109
+ ```ts
110
+ export const POST = createPublicHandler({
111
+ connector, suppliers, siteUrl,
112
+ // Where the request comes from: 'web' | 'app' | 'whatsapp' | null. Sent to the core as
113
+ // trip.channel on searches, bookings, amendments and confirmed quotes. Default: the
114
+ // x-fleetbridge-channel header when it names a known channel, else null (no channel rule).
115
+ getChannel: (request) => (isMyApp(request) ? 'app' : 'web'),
116
+ // The signed-in customer from your own login, or null. Switches on claim_coupon and my_coupons.
117
+ // authUserId is your login's user id (for example Clerk's userId): coupon uses are counted per person by it.
118
+ getCustomer: async (request) => {
119
+ const user = await verifySession(request);
120
+ return user ? { authUserId: user.id, email: user.email, firstName: user.firstName, lastName: user.lastName } : null;
121
+ },
122
+ hooks: { onCouponClaimed: async ({ coupon, customer, channel, claimType }) => { /* analytics */ } },
123
+ });
124
+ ```
125
+
126
+ - `claim_coupon` (fields `code`, `claimType?`) runs `promotions.coupons.claim` with the channel (`web` when getChannel gives none) and the customer (`email`, plus `authUserId` when getCustomer gives one), and answers `{ coupon: AppCoupon }`. Refusals: 401 not signed in, 403 wrong channel, 404 unknown code, 409 already claimed, 410 ended or used up; `data.code` carries the core's code. About 10 an hour per visitor.
127
+ - `my_coupons` (no fields) runs `promotions.coupons.claims` with `{ email, authUserId? }` (the core looks up by login id first, then email) and answers `{ coupons: AppCoupon[] }`, each with this customer's uses (`usesByCustomer`, `perCustomerLimit`, `remainingForCustomer`), `requireClaim`, the supplier (`supplierIdentifier`, `supplierName`) and `unlocksSpecialRate`. About 60 an hour.
128
+ - Without `getCustomer` both answer 404 "Unknown action.".
129
+ - When getCustomer knows the customer, `search_availability` (with a promo code) sends `trip.customer: { authUserId, email }`, and `booking_request`, `confirm_quote` and `amend_my_booking` send `customer.authUserId` (the email is the form's). A guest's earlier uses (same email) count once they sign in. A search whose coupon this customer cannot use any more answers `couponNotice: { code, message, couponCode }` (also on its 404 "no cars" answer); show `message`.
130
+ - Coupon refusals of search, booking and amend show the core's message with the core's status (`CUSTOMER_ERRORS`): `COUPON_WRONG_CHANNEL` 422, `COUPON_EXPIRED` 422, `COUPON_REQUIRED` 422 (a special rate booked without its coupon), `COUPON_LIMIT_REACHED` 409, `COUPON_CUSTOMER_LIMIT` 409 ("You've already used this coupon."), `COUPON_NOT_CLAIMED` 403. Two bookings at once for the last use: the loser gets `COUPON_LIMIT_REACHED`, or `UNAVAILABLE` 409 "…Please try again."
131
+ - These actions need the site's tenant tables at version 23 (CouponRedemptions, Customers.authUserId, Coupons.perCustomerLimit / requireClaim): search, booking and amend too. Set `FLEETBRIDGE_SCHEMA_LEVEL=23` after migrating.
132
+
133
+ **4. Cache version for apps (optional stamp)**
134
+
135
+ `cache_version` (no fields) answers `{ version, generatedAt }`. `version` is a 16-character hash of everything an app keeps between launches: the live branches (count, newest `updatedAt`, and the names, regions, addresses, airport and international flags `public_branches` lists), the suppliers from `suppliers()` (id, identifier, name, logo, enabled, and whether a login is set, never the login itself) and an optional stamp from the site. Apps compare it with the one they stored and reload locations and suppliers when it differs. It is worked out at most every 30 seconds per server (`cacheVersionTtlSeconds`), and allows about 600 calls an hour per visitor.
136
+
137
+ ```ts
138
+ export const POST = createPublicHandler({
139
+ connector, suppliers, siteUrl,
140
+ // A value staff can change to make every app reload (for example a "Clear app caches"
141
+ // button that saves the current time). Null, or a failure, counts as no stamp.
142
+ extraCacheStamp: async () => (await readSetting('appCacheStamp')) ?? null,
143
+ });
144
+ ```
145
+
107
146
  ## WordPress
108
147
 
109
148
  The plugin's public bundle (`packages/wordpress/src/public-main.tsx`) mounts each shortcode like this:
@@ -124,11 +163,11 @@ const transport = wordpressTransport({ ajaxUrl: el.dataset.ajaxUrl, nonce: el.da
124
163
  interface FleetBridgeTransport {
125
164
  post<T>(action: string, fields?: Record<string, string>): Promise<QueryResult<T>>;
126
165
  }
127
- type QueryResult<T> = { success: true; data: T } | { success: false; error: string; data?: Partial<T> };
166
+ type QueryResult<T> = { success: true; data: T } | { success: false; error: string; status?: number; data?: Partial<T> };
128
167
  ```
129
168
 
130
169
  - `wordpressTransport({ ajaxUrl, nonce })`: FormData `action=fleetbridge_<name>`, `nonce`, fields → admin-ajax.php.
131
- - `httpTransport({ endpoint, headers?, credentials? })`: FormData `action=<name>`, fields → your endpoint.
170
+ - `httpTransport({ endpoint, headers?, credentials?, channel?, timeoutMs? })`: FormData `action=<name>`, fields → your endpoint. `channel` is sent as `x-fleetbridge-channel`; `timeoutMs` is a number or `(action) => number`.
132
171
 
133
172
  Both read the `{ success, data }` envelope (`data.message` on failure) and give the same customer-facing copy for a 429 ("Too many requests…") or an unreachable site. Write your own transport for anything else (an RPC layer, tests): any object with `post` works. Outside React, call `setTransport(transport)`.
134
173
 
@@ -140,6 +179,7 @@ Both read the `{ success, data }` envelope (`data.message` on failure) and give
140
179
  |---|---|
141
180
  | `public_branches` | search form: pick-up/return places |
142
181
  | `search_sources` | results: which sources to ask (each is then searched on its own) |
182
+ | `cache_version` | apps: has the site's branch or supplier list changed since I cached it? |
143
183
  | `search_availability` | results: cars for one source, or all at once |
144
184
  | `get_estimation` | car card / wizard: live quote (deposit, excess…) |
145
185
  | `get_extras` | wizard: add-ons |
@@ -150,6 +190,7 @@ Both read the `{ success, data }` envelope (`data.message` on failure) and give
150
190
  | `lookup_quote`, `confirm_quote` | manage booking: quotes |
151
191
  | `lookup_booking`, `cancel_my_booking`, `amend_my_booking` | manage booking |
152
192
  | `payment_methods`, `start_payment`, `verify_payment` | wizard + PaymentReturn: pay online |
193
+ | `claim_coupon`, `my_coupons` | apps and account pages: a signed-in customer's coupons (needs `getCustomer`) |
153
194
 
154
195
  `PUBLIC_ACTIONS` exports this list.
155
196