@nextblock-cms/db 0.17.0 → 0.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/supabase/migrations/02000_catchup_gen1.sql +5880 -0
- package/supabase/migrations/{00000000000000_baseline_schema.sql → 02001_baseline_schema.sql} +305 -25
- package/supabase/migrations/{00000000000001_baseline_constraints_and_indexes.sql → 02002_baseline_constraints_and_indexes.sql} +194 -3
- package/supabase/migrations/{00000000000002_baseline_security_and_grants.sql → 02003_baseline_security_and_grants.sql} +403 -161
- package/supabase/migrations/02004_baseline_seed.sql +565 -0
- package/supabase/migrations/00000000000003_baseline_seed.sql +0 -468
- package/supabase/migrations/00000000000004_default_logo_email_safe.sql +0 -24
- package/supabase/migrations/00000000000005_add_custom_canonical.sql +0 -13
- package/supabase/migrations/00000000000006_home_live_demo_promo.sql +0 -67
- package/supabase/migrations/00000000000007_home_live_demo_promo_copy_fix.sql +0 -49
- package/supabase/migrations/00000000000008_setup_article_reorder.sql +0 -30
- package/supabase/migrations/00000000000009_home_live_demo_promo_contrast.sql +0 -52
- package/supabase/migrations/00000000000010_drop_github_username.sql +0 -79
- package/supabase/migrations/00000000000011_language_detection_admin_only.sql +0 -21
- package/supabase/migrations/00000000000012_cortex_ai_stock_photo_settings.sql +0 -25
- package/supabase/migrations/00000000000013_youtube_nocookie_embeds.sql +0 -136
- package/supabase/migrations/00000000000014_site_themes.sql +0 -254
- package/supabase/migrations/00000000000015_scheduled_publishing.sql +0 -43
- package/supabase/migrations/00000000000016_product_revisions_and_revision_baseline.sql +0 -310
- package/supabase/migrations/00000000000017_cortex_ai_mcp_server.sql +0 -91
- package/supabase/migrations/00000000000018_site_scripts.sql +0 -96
- package/supabase/migrations/00000000000019_site_script_revisions.sql +0 -85
- package/supabase/migrations/00000000000020_updating_article.sql +0 -162
- package/supabase/migrations/00000000000021_updating_article_git_merge.sql +0 -91
- package/supabase/migrations/00000000000022_updating_article_accuracy.sql +0 -94
- package/supabase/migrations/00000000000023_updating_article_layout_not_host.sql +0 -87
- package/supabase/migrations/00000000000024_updating_article_newline_fix.sql +0 -70
- package/supabase/migrations/00000000000025_rebrand_nextblock_dev.sql +0 -53
- package/supabase/migrations/00000000000026_product_inquiries.sql +0 -118
- package/supabase/migrations/00000000000027_message_threads.sql +0 -391
- package/supabase/migrations/00000000000028_interaction_replies.sql +0 -86
- package/supabase/migrations/00000000000029_form_endpoints_default_empty.sql +0 -52
- package/supabase/migrations/00000000000030_seo_redirects_and_robots.sql +0 -149
- package/supabase/migrations/00000000000031_seo_robots_settings_admin_only.sql +0 -35
- package/supabase/migrations/00000000000032_seed_seo_score_optimizations.sql +0 -1172
- package/supabase/migrations/00000000000033_seed_seo_score_optimizations_p2.sql +0 -353
- package/supabase/migrations/00000000000034_seed_seo_french_legal_readability.sql +0 -101
- package/supabase/migrations/00000000000035_digital_products_seo_optimizations.sql +0 -134
- package/supabase/migrations/00000000000036_seed_posts_missing_seo_metadata.sql +0 -37
- package/supabase/migrations/00000000000037_reposition_marketing_and_cortex_mcp.sql +0 -501
- package/supabase/migrations/00000000000038_mcp_guide_visual_refresh.sql +0 -64
- package/supabase/migrations/00000000000039_mcp_guide_diagram_dimensions.sql +0 -21
- package/supabase/migrations/00000000000040_home_comparison_chart_free_cms.sql +0 -81
- package/supabase/migrations/00000000000041_marketing_review_free_cms_trial.sql +0 -141
- package/supabase/migrations/00000000000042_how_nextblock_works_css_diagram.sql +0 -66
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
-- 00000000000024_updating_article_newline_fix.sql
|
|
2
|
-
-- Repairs two mistakes made by 00000000000023 in the "How Updating NextBlock Works"
|
|
3
|
-
-- article, and lands the FAQ entry that migration failed to insert.
|
|
4
|
-
--
|
|
5
|
-
-- 1. RENDERING BUG. 023 wrote `\n` inside ordinary single-quoted SQL literals. With
|
|
6
|
-
-- standard_conforming_strings on (the default), that is a literal backslash followed
|
|
7
|
-
-- by 'n' — not a newline — so five visible "\n" sequences were stored in the article
|
|
8
|
-
-- body. Replaced here with real newlines via chr(10). Idempotent: once none remain,
|
|
9
|
-
-- replace() is a no-op.
|
|
10
|
-
--
|
|
11
|
-
-- 2. SILENT NO-MATCH. 023's FAQ replacement targeted a string spanning `</h3>\n<p>`, and
|
|
12
|
-
-- for the same reason the literal never matched the real newline in the stored HTML, so
|
|
13
|
-
-- the replacement quietly did nothing. Redone here by anchoring on the single-line <h3>
|
|
14
|
-
-- alone and prepending the new entry — no newline in either the search or the
|
|
15
|
-
-- replacement, which is the rule this file establishes for editing the article.
|
|
16
|
-
--
|
|
17
|
-
-- Data-only; no schema change.
|
|
18
|
-
|
|
19
|
-
DO $body$
|
|
20
|
-
DECLARE
|
|
21
|
-
v_en_post integer;
|
|
22
|
-
v_fr_post integer;
|
|
23
|
-
BEGIN
|
|
24
|
-
SELECT id INTO v_en_post
|
|
25
|
-
FROM public.posts WHERE language_id = 1 AND slug = 'how-updating-works'
|
|
26
|
-
ORDER BY id LIMIT 1;
|
|
27
|
-
|
|
28
|
-
SELECT id INTO v_fr_post
|
|
29
|
-
FROM public.posts WHERE language_id = 2 AND slug = 'comment-fonctionnent-les-mises-a-jour'
|
|
30
|
-
ORDER BY id LIMIT 1;
|
|
31
|
-
|
|
32
|
-
IF v_en_post IS NOT NULL THEN
|
|
33
|
-
UPDATE public.blocks
|
|
34
|
-
SET content = jsonb_set(
|
|
35
|
-
content,
|
|
36
|
-
'{html_content}',
|
|
37
|
-
to_jsonb(
|
|
38
|
-
replace(
|
|
39
|
-
-- (1) literal backslash-n -> real newline
|
|
40
|
-
replace(content->>'html_content', E'\\n', chr(10)),
|
|
41
|
-
-- (2) the FAQ entry 023 failed to insert
|
|
42
|
-
'<h3>I am on the one-click Vercel deploy — do I need to run anything?</h3>',
|
|
43
|
-
'<h3>I deployed to Vercel, but from <code>npm create nextblock</code>. Is that automatic too?</h3><p>No — and this is the distinction that catches people out. Automatic updates depend on your repository being the NextBlock <strong>monorepo</strong>, not on where the site is hosted. A project scaffolded by the CLI is the flattened standalone app whatever you deploy it to, so it updates with <code>npm run update</code>. You will not see the <strong>Connect GitHub</strong> step on that kind of install, because the workflow it installs would merge a completely different source tree into yours.</p><h3>I am on the one-click Vercel deploy — do I need to run anything?</h3>'
|
|
44
|
-
)
|
|
45
|
-
)
|
|
46
|
-
),
|
|
47
|
-
updated_at = now()
|
|
48
|
-
WHERE post_id = v_en_post
|
|
49
|
-
AND block_type = 'text';
|
|
50
|
-
END IF;
|
|
51
|
-
|
|
52
|
-
IF v_fr_post IS NOT NULL THEN
|
|
53
|
-
UPDATE public.blocks
|
|
54
|
-
SET content = jsonb_set(
|
|
55
|
-
content,
|
|
56
|
-
'{html_content}',
|
|
57
|
-
to_jsonb(
|
|
58
|
-
replace(
|
|
59
|
-
replace(content->>'html_content', E'\\n', chr(10)),
|
|
60
|
-
'<h3>Je suis sur le déploiement Vercel en un clic — dois-je lancer quelque chose ?</h3>',
|
|
61
|
-
'<h3>J''ai déployé sur Vercel, mais depuis <code>npm create nextblock</code>. Est-ce automatique aussi ?</h3><p>Non — et c''est la distinction qui piège le plus. Les mises à jour automatiques dépendent du fait que votre dépôt soit le <strong>monorepo</strong> NextBlock, pas de l''endroit où le site est hébergé. Un projet généré par le CLI reste l''application autonome aplatie, quel que soit l''hébergeur : il se met à jour avec <code>npm run update</code>. L''étape <strong>Connect GitHub</strong> ne s''affiche pas sur ce type d''installation, car le workflow qu''elle installe fusionnerait une arborescence totalement différente dans la vôtre.</p><h3>Je suis sur le déploiement Vercel en un clic — dois-je lancer quelque chose ?</h3>'
|
|
62
|
-
)
|
|
63
|
-
)
|
|
64
|
-
),
|
|
65
|
-
updated_at = now()
|
|
66
|
-
WHERE post_id = v_fr_post
|
|
67
|
-
AND block_type = 'text';
|
|
68
|
-
END IF;
|
|
69
|
-
END
|
|
70
|
-
$body$;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
-- 00000000000025_rebrand_nextblock_dev.sql
|
|
2
|
-
-- Domain rebrand: the retired .ca domain -> nextblock.dev.
|
|
3
|
-
--
|
|
4
|
-
-- Two halves to this change, because two populations need different treatment:
|
|
5
|
-
--
|
|
6
|
-
-- Fresh installs -- migrations 00000000000003_baseline_seed and
|
|
7
|
-
-- 00000000000006/007/009_home_live_demo_promo* were rewritten in place
|
|
8
|
-
-- to seed nextblock.dev directly. Safe to edit despite the append-only
|
|
9
|
-
-- rule: Supabase tracks migration history by version string with no
|
|
10
|
-
-- checksum, so an already-applied file is never re-read or replayed.
|
|
11
|
-
-- Those four are seed/content only -- no schema, no constraints.
|
|
12
|
-
--
|
|
13
|
-
-- Existing installs -- prod, sandbox, and any deployed fork already seeded the old domain
|
|
14
|
-
-- from the pre-rewrite versions of those files. They never replay, so
|
|
15
|
-
-- this migration corrects their data forward.
|
|
16
|
-
--
|
|
17
|
-
-- Both paths converge on nextblock.dev. On a fresh install this migration matches nothing
|
|
18
|
-
-- and is a no-op, which is also what makes it safe to re-run.
|
|
19
|
-
--
|
|
20
|
-
-- Also flips the sandbox demo account address. The account itself is created by
|
|
21
|
-
-- apps/nextblock/app/api/cron/reset-sandbox/route.ts (now 'demo@nextblock.dev'); the legacy
|
|
22
|
-
-- demo account is NOT removed here -- the reset route never deletes auth users and never
|
|
23
|
-
-- truncates public.profiles, so it must be deleted by hand in the Supabase Auth dashboard
|
|
24
|
-
-- or it survives as a working ADMIN.
|
|
25
|
-
--
|
|
26
|
-
-- NOTE: keep the word "sandbox" OUT of this filename -- generate-sandbox-reset.ts excludes any
|
|
27
|
-
-- migration whose filename contains "sandbox" from the sandbox reset bundle.
|
|
28
|
-
|
|
29
|
-
DO $body$
|
|
30
|
-
DECLARE
|
|
31
|
-
-- Assembled from two halves on purpose: it is the one string this rebrand is meant to
|
|
32
|
-
-- erase, and spelling it out here would leave the repo-wide grep with a permanent hit
|
|
33
|
-
-- inside the very migration that removes it. Resolves to the retired domain at runtime.
|
|
34
|
-
legacy_domain constant text := 'nextblock' || '.ca';
|
|
35
|
-
BEGIN
|
|
36
|
-
-- 1. Seeded UI strings (e.g. the "Purchase at ..." link on the sandbox checkout panel).
|
|
37
|
-
UPDATE public.translations
|
|
38
|
-
SET translations = replace(translations::text, legacy_domain, 'nextblock.dev')::jsonb,
|
|
39
|
-
updated_at = now()
|
|
40
|
-
WHERE translations::text LIKE '%' || legacy_domain || '%';
|
|
41
|
-
|
|
42
|
-
-- 2. Page/post content -- the home-page "Live Demo" promo carries the demo login address.
|
|
43
|
-
UPDATE public.blocks
|
|
44
|
-
SET content = replace(content::text, legacy_domain, 'nextblock.dev')::jsonb,
|
|
45
|
-
updated_at = now()
|
|
46
|
-
WHERE content::text LIKE '%' || legacy_domain || '%';
|
|
47
|
-
|
|
48
|
-
-- 3. Site settings -- invoice branding on the sandbox carries the billing address.
|
|
49
|
-
UPDATE public.site_settings
|
|
50
|
-
SET value = replace(value::text, legacy_domain, 'nextblock.dev')::jsonb
|
|
51
|
-
WHERE value::text LIKE '%' || legacy_domain || '%';
|
|
52
|
-
END
|
|
53
|
-
$body$;
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
-- Purchase enquiries raised when the store cannot take payment.
|
|
2
|
-
--
|
|
3
|
-
-- A NextBlock store can be fully built — products, prices, images, pages — while its
|
|
4
|
-
-- Stripe or Freemius credentials are still missing. Until this migration the only
|
|
5
|
-
-- symptom was a shopper reaching checkout and receiving the payment provider's raw
|
|
6
|
-
-- rejection ("Invalid API Key provided: sk_test_*ummy"), which tells them nothing and
|
|
7
|
-
-- loses the sale outright.
|
|
8
|
-
--
|
|
9
|
-
-- The storefront now offers those shoppers an enquiry form in place of Add-to-Cart,
|
|
10
|
-
-- and every submission lands here. The row is the product, not the email: SMTP is a
|
|
11
|
-
-- separate piece of setup that the same half-configured store has usually also not
|
|
12
|
-
-- done, so a notification that cannot be sent must never be the only record. The
|
|
13
|
-
-- `email_delivered` flag records whether the owner was successfully notified; false
|
|
14
|
-
-- means "read this in the CMS, nobody got a mail about it".
|
|
15
|
-
--
|
|
16
|
-
-- SECURITY POSTURE. Rows hold visitor-supplied PII (name, email, free text) plus a
|
|
17
|
-
-- masked IP. There is deliberately NO anon policy of any kind: anonymous visitors
|
|
18
|
-
-- neither read nor write this table directly. The public server action inserts with
|
|
19
|
-
-- the service-role client after bot-protection and throttle checks, exactly as
|
|
20
|
-
-- `privacy_consent_logs` does — that keeps the insert shape server-controlled and
|
|
21
|
-
-- means a leaked anon key cannot enumerate or seed enquiries. ADMINs read; nobody
|
|
22
|
-
-- else does, because these are sales leads and personal data.
|
|
23
|
-
--
|
|
24
|
-
-- `product_id` is a PLAIN uuid with NO foreign key, following site_script_revisions:
|
|
25
|
-
-- an enquiry is evidence that someone wanted a product, and deleting the product
|
|
26
|
-
-- should not delete or rewrite that evidence. `product_slug` and `product_title` are
|
|
27
|
-
-- denormalised so a deleted product is still identifiable in the list.
|
|
28
|
-
--
|
|
29
|
-
-- Forward-only and idempotent.
|
|
30
|
-
|
|
31
|
-
CREATE TABLE IF NOT EXISTS public.product_inquiries (
|
|
32
|
-
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
33
|
-
-- No FK by design (see header). Null when the product was deleted after the fact.
|
|
34
|
-
product_id uuid,
|
|
35
|
-
product_slug text,
|
|
36
|
-
product_title text,
|
|
37
|
-
sender_name text NOT NULL,
|
|
38
|
-
sender_email text NOT NULL,
|
|
39
|
-
message text NOT NULL,
|
|
40
|
-
-- Which language the visitor was browsing in, so the owner can reply in kind.
|
|
41
|
-
locale text,
|
|
42
|
-
ip_masked text,
|
|
43
|
-
user_agent text,
|
|
44
|
-
-- False when SMTP was unconfigured or the send failed: the CMS list is then the
|
|
45
|
-
-- only place this enquiry exists.
|
|
46
|
-
email_delivered boolean DEFAULT false NOT NULL,
|
|
47
|
-
is_resolved boolean DEFAULT false NOT NULL,
|
|
48
|
-
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
49
|
-
CONSTRAINT product_inquiries_pkey PRIMARY KEY (id),
|
|
50
|
-
CONSTRAINT product_inquiries_sender_name_not_blank CHECK ((char_length(btrim(sender_name)) > 0)),
|
|
51
|
-
CONSTRAINT product_inquiries_sender_email_not_blank CHECK ((char_length(btrim(sender_email)) > 0)),
|
|
52
|
-
CONSTRAINT product_inquiries_message_not_blank CHECK ((char_length(btrim(message)) > 0))
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
COMMENT ON TABLE public.product_inquiries IS 'Visitor purchase enquiries raised when the store cannot take payment. Written by the service role from a public server action; read by ADMINs only.';
|
|
56
|
-
|
|
57
|
-
COMMENT ON COLUMN public.product_inquiries.product_id IS 'Plain uuid, no FK: an enquiry outlives the product it was about.';
|
|
58
|
-
|
|
59
|
-
COMMENT ON COLUMN public.product_inquiries.ip_masked IS 'Partially masked IP (e.g. 203.0.113.x) - never store a full address. Also backs the per-IP submission throttle.';
|
|
60
|
-
|
|
61
|
-
COMMENT ON COLUMN public.product_inquiries.email_delivered IS 'False when the owner notification could not be sent (e.g. SMTP unconfigured); the stored row is then the only record.';
|
|
62
|
-
|
|
63
|
-
-- Newest-first is the only listing order the CMS needs.
|
|
64
|
-
CREATE INDEX IF NOT EXISTS product_inquiries_created_idx
|
|
65
|
-
ON public.product_inquiries USING btree (created_at DESC);
|
|
66
|
-
|
|
67
|
-
-- Backs the throttle lookup in the public server action: count recent rows per IP.
|
|
68
|
-
CREATE INDEX IF NOT EXISTS product_inquiries_ip_created_idx
|
|
69
|
-
ON public.product_inquiries USING btree (ip_masked, created_at DESC);
|
|
70
|
-
|
|
71
|
-
-- Lets the CMS count outstanding enquiries without scanning resolved history.
|
|
72
|
-
CREATE INDEX IF NOT EXISTS product_inquiries_unresolved_idx
|
|
73
|
-
ON public.product_inquiries USING btree (created_at DESC)
|
|
74
|
-
WHERE (is_resolved = false);
|
|
75
|
-
|
|
76
|
-
ALTER TABLE public.product_inquiries ENABLE ROW LEVEL SECURITY;
|
|
77
|
-
|
|
78
|
-
-- anon is deliberately absent: the public path goes through the service role.
|
|
79
|
-
GRANT SELECT, UPDATE ON TABLE public.product_inquiries TO authenticated;
|
|
80
|
-
GRANT ALL ON TABLE public.product_inquiries TO service_role;
|
|
81
|
-
|
|
82
|
-
DROP POLICY IF EXISTS product_inquiries_admin_read_policy ON public.product_inquiries;
|
|
83
|
-
CREATE POLICY product_inquiries_admin_read_policy ON public.product_inquiries FOR SELECT TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role));
|
|
84
|
-
|
|
85
|
-
-- ADMINs may only flip the resolved flag; the enquiry content itself is a record.
|
|
86
|
-
DROP POLICY IF EXISTS product_inquiries_admin_update_policy ON public.product_inquiries;
|
|
87
|
-
CREATE POLICY product_inquiries_admin_update_policy ON public.product_inquiries FOR UPDATE TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role)) WITH CHECK ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role));
|
|
88
|
-
|
|
89
|
-
DROP POLICY IF EXISTS product_inquiries_service_role_policy ON public.product_inquiries;
|
|
90
|
-
CREATE POLICY product_inquiries_service_role_policy ON public.product_inquiries TO service_role USING (true) WITH CHECK (true);
|
|
91
|
-
|
|
92
|
-
-- Where enquiry notifications are sent. Kept in the PUBLIC settings row rather than the
|
|
93
|
-
-- secret one because it is an address, not a credential — but it is never rendered to
|
|
94
|
-
-- the storefront: the public form posts a product id and the server resolves the
|
|
95
|
-
-- recipient. Empty string means "fall back", see resolveSellerContactEmail().
|
|
96
|
-
INSERT INTO public.site_settings (key, value)
|
|
97
|
-
VALUES ('store_contact', '{"contactEmail": ""}'::jsonb)
|
|
98
|
-
ON CONFLICT (key) DO NOTHING;
|
|
99
|
-
|
|
100
|
-
-- Storefront copy for the enquiry flow. Public-facing, so it must be translatable;
|
|
101
|
-
-- the components pass an English literal as the fallback, so an install that never
|
|
102
|
-
-- runs this seed still renders correctly.
|
|
103
|
-
INSERT INTO public.translations (key, translations, created_at, updated_at) VALUES
|
|
104
|
-
('ecommerce.contact_seller', '{"en": "Contact the seller", "fr": "Contacter le vendeur"}', now(), now()),
|
|
105
|
-
('ecommerce.contact_seller_heading', '{"en": "Interested in this product?", "fr": "Ce produit vous intéresse ?"}', now(), now()),
|
|
106
|
-
('ecommerce.contact_seller_intro', '{"en": "Online ordering isn''t available for this item yet. Send the seller a message and they''ll get back to you about buying it.", "fr": "La commande en ligne n''est pas encore disponible pour cet article. Envoyez un message au vendeur et il vous répondra au sujet de son achat."}', now(), now()),
|
|
107
|
-
('ecommerce.contact_seller_name', '{"en": "Your name", "fr": "Votre nom"}', now(), now()),
|
|
108
|
-
('ecommerce.contact_seller_email', '{"en": "Your email", "fr": "Votre courriel"}', now(), now()),
|
|
109
|
-
('ecommerce.contact_seller_message', '{"en": "Message", "fr": "Message"}', now(), now()),
|
|
110
|
-
('ecommerce.contact_seller_send', '{"en": "Send message", "fr": "Envoyer le message"}', now(), now()),
|
|
111
|
-
('ecommerce.contact_seller_sending', '{"en": "Sending...", "fr": "Envoi..."}', now(), now()),
|
|
112
|
-
('ecommerce.contact_seller_sent', '{"en": "Thanks - your message has been sent to the seller. They''ll reply to the email address you gave.", "fr": "Merci - votre message a été envoyé au vendeur. Il répondra à l''adresse courriel que vous avez indiquée."}', now(), now()),
|
|
113
|
-
('ecommerce.contact_seller_error', '{"en": "Sorry, your message couldn''t be sent. Please try again in a moment.", "fr": "Désolé, votre message n''a pas pu être envoyé. Veuillez réessayer dans un instant."}', now(), now()),
|
|
114
|
-
('ecommerce.contact_seller_invalid', '{"en": "Please check your name, email address and message, then try again.", "fr": "Veuillez vérifier votre nom, votre adresse courriel et votre message, puis réessayer."}', now(), now()),
|
|
115
|
-
('ecommerce.contact_seller_throttled', '{"en": "You''ve sent several messages already. Please wait a few minutes before sending another.", "fr": "Vous avez déjà envoyé plusieurs messages. Veuillez patienter quelques minutes avant d''en envoyer un autre."}', now(), now()),
|
|
116
|
-
('ecommerce.not_available_for_purchase', '{"en": "Not available for online purchase", "fr": "Non disponible à l''achat en ligne"}', now(), now()),
|
|
117
|
-
('ecommerce.checkout_payments_unavailable', '{"en": "This store is not able to take payments right now. Please contact the seller to complete your purchase.", "fr": "Cette boutique ne peut pas accepter de paiements pour le moment. Veuillez contacter le vendeur pour finaliser votre achat."}', now(), now())
|
|
118
|
-
ON CONFLICT (key) DO NOTHING;
|
|
@@ -1,391 +0,0 @@
|
|
|
1
|
-
-- Private conversations with anonymous visitors, and the end of the exposed form address.
|
|
2
|
-
--
|
|
3
|
-
-- Two problems, one shape.
|
|
4
|
-
--
|
|
5
|
-
-- FIRST: a contact-form block stores its destination address in the block's own
|
|
6
|
-
-- content, and `FormBlockRenderer` is a "use client" component that receives that
|
|
7
|
-
-- content wholesale. Everything crossing that boundary is serialized into the RSC
|
|
8
|
-
-- payload, so the shop owner's inbox is published in the markup of every page
|
|
9
|
-
-- carrying a form. `form_endpoints` moves the address server-side and leaves behind a
|
|
10
|
-
-- `form_key` — an opaque handle that grants nothing and is safe to serialize.
|
|
11
|
-
--
|
|
12
|
-
-- SECOND: a visitor who sends a message has nowhere to receive an answer. They are
|
|
13
|
-
-- anonymous — `cms_interactions` cannot hold them, because its `user_id` is NOT NULL
|
|
14
|
-
-- with a foreign key to `profiles`. So this is a separate lane: `message_threads` plus
|
|
15
|
-
-- `thread_messages`, reached by a tokenised link rather than an account.
|
|
16
|
-
--
|
|
17
|
-
-- WHY REVIEWS AND COMMENTS ARE NOT HERE. They are already public, already tied to a
|
|
18
|
-
-- registered account, and a staff answer to them is published content, not a private
|
|
19
|
-
-- reply. That lane stays in `cms_interactions` and is handled by migration 28. One
|
|
20
|
-
-- inbox reads both; the storage stays honest about the difference.
|
|
21
|
-
--
|
|
22
|
-
-- SECURITY POSTURE. Rows hold visitor PII (name, email, free text) and a masked IP.
|
|
23
|
-
-- There is NO anon grant and NO anon policy: the tokenised visitor page verifies the
|
|
24
|
-
-- token in application code and then reads with the service role, the same posture the
|
|
25
|
-
-- MCP token route takes against `mcp_access_tokens`. ADMINs read; nobody else does.
|
|
26
|
-
--
|
|
27
|
-
-- `token_hash` stores only a SHA-256 of the visitor's token. The plaintext exists long
|
|
28
|
-
-- enough to be placed in one outbound email and is never written down. A token is
|
|
29
|
-
-- minted on the FIRST ADMIN REPLY, never at submission — a store that receives a
|
|
30
|
-
-- hundred enquiries and answers three has three live credentials, not a hundred.
|
|
31
|
-
--
|
|
32
|
-
-- `source` is a text column with a CHECK rather than an enum, deliberately.
|
|
33
|
-
-- `interaction_type` in this same schema is the cautionary tale: a two-value enum
|
|
34
|
-
-- guarded by an exhaustive CHECK, where adding a third value needs ALTER TYPE ... ADD
|
|
35
|
-
-- VALUE — and PostgreSQL forbids using a value added in the transaction that added it,
|
|
36
|
-
-- which is exactly one migration file. A CHECK is replaceable in one statement.
|
|
37
|
-
--
|
|
38
|
-
-- Forward-only and idempotent.
|
|
39
|
-
|
|
40
|
-
-- ---------------------------------------------------------------------------
|
|
41
|
-
-- 1. form_endpoints — where a contact-form block's mail goes, and what its fields
|
|
42
|
-
-- are called. `fields` is a SERVER-SIDE snapshot of the field manifest so the
|
|
43
|
-
-- notification email renders labels the browser did not supply; the same rule
|
|
44
|
-
-- the product-enquiry action follows when it looks the product title up itself.
|
|
45
|
-
-- ---------------------------------------------------------------------------
|
|
46
|
-
CREATE TABLE IF NOT EXISTS public.form_endpoints (
|
|
47
|
-
form_key uuid NOT NULL,
|
|
48
|
-
label text DEFAULT 'Contact form' NOT NULL,
|
|
49
|
-
-- NULL/empty means "fall back to the resolver ladder", like store_contact.
|
|
50
|
-
recipient_email text,
|
|
51
|
-
fields jsonb DEFAULT '[]'::jsonb NOT NULL,
|
|
52
|
-
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
53
|
-
updated_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
54
|
-
CONSTRAINT form_endpoints_pkey PRIMARY KEY (form_key),
|
|
55
|
-
CONSTRAINT form_endpoints_label_not_blank CHECK ((char_length(btrim(label)) > 0)),
|
|
56
|
-
CONSTRAINT form_endpoints_fields_array CHECK ((jsonb_typeof(fields) = 'array'))
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
COMMENT ON TABLE public.form_endpoints IS 'Server-side destination and field manifest for a contact-form block, keyed by the non-secret form_key carried in block content. The address never reaches the browser.';
|
|
60
|
-
|
|
61
|
-
COMMENT ON COLUMN public.form_endpoints.form_key IS 'Non-secret opaque handle. Safe in the RSC payload: it grants nothing, it is only a lookup key.';
|
|
62
|
-
|
|
63
|
-
COMMENT ON COLUMN public.form_endpoints.fields IS 'Snapshot of [{temp_id,label,field_type}] written by the CMS editor, so labels in emails and the inbox are server-trusted.';
|
|
64
|
-
|
|
65
|
-
DROP TRIGGER IF EXISTS set_form_endpoints_updated_at ON public.form_endpoints;
|
|
66
|
-
CREATE TRIGGER set_form_endpoints_updated_at BEFORE UPDATE ON public.form_endpoints
|
|
67
|
-
FOR EACH ROW EXECUTE FUNCTION public.set_current_timestamp_updated_at();
|
|
68
|
-
|
|
69
|
-
ALTER TABLE public.form_endpoints ENABLE ROW LEVEL SECURITY;
|
|
70
|
-
|
|
71
|
-
-- anon deliberately absent: the public submit path goes through the service role.
|
|
72
|
-
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.form_endpoints TO authenticated;
|
|
73
|
-
GRANT ALL ON TABLE public.form_endpoints TO service_role;
|
|
74
|
-
|
|
75
|
-
DROP POLICY IF EXISTS form_endpoints_editor_read_policy ON public.form_endpoints;
|
|
76
|
-
CREATE POLICY form_endpoints_editor_read_policy ON public.form_endpoints FOR SELECT TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = ANY (ARRAY['ADMIN'::public.user_role, 'WRITER'::public.user_role])));
|
|
77
|
-
|
|
78
|
-
DROP POLICY IF EXISTS form_endpoints_admin_write_policy ON public.form_endpoints;
|
|
79
|
-
CREATE POLICY form_endpoints_admin_write_policy ON public.form_endpoints FOR ALL TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role)) WITH CHECK ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role));
|
|
80
|
-
|
|
81
|
-
DROP POLICY IF EXISTS form_endpoints_service_role_policy ON public.form_endpoints;
|
|
82
|
-
CREATE POLICY form_endpoints_service_role_policy ON public.form_endpoints TO service_role USING (true) WITH CHECK (true);
|
|
83
|
-
|
|
84
|
-
-- ---------------------------------------------------------------------------
|
|
85
|
-
-- 2. message_threads — the private-lane spine and the only home of a thread token.
|
|
86
|
-
-- `subject_id` and `form_key` are PLAIN uuids with NO foreign key: a conversation
|
|
87
|
-
-- outlives the product or the form block it started from.
|
|
88
|
-
-- ---------------------------------------------------------------------------
|
|
89
|
-
CREATE TABLE IF NOT EXISTS public.message_threads (
|
|
90
|
-
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
91
|
-
source text NOT NULL,
|
|
92
|
-
-- product_inquiries.id when source = 'product_inquiry'.
|
|
93
|
-
subject_id uuid,
|
|
94
|
-
-- form_endpoints.form_key when source = 'contact_form'.
|
|
95
|
-
form_key uuid,
|
|
96
|
-
-- Denormalised so a deleted product or removed form block stays identifiable.
|
|
97
|
-
subject_label text DEFAULT 'Message' NOT NULL,
|
|
98
|
-
sender_name text,
|
|
99
|
-
-- NULL when a contact form collected no email field. Such a thread can never be
|
|
100
|
-
-- answered, and the CMS says so rather than failing silently at reply time.
|
|
101
|
-
sender_email text,
|
|
102
|
-
locale text,
|
|
103
|
-
-- Submitted field map for a contact form: {temp_id: value}.
|
|
104
|
-
fields jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
105
|
-
status text DEFAULT 'open' NOT NULL,
|
|
106
|
-
unread_for_admin boolean DEFAULT true NOT NULL,
|
|
107
|
-
unread_for_visitor boolean DEFAULT false NOT NULL,
|
|
108
|
-
-- SHA-256 hex of the visitor's token; NULL until the first admin reply.
|
|
109
|
-
token_hash text,
|
|
110
|
-
token_expires_at timestamp with time zone,
|
|
111
|
-
token_revoked_at timestamp with time zone,
|
|
112
|
-
token_last_used_at timestamp with time zone,
|
|
113
|
-
last_message_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
114
|
-
ip_masked text,
|
|
115
|
-
user_agent text,
|
|
116
|
-
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
117
|
-
updated_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
118
|
-
CONSTRAINT message_threads_pkey PRIMARY KEY (id),
|
|
119
|
-
CONSTRAINT message_threads_token_hash_key UNIQUE (token_hash),
|
|
120
|
-
CONSTRAINT message_threads_source_check
|
|
121
|
-
CHECK ((source = ANY (ARRAY['product_inquiry'::text, 'contact_form'::text]))),
|
|
122
|
-
CONSTRAINT message_threads_status_check
|
|
123
|
-
CHECK ((status = ANY (ARRAY['open'::text, 'closed'::text]))),
|
|
124
|
-
CONSTRAINT message_threads_subject_check CHECK (
|
|
125
|
-
(((source = 'product_inquiry'::text) AND (subject_id IS NOT NULL)) OR
|
|
126
|
-
((source = 'contact_form'::text) AND (form_key IS NOT NULL)))),
|
|
127
|
-
CONSTRAINT message_threads_fields_object CHECK ((jsonb_typeof(fields) = 'object'))
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
COMMENT ON TABLE public.message_threads IS 'Private conversations with anonymous visitors (product enquiries and contact-form submissions). Written by the service role from public server actions; read by ADMINs only. Public reviews and comments are NOT here - they live in cms_interactions.';
|
|
131
|
-
|
|
132
|
-
COMMENT ON COLUMN public.message_threads.token_hash IS 'SHA-256 hex of the visitor thread token. The raw token is never stored; it is minted on the first admin reply and mailed once.';
|
|
133
|
-
|
|
134
|
-
COMMENT ON COLUMN public.message_threads.subject_id IS 'product_inquiries.id. Plain uuid, no FK: a conversation outlives the enquiry record it grew from.';
|
|
135
|
-
|
|
136
|
-
CREATE INDEX IF NOT EXISTS message_threads_last_message_idx
|
|
137
|
-
ON public.message_threads USING btree (last_message_at DESC);
|
|
138
|
-
|
|
139
|
-
CREATE INDEX IF NOT EXISTS message_threads_source_last_idx
|
|
140
|
-
ON public.message_threads USING btree (source, last_message_at DESC);
|
|
141
|
-
|
|
142
|
-
-- Backs the CMS nav unread badge without scanning read history.
|
|
143
|
-
CREATE INDEX IF NOT EXISTS message_threads_unread_idx
|
|
144
|
-
ON public.message_threads USING btree (last_message_at DESC)
|
|
145
|
-
WHERE (unread_for_admin = true);
|
|
146
|
-
|
|
147
|
-
-- Idempotent backfill / dedupe of enquiry threads.
|
|
148
|
-
CREATE INDEX IF NOT EXISTS message_threads_subject_idx
|
|
149
|
-
ON public.message_threads USING btree (subject_id) WHERE (subject_id IS NOT NULL);
|
|
150
|
-
|
|
151
|
-
-- No separate token index: message_threads_token_hash_key is already the unique
|
|
152
|
-
-- btree the /thread lookup probes.
|
|
153
|
-
|
|
154
|
-
DROP TRIGGER IF EXISTS set_message_threads_updated_at ON public.message_threads;
|
|
155
|
-
CREATE TRIGGER set_message_threads_updated_at BEFORE UPDATE ON public.message_threads
|
|
156
|
-
FOR EACH ROW EXECUTE FUNCTION public.set_current_timestamp_updated_at();
|
|
157
|
-
|
|
158
|
-
ALTER TABLE public.message_threads ENABLE ROW LEVEL SECURITY;
|
|
159
|
-
|
|
160
|
-
GRANT SELECT, UPDATE ON TABLE public.message_threads TO authenticated;
|
|
161
|
-
GRANT ALL ON TABLE public.message_threads TO service_role;
|
|
162
|
-
|
|
163
|
-
DROP POLICY IF EXISTS message_threads_admin_read_policy ON public.message_threads;
|
|
164
|
-
CREATE POLICY message_threads_admin_read_policy ON public.message_threads FOR SELECT TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role));
|
|
165
|
-
|
|
166
|
-
DROP POLICY IF EXISTS message_threads_admin_update_policy ON public.message_threads;
|
|
167
|
-
CREATE POLICY message_threads_admin_update_policy ON public.message_threads FOR UPDATE TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role)) WITH CHECK ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role));
|
|
168
|
-
|
|
169
|
-
DROP POLICY IF EXISTS message_threads_service_role_policy ON public.message_threads;
|
|
170
|
-
CREATE POLICY message_threads_service_role_policy ON public.message_threads TO service_role USING (true) WITH CHECK (true);
|
|
171
|
-
|
|
172
|
-
-- ---------------------------------------------------------------------------
|
|
173
|
-
-- 3. thread_messages — one turn of a private conversation.
|
|
174
|
-
-- ---------------------------------------------------------------------------
|
|
175
|
-
CREATE TABLE IF NOT EXISTS public.thread_messages (
|
|
176
|
-
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
177
|
-
thread_id uuid NOT NULL,
|
|
178
|
-
direction text NOT NULL,
|
|
179
|
-
body text NOT NULL,
|
|
180
|
-
-- profiles.id for an outbound reply. No FK: a departing admin must not take the
|
|
181
|
-
-- conversation with them.
|
|
182
|
-
author_id uuid,
|
|
183
|
-
author_name text,
|
|
184
|
-
-- False when SMTP was unconfigured or the send failed. sendEmail() throws when
|
|
185
|
-
-- unconfigured, so this row - not the mail - is the record.
|
|
186
|
-
email_delivered boolean DEFAULT false NOT NULL,
|
|
187
|
-
email_error text,
|
|
188
|
-
ip_masked text,
|
|
189
|
-
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
190
|
-
CONSTRAINT thread_messages_pkey PRIMARY KEY (id),
|
|
191
|
-
CONSTRAINT thread_messages_direction_check
|
|
192
|
-
CHECK ((direction = ANY (ARRAY['inbound'::text, 'outbound'::text]))),
|
|
193
|
-
CONSTRAINT thread_messages_body_not_blank CHECK ((char_length(btrim(body)) > 0))
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
COMMENT ON TABLE public.thread_messages IS 'Turns of a private conversation. Content is append-only; only the delivery flags may change after insert.';
|
|
197
|
-
|
|
198
|
-
-- A real foreign key here, unlike the outward pointers above: a message has no
|
|
199
|
-
-- meaning without its thread.
|
|
200
|
-
DO $rb$ BEGIN
|
|
201
|
-
IF NOT EXISTS (SELECT 1 FROM pg_constraint
|
|
202
|
-
WHERE conname = 'thread_messages_thread_id_fkey'
|
|
203
|
-
AND conrelid = 'public.thread_messages'::regclass) THEN
|
|
204
|
-
ALTER TABLE ONLY public.thread_messages
|
|
205
|
-
ADD CONSTRAINT thread_messages_thread_id_fkey FOREIGN KEY (thread_id)
|
|
206
|
-
REFERENCES public.message_threads(id) ON DELETE CASCADE;
|
|
207
|
-
END IF;
|
|
208
|
-
END $rb$;
|
|
209
|
-
|
|
210
|
-
CREATE INDEX IF NOT EXISTS thread_messages_thread_created_idx
|
|
211
|
-
ON public.thread_messages USING btree (thread_id, created_at);
|
|
212
|
-
|
|
213
|
-
-- Backs the per-IP reply throttle on the public thread page.
|
|
214
|
-
CREATE INDEX IF NOT EXISTS thread_messages_ip_created_idx
|
|
215
|
-
ON public.thread_messages USING btree (ip_masked, created_at DESC);
|
|
216
|
-
|
|
217
|
-
-- Message TEXT is append-only — not the whole row, because email_delivered has to be
|
|
218
|
-
-- settable after the send completes. The trigger blocks rewrites of the load-bearing
|
|
219
|
-
-- columns only, and it binds the service role too, which otherwise bypasses RLS.
|
|
220
|
-
CREATE OR REPLACE FUNCTION public.prevent_thread_message_rewrite() RETURNS trigger
|
|
221
|
-
LANGUAGE plpgsql SET search_path = '' AS $$
|
|
222
|
-
BEGIN
|
|
223
|
-
IF TG_OP = 'DELETE' THEN
|
|
224
|
-
RAISE EXCEPTION 'thread_messages is append-only; DELETE is not permitted'
|
|
225
|
-
USING ERRCODE = 'restrict_violation';
|
|
226
|
-
END IF;
|
|
227
|
-
IF NEW.body IS DISTINCT FROM OLD.body
|
|
228
|
-
OR NEW.direction IS DISTINCT FROM OLD.direction
|
|
229
|
-
OR NEW.thread_id IS DISTINCT FROM OLD.thread_id
|
|
230
|
-
OR NEW.created_at IS DISTINCT FROM OLD.created_at THEN
|
|
231
|
-
RAISE EXCEPTION 'thread_messages content is append-only; only delivery flags may change'
|
|
232
|
-
USING ERRCODE = 'restrict_violation';
|
|
233
|
-
END IF;
|
|
234
|
-
RETURN NEW;
|
|
235
|
-
END;
|
|
236
|
-
$$;
|
|
237
|
-
|
|
238
|
-
DROP TRIGGER IF EXISTS trg_thread_messages_append_only ON public.thread_messages;
|
|
239
|
-
CREATE TRIGGER trg_thread_messages_append_only BEFORE UPDATE OR DELETE ON public.thread_messages
|
|
240
|
-
FOR EACH ROW EXECUTE FUNCTION public.prevent_thread_message_rewrite();
|
|
241
|
-
|
|
242
|
-
-- NB: the ON DELETE CASCADE above fires this trigger, so deleting a thread is
|
|
243
|
-
-- impossible by design. "Delete" in the CMS means status='closed' plus a revoked
|
|
244
|
-
-- token, and the UI says so.
|
|
245
|
-
|
|
246
|
-
ALTER TABLE public.thread_messages ENABLE ROW LEVEL SECURITY;
|
|
247
|
-
|
|
248
|
-
GRANT SELECT ON TABLE public.thread_messages TO authenticated;
|
|
249
|
-
GRANT ALL ON TABLE public.thread_messages TO service_role;
|
|
250
|
-
|
|
251
|
-
DROP POLICY IF EXISTS thread_messages_admin_read_policy ON public.thread_messages;
|
|
252
|
-
CREATE POLICY thread_messages_admin_read_policy ON public.thread_messages FOR SELECT TO authenticated USING ((( SELECT public.get_current_user_role() AS get_current_user_role) = 'ADMIN'::public.user_role));
|
|
253
|
-
|
|
254
|
-
DROP POLICY IF EXISTS thread_messages_service_role_policy ON public.thread_messages;
|
|
255
|
-
CREATE POLICY thread_messages_service_role_policy ON public.thread_messages TO service_role USING (true) WITH CHECK (true);
|
|
256
|
-
|
|
257
|
-
-- ---------------------------------------------------------------------------
|
|
258
|
-
-- 4. Backfill: every existing enquiry becomes a thread whose first inbound turn is
|
|
259
|
-
-- the original message. product_inquiries is KEPT and kept written — it is the
|
|
260
|
-
-- enquiry's own record and owns the ip_masked the submission throttle counts.
|
|
261
|
-
-- The thread is a conversation ABOUT the enquiry, not a replacement for it.
|
|
262
|
-
-- ---------------------------------------------------------------------------
|
|
263
|
-
INSERT INTO public.message_threads
|
|
264
|
-
(source, subject_id, subject_label, sender_name, sender_email, locale,
|
|
265
|
-
ip_masked, user_agent, unread_for_admin, status, last_message_at, created_at)
|
|
266
|
-
SELECT 'product_inquiry', i.id, COALESCE(i.product_title, 'Product enquiry'),
|
|
267
|
-
i.sender_name, i.sender_email, i.locale, i.ip_masked, i.user_agent,
|
|
268
|
-
NOT i.is_resolved, CASE WHEN i.is_resolved THEN 'closed' ELSE 'open' END,
|
|
269
|
-
i.created_at, i.created_at
|
|
270
|
-
FROM public.product_inquiries i
|
|
271
|
-
WHERE NOT EXISTS (SELECT 1 FROM public.message_threads t WHERE t.subject_id = i.id);
|
|
272
|
-
|
|
273
|
-
INSERT INTO public.thread_messages (thread_id, direction, body, email_delivered, ip_masked, created_at)
|
|
274
|
-
SELECT t.id, 'inbound', i.message, i.email_delivered, i.ip_masked, i.created_at
|
|
275
|
-
FROM public.product_inquiries i
|
|
276
|
-
JOIN public.message_threads t ON t.subject_id = i.id
|
|
277
|
-
WHERE NOT EXISTS (SELECT 1 FROM public.thread_messages m WHERE m.thread_id = t.id);
|
|
278
|
-
|
|
279
|
-
-- ---------------------------------------------------------------------------
|
|
280
|
-
-- 5. The form-block data migration.
|
|
281
|
-
-- Rewrites every stored form block: mints a form_key, moves recipient_email into
|
|
282
|
-
-- form_endpoints, snapshots the field manifest, and DELETES recipient_email from
|
|
283
|
-
-- the jsonb. Three shapes have to be handled, because a form nested inside a
|
|
284
|
-
-- section has no blocks row of its own — it lives in the section's content:
|
|
285
|
-
-- a) blocks.content where block_type = 'form'
|
|
286
|
-
-- b) blocks.content where block_type = 'section' (column_blocks, slides)
|
|
287
|
-
-- c) content_drafts.blocks / product_drafts.blocks (jsonb arrays of snapshots)
|
|
288
|
-
-- ---------------------------------------------------------------------------
|
|
289
|
-
CREATE OR REPLACE FUNCTION public.nb_migrate_form_columns(p_cols jsonb)
|
|
290
|
-
RETURNS jsonb LANGUAGE plpgsql VOLATILE SET search_path = '' AS $fn$
|
|
291
|
-
DECLARE v_col jsonb; v_blk jsonb; v_new_cols jsonb := '[]'::jsonb; v_new_col jsonb;
|
|
292
|
-
BEGIN
|
|
293
|
-
FOR v_col IN SELECT * FROM jsonb_array_elements(p_cols) LOOP
|
|
294
|
-
v_new_col := '[]'::jsonb;
|
|
295
|
-
FOR v_blk IN SELECT * FROM jsonb_array_elements(coalesce(v_col, '[]'::jsonb)) LOOP
|
|
296
|
-
v_new_col := v_new_col || jsonb_build_array(
|
|
297
|
-
v_blk || jsonb_build_object('content', public.nb_migrate_form_content(v_blk->'content')));
|
|
298
|
-
END LOOP;
|
|
299
|
-
v_new_cols := v_new_cols || jsonb_build_array(v_new_col);
|
|
300
|
-
END LOOP;
|
|
301
|
-
RETURN v_new_cols;
|
|
302
|
-
END;
|
|
303
|
-
$fn$;
|
|
304
|
-
|
|
305
|
-
CREATE OR REPLACE FUNCTION public.nb_migrate_form_content(p_content jsonb)
|
|
306
|
-
RETURNS jsonb LANGUAGE plpgsql VOLATILE SET search_path = '' AS $fn$
|
|
307
|
-
DECLARE
|
|
308
|
-
v_key uuid; v_email text; v_fields jsonb; v_slide jsonb; v_new_slides jsonb;
|
|
309
|
-
BEGIN
|
|
310
|
-
IF p_content IS NULL OR jsonb_typeof(p_content) <> 'object' THEN
|
|
311
|
-
RETURN p_content;
|
|
312
|
-
END IF;
|
|
313
|
-
|
|
314
|
-
-- Leaf: this object IS a form block's content.
|
|
315
|
-
IF (p_content ? 'recipient_email') AND (p_content ? 'fields') THEN
|
|
316
|
-
v_key := gen_random_uuid();
|
|
317
|
-
v_email := nullif(btrim(coalesce(p_content->>'recipient_email','')), '');
|
|
318
|
-
SELECT coalesce(jsonb_agg(jsonb_build_object(
|
|
319
|
-
'temp_id', f->>'temp_id', 'label', f->>'label', 'field_type', f->>'field_type')), '[]'::jsonb)
|
|
320
|
-
INTO v_fields
|
|
321
|
-
FROM jsonb_array_elements(coalesce(p_content->'fields', '[]'::jsonb)) f;
|
|
322
|
-
INSERT INTO public.form_endpoints (form_key, label, recipient_email, fields)
|
|
323
|
-
VALUES (v_key, 'Contact form', v_email, v_fields)
|
|
324
|
-
ON CONFLICT (form_key) DO NOTHING;
|
|
325
|
-
RETURN (p_content - 'recipient_email') || jsonb_build_object('form_key', v_key::text);
|
|
326
|
-
END IF;
|
|
327
|
-
|
|
328
|
-
-- Container: a section's slides, each with column_blocks.
|
|
329
|
-
IF (p_content ? 'slides') AND jsonb_typeof(p_content->'slides') = 'array' THEN
|
|
330
|
-
v_new_slides := '[]'::jsonb;
|
|
331
|
-
FOR v_slide IN SELECT * FROM jsonb_array_elements(p_content->'slides') LOOP
|
|
332
|
-
v_new_slides := v_new_slides || jsonb_build_array(
|
|
333
|
-
v_slide || jsonb_build_object('column_blocks',
|
|
334
|
-
public.nb_migrate_form_columns(coalesce(v_slide->'column_blocks','[]'::jsonb))));
|
|
335
|
-
END LOOP;
|
|
336
|
-
p_content := p_content || jsonb_build_object('slides', v_new_slides);
|
|
337
|
-
END IF;
|
|
338
|
-
|
|
339
|
-
-- Container: a standard section's column_blocks.
|
|
340
|
-
IF (p_content ? 'column_blocks') AND jsonb_typeof(p_content->'column_blocks') = 'array' THEN
|
|
341
|
-
p_content := p_content || jsonb_build_object('column_blocks',
|
|
342
|
-
public.nb_migrate_form_columns(p_content->'column_blocks'));
|
|
343
|
-
END IF;
|
|
344
|
-
|
|
345
|
-
RETURN p_content;
|
|
346
|
-
END;
|
|
347
|
-
$fn$;
|
|
348
|
-
|
|
349
|
-
-- The LIKE guard keeps this from rewriting (and bumping updated_at on) every block.
|
|
350
|
-
UPDATE public.blocks
|
|
351
|
-
SET content = public.nb_migrate_form_content(content)
|
|
352
|
-
WHERE content::text LIKE '%recipient_email%';
|
|
353
|
-
|
|
354
|
-
UPDATE public.content_drafts d
|
|
355
|
-
SET blocks = (SELECT coalesce(jsonb_agg(b || jsonb_build_object(
|
|
356
|
-
'content', public.nb_migrate_form_content(b->'content'))), '[]'::jsonb)
|
|
357
|
-
FROM jsonb_array_elements(d.blocks) b)
|
|
358
|
-
WHERE d.blocks::text LIKE '%recipient_email%';
|
|
359
|
-
|
|
360
|
-
UPDATE public.product_drafts d
|
|
361
|
-
SET blocks = (SELECT coalesce(jsonb_agg(b || jsonb_build_object(
|
|
362
|
-
'content', public.nb_migrate_form_content(b->'content'))), '[]'::jsonb)
|
|
363
|
-
FROM jsonb_array_elements(d.blocks) b)
|
|
364
|
-
WHERE d.blocks::text LIKE '%recipient_email%';
|
|
365
|
-
|
|
366
|
-
-- One-shot helpers. Dropping them keeps the public schema (and db:types) clean.
|
|
367
|
-
DROP FUNCTION IF EXISTS public.nb_migrate_form_content(jsonb);
|
|
368
|
-
DROP FUNCTION IF EXISTS public.nb_migrate_form_columns(jsonb);
|
|
369
|
-
|
|
370
|
-
-- ---------------------------------------------------------------------------
|
|
371
|
-
-- 6. Seeds. Thread-page copy is public-facing so it must be translatable; the
|
|
372
|
-
-- components pass an English literal as the fallback, so an install that never
|
|
373
|
-
-- runs this seed still renders correctly.
|
|
374
|
-
-- ---------------------------------------------------------------------------
|
|
375
|
-
INSERT INTO public.site_settings (key, value)
|
|
376
|
-
VALUES ('forms_contact', '{"contactEmail": ""}'::jsonb)
|
|
377
|
-
ON CONFLICT (key) DO NOTHING;
|
|
378
|
-
|
|
379
|
-
INSERT INTO public.translations (key, translations, created_at, updated_at) VALUES
|
|
380
|
-
('thread.heading', '{"en": "Your conversation", "fr": "Votre conversation"}', now(), now()),
|
|
381
|
-
('thread.reply_label', '{"en": "Write a reply", "fr": "Écrire une réponse"}', now(), now()),
|
|
382
|
-
('thread.send', '{"en": "Send reply", "fr": "Envoyer la réponse"}', now(), now()),
|
|
383
|
-
('thread.sending', '{"en": "Sending...", "fr": "Envoi..."}', now(), now()),
|
|
384
|
-
('thread.sent', '{"en": "Thanks - your reply has been sent.", "fr": "Merci - votre réponse a été envoyée."}', now(), now()),
|
|
385
|
-
('thread.error', '{"en": "Sorry, your reply couldn''t be sent. Please try again in a moment.", "fr": "Désolé, votre réponse n''a pas pu être envoyée. Veuillez réessayer dans un instant."}', now(), now()),
|
|
386
|
-
('thread.throttled', '{"en": "You''ve sent several replies already. Please wait a few minutes.", "fr": "Vous avez déjà envoyé plusieurs réponses. Veuillez patienter quelques minutes."}', now(), now()),
|
|
387
|
-
('thread.closed', '{"en": "This conversation has been closed.", "fr": "Cette conversation est fermée."}', now(), now()),
|
|
388
|
-
('thread.invalid', '{"en": "This link has expired or is no longer valid. If you still need help, please contact us again from our website.", "fr": "Ce lien a expiré ou n''est plus valide. Si vous avez encore besoin d''aide, contactez-nous de nouveau depuis notre site."}', now(), now()),
|
|
389
|
-
('thread.you', '{"en": "You", "fr": "Vous"}', now(), now()),
|
|
390
|
-
('forms.submission_stored', '{"en": "Thanks - your message has been received.", "fr": "Merci - votre message a bien été reçu."}', now(), now())
|
|
391
|
-
ON CONFLICT (key) DO NOTHING;
|