KEEP EXPLORING
Follow your curiosity.
- {getRelatedFood(entry).map((related) => (
+ {(related ?? getRelatedFood(entry)).map((related) => (
{related.title}
diff --git a/src/db/migrations/0118_food_encyclopedia_editorial.sql b/src/db/migrations/0118_food_encyclopedia_editorial.sql
new file mode 100644
index 00000000..0659d893
--- /dev/null
+++ b/src/db/migrations/0118_food_encyclopedia_editorial.sql
@@ -0,0 +1,573 @@
+-- 0118 Food Encyclopedia editorial foundation. Additive / app+worker direct access denied.
+-- IMPORTANT: #366 reserves 0115/0116; reconcile migration journal after that PR BEFORE merge.
+-- Public data remains empty until a separately approved human editorial review.
+CREATE TABLE food_entries (
+ id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
+ kind text NOT NULL CHECK (kind IN ('dishes','ingredients','cuisines','terms','compare')),
+ slug text NOT NULL CHECK (slug ~ '^[a-z0-9]+(-[a-z0-9]+)*$' AND length(slug)<=90),
+ status text NOT NULL DEFAULT 'draft'
+ CHECK (status IN ('draft','in_review','approved','published','archived')),
+ version integer NOT NULL DEFAULT 0 CHECK (version >= 0),
+ draft_revision_id text,
+ review_revision_id text,
+ approved_revision_id text,
+ published_revision_id text,
+ published_at timestamptz,
+ search_text text NOT NULL DEFAULT '',
+ created_by text NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
+ updated_by text NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ CONSTRAINT food_entries_kind_slug_key UNIQUE(kind,slug),
+ CONSTRAINT food_entries_published_pointer_check
+ CHECK ((published_revision_id IS NULL) = (published_at IS NULL))
+);
+CREATE INDEX food_entries_published_at_idx ON food_entries(published_at DESC,id)
+ WHERE published_revision_id IS NOT NULL;
+CREATE INDEX food_entries_search_idx ON food_entries
+ USING gin(to_tsvector('simple',search_text));
+
+CREATE TABLE food_revisions (
+ id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
+ entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE RESTRICT,
+ version integer NOT NULL CHECK (version > 0),
+ content jsonb NOT NULL CHECK (jsonb_typeof(content)='object'),
+ digest text NOT NULL CHECK (digest ~ '^[0-9a-f]{64}$'),
+ author_id text NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
+ approved boolean NOT NULL DEFAULT false,
+ reviewed_by text REFERENCES users(id) ON DELETE RESTRICT,
+ reviewed_at timestamptz,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ CONSTRAINT food_revisions_entry_version_key UNIQUE(entry_id,version),
+ CONSTRAINT food_revisions_entry_id_key UNIQUE(entry_id,id),
+ CONSTRAINT food_revisions_approval_check CHECK
+ ((approved=false AND reviewed_by IS NULL AND reviewed_at IS NULL) OR
+ (approved=true AND reviewed_by IS NOT NULL AND reviewed_at IS NOT NULL AND reviewed_by<>author_id))
+);
+ALTER TABLE food_entries
+ ADD CONSTRAINT food_entries_draft_revision_fk
+ FOREIGN KEY(id,draft_revision_id) REFERENCES food_revisions(entry_id,id),
+ ADD CONSTRAINT food_entries_review_revision_fk
+ FOREIGN KEY(id,review_revision_id) REFERENCES food_revisions(entry_id,id),
+ ADD CONSTRAINT food_entries_approved_revision_fk
+ FOREIGN KEY(id,approved_revision_id) REFERENCES food_revisions(entry_id,id),
+ ADD CONSTRAINT food_entries_published_revision_fk
+ FOREIGN KEY(id,published_revision_id) REFERENCES food_revisions(entry_id,id);
+
+CREATE TABLE food_sources (
+ id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
+ url text NOT NULL UNIQUE CHECK (length(url) BETWEEN 10 AND 1000 AND url ~ '^https://'),
+ title text NOT NULL DEFAULT '' CHECK (length(title)<=240),
+ publisher text NOT NULL DEFAULT '' CHECK (length(publisher)<=140),
+ retrieved_at timestamptz,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp()
+);
+CREATE TABLE food_entry_sources (
+ revision_id text NOT NULL REFERENCES food_revisions(id) ON DELETE RESTRICT,
+ source_id text NOT NULL REFERENCES food_sources(id) ON DELETE RESTRICT,
+ section_id text NOT NULL DEFAULT '' CHECK (length(section_id)<=90),
+ PRIMARY KEY(revision_id,source_id,section_id)
+);
+CREATE TABLE food_aliases (
+ entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE CASCADE,
+ alias text NOT NULL CHECK(length(alias) BETWEEN 2 AND 90),
+ normalized text NOT NULL CHECK(length(normalized) BETWEEN 2 AND 90),
+ PRIMARY KEY(entry_id,normalized)
+);
+CREATE INDEX food_aliases_normalized_idx ON food_aliases(normalized);
+CREATE TABLE food_relations (
+ entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE CASCADE,
+ related_entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE CASCADE,
+ relation text NOT NULL DEFAULT 'related' CHECK (relation IN ('related','ingredient','cuisine','comparison')),
+ PRIMARY KEY(entry_id,related_entry_id,relation),
+ CONSTRAINT food_relations_not_self CHECK(entry_id<>related_entry_id)
+);
+CREATE TABLE food_media (
+ id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
+ entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE CASCADE,
+ path text NOT NULL CHECK (path ~ '^/assets/food/dishes/[a-zA-Z0-9/_-]+[.](png|webp|avif)$'),
+ alt_text text NOT NULL CHECK (length(alt_text) BETWEEN 15 AND 200),
+ license text NOT NULL DEFAULT 'pending' CHECK (length(license)<=100),
+ attribution text NOT NULL DEFAULT '' CHECK (length(attribution)<=240),
+ rights_approved boolean NOT NULL DEFAULT false,
+ verified_by text REFERENCES users(id) ON DELETE RESTRICT,
+ verified_at timestamptz,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ CONSTRAINT food_media_rights_check CHECK(
+ (rights_approved=false AND verified_by IS NULL AND verified_at IS NULL) OR
+ (rights_approved=true AND verified_by IS NOT NULL AND verified_at IS NOT NULL)
+ )
+);
+CREATE TABLE food_editorial_events (
+ id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
+ entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE RESTRICT,
+ revision_id text REFERENCES food_revisions(id) ON DELETE RESTRICT,
+ action text NOT NULL CHECK (action IN ('create','save','submit','approve','publish','unpublish','restore','import')),
+ actor_id text NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
+ details jsonb NOT NULL DEFAULT '{}'::jsonb,
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp()
+);
+CREATE INDEX food_editorial_events_entry_idx ON food_editorial_events(entry_id,created_at DESC);
+CREATE TABLE food_redirects (
+ kind text NOT NULL CHECK (kind IN ('dishes','ingredients','cuisines','terms','compare')),
+ old_slug text NOT NULL CHECK (old_slug ~ '^[a-z0-9]+(-[a-z0-9]+)*$'),
+ entry_id text NOT NULL REFERENCES food_entries(id) ON DELETE RESTRICT,
+ PRIMARY KEY(kind,old_slug)
+);
+
+-- Migration role is the only direct-reader/writer. Definer gateway functions (appended in
+-- the next commit) whitelist public projection and demand live operator+step-up for edits.
+DO $block$ DECLARE t text; BEGIN
+ FOREACH t IN ARRAY ARRAY[
+ 'food_entries','food_revisions','food_sources','food_entry_sources',
+ 'food_aliases','food_relations','food_media','food_editorial_events','food_redirects'
+ ] LOOP
+ EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY',t);
+ EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY',t);
+ EXECUTE format('REVOKE ALL ON %I FROM PUBLIC,menuminute_app,menuminute_worker',t);
+ EXECUTE format('CREATE POLICY food_owner_only ON %I FOR ALL TO %I USING (true) WITH CHECK (true)',t,current_user);
+ END LOOP;
+END $block$;
+
+-- Approved media identity is entry+path, never a generic merchant upload.
+CREATE UNIQUE INDEX food_media_entry_path_key ON food_media(entry_id,path);
+
+-- The whitelisted public projection never includes draft notes, editor history,
+-- candidate imports, privileged event metadata or unreviewed revisions.
+CREATE FUNCTION app_private.food_public_payload(p_entry food_entries,p_revision food_revisions)
+RETURNS jsonb LANGUAGE sql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $payload$
+ SELECT jsonb_strip_nulls(jsonb_build_object(
+ 'id',p_entry.id,'kind',p_entry.kind,'slug',p_entry.slug,
+ 'title',p_revision.content->>'title',
+ 'eyebrow',p_revision.content->>'eyebrow',
+ 'description',p_revision.content->>'description',
+ 'tint',p_revision.content->>'tint',
+ 'imagePath',p_revision.content->>'imagePath',
+ 'imageAlt',p_revision.content->>'imageAlt',
+ 'aliases',coalesce(p_revision.content->'aliases','[]'::jsonb),
+ 'facts',coalesce(p_revision.content->'facts','[]'::jsonb),
+ 'sections',coalesce(p_revision.content->'sections','[]'::jsonb),
+ 'relatedSlugs',coalesce(p_revision.content->'relatedSlugs','[]'::jsonb),
+ 'caution',p_revision.content->>'caution',
+ 'sourceUrls',coalesce(p_revision.content->'sourceUrls','[]'::jsonb),
+ 'status','published',
+ 'reviewedBy',coalesce((SELECT u.name FROM users u WHERE u.id=p_revision.reviewed_by),'MenuMinute Editorial Team'),
+ 'reviewedAt',to_char(p_revision.reviewed_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'),
+ 'publishedAt',to_char(p_entry.published_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')
+ ));
+$payload$;
+REVOKE ALL ON FUNCTION app_private.food_public_payload(food_entries,food_revisions)
+ FROM PUBLIC,menuminute_app,menuminute_worker;
+
+CREATE FUNCTION app_private.food_public_entry(p_kind text,p_slug text)
+RETURNS jsonb LANGUAGE sql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $entry$
+ SELECT app_private.food_public_payload(e,r)
+ FROM food_entries e JOIN food_revisions r
+ ON r.entry_id=e.id AND r.id=e.published_revision_id
+ WHERE e.kind=p_kind AND e.slug=p_slug
+ AND e.published_at IS NOT NULL AND r.approved
+ AND r.reviewed_at IS NOT NULL AND r.reviewed_by IS NOT NULL
+ LIMIT 1;
+$entry$;
+REVOKE ALL ON FUNCTION app_private.food_public_entry(text,text) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_public_entry(text,text) TO menuminute_app;
+
+-- Bounded search across reviewed publication only; drafts and archived revisions never leak.
+CREATE FUNCTION app_private.food_public_list(p_kind text,p_query text,p_limit integer,p_offset integer)
+RETURNS TABLE(entry jsonb) LANGUAGE sql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $list$
+ SELECT app_private.food_public_payload(e,r) AS entry
+ FROM food_entries e JOIN food_revisions r
+ ON r.entry_id=e.id AND r.id=e.published_revision_id
+ WHERE e.published_at IS NOT NULL AND r.approved
+ AND r.reviewed_by IS NOT NULL AND r.reviewed_at IS NOT NULL
+ AND (p_kind IS NULL OR p_kind='' OR e.kind=p_kind)
+ AND (p_query IS NULL OR btrim(p_query)='' OR
+ to_tsvector('simple',e.search_text) @@ plainto_tsquery('simple',left(p_query,100))
+ OR strpos(lower(e.search_text),lower(left(p_query,100)))>0
+ )
+ ORDER BY e.published_at DESC,e.id
+ LIMIT GREATEST(0,LEAST(coalesce(p_limit,30),100))
+ OFFSET GREATEST(0,LEAST(coalesce(p_offset,0),50000));
+$list$;
+REVOKE ALL ON FUNCTION app_private.food_public_list(text,text,integer,integer) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_public_list(text,text,integer,integer) TO menuminute_app;
+
+
+-- Approved, public-only sitemaps must remain complete as the encyclopedia scales.
+CREATE FUNCTION app_private.food_public_sitemap(p_limit integer)
+RETURNS TABLE(kind text,slug text,reviewed_at timestamptz)
+LANGUAGE sql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $sitemap$
+ SELECT e.kind,e.slug,r.reviewed_at FROM food_entries e
+ JOIN food_revisions r ON r.entry_id=e.id AND r.id=e.published_revision_id
+ WHERE e.published_revision_id IS NOT NULL AND e.published_at IS NOT NULL
+ AND r.approved AND r.reviewed_by IS NOT NULL AND r.reviewed_at IS NOT NULL
+ ORDER BY e.published_at DESC,e.id
+ LIMIT GREATEST(0,LEAST(coalesce(p_limit,45000),45000));
+$sitemap$;
+REVOKE ALL ON FUNCTION app_private.food_public_sitemap(integer) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_public_sitemap(integer) TO menuminute_app;
+
+CREATE FUNCTION app_private.food_public_redirect(p_kind text,p_slug text)
+RETURNS text LANGUAGE sql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $redirect$
+ SELECT e.slug FROM food_redirects d JOIN food_entries e ON e.id=d.entry_id
+ WHERE d.kind=p_kind AND d.old_slug=p_slug AND e.published_revision_id IS NOT NULL
+ LIMIT 1;
+$redirect$;
+REVOKE ALL ON FUNCTION app_private.food_public_redirect(text,text) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_public_redirect(text,text) TO menuminute_app;
+
+CREATE FUNCTION app_private.food_editorial_list(p_query text,p_status text,p_limit integer)
+RETURNS TABLE(item jsonb) LANGUAGE plpgsql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $list$
+BEGIN
+ IF NOT app_private.is_super_admin(NULLIF(current_setting('app.actor_id',true),''))
+ THEN RAISE EXCEPTION 'Unauthorized editorial access' USING ERRCODE='42501'; END IF;
+ RETURN QUERY
+ SELECT jsonb_build_object(
+ 'id',e.id,'kind',e.kind,'slug',e.slug,
+ 'title',coalesce(r.content->>'title',e.slug),'status',e.status,
+ 'version',e.version,'createdAt',e.created_at,'updatedAt',e.updated_at,
+ 'hasPublishedVersion',e.published_revision_id IS NOT NULL,'publishedAt',e.published_at
+ )
+ FROM food_entries e
+ LEFT JOIN food_revisions r ON r.id=coalesce(e.draft_revision_id,e.published_revision_id)
+ WHERE (p_status IS NULL OR p_status='' OR e.status=p_status)
+ AND (p_query IS NULL OR btrim(p_query)='' OR e.slug ILIKE '%'||left(p_query,100)||'%'
+ OR r.content->>'title' ILIKE '%'||left(p_query,100)||'%')
+ ORDER BY e.updated_at DESC,e.id
+ LIMIT GREATEST(1,LEAST(coalesce(p_limit,80),100));
+END;
+$list$;
+REVOKE ALL ON FUNCTION app_private.food_editorial_list(text,text,integer) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_editorial_list(text,text,integer) TO menuminute_app;
+
+CREATE FUNCTION app_private.food_editorial_detail(p_id text)
+RETURNS jsonb LANGUAGE plpgsql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $detail$
+DECLARE e food_entries%ROWTYPE;
+BEGIN
+ IF NOT app_private.is_super_admin(NULLIF(current_setting('app.actor_id',true),''))
+ THEN RAISE EXCEPTION 'Unauthorized editorial access' USING ERRCODE='42501'; END IF;
+ SELECT * INTO e FROM food_entries WHERE id=p_id;
+ IF NOT FOUND THEN RETURN NULL; END IF;
+ RETURN jsonb_build_object(
+ 'id',e.id,'kind',e.kind,'slug',e.slug,
+ 'title',coalesce((SELECT content->>'title' FROM food_revisions WHERE id=e.draft_revision_id),e.slug),
+ 'status',e.status,'version',e.version,
+ 'createdAt',e.created_at,'updatedAt',e.updated_at,
+ 'hasPublishedVersion',e.published_revision_id IS NOT NULL,'publishedAt',e.published_at,
+ 'draftRevisionId',e.draft_revision_id,'reviewRevisionId',e.review_revision_id,
+ 'approvedRevisionId',e.approved_revision_id,'publishedRevisionId',e.published_revision_id,
+ 'draft',(SELECT r.content FROM food_revisions r WHERE r.id=e.draft_revision_id),
+ 'versions',coalesce((SELECT jsonb_agg(jsonb_build_object(
+ 'id',r.id,'version',r.version,'authorId',r.author_id,'reviewedBy',r.reviewed_by,
+ 'reviewedAt',r.reviewed_at,'createdAt',r.created_at,'approved',r.approved
+ ) ORDER BY r.version DESC)
+ FROM food_revisions r WHERE r.entry_id=e.id),'[]'::jsonb),
+ 'events',coalesce((SELECT jsonb_agg(jsonb_build_object(
+ 'action',v.action,'actorId',v.actor_id,'createdAt',v.created_at,
+ 'revisionId',v.revision_id
+ ) ORDER BY v.created_at DESC)
+ FROM (SELECT * FROM food_editorial_events WHERE entry_id=e.id
+ ORDER BY created_at DESC LIMIT 30) v),'[]'::jsonb)
+ );
+END;
+$detail$;
+REVOKE ALL ON FUNCTION app_private.food_editorial_detail(text) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_editorial_detail(text) TO menuminute_app;
+
+-- One entrypoint for editorial mutations. Each request is recorded once;
+-- the same idempotency key returns the same receipt without consuming a second TOTP.
+CREATE FUNCTION app_private.food_editorial_command(
+ p_action text,p_target text,p_data jsonb,p_key text
+) RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $command$
+DECLARE
+ actor text := nullif(current_setting('app.actor_id',true),'');
+ session_id text := nullif(current_setting('app.session_id',true),'');
+ existing admin_command_receipts%ROWTYPE;
+ e food_entries%ROWTYPE;
+ r food_revisions%ROWTYPE;
+ rev_id text;
+ payload jsonb;
+ result jsonb := '{}'::jsonb;
+ content_digest text;
+ source_count integer;
+ s text;
+ target_revision text;
+ affected integer;
+ reason text;
+BEGIN
+ IF actor IS NULL OR session_id IS NULL OR NOT app_private.is_super_admin(actor)
+ OR p_action NOT IN ('create','save','submit','approve','publish','unpublish','restore','media.approve','import')
+ OR p_key IS NULL OR p_key !~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
+ OR p_target IS NULL OR length(p_target) NOT BETWEEN 1 AND 100
+ OR p_data IS NULL OR jsonb_typeof(p_data)<>'object'
+ THEN RAISE EXCEPTION 'Invalid or unauthorized editorial command' USING ERRCODE='42501'; END IF;
+
+ PERFORM pg_advisory_xact_lock(hashtextextended(actor||':'||p_key,0));
+ SELECT * INTO existing FROM admin_command_receipts
+ WHERE actor_id=actor AND request_key=p_key;
+ IF FOUND THEN
+ IF existing.action<>'food.'||p_action OR existing.target_id<>p_target OR existing.payload<>p_data
+ THEN RAISE EXCEPTION 'Editorial idempotency conflict' USING ERRCODE='23505'; END IF;
+ RETURN existing.result;
+ END IF;
+
+ IF NOT app_private.require_step_up()
+ THEN RAISE EXCEPTION 'Verify a privileged session at /account/security' USING ERRCODE='42501'; END IF;
+
+ IF p_action IN ('create','save','import') THEN
+ payload := p_data->'content';
+ IF jsonb_typeof(payload)<>'object' OR payload->>'kind' NOT IN ('dishes','ingredients','cuisines','terms','compare')
+ OR coalesce(payload->>'slug','') !~ '^[a-z0-9]+(-[a-z0-9]+)*$'
+ OR length(payload->>'slug')>90
+ OR length(btrim(coalesce(payload->>'title',''))) NOT BETWEEN 3 AND 100
+ OR length(btrim(coalesce(payload->>'description',''))) NOT BETWEEN 35 AND 320
+ OR jsonb_typeof(payload->'sections')<>'array'
+ OR jsonb_array_length(payload->'sections') NOT BETWEEN 2 AND 12
+ OR jsonb_typeof(payload->'sourceUrls')<>'array'
+ OR jsonb_array_length(payload->'sourceUrls')>20
+ OR jsonb_typeof(payload->'facts')<>'array'
+ OR jsonb_array_length(payload->'facts') NOT BETWEEN 2 AND 8
+ OR length(payload::text)>28000
+ THEN RAISE EXCEPTION 'Invalid editorial content fields' USING ERRCODE='22023'; END IF;
+ IF EXISTS(SELECT 1 FROM jsonb_array_elements_text(payload->'sourceUrls') AS source(url)
+ WHERE url !~ '^https://' OR length(url)>1000)
+ THEN RAISE EXCEPTION 'Only HTTPS editorial sources are permitted' USING ERRCODE='22023'; END IF;
+ IF p_action IN ('create','import') THEN
+ IF p_target<>'new' THEN RAISE EXCEPTION 'Creation must target new' USING ERRCODE='22023'; END IF;
+ INSERT INTO food_entries(kind,slug,created_by,updated_by)
+ VALUES(payload->>'kind',payload->>'slug',actor,actor) RETURNING * INTO e;
+ ELSE
+ SELECT * INTO e FROM food_entries WHERE id=p_target FOR UPDATE;
+ IF NOT FOUND THEN RAISE EXCEPTION 'Unknown food entry' USING ERRCODE='22023'; END IF;
+ IF (p_data->>'expectedVersion')::int IS DISTINCT FROM e.version
+ OR e.kind<>payload->>'kind' OR e.slug<>payload->>'slug'
+ THEN RAISE EXCEPTION 'Concurrent editorial revision or canonical identity change' USING ERRCODE='40001'; END IF;
+ END IF;
+ content_digest := encode(sha256(convert_to(payload::text,'UTF8')),'hex');
+ INSERT INTO food_revisions(entry_id,version,content,digest,author_id)
+ VALUES(e.id,e.version+1,payload,content_digest,actor) RETURNING id INTO rev_id;
+ UPDATE food_entries SET version=version+1, status='draft',draft_revision_id=rev_id,
+ review_revision_id=NULL,approved_revision_id=NULL,updated_by=actor,updated_at=clock_timestamp()
+ WHERE id=e.id;
+ INSERT INTO food_sources(url)
+ SELECT DISTINCT source.url FROM jsonb_array_elements_text(payload->'sourceUrls') source(url)
+ ON CONFLICT(url) DO NOTHING;
+ INSERT INTO food_entry_sources(revision_id,source_id)
+ SELECT rev_id, s.id FROM food_sources s
+ JOIN jsonb_array_elements_text(payload->'sourceUrls') source(url) ON source.url=s.url
+ ON CONFLICT DO NOTHING;
+ IF payload->>'imagePath' IS NOT NULL THEN
+ INSERT INTO food_media(entry_id,path,alt_text)
+ VALUES(e.id,payload->>'imagePath',payload->>'imageAlt')
+ ON CONFLICT(entry_id,path) DO NOTHING;
+ END IF;
+ result:=jsonb_build_object('id',e.id,'revisionId',rev_id,
+ 'version',e.version+1,'status','draft','digest',content_digest);
+ ELSIF p_action='media.approve' THEN
+ SELECT * INTO e FROM food_entries WHERE id=p_target FOR UPDATE;
+ IF NOT FOUND OR p_data->>'path' IS NULL
+ OR length(coalesce(p_data->>'attribution',''))>240
+ OR p_data->>'license' NOT IN ('owned','commissioned','cc0','cc-by','licensed')
+ THEN RAISE EXCEPTION 'Invalid media verification' USING ERRCODE='22023'; END IF;
+ UPDATE food_media SET rights_approved=true,license=p_data->>'license',
+ attribution=coalesce(p_data->>'attribution',''),
+ verified_by=actor,verified_at=clock_timestamp()
+ WHERE entry_id=e.id AND path=p_data->>'path' AND rights_approved=false;
+ GET DIAGNOSTICS affected=ROW_COUNT;
+ IF affected<>1 THEN RAISE EXCEPTION 'Media not found or already verified' USING ERRCODE='22023'; END IF;
+ result:=jsonb_build_object('id',e.id,'mediaApproved',true);
+ ELSE
+ SELECT * INTO e FROM food_entries WHERE id=p_target FOR UPDATE;
+ IF NOT FOUND THEN RAISE EXCEPTION 'Unknown food entry' USING ERRCODE='22023'; END IF;
+ IF (p_data->>'expectedVersion')::int IS DISTINCT FROM e.version THEN
+ RAISE EXCEPTION 'Concurrent editorial revision' USING ERRCODE='40001'; END IF;
+ IF p_action='submit' THEN
+ IF e.status<>'draft' OR e.draft_revision_id IS NULL
+ THEN RAISE EXCEPTION 'Only current drafts may be submitted' USING ERRCODE='22023'; END IF;
+ SELECT * INTO r FROM food_revisions WHERE id=e.draft_revision_id;
+ SELECT count(DISTINCT source_id) INTO source_count
+ FROM food_entry_sources WHERE revision_id=r.id;
+ IF source_count<2 OR length(r.content::text)<950
+ THEN RAISE EXCEPTION 'Article needs substantive researched content and two references' USING ERRCODE='22023'; END IF;
+ UPDATE food_entries SET status='in_review',review_revision_id=r.id,
+ updated_by=actor,updated_at=clock_timestamp() WHERE id=e.id;
+ rev_id:=r.id;
+ result:=jsonb_build_object('id',e.id,'status','in_review');
+ ELSIF p_action='approve' THEN
+ IF e.status<>'in_review' OR e.review_revision_id IS NULL
+ THEN RAISE EXCEPTION 'A submitted revision is required' USING ERRCODE='22023'; END IF;
+ SELECT * INTO r FROM food_revisions WHERE id=e.review_revision_id FOR UPDATE;
+ IF r.author_id=actor THEN
+ RAISE EXCEPTION 'A separate reviewer must approve authored content' USING ERRCODE='42501'; END IF;
+ SELECT count(DISTINCT source_id) INTO source_count
+ FROM food_entry_sources WHERE revision_id=r.id;
+ IF source_count<2 THEN RAISE EXCEPTION 'Two sources required' USING ERRCODE='22023'; END IF;
+ IF r.content->>'imagePath' IS NOT NULL AND NOT EXISTS(
+ SELECT 1 FROM food_media m WHERE m.entry_id=e.id AND m.path=r.content->>'imagePath'
+ AND m.rights_approved AND m.verified_by IS NOT NULL
+ ) THEN RAISE EXCEPTION 'Artwork rights verification required' USING ERRCODE='22023'; END IF;
+ UPDATE food_revisions SET approved=true,reviewed_by=actor,reviewed_at=clock_timestamp()
+ WHERE id=r.id AND NOT approved;
+ IF NOT FOUND THEN RAISE EXCEPTION 'Revision already approved' USING ERRCODE='22023'; END IF;
+ UPDATE food_entries SET status='approved',approved_revision_id=r.id,
+ updated_by=actor,updated_at=clock_timestamp() WHERE id=e.id;
+ rev_id:=r.id;
+ result:=jsonb_build_object('id',e.id,'status','approved');
+ ELSIF p_action='publish' THEN
+ IF e.status<>'approved' OR e.approved_revision_id IS NULL
+ THEN RAISE EXCEPTION 'Only an independently approved revision can publish' USING ERRCODE='22023'; END IF;
+ SELECT * INTO r FROM food_revisions WHERE id=e.approved_revision_id AND entry_id=e.id;
+ IF NOT FOUND OR NOT r.approved OR r.reviewed_by IS NULL
+ OR r.reviewed_at IS NULL OR r.reviewed_by=r.author_id
+ THEN RAISE EXCEPTION 'Invalid reviewed content' USING ERRCODE='42501'; END IF;
+ UPDATE food_entries SET published_revision_id=r.id,published_at=clock_timestamp(),
+ status='published',search_text=coalesce(r.content->>'title','')||' '||
+ coalesce(r.content->>'description','')||' '||coalesce(r.content->>'slug','')||' '||
+ coalesce((SELECT string_agg(value,' ') FROM jsonb_array_elements_text(r.content->'aliases')),''),
+ updated_by=actor,updated_at=clock_timestamp() WHERE id=e.id;
+ DELETE FROM food_aliases WHERE entry_id=e.id;
+ INSERT INTO food_aliases(entry_id,alias,normalized)
+ SELECT DISTINCT ON (lower(btrim(alias))) e.id,alias,lower(btrim(alias))
+ FROM jsonb_array_elements_text(r.content->'aliases') x(alias)
+ WHERE length(btrim(alias)) BETWEEN 2 AND 90
+ ORDER BY lower(btrim(alias)),alias;
+ DELETE FROM food_relations WHERE entry_id=e.id;
+ INSERT INTO food_relations(entry_id,related_entry_id,relation)
+ SELECT e.id,target.id,'related'
+ FROM food_entries target
+ JOIN jsonb_array_elements_text(r.content->'relatedSlugs') related(slug)
+ ON target.slug=related.slug AND target.id<>e.id
+ WHERE target.published_revision_id IS NOT NULL
+ ON CONFLICT DO NOTHING;
+ rev_id:=r.id;
+ result:=jsonb_build_object('id',e.id,'status','published','revisionId',rev_id);
+ ELSIF p_action='unpublish' THEN
+ IF e.published_revision_id IS NULL THEN
+ RAISE EXCEPTION 'Entry is not published' USING ERRCODE='22023'; END IF;
+ rev_id:=e.published_revision_id;
+ UPDATE food_entries SET published_revision_id=NULL,published_at=NULL,
+ status='archived',search_text='',updated_by=actor,updated_at=clock_timestamp()
+ WHERE id=e.id;
+ DELETE FROM food_aliases WHERE entry_id=e.id;
+ result:=jsonb_build_object('id',e.id,'status','archived');
+ ELSIF p_action='restore' THEN
+ target_revision:=p_data->>'revisionId';
+ SELECT * INTO r FROM food_revisions WHERE id=target_revision AND entry_id=e.id;
+ IF NOT FOUND OR NOT r.approved OR r.reviewed_by IS NULL OR r.reviewed_at IS NULL
+ THEN RAISE EXCEPTION 'Only a previously approved revision can be restored' USING ERRCODE='22023'; END IF;
+ UPDATE food_entries SET published_revision_id=r.id,published_at=clock_timestamp(),
+ status='published',search_text=coalesce(r.content->>'title','')||' '||
+ coalesce(r.content->>'description','')||' '||coalesce(r.content->>'slug',''),
+ updated_by=actor,updated_at=clock_timestamp() WHERE id=e.id;
+ rev_id:=r.id;
+ result:=jsonb_build_object('id',e.id,'status','published','revisionId',rev_id);
+ END IF;
+ END IF;
+
+ INSERT INTO food_editorial_events(entry_id,revision_id,action,actor_id,details)
+ VALUES(e.id,rev_id,p_action,actor,p_data - 'content');
+ INSERT INTO admin_audit_logs(id,actor_id,actor_email,action,target_type,target_id,details)
+ VALUES(gen_random_uuid()::text,actor,
+ (SELECT u.email FROM users u WHERE u.id=actor),
+ 'food.'||p_action,'food_entry',e.id,
+ jsonb_build_object('revisionId',rev_id,'requestKey',p_key));
+ INSERT INTO admin_command_receipts(actor_id,request_key,action,target_id,payload,result)
+ VALUES(actor,p_key,'food.'||p_action,p_target,p_data,result);
+ RETURN result;
+END;
+$command$;
+REVOKE ALL ON FUNCTION app_private.food_editorial_command(text,text,jsonb,text) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_editorial_command(text,text,jsonb,text) TO menuminute_app;
+
+-- Idempotent catalogue candidate queue. NOT an article, revision or public page.
+CREATE TABLE food_candidates (
+ catalogue_id text PRIMARY KEY CHECK (length(catalogue_id) BETWEEN 5 AND 80),
+ name text NOT NULL CHECK (length(name) BETWEEN 3 AND 100),
+ category_hint text NOT NULL DEFAULT '' CHECK(length(category_hint)<=90),
+ suggestion text NOT NULL DEFAULT '' CHECK(length(suggestion)<=320),
+ aliases jsonb NOT NULL DEFAULT '[]'::jsonb CHECK (jsonb_typeof(aliases)='array'),
+ status text NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','promoted','dismissed')),
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
+);
+ALTER TABLE food_candidates ENABLE ROW LEVEL SECURITY;
+ALTER TABLE food_candidates FORCE ROW LEVEL SECURITY;
+REVOKE ALL ON food_candidates FROM PUBLIC,menuminute_app,menuminute_worker;
+DO $owner$ BEGIN
+ EXECUTE format('CREATE POLICY food_owner_only ON food_candidates FOR ALL TO %I USING(true) WITH CHECK(true)',current_user);
+END $owner$;
+
+CREATE FUNCTION app_private.food_editorial_candidates(p_limit integer)
+RETURNS TABLE(candidate jsonb) LANGUAGE plpgsql STABLE SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $candidates$
+BEGIN
+ IF NOT app_private.is_super_admin(NULLIF(current_setting('app.actor_id',true),''))
+ THEN RAISE EXCEPTION 'Unauthorized catalogue queue' USING ERRCODE='42501'; END IF;
+ RETURN QUERY SELECT jsonb_build_object(
+ 'id',c.catalogue_id,'name',c.name,'hint',c.category_hint,
+ 'suggestion',c.suggestion,'aliases',c.aliases,'status',c.status
+ ) FROM food_candidates c ORDER BY c.name,c.catalogue_id
+ LIMIT GREATEST(1,LEAST(coalesce(p_limit,250),300));
+END;
+$candidates$;
+REVOKE ALL ON FUNCTION app_private.food_editorial_candidates(integer) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_editorial_candidates(integer) TO menuminute_app;
+
+CREATE FUNCTION app_private.food_import_candidates(p_candidates jsonb,p_key text)
+RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER
+SET search_path=pg_catalog,public,pg_temp AS $import$
+DECLARE actor text:=nullif(current_setting('app.actor_id',true),'');
+ existing admin_command_receipts%ROWTYPE;
+ item jsonb;
+ added integer:=0;
+ result jsonb;
+BEGIN
+ IF actor IS NULL OR NOT app_private.is_super_admin(actor)
+ OR p_key IS NULL OR p_key !~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
+ OR jsonb_typeof(p_candidates)<>'array' OR jsonb_array_length(p_candidates)>250
+ THEN RAISE EXCEPTION 'Invalid candidate import authorization' USING ERRCODE='42501'; END IF;
+ PERFORM pg_advisory_xact_lock(hashtextextended(actor||':'||p_key,0));
+ SELECT * INTO existing FROM admin_command_receipts WHERE actor_id=actor AND request_key=p_key;
+ IF FOUND THEN
+ IF existing.action<>'food.candidates.import' OR existing.payload<>jsonb_build_object('candidates',p_candidates)
+ THEN RAISE EXCEPTION 'Candidate import key conflict' USING ERRCODE='23505'; END IF;
+ RETURN existing.result;
+ END IF;
+ IF NOT app_private.require_step_up() THEN
+ RAISE EXCEPTION 'Verify a recent privileged step-up' USING ERRCODE='42501'; END IF;
+ FOR item IN SELECT value FROM jsonb_array_elements(p_candidates)
+ LOOP
+ IF jsonb_typeof(item)<>'object' OR
+ length(coalesce(item->>'id','')) NOT BETWEEN 5 AND 80 OR
+ length(coalesce(item->>'name','')) NOT BETWEEN 3 AND 100 OR
+ length(coalesce(item->>'hint',''))>90 OR
+ length(coalesce(item->>'suggestion',''))>320 OR
+ jsonb_typeof(item->'aliases')<>'array' OR
+ jsonb_array_length(item->'aliases')>20
+ THEN RAISE EXCEPTION 'Invalid catalogue candidate' USING ERRCODE='22023'; END IF;
+ INSERT INTO food_candidates(catalogue_id,name,category_hint,suggestion,aliases)
+ VALUES(item->>'id',item->>'name',coalesce(item->>'hint',''),
+ coalesce(item->>'suggestion',''),item->'aliases')
+ ON CONFLICT(catalogue_id) DO NOTHING;
+ IF FOUND THEN added:=added+1; END IF;
+ END LOOP;
+ result:=jsonb_build_object('imported',added,'total',jsonb_array_length(p_candidates));
+ INSERT INTO admin_audit_logs(id,actor_id,actor_email,action,target_type,target_id,details)
+ VALUES(gen_random_uuid()::text,actor,(SELECT email FROM users WHERE id=actor),
+ 'food.candidates.import','food_catalogue','candidate-batch',result);
+ INSERT INTO admin_command_receipts(actor_id,request_key,action,target_id,payload,result)
+ VALUES(actor,p_key,'food.candidates.import','candidate-batch',
+ jsonb_build_object('candidates',p_candidates),result);
+ RETURN result;
+END;
+$import$;
+REVOKE ALL ON FUNCTION app_private.food_import_candidates(jsonb,text) FROM PUBLIC;
+GRANT EXECUTE ON FUNCTION app_private.food_import_candidates(jsonb,text) TO menuminute_app;
diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json
index 8ccf2241..2f66b85c 100644
--- a/src/db/migrations/meta/_journal.json
+++ b/src/db/migrations/meta/_journal.json
@@ -806,6 +806,13 @@
"when": 1797166800046,
"tag": "0115_receipt_upi_payment",
"breakpoints": true
+ },
+ {
+ "idx": 115,
+ "version": "7",
+ "when": 1797166800047,
+ "tag": "0118_food_encyclopedia_editorial",
+ "breakpoints": true
}
]
}
diff --git a/src/db/schema/food-encyclopedia.ts b/src/db/schema/food-encyclopedia.ts
new file mode 100644
index 00000000..bc9610c0
--- /dev/null
+++ b/src/db/schema/food-encyclopedia.ts
@@ -0,0 +1,129 @@
+// Platform-owned editorial storage; no merchant tenancy scope or runtime table grants.
+// Migration 0118 is authoritative; access only through narrow SECURITY DEFINER functions.
+import { sql } from "drizzle-orm";
+import {
+ boolean,
+ check,
+ index,
+ integer,
+ jsonb,
+ pgTable,
+ text,
+ timestamp,
+ unique,
+} from "drizzle-orm/pg-core";
+const clock = () =>
+ timestamp("created_at", { withTimezone: true })
+ .notNull()
+ .default(sql`clock_timestamp()`);
+export const foodEntries = pgTable(
+ "food_entries",
+ {
+ id: text("id")
+ .primaryKey()
+ .default(sql`gen_random_uuid()::text`),
+ kind: text("kind").notNull(),
+ slug: text("slug").notNull(),
+ status: text("status").notNull().default("draft"),
+ version: integer("version").notNull().default(0),
+ draftRevisionId: text("draft_revision_id"),
+ reviewRevisionId: text("review_revision_id"),
+ approvedRevisionId: text("approved_revision_id"),
+ publishedRevisionId: text("published_revision_id"),
+ publishedAt: timestamp("published_at", { withTimezone: true }),
+ searchText: text("search_text").notNull().default(""),
+ createdBy: text("created_by").notNull(),
+ updatedBy: text("updated_by").notNull(),
+ createdAt: clock(),
+ updatedAt: timestamp("updated_at", { withTimezone: true })
+ .notNull()
+ .default(sql`clock_timestamp()`),
+ },
+ (t) => [
+ unique("food_entries_kind_slug_key").on(t.kind, t.slug),
+ check(
+ "food_entries_published_pointer_check",
+ sql`(${t.publishedRevisionId} IS NULL) = (${t.publishedAt} IS NULL)`
+ ),
+ ]
+);
+export const foodRevisions = pgTable(
+ "food_revisions",
+ {
+ id: text("id")
+ .primaryKey()
+ .default(sql`gen_random_uuid()::text`),
+ entryId: text("entry_id").notNull(),
+ version: integer("version").notNull(),
+ content: jsonb("content").$type
>().notNull(),
+ digest: text("digest").notNull(),
+ authorId: text("author_id").notNull(),
+ approved: boolean("approved").notNull().default(false),
+ reviewedBy: text("reviewed_by"),
+ reviewedAt: timestamp("reviewed_at", { withTimezone: true }),
+ createdAt: clock(),
+ },
+ (t) => [
+ unique("food_revisions_entry_version_key").on(t.entryId, t.version),
+ unique("food_revisions_entry_id_key").on(t.entryId, t.id),
+ ]
+);
+export const foodSources = pgTable("food_sources", {
+ id: text("id")
+ .primaryKey()
+ .default(sql`gen_random_uuid()::text`),
+ url: text("url").notNull().unique(),
+ title: text("title").notNull().default(""),
+ publisher: text("publisher").notNull().default(""),
+ retrievedAt: timestamp("retrieved_at", { withTimezone: true }),
+ createdAt: clock(),
+});
+export const foodEntrySources = pgTable("food_entry_sources", {
+ revisionId: text("revision_id").notNull(),
+ sourceId: text("source_id").notNull(),
+ sectionId: text("section_id").notNull().default(""),
+});
+export const foodAliases = pgTable("food_aliases", {
+ entryId: text("entry_id").notNull(),
+ alias: text("alias").notNull(),
+ normalized: text("normalized").notNull(),
+});
+export const foodRelations = pgTable("food_relations", {
+ entryId: text("entry_id").notNull(),
+ relatedEntryId: text("related_entry_id").notNull(),
+ relation: text("relation").notNull().default("related"),
+});
+export const foodMedia = pgTable("food_media", {
+ id: text("id")
+ .primaryKey()
+ .default(sql`gen_random_uuid()::text`),
+ entryId: text("entry_id").notNull(),
+ path: text("path").notNull(),
+ altText: text("alt_text").notNull(),
+ license: text("license").notNull().default("pending"),
+ attribution: text("attribution").notNull().default(""),
+ rightsApproved: boolean("rights_approved").notNull().default(false),
+ verifiedBy: text("verified_by"),
+ verifiedAt: timestamp("verified_at", { withTimezone: true }),
+ createdAt: clock(),
+});
+export const foodEditorialEvents = pgTable(
+ "food_editorial_events",
+ {
+ id: text("id")
+ .primaryKey()
+ .default(sql`gen_random_uuid()::text`),
+ entryId: text("entry_id").notNull(),
+ revisionId: text("revision_id"),
+ action: text("action").notNull(),
+ actorId: text("actor_id").notNull(),
+ details: jsonb("details").$type>().notNull().default({}),
+ createdAt: clock(),
+ },
+ (t) => [index("food_editorial_events_entry_idx").on(t.entryId, t.createdAt)]
+);
+export const foodRedirects = pgTable("food_redirects", {
+ kind: text("kind").notNull(),
+ oldSlug: text("old_slug").notNull(),
+ entryId: text("entry_id").notNull(),
+});
diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts
index f05e512f..4a1d86ba 100644
--- a/src/db/schema/index.ts
+++ b/src/db/schema/index.ts
@@ -33,3 +33,5 @@ export * from "./hospitality";
export * from "./crm";
export * from "./tiffin";
+
+export * from "./food-encyclopedia";
diff --git a/src/modules/food-encyclopedia/catalogue.ts b/src/modules/food-encyclopedia/catalogue.ts
new file mode 100644
index 00000000..c2a3e6f8
--- /dev/null
+++ b/src/modules/food-encyclopedia/catalogue.ts
@@ -0,0 +1,69 @@
+import rawCatalogue from "@/data/dish-catalogue.json";
+import { z } from "zod";
+import { sql } from "drizzle-orm";
+import { withActor } from "@/modules/tenancy/service";
+import type { AdminActor } from "@/lib/admin-guard";
+import type { FoodDraft } from "./editorial-contracts";
+import { newEditorialDraft } from "./editorial-template";
+const RowSchema = z.object({
+ id: z.string().max(80).min(5),
+ name: z.string().max(100).min(3),
+ categoryHint: z.string().max(90),
+ aliases: z.array(z.string().max(90)).max(20),
+ publicDescription: z.string().max(320),
+});
+export const catalogueCandidates = rawCatalogue.flatMap((record) => {
+ const parsed = RowSchema.safeParse(record);
+ if (!parsed.success) return [];
+ return [
+ {
+ id: parsed.data.id,
+ name: parsed.data.name,
+ hint: parsed.data.categoryHint,
+ suggestion: parsed.data.publicDescription,
+ aliases: parsed.data.aliases,
+ },
+ ];
+});
+export type Candidate = (typeof catalogueCandidates)[number];
+export type StoredCandidate = Candidate & { status: "pending" | "promoted" | "dismissed" };
+const slugFor = (name: string): string =>
+ name
+ .normalize("NFKD")
+ .toLowerCase()
+ .replace(/[^a-z0-9\s-]/g, "")
+ .trim()
+ .replace(/\s+/g, "-")
+ .replace(/-+/g, "-")
+ .slice(0, 90);
+export function draftFromCandidate(candidate: Candidate): FoodDraft {
+ const draft = newEditorialDraft("dishes", slugFor(candidate.name) || "untitled-food");
+ return {
+ ...draft,
+ title: candidate.name,
+ aliases: candidate.aliases.filter((a) => a.length >= 2),
+ // Suggestion text is deliberately NOT copied into a factual claim.
+ description: `Research pending: learn about ${candidate.name} through a source-backed guide that has not yet been written or approved.`,
+ eyebrow: "CATALOGUE CANDIDATE / UNREVIEWED",
+ };
+}
+export async function listStoredCandidates(actor: AdminActor): Promise {
+ return withActor({ userId: actor.id, sessionId: actor.sessionId }, async (tx) => {
+ const r = await tx.execute<{ candidate: StoredCandidate }>(
+ sql`SELECT candidate FROM app_private.food_editorial_candidates(250)`
+ );
+ return r.rows.map((x) => x.candidate);
+ });
+}
+export async function importCandidateQueue(
+ actor: AdminActor,
+ requestKey: string
+): Promise<{ imported: number; total: number }> {
+ return withActor({ userId: actor.id, sessionId: actor.sessionId }, async (tx) => {
+ const r = await tx.execute<{ result: { imported: number; total: number } }>(
+ sql`SELECT app_private.food_import_candidates(${JSON.stringify(catalogueCandidates)}::jsonb,${requestKey}) AS result`
+ );
+ if (!r.rows[0]) throw new Error("No candidate import receipt");
+ return r.rows[0].result;
+ });
+}
diff --git a/src/modules/food-encyclopedia/editorial-contracts.test.ts b/src/modules/food-encyclopedia/editorial-contracts.test.ts
new file mode 100644
index 00000000..d36187d2
--- /dev/null
+++ b/src/modules/food-encyclopedia/editorial-contracts.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest";
+import { FoodDraftSchema, editorialQualityIssues, transitionAllowed } from "./editorial-contracts";
+import { getFoodEntry } from "./pilot";
+
+describe("Food Encyclopedia editorial gates", () => {
+ const fixture = getFoodEntry("dishes", "paneer-tikka")!;
+ const draft = {
+ kind: fixture.kind,
+ slug: fixture.slug,
+ title: fixture.title,
+ eyebrow: fixture.eyebrow,
+ description: fixture.description,
+ tint: fixture.tint,
+ imagePath: fixture.imagePath,
+ imageAlt: fixture.imageAlt,
+ aliases: fixture.aliases,
+ facts: fixture.facts,
+ sections: fixture.sections,
+ sourceUrls: fixture.sourceUrls,
+ relatedSlugs: fixture.relatedSlugs,
+ caution: fixture.caution,
+ };
+ it("accepts a bounded draft, not a published record or executable markup", () => {
+ expect(FoodDraftSchema.parse(draft).slug).toBe("paneer-tikka");
+ expect(FoodDraftSchema.safeParse({ ...draft, status: "published" }).success).toBe(false);
+ expect(FoodDraftSchema.safeParse({ ...draft, slug: "../admin" }).success).toBe(false);
+ expect(
+ FoodDraftSchema.safeParse({ ...draft, sourceUrls: ["http://example.com"] }).success
+ ).toBe(false);
+ });
+ it("never qualifies a content fixture as human-reviewed by default", () => {
+ expect(editorialQualityIssues(FoodDraftSchema.parse(draft)).length).toBeGreaterThan(0);
+ });
+ it("rejects bypasses of submit/approve/publish transition order", () => {
+ expect(transitionAllowed("draft", "approve")).toBe(false);
+ expect(transitionAllowed("draft", "publish")).toBe(false);
+ expect(transitionAllowed("in_review", "publish")).toBe(false);
+ expect(transitionAllowed("approved", "publish")).toBe(true);
+ expect(transitionAllowed("published", "save")).toBe(true); // published revision remains immutable
+ });
+});
diff --git a/src/modules/food-encyclopedia/editorial-contracts.ts b/src/modules/food-encyclopedia/editorial-contracts.ts
new file mode 100644
index 00000000..d32b4f6d
--- /dev/null
+++ b/src/modules/food-encyclopedia/editorial-contracts.ts
@@ -0,0 +1,151 @@
+import { z } from "zod";
+import { foodKinds } from "./contracts";
+
+/** Editorial identity is platform-wide, never a merchant-owned dish or tenancy scope. */
+const slug = z
+ .string()
+ .trim()
+ .regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)
+ .max(90);
+const httpsUrl = z
+ .string()
+ .url()
+ .max(1000)
+ .refine((value) => {
+ try {
+ const url = new URL(value);
+ return url.protocol === "https:" && !url.username && !url.password;
+ } catch {
+ return false;
+ }
+ }, "A public HTTPS source is required");
+
+export const FoodDraftSchema = z
+ .object({
+ kind: z.enum(foodKinds),
+ slug,
+ title: z.string().trim().min(3).max(100),
+ eyebrow: z.string().trim().min(3).max(120),
+ description: z.string().trim().min(35).max(320),
+ tint: z.enum(["cream", "sage", "blue", "peach", "lavender"]),
+ imagePath: z
+ .string()
+ .regex(/^\/assets\/food\/dishes\/[a-zA-Z0-9/_-]+\.(?:png|webp|avif)$/)
+ .max(240)
+ .optional(),
+ imageAlt: z.string().trim().max(200).optional(),
+ aliases: z.array(z.string().trim().min(2).max(90)).max(20).default([]),
+ facts: z
+ .array(
+ z
+ .object({
+ label: z.string().trim().min(2).max(40),
+ value: z.string().trim().min(2).max(140),
+ })
+ .strict()
+ )
+ .min(2)
+ .max(8),
+ sections: z
+ .array(
+ z
+ .object({
+ id: slug,
+ eyebrow: z.string().trim().min(3).max(80),
+ title: z.string().trim().min(3).max(150),
+ body: z.array(z.string().trim().min(25).max(1800)).min(1).max(6),
+ })
+ .strict()
+ )
+ .min(2)
+ .max(12),
+ sourceUrls: z.array(httpsUrl).max(20).default([]),
+ relatedSlugs: z.array(slug).max(20).default([]),
+ caution: z.string().trim().min(20).max(650).optional(),
+ })
+ .strict();
+export type FoodDraft = z.infer;
+export const FoodEditorialActionSchema = z.enum([
+ "create",
+ "save",
+ "submit",
+ "approve",
+ "publish",
+ "unpublish",
+ "restore",
+]);
+export type FoodEditorialAction = z.infer;
+export type FoodWorkflowStatus = "draft" | "in_review" | "approved" | "published" | "archived";
+export type FoodEditorialRow = {
+ id: string;
+ kind: FoodDraft["kind"];
+ slug: string;
+ title: string;
+ status: FoodWorkflowStatus;
+ version: number;
+ createdAt: string;
+ updatedAt: string;
+ hasPublishedVersion: boolean;
+ publishedAt: string | null;
+};
+export type FoodEditorialDetail = FoodEditorialRow & {
+ draft: FoodDraft | null;
+ versions: Array<{
+ id: string;
+ version: number;
+ authorId: string;
+ reviewedBy: string | null;
+ reviewedAt: string | null;
+ createdAt: string;
+ approved: boolean;
+ }>;
+ publishedRevisionId: string | null;
+ draftRevisionId: string | null;
+ reviewRevisionId: string | null;
+ approvedRevisionId: string | null;
+ events: Array<{ action: string; actorId: string; createdAt: string; revisionId: string | null }>;
+};
+
+export type QualityIssue = { field: string; detail: string };
+/** Human editorial review is still mandatory even when this local checker is satisfied. */
+export function editorialQualityIssues(draft: FoodDraft): QualityIssue[] {
+ const issues: QualityIssue[] = [];
+ if (draft.sourceUrls.length < 2)
+ issues.push({
+ field: "sourceUrls",
+ detail: "At least two credible independent sources are required",
+ });
+ if (new Set(draft.sourceUrls).size !== draft.sourceUrls.length)
+ issues.push({ field: "sourceUrls", detail: "Duplicate source URLs" });
+ if (new Set(draft.sections.map((s) => s.id)).size !== draft.sections.length)
+ issues.push({ field: "sections", detail: "Section IDs must be unique" });
+ if (new Set(draft.aliases.map((a) => a.toLocaleLowerCase())).size !== draft.aliases.length)
+ issues.push({ field: "aliases", detail: "Aliases must be unique" });
+ const copy = draft.sections.flatMap((s) => s.body).join(" ");
+ if (copy.trim().split(/\s+/u).length < 110)
+ issues.push({
+ field: "sections",
+ detail:
+ "Article needs more useful, original explanatory content (110 words minimum for this pilot)",
+ });
+ if (draft.imagePath && (!draft.imageAlt || draft.imageAlt.length < 15))
+ issues.push({
+ field: "imageAlt",
+ detail: "Meaningful alternative text is required for illustrations",
+ });
+ return issues;
+}
+
+export function transitionAllowed(
+ status: FoodWorkflowStatus,
+ action: FoodEditorialAction
+): boolean {
+ if (action === "create") return status === "draft";
+ if (action === "save") return true; // creates a NEW revision; never edits an approved one
+ if (action === "submit") return status === "draft";
+ if (action === "approve") return status === "in_review";
+ if (action === "publish") return status === "approved";
+ if (action === "unpublish") return status === "published";
+ if (action === "restore") return true; // requires a previously approved revision in SQL
+ return false;
+}
diff --git a/src/modules/food-encyclopedia/editorial-service.ts b/src/modules/food-encyclopedia/editorial-service.ts
new file mode 100644
index 00000000..67c799ef
--- /dev/null
+++ b/src/modules/food-encyclopedia/editorial-service.ts
@@ -0,0 +1,142 @@
+import { getPool } from "@/db";
+import { withActor } from "@/modules/tenancy/service";
+import { sql } from "drizzle-orm";
+import type { AdminActor } from "@/lib/admin-guard";
+import {
+ FoodDraftSchema,
+ type FoodDraft,
+ type FoodEditorialDetail,
+ type FoodEditorialRow,
+ type FoodEditorialAction,
+} from "./editorial-contracts";
+import { type FoodEntry, FoodEntrySchema, type FoodKind } from "./contracts";
+
+function parseFoodRow(value: unknown): FoodEntry | null {
+ const result = FoodEntrySchema.safeParse(value);
+ return result.success && result.data.status === "published" ? result.data : null;
+}
+/** Queries read only a vetted projection of approved and published revisions. */
+export async function getPublishedFoodEntry(
+ kind: FoodKind,
+ slug: string
+): Promise {
+ try {
+ const r = await getPool().query<{ entry: unknown }>(
+ "SELECT app_private.food_public_entry($1,$2) AS entry",
+ [kind, slug]
+ );
+ return parseFoodRow(r.rows[0]?.entry);
+ } catch {
+ // During additive deployments, a missing migration must fail closed rather
+ // than exposing drafts, throwing a site-wide 500, or claiming published content.
+ return null;
+ }
+}
+export async function listPublishedFood(
+ kind?: FoodKind,
+ query?: string,
+ limit = 40,
+ offset = 0
+): Promise {
+ try {
+ const r = await getPool().query<{ entry: unknown }>(
+ "SELECT entry FROM app_private.food_public_list($1,$2,$3,$4)",
+ [
+ kind ?? null,
+ (query ?? "").trim().slice(0, 100),
+ Math.min(100, Math.max(1, limit)),
+ Math.max(0, Math.min(offset, 50000)),
+ ]
+ );
+ return r.rows.flatMap(({ entry }) => {
+ const parsed = parseFoodRow(entry);
+ return parsed ? [parsed] : [];
+ });
+ } catch {
+ return [];
+ }
+}
+export async function getPublicFoodRedirect(kind: FoodKind, slug: string): Promise {
+ try {
+ const r = await getPool().query<{ slug: string | null }>(
+ "SELECT app_private.food_public_redirect($1,$2) AS slug",
+ [kind, slug]
+ );
+ return r.rows[0]?.slug ?? null;
+ } catch {
+ return null;
+ }
+}
+export async function listEditorialEntries(
+ actor: AdminActor,
+ query = "",
+ status = ""
+): Promise {
+ return withActor({ userId: actor.id, sessionId: actor.sessionId }, async (tx) => {
+ const result = await tx.execute<{ item: FoodEditorialRow }>(
+ sql`SELECT item FROM app_private.food_editorial_list(${query.slice(0, 100)},${status},80)`
+ );
+ return result.rows.map((row) => row.item);
+ });
+}
+export async function getEditorialDetail(
+ actor: AdminActor,
+ id: string
+): Promise {
+ return withActor({ userId: actor.id, sessionId: actor.sessionId }, async (tx) => {
+ const result = await tx.execute<{ detail: FoodEditorialDetail | null }>(
+ sql`SELECT app_private.food_editorial_detail(${id}) AS detail`
+ );
+ return result.rows[0]?.detail ?? null;
+ });
+}
+export type EditorialCommandReceipt = {
+ id: string;
+ status?: string;
+ revisionId?: string;
+ version?: number;
+ digest?: string;
+};
+export async function runEditorialCommand(
+ actor: AdminActor,
+ action: FoodEditorialAction | "media.approve" | "import",
+ targetId: string,
+ data: {
+ content?: FoodDraft;
+ expectedVersion?: number;
+ revisionId?: string;
+ license?: string;
+ attribution?: string;
+ path?: string;
+ },
+ requestKey: string = crypto.randomUUID()
+): Promise {
+ if (!actor.sessionId) throw new Error("A live authenticated administrative session is required");
+ const payload = {
+ ...data,
+ ...(data.content ? { content: FoodDraftSchema.parse(data.content) } : {}),
+ };
+ return withActor({ userId: actor.id, sessionId: actor.sessionId }, async (tx) => {
+ const result = await tx.execute<{ result: EditorialCommandReceipt }>(
+ sql`SELECT app_private.food_editorial_command(
+ ${action},${targetId},${JSON.stringify(payload)}::jsonb,${requestKey}
+ ) AS result`
+ );
+ if (!result.rows[0]) throw new Error("No editorial command receipt");
+ return result.rows[0].result;
+ });
+}
+
+export async function listPublishedFoodSitemap(
+ limit = 45000
+): Promise> {
+ try {
+ const result = await getPool().query<{ kind: FoodKind; slug: string; reviewed_at: Date }>(
+ "SELECT kind,slug,reviewed_at FROM app_private.food_public_sitemap($1)",
+ [Math.max(0, Math.min(limit, 45000))]
+ );
+ return result.rows;
+ } catch {
+ return [];
+ }
+}
diff --git a/src/modules/food-encyclopedia/editorial-template.ts b/src/modules/food-encyclopedia/editorial-template.ts
new file mode 100644
index 00000000..33768472
--- /dev/null
+++ b/src/modules/food-encyclopedia/editorial-template.ts
@@ -0,0 +1,42 @@
+import type { FoodDraft } from "./editorial-contracts";
+export function newEditorialDraft(
+ kind: FoodDraft["kind"] = "dishes",
+ slug = "new-guide"
+): FoodDraft {
+ return {
+ kind,
+ slug,
+ title: "New food guide",
+ eyebrow: "FOOD ATLAS / EDITORIAL",
+ description:
+ "A concise explanation of this food, including the context that readers should know before they order or prepare it.",
+ tint: "cream",
+ aliases: [],
+ facts: [
+ { label: "Category", value: "To be researched" },
+ { label: "Tradition", value: "To be researched" },
+ ],
+ sections: [
+ {
+ id: "introduction",
+ eyebrow: "01 / DEFINITION",
+ title: "What is this food?",
+ body: [
+ "Introduce the dish or ingredient with independently researched facts and enough detail to help a reader understand its significance.",
+ ],
+ },
+ {
+ id: "background",
+ eyebrow: "02 / BACKGROUND",
+ title: "Ingredients, tastes and variations",
+ body: [
+ "Explain the typical ingredients, preparation differences and cultural context. Note important exceptions and avoid implying safety for individual restaurant servings.",
+ ],
+ },
+ ],
+ sourceUrls: [],
+ relatedSlugs: [],
+ caution:
+ "Preparations and food handling vary. Ask the restaurant for details about any allergy or dietary restriction.",
+ };
+}
diff --git a/src/modules/food-encyclopedia/provenance.test.ts b/src/modules/food-encyclopedia/provenance.test.ts
new file mode 100644
index 00000000..d5de9ee9
--- /dev/null
+++ b/src/modules/food-encyclopedia/provenance.test.ts
@@ -0,0 +1,32 @@
+import { describe, it, expect } from "vitest";
+import { prepareForHumanReview, referenceHosts, requiresArtworkReview } from "./provenance";
+import { newEditorialDraft } from "./editorial-template";
+describe("Food Encyclopedia editorial provenance", () => {
+ it("requires citations before a guide can enter human review", () => {
+ const draft = newEditorialDraft();
+ const result = prepareForHumanReview(draft);
+ expect(result.ok).toBe(false);
+ if (!result.ok) expect(result.issues.some((i) => i.includes("sourceUrls"))).toBe(true);
+ });
+ it("rejects embedded credentials and HTTP sources", () => {
+ const draft = {
+ ...newEditorialDraft(),
+ sourceUrls: ["https://user:pass@example.com/source", "http://example.org/page"],
+ };
+ expect(prepareForHumanReview(draft).ok).toBe(false);
+ });
+ it("checks hostnames without performing a network request", () => {
+ const draft = {
+ ...newEditorialDraft(),
+ sourceUrls: ["https://www.fao.org/foo", "https://fdc.nal.usda.gov/bar"],
+ };
+ expect(referenceHosts(draft)).toEqual(["www.fao.org", "fdc.nal.usda.gov"]);
+ });
+ it("never infers media rights from a local asset path", () => {
+ const draft = {
+ ...newEditorialDraft(),
+ imagePath: "/assets/food/dishes/indian/paneer-tikka.png",
+ };
+ expect(requiresArtworkReview(draft)).toBe(true);
+ });
+});
diff --git a/src/modules/food-encyclopedia/provenance.ts b/src/modules/food-encyclopedia/provenance.ts
new file mode 100644
index 00000000..37a09974
--- /dev/null
+++ b/src/modules/food-encyclopedia/provenance.ts
@@ -0,0 +1,25 @@
+import { FoodDraftSchema, editorialQualityIssues, type FoodDraft } from "./editorial-contracts";
+
+/** Only syntactic and editorial-presentation checks; cannot establish factual accuracy. */
+export function prepareForHumanReview(
+ raw: unknown
+): { ok: true; draft: FoodDraft } | { ok: false; issues: string[] } {
+ const result = FoodDraftSchema.safeParse(raw);
+ if (!result.success)
+ return {
+ ok: false,
+ issues: result.error.issues.map((i) => i.path.join(".") + ": " + i.message),
+ };
+ const issues = editorialQualityIssues(result.data);
+ return issues.length
+ ? { ok: false, issues: issues.map((i) => i.field + ": " + i.detail) }
+ : { ok: true, draft: result.data };
+}
+/** Do not fetch these URLs from the MenuMinute VPS. External references are untrusted data. */
+export function referenceHosts(draft: FoodDraft): string[] {
+ return draft.sourceUrls.map((url) => new URL(url).hostname.toLowerCase());
+}
+/** An attached image never becomes approved because its filename is "approved". */
+export function requiresArtworkReview(draft: FoodDraft): boolean {
+ return Boolean(draft.imagePath);
+}
diff --git a/tests/e2e/food-encyclopedia-atlas.spec.ts b/tests/e2e/food-encyclopedia-atlas.spec.ts
index 42f15279..77eac946 100644
--- a/tests/e2e/food-encyclopedia-atlas.spec.ts
+++ b/tests/e2e/food-encyclopedia-atlas.spec.ts
@@ -1,19 +1,21 @@
import { expect, test } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
-test("Atlas landing and browse render real routes without search-indexing previews", async ({
- page,
-}) => {
+// P2: the catalogue is not indexable until independently reviewed PostgreSQL
+// revisions are published. The previous P1 sample articles stay available only
+// in local development; production must not leak an unapproved demo article.
+test("Atlas homepage and browse fail closed without reviewed content", async ({ page }) => {
await page.goto("/explore");
await expect(page.getByRole("heading", { name: /Every dish.*has a story/i })).toBeVisible();
await expect(page.locator('meta[name="robots"]')).toHaveAttribute("content", /noindex/);
await expect(page.getByRole("searchbox", { name: "Search food guides" })).toBeVisible();
await page.getByRole("link", { name: /Dishes The foods we love/i }).click();
await expect(page).toHaveURL(/\/explore\/browse\?kind=dishes/);
- await expect(page.getByRole("link", { name: /Paneer Tikka/i }).first()).toBeVisible();
+ await expect(page.getByText(/first reviewed guides are being prepared/i)).toBeVisible();
+ await expect(page.getByRole("link", { name: /Paneer Tikka/i })).toHaveCount(0);
});
-test("Atlas search and editorial article work without ads or external AI calls", async ({
+test("Search indexes only published articles and never shows AdSense", async ({
page,
request,
}) => {
@@ -23,21 +25,16 @@ test("Atlas search and editorial article work without ads or external AI calls",
await search.press("Enter");
await expect(page).toHaveURL(/\/explore\/search\?q=paneer/);
await expect(page.getByRole("heading", { name: /What are you curious about/i })).toBeVisible();
- await page
- .getByRole("link", { name: /Paneer Tikka/i })
- .first()
- .click();
- await expect(page).toHaveURL(/\/explore\/dishes\/paneer-tikka/);
- await expect(page.getByRole("heading", { name: "Paneer Tikka", exact: true })).toBeVisible();
- await expect(page.getByText("Editorial preview.")).toBeVisible();
await expect(page.locator('meta[name="robots"]')).toHaveAttribute("content", /noindex/);
+ await expect(page.getByText(/No reviewed guide matched your search/i)).toBeVisible();
await expect(page.locator(".atlas-ad-reservation")).toHaveCount(0);
+ expect((await request.get("/explore/dishes/paneer-tikka")).status()).toBe(404);
expect((await request.get("/explore/dishes/missing-entry")).status()).toBe(404);
});
-test("Atlas mobile navigation and article remain within the viewport", async ({ page }) => {
+test("Atlas mobile navigation remains within the viewport", async ({ page }) => {
await page.setViewportSize({ width: 390, height: 844 });
- await page.goto("/explore/dishes/paneer-tikka");
+ await page.goto("/explore");
const overflow = await page.evaluate(
() => document.documentElement.scrollWidth - window.innerWidth
);
@@ -48,11 +45,10 @@ test("Atlas mobile navigation and article remain within the viewport", async ({
.getByRole("navigation", { name: "Mobile encyclopedia" })
.getByRole("link", { name: /Ingredients/ })
).toBeVisible();
- await expect(page.getByRole("navigation", { name: "On this page" })).toBeVisible();
});
-test("Atlas article has no serious WCAG A/AA violations", async ({ page }) => {
- await page.goto("/explore/dishes/paneer-tikka");
+test("Atlas public homepage has no serious WCAG A/AA violations", async ({ page }) => {
+ await page.goto("/explore");
const results = await new AxeBuilder({ page })
.withTags(["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"])
.analyze();
@@ -61,10 +57,15 @@ test("Atlas article has no serious WCAG A/AA violations", async ({ page }) => {
).toEqual([]);
});
-test("Atlas previews stay out of the public sitemap", async ({ request }) => {
+test("No draft or sample page is emitted in the public sitemap", async ({ request }) => {
const response = await request.get("/sitemap.xml");
expect(response.status()).toBe(200);
const xml = await response.text();
expect(xml).not.toContain("/explore/dishes/paneer-tikka");
expect(xml).not.toContain("/explore/search");
});
+
+test("Editorial admin requires authenticated privileged session", async ({ request }) => {
+ const response = await request.get("/admin/encyclopedia", { maxRedirects: 0 });
+ expect([302, 303, 307, 308, 404]).toContain(response.status());
+});
diff --git a/tests/food-encyclopedia-editorial.integration.test.ts b/tests/food-encyclopedia-editorial.integration.test.ts
new file mode 100644
index 00000000..e010ac8e
--- /dev/null
+++ b/tests/food-encyclopedia-editorial.integration.test.ts
@@ -0,0 +1,101 @@
+import { describe, expect, it } from "vitest";
+import { Pool } from "pg";
+import { admin, fixture } from "./helpers";
+
+// Every test uses the isolated CI database after the ordered migration ledger.
+const tables = [
+ "food_entries",
+ "food_revisions",
+ "food_sources",
+ "food_entry_sources",
+ "food_aliases",
+ "food_relations",
+ "food_media",
+ "food_editorial_events",
+ "food_redirects",
+ "food_candidates",
+];
+
+async function app(fn: (pool: Pool) => Promise): Promise {
+ const pool = new Pool({ connectionString: process.env["DATABASE_URL"], max: 1 });
+ try {
+ return await fn(pool);
+ } finally {
+ await pool.end();
+ }
+}
+
+describe("Food Encyclopedia P2 database isolation", () => {
+ it("has all private tables FORCE RLS and no app direct-table privileges", async () => {
+ await admin(async (_db, pool) => {
+ const res = await pool.query<{
+ relname: string;
+ relrowsecurity: boolean;
+ relforcerowsecurity: boolean;
+ }>(
+ "select relname,relrowsecurity,relforcerowsecurity from pg_class where relname=any($1::text[])",
+ [tables]
+ );
+ expect(res.rows.map((x) => x.relname).sort()).toEqual([...tables].sort());
+ expect(res.rows.every((x) => x.relrowsecurity && x.relforcerowsecurity)).toBe(true);
+ });
+ await app(async (pool) => {
+ for (const table of tables) {
+ const result = await pool.query<{ can_read: boolean; can_write: boolean }>(
+ "select has_table_privilege(current_user,$1,'SELECT') as can_read,has_table_privilege(current_user,$1,'INSERT') as can_write",
+ [table]
+ );
+ expect(result.rows[0]?.can_read).toBe(false);
+ expect(result.rows[0]?.can_write).toBe(false);
+ }
+ });
+ });
+ it("exposes only narrow functions to the runtime role", async () => {
+ await app(async (pool) => {
+ const r = await pool.query<{ public_read: boolean; editor_write: boolean }>(
+ "select has_function_privilege(current_user,'app_private.food_public_entry(text,text)','EXECUTE') as public_read,has_function_privilege(current_user,'app_private.food_editorial_command(text,text,jsonb,text)','EXECUTE') as editor_write"
+ );
+ expect(r.rows[0]).toMatchObject({ public_read: true, editor_write: true });
+ });
+ });
+ it("never exposes an unpublished entry through the public projection", async () => {
+ const f = await fixture();
+ const slug = "unreviewed-" + crypto.randomUUID().slice(0, 8);
+ await admin(async (_db, pool) => {
+ await pool.query(
+ "INSERT INTO food_entries(kind,slug,created_by,updated_by) VALUES ('dishes',$1,$2,$2)",
+ [slug, f.actor.userId]
+ );
+ });
+ await app(async (pool) => {
+ const r = await pool.query<{ entry: unknown }>(
+ "SELECT app_private.food_public_entry($1,$2) AS entry",
+ ["dishes", slug]
+ );
+ expect(r.rows[0]?.entry).toBeNull();
+ const list = await pool.query<{ entry: unknown }>(
+ "SELECT entry FROM app_private.food_public_list($1,$2,$3,$4)",
+ ["dishes", slug, 20, 0]
+ );
+ expect(list.rows).toEqual([]);
+ });
+ });
+ it("denies all unbound editorial mutations without session and step-up", async () => {
+ await app(async (pool) => {
+ await expect(
+ pool.query("SELECT app_private.food_editorial_command($1,$2,$3::jsonb,$4)", [
+ "create",
+ "new",
+ "{}",
+ crypto.randomUUID(),
+ ])
+ ).rejects.toMatchObject({ code: "42501" });
+ await expect(
+ pool.query("SELECT app_private.food_import_candidates($1::jsonb,$2)", [
+ "[]",
+ crypto.randomUUID(),
+ ])
+ ).rejects.toMatchObject({ code: "42501" });
+ });
+ });
+});