Skip to content

feat(api): migrate data layer to SahajCloud PayloadCMS #10

Closed

Description

@Ardnived

Summary

Migrate the Atlas widget's data layer off the legacy Rails REST API (https://atlas.sydevelopers.com/api) and onto SahajCloud PayloadCMS (Payload v3, REST-only). The widget runs entirely in the browser as a public, third-party-embedded bundle, so we keep axios + zod (the zod boundary is a runtime-resilience feature; no Payload SDK, no new bundle deps), repoint at SahajCloud, rewrite the zod schemas to mirror the SahajCloud API, update consuming components to the new shapes, and compute the few derived values (eventCount, bounds, distance) client-side from the GeoJSON feed. payload-types are synced as supplementary compile-time types.

Note: there is no GraphQL in Atlas today — this is a REST → PayloadCMS-REST migration.

Depends on the SahajCloud backend epic: sydevs/SahajCloud#508 (region slug, GeoJSON feed, /register endpoint, reseeded data, exported response types). Develop against a local SahajCloud (http://localhost:3000/api) with a published dev sahaj-atlas-client key.

Proposed changes

Type-sync utility

  • Add a pnpm types:cms script (model: WeMeditateWeb package.json types:cms + scripts/strip-payload-augmentation.mjs) that curls SahajCloud's payload-types.ts + the geojson/register response types from GitHub raw into src/types/payload/, stripping the declare module 'payload' augmentation.
  • Use the synced types to type select/populate objects (satisfies) and to align the zod schemas.

Rewrite zod schemas + update components

  • Rewrite the schemas in src/types/ to mirror SahajCloud response shapes using internal field names (eventType, region, breadcrumbs, recurrenceType: DAILY|WEEKLY|MONTHLY, upcomingDates, registrationQuestions, images url/alt, etc.), cross-checked against the synced payload-types.
  • Update consuming components to the new fields: EventTime / EventItem / EventSoon / EventDetails, the event pages, EventPanel (description rendering), and Mapbox feature-property access in src/components/organisms/Mapbox/ (currently reads path / cluster props off features) — adapt to the GeoJSON feed's internal-name properties.

Axios layer (src/config/api/fetch.ts, src/config/api/auth.ts)

  • Base URL → SahajCloud /api.
  • Interceptor header Authorization: Bearer → clients API-Key <key>.
  • Build bracket-notation select/populate params (add qs or Payload's query stringify) — SahajCloud rejects client reads without select, and depth>1 without populate.
  • Map the locale param to SahajCloud locale codes.
  • getClient → GET /api/clients/me (API-key self-read).

Fetchers → raw reads + minimal derived shaping (keep query keys + zod .parse())

  • getGeojson → the custom GeoJSON endpoint (with select/populate).
  • getCountries / getCountry(slug) / getRegion / getArea / getVenue → raw /api/regions (where[level] + where[slug], breadcrumbs for path, a children query); derive eventCount + bounds from the GeoJSON feed via a new src/lib/geo.ts (@turf).
  • getEvents(lat, lng, onlineOnly) → pick nearest from the GeoJSON feed (add @turf/distance), hydrate details via raw /api/events?where[id][in]=….
  • getEvent(id) → raw /api/events/:id?depth=2&select&populate&locale; the response maps almost 1:1 to the rewritten EventSchema. Remaining client work: descriptionHtml via a minimal client-side Lexical serializer (italic / H3 / link only), rendered through the existing dompurify path in src/components/organisms/EventPanel/EventPanel.tsx.

Derived-only shaping module

  • src/lib/shape/ (pure, unit-tested): geojson→counts/bounds, breadcrumb→path, the small Lexical→HTML serializer. No recurrence engine — upcomingDates comes pre-computed from SahajCloud's virtual field.

Mutation (src/config/api/mutate.ts)

  • createRegistration → POST /api/events/:id/register.

Routing

  • Country route param ISO code → slug; entity identity via slug/id; update <Link> / useNavigate and pages/{country,region,area,venue,event}.tsx.

Tests

  • Update src/config/api/fetch.test.ts (header / paths / select params / shapes) and the schema-typed mocks in src/mocks/.
  • Add specs for src/lib/shape/ + src/lib/geo.ts.

Docs

  • Update CLAUDE.md (data-layer section), .claude/rules/data-layer.md, .claude/docs/environment.md, and STORYBOOK.md / DESIGN_SYSTEM.md where schema shapes are referenced.

Deps

  • Add qs (or Payload query helper) + @turf/distance. No SDK.

Acceptance criteria

  • pnpm types:cms syncs types into src/types/payload/; pnpm typecheck passes.
  • zod schemas mirror the SahajCloud API; consuming components updated; derived values (eventCount, bounds, distance) computed client-side from the GeoJSON feed.
  • Every fetcher hits SahajCloud (raw reads + geojson), sends a valid select/populate, parses through its zod schema, and keeps its existing query key.
  • Full hierarchy (country → region → area → venue → event), map clustering, events-near-point, event detail (timing / description / images), and in-app registration all work.
  • Auth via clients API-Key; routing by slug.
  • Atlas docs updated; pnpm lint, pnpm typecheck, pnpm test:run, pnpm build, pnpm ladle:build all pass; no new runtime SDK dependency.

Files affected

  • package.json, scripts/strip-payload-augmentation.mjs (new), src/types/payload/ (new, synced)
  • src/types/*.ts (schemas), src/config/api/{fetch,mutate,auth}.ts
  • src/lib/geo.ts (new), src/lib/shape/ (new)
  • src/components/molecules/{EventTime,EventItem,EventSoon}, src/components/organisms/{EventDetails,EventPanel,Mapbox}
  • src/pages/{country,region,area,venue,event}.tsx
  • src/config/api/fetch.test.ts, src/mocks/
  • .env, .env.local, CLAUDE.md, .claude/rules/data-layer.md, .claude/docs/environment.md

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions