mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
feat: Apple Wallet passes for bookings
"Add to Apple Wallet" on a booking in the iOS app turns it into a TREK pass: a boarding pass from and to for flights, trains, buses and boats, check-in and check-out for a stay, pickup and drop-off for a car, the time for anything else, with the confirmation code as a QR code. Wallet brings it up on the lock screen around its time and place: times carry the offset of the endpoint's zone (looked up from the coordinates where the endpoint has none), stays and restaurants their place's coordinates. The labels come from the booking forms' own strings, so nothing new to translate on the pass. Only liketrek holds the Pass Type ID certificate, so the server sends the pass's texts to the TREK services' signer (TREK_WALLET_PASS_URL, push.liketrek.com) once per tap; it stores nothing, and TREK_SERVICES=off turns it off with the other services. Texts are cleaned of links and shortened here, the answer must be a .pkpass within 512 KB, and a user gets 20 passes a minute. - New wallet-pass domain: GET /api/trips/:tripId/reservations/:id/wallet-pass behind the trip guard, the booking checked to belong to the trip; trip title and place come from the reservations domain (findPassContext) - MWalletButton in the transport and booking sheets, only where the app binary has the wallet feature; undated bookings and failures are told apart - TREK_WALLET_PASS_URL in .env.example, compose, Helm and the wiki
This commit is contained in:
@@ -9,7 +9,7 @@ vi.mock('./capabilities', () => ({ hasNativeFeature: (name: string) => name ===
|
||||
vi.mock('./trekShell', () => ({ TrekShell: shell }));
|
||||
vi.mock('./reportFailure', () => ({ reportNativeFailure: report }));
|
||||
|
||||
import { addToWallet, canAddToWallet, isWalletPassName } from './wallet';
|
||||
import { addReservationToWallet, addToWallet, canAddToWallet, isWalletPassName } from './wallet';
|
||||
|
||||
const pass = new Blob(['PK\u0003\u0004pass'], { type: 'application/vnd.apple.pkpass' });
|
||||
|
||||
@@ -62,3 +62,37 @@ describe('Apple Wallet', () => {
|
||||
expect(shell.addToWallet).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('a booking into Wallet', () => {
|
||||
const answer = (status: number) =>
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(status === 200 ? pass : '{}', { status }));
|
||||
|
||||
it('fetches the booking pass in the user language and hands it to Wallet', async () => {
|
||||
const fetchSpy = answer(200);
|
||||
shell.addToWallet.mockResolvedValue({ result: 'added' });
|
||||
expect(await addReservationToWallet(42, 7, 'de')).toBe('added');
|
||||
expect(fetchSpy).toHaveBeenCalledWith('/api/trips/42/reservations/7/wallet-pass?lang=de', {
|
||||
credentials: 'include',
|
||||
});
|
||||
fetchSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('tells an undated booking from a failure', async () => {
|
||||
let fetchSpy = answer(422);
|
||||
expect(await addReservationToWallet(42, 7, 'en')).toBe('undated');
|
||||
fetchSpy.mockRestore();
|
||||
fetchSpy = answer(502);
|
||||
expect(await addReservationToWallet(42, 7, 'en')).toBe('failed');
|
||||
fetchSpy.mockRestore();
|
||||
fetchSpy = vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('offline'));
|
||||
expect(await addReservationToWallet(42, 7, 'en')).toBe('failed');
|
||||
fetchSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('fails when Wallet does not take the pass', async () => {
|
||||
const fetchSpy = answer(200);
|
||||
shell.addToWallet.mockRejectedValue({ code: 'INVALID' });
|
||||
expect(await addReservationToWallet(42, 7, 'en')).toBe('failed');
|
||||
fetchSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -32,3 +32,30 @@ export async function addToWallet(blob: Blob): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** What a tap on "Add to Apple Wallet" came to. */
|
||||
export type ReservationWalletOutcome = 'added' | 'undated' | 'failed';
|
||||
|
||||
/**
|
||||
* A booking's pass, made by this server (GET .../reservations/:id/wallet-pass,
|
||||
* signed by the TREK services) and handed to Apple Wallet's sheet. `undated`:
|
||||
* the booking has no date yet, which Wallet needs to bring the pass up.
|
||||
*/
|
||||
export async function addReservationToWallet(
|
||||
tripId: number,
|
||||
reservationId: number,
|
||||
language: string
|
||||
): Promise<ReservationWalletOutcome> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(
|
||||
`/api/trips/${tripId}/reservations/${reservationId}/wallet-pass?lang=${encodeURIComponent(language)}`,
|
||||
{ credentials: 'include' }
|
||||
);
|
||||
} catch {
|
||||
return 'failed';
|
||||
}
|
||||
if (response.status === 422) return 'undated';
|
||||
if (!response.ok) return 'failed';
|
||||
return (await addToWallet(await response.blob())) ? 'added' : 'failed';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user