manicanldes-backend/docs/seasonal-campaigns-api.md

5.4 KiB
Raw Permalink Blame History

Seasonal campaigns (Phase 4C)

Seasonal campaigns promote existing catalog collections during an explicit time window. They contain public title/summary copy and a collection link. Collection membership remains live; checkout prices and coupon eligibility continue to use the existing commerce rules.

Method Path under /api/v1 Access
GET /campaigns campaigns.read; optional persisted state filter
GET /campaigns/:id campaigns.read; scoped campaign and live status
POST /campaigns campaigns.manage; create draft with a reusable UUID idempotency key
PATCH /campaigns/:id campaigns.manage; edit draft with its current version
POST /campaigns/:id/publication campaigns.manage; publish/schedule the specified draft version
POST /campaigns/:id/cancellation campaigns.manage; cancel with a reason
GET /storefront/:organizationId/campaigns Public; active campaigns only
GET /storefront/:organizationId/campaigns/:slug Public; active campaign by slug

Lists use the existing limit (1–100, default 25) and offset (0–10,000, default 0) contract. Staff resources are organization-scoped. Public routes share the existing storefront rate limit.

Create body: slug (existing collection slug format, at most 100 characters), title (1–160), summary (0–500; default empty), collectionId (UUID), startsAt, endsAt, and idempotencyKey (UUID). Times must be ISO timestamps with a timezone/UTC offset and are normalized to UTC milliseconds; end must be after start at that precision. Unknown fields and HTML/control characters in copy are rejected.

PATCH takes the current integer version plus one or more of title, summary, collectionId, startsAt, or endsAt. Slug and original creation identity are fixed. A stale version returns 409; callers should refetch after an ambiguous timeout before deciding whether to retry. A patch that changes no values leaves version and audit history unchanged. Actual changes increment version.

Persisted state is DRAFT, PUBLISHED, or CANCELLED. Staff responses also include computed status: DRAFT, SCHEDULED, ACTIVE, ENDED, or CANCELLED. Status describes the lifecycle/time window; public visibility additionally requires an eligible collection. Active visibility uses startsAt <= now < endsAt; it requires no scheduler, worker, or cron job. Publication requires an unexpired end time and a collection in the caller's organization containing at least one published product with an active variant.

Publication body is { "version": 0 }, using the draft version just read. A stale version returns 409. Published campaign terms cannot be edited. Repeating the original publication version returns its current representation without another audit event; a different version conflicts. Cancelled campaigns cannot be published. Cancellation body is { "reason": "Campaign withdrawn" } (3–500 characters). Cancellation applies to drafts and published campaigns; repeating the same reason returns the current record, while a conflicting reason returns 409. Cancellation metadata is immutable. No hard-delete endpoint exists.

Public responses include only campaign ID, slug, title, summary, UTC start/end, and collection ID/name/slug. Drafts, future, expired, and cancelled campaigns are absent. Changing a linked group to a category or removing all eligible products also hides the campaign. An inactive or unknown public slug returns 404. Public collection links can use the existing storefront product list with groupId.

Creation keys are organization-scoped and retained with the campaign. Identical retries return the current representation; different original content using the same key returns 409. Unique slugs are organization-scoped. All actual writes record audit events within the existing transaction/organization lock. Inputs derive organization and staff identity from the bearer session.

Acceptance: private drafts; exact start/end boundaries; versioned edits; immutable published terms; repeatable publication/cancellation; scoped permissions/collections; dynamic collection suppression; public-field allowlist; preserved checkout behavior. Verify with pnpm check and pnpm db:test, plus focused Jest campaign tests. Implementation lives in src/campaigns, tests in test, and the additive migration in prisma/migrations.

Deploy the migration before the application. It adds one campaign table, scoped foreign keys, unique/index/time constraints, and lifecycle guards, and grants campaigns.read/campaigns.manage to existing system Owner roles. Custom roles require explicit grants. Rollback can redeploy the previous application while retaining the new table and audit history. This milestone provides collection discovery; campaign discounts, media assets, outbound messaging, and channel integrations are separate work.