Expo stretch integration

You build app/lib/stretch.ts and update one Expo screen to show inventory availability or simulate payment through the existing API client. The helpers must preserve server payment statuses and recognise only a 409 sold_out error.

Ticket

Build app/lib/stretch.ts as the boundary between your stretch feature and the Expo screens. It provides stock display state and sold-out error recognition for inventory, and payment requests and display state for payments. The tests cover all four helpers.

The item and order screens should use these helpers instead of implementing stretch-specific business logic themselves. Then update one screen, for whichever stretch you want to show in the app. The next exercise, Final pass: tests, lint, govulncheck and a k6 numbers table, verifies the completed capstone.

The tests call these exported functions directly:

typescript
import type { ApiClient } from './api';

export function stockLabel(onHand: number | null | undefined): {
  text: string | null;
  disabled: boolean;
};

export function isSoldOutError(err: unknown): boolean;

export function payOrder(
  api: ApiClient,
  orderId: number,
  outcome: 'success' | 'fail'
): Promise<{ status: string }>;

export function payScreenState(status: string): {
  showPay: boolean;
  title: string;
};

Acceptance criteria

  • stockLabel(null) and stockLabel(undefined) both return { text: null, disabled: false }.
  • stockLabel(6) returns { text: null, disabled: false }.
  • stockLabel(5) returns { text: 'Only 5 left', disabled: false }, and stockLabel(1) returns { text: 'Only 1 left', disabled: false }.
  • stockLabel(0) returns { text: 'Sold out', disabled: true }.
  • isSoldOutError returns true for an ApiError with status 409 and response body { code: 'sold_out' }.
  • isSoldOutError returns false for an ApiError with status 409 and code in_flight, an ApiError with status 500 and code sold_out, a network TypeError, and null.
  • payOrder(api, 42, 'success') sends a POST request to http://api/v1/payments/42/simulate with body { outcome: 'success' } and the Authorization header Bearer A.
  • When the server responds with { status: 'CANCELLED' }, payOrder resolves with { status: 'CANCELLED' }.
  • When the payment request responds with status 404 and body { error: 'unknown order' }, payOrder rejects with an ApiError.
  • payScreenState('PENDING_PAYMENT') returns { showPay: true, title: 'Pay now' }.
  • payScreenState('PLACED') returns { showPay: false, title: 'Payment received' }.
  • payScreenState('CANCELLED') returns { showPay: false, title: 'Payment failed, order cancelled' }.
  • payScreenState('PREPARING') returns { showPay: false, title: 'Order in progress' }.

Constraints

  • Keep the tested helpers in app/lib/stretch.ts, which the tests import as @app/stretch.
  • Reuse the existing ApiClient and ApiError from app/lib/api.ts. The payment request must use the existing authenticated client.
  • Keep the implementation compatible with the strict TypeScript configuration.
  • Do not modify the provided tests.

Design direction

Keep the four helper responsibilities in app/lib/stretch.ts. Let each function convert application data into a small result that a screen can render or act on.

Choose which stretch to show in the app:

  • For inventory, use stockLabel beneath the item name and use its disabled value for the Add to cart action. Use isSoldOutError when order placement fails.
  • For payments, derive the order screen's title and payment-button visibility from payScreenState. Use payOrder for both simulated outcomes, then refresh the order query after a successful request.

Keep the screen responsible for rendering and user interaction. Keep HTTP error inspection, payment request construction and status mapping in the library. The inventory label is only a customer-facing indication: the backend still decides whether stock can be reserved. For payments, refresh from the backend instead of assuming that a successful request means the order is already PLACED.

Any layout that meets the contract passes. Keep this change limited to one screen. You do not need to build another stretch screen or repeat the backend workflow in the app.

How to check your work

Run the app helper tests:

bash
cd backend
make test W=8B3

Then check TypeScript compilation:

bash
cd app
npx tsc --noEmit

Read the first failing assertion to identify which helper contract is broken before changing the implementation.

After the automated checks pass, manually verify only your selected integration:

  • Inventory: Open an item with stock information. Confirm that app/app/item/[id].tsx displays the stock label below the item name, disables Add to cart when disabled is true, and displays Sold out, remove it from your cart when order placement returns a 409 sold_out response.
  • Payments: Open an order in PENDING_PAYMENT. Confirm that app/app/orders/[id].tsx displays Pay (success) and Pay (fail). Confirm that each button calls payOrder with its corresponding outcome and refreshes the order query after success. Neither button should appear for other order statuses.
Finished Exercise 8.5?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. make test W=8B3 passes all 13 stretch helper tests.
  2. TypeScript compilation succeeds with npx tsc --noEmit.
  3. Stock labels show the correct availability state.
  4. Sold-out detection rejects unrelated errors.
  5. Payment requests and order screen states match the server contract.
  6. The selected Expo screen integration works.

Commands

terminal
cd backend && make test W=8B3
terminal
cd app && npx tsc --noEmit

Tests

  • W8 B3 stretch screens (13 tests) in app-tests/lib/stretch.test.ts