create-nextblock 0.15.8 → 0.15.10

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.
Files changed (71) hide show
  1. package/package.json +1 -1
  2. package/templates/nextblock-template/app/ToasterProvider.tsx +26 -17
  3. package/templates/nextblock-template/app/actions/contactSellerActions.test.ts +280 -0
  4. package/templates/nextblock-template/app/actions/contactSellerActions.ts +222 -0
  5. package/templates/nextblock-template/app/actions/email-retry.test.ts +62 -0
  6. package/templates/nextblock-template/app/actions/email.ts +241 -110
  7. package/templates/nextblock-template/app/actions/formActions.ts +245 -116
  8. package/templates/nextblock-template/app/actions/interactions.ts +489 -396
  9. package/templates/nextblock-template/app/actions/threadActions.ts +166 -0
  10. package/templates/nextblock-template/app/api/checkout/route.ts +162 -146
  11. package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +664 -1
  12. package/templates/nextblock-template/app/checkout/page.tsx +57 -52
  13. package/templates/nextblock-template/app/cms/CmsClientLayout.tsx +552 -529
  14. package/templates/nextblock-template/app/cms/blocks/editors/FormBlockEditor.tsx +304 -181
  15. package/templates/nextblock-template/app/cms/components/ContactReminderBanner.tsx +75 -0
  16. package/templates/nextblock-template/app/cms/components/PaymentsReminderBanner.tsx +58 -0
  17. package/templates/nextblock-template/app/cms/components/VisibilityControl.tsx +542 -528
  18. package/templates/nextblock-template/app/cms/inquiries/actions.ts +66 -0
  19. package/templates/nextblock-template/app/cms/inquiries/page.tsx +12 -0
  20. package/templates/nextblock-template/app/cms/interactions/page.tsx +12 -51
  21. package/templates/nextblock-template/app/cms/layout.tsx +101 -73
  22. package/templates/nextblock-template/app/cms/messages/MessagesClient.tsx +661 -0
  23. package/templates/nextblock-template/app/cms/messages/actions.ts +404 -0
  24. package/templates/nextblock-template/app/cms/messages/loadInbox.ts +333 -0
  25. package/templates/nextblock-template/app/cms/messages/page.tsx +87 -0
  26. package/templates/nextblock-template/app/cms/messages/require-admin.ts +37 -0
  27. package/templates/nextblock-template/app/cms/products/[id]/edit/page.tsx +370 -362
  28. package/templates/nextblock-template/app/cms/revisions/service.ts +20 -0
  29. package/templates/nextblock-template/app/cms/settings/email/components/EmailForm.tsx +227 -185
  30. package/templates/nextblock-template/app/layout.tsx +671 -671
  31. package/templates/nextblock-template/app/product/[slug]/page.tsx +502 -482
  32. package/templates/nextblock-template/app/providers.tsx +96 -96
  33. package/templates/nextblock-template/app/thread/ThreadView.tsx +164 -0
  34. package/templates/nextblock-template/app/thread/[token]/route.ts +57 -0
  35. package/templates/nextblock-template/app/thread/layout.tsx +15 -0
  36. package/templates/nextblock-template/app/thread/page.tsx +98 -0
  37. package/templates/nextblock-template/components/BlockRenderer.tsx +312 -296
  38. package/templates/nextblock-template/components/ContactSellerSection.tsx +188 -0
  39. package/templates/nextblock-template/components/PostCommentsSection.tsx +378 -369
  40. package/templates/nextblock-template/components/ProductReviewsSection.tsx +426 -419
  41. package/templates/nextblock-template/components/StaffReplies.tsx +102 -0
  42. package/templates/nextblock-template/components/blocks/renderers/CartBlockRenderer.tsx +18 -17
  43. package/templates/nextblock-template/components/blocks/renderers/CheckoutBlockRenderer.tsx +20 -19
  44. package/templates/nextblock-template/components/blocks/renderers/FeaturedProductBlockRenderer.tsx +25 -22
  45. package/templates/nextblock-template/components/blocks/renderers/FormBlockRenderer.tsx +385 -381
  46. package/templates/nextblock-template/components/blocks/renderers/ProductDetailsBlockRenderer.tsx +157 -92
  47. package/templates/nextblock-template/components/blocks/renderers/ProductGridBlockRenderer.tsx +34 -31
  48. package/templates/nextblock-template/components/blocks/renderers/SectionBlockRenderer.tsx +612 -600
  49. package/templates/nextblock-template/components/commerce/PaymentReadinessBoundary.tsx +32 -0
  50. package/templates/nextblock-template/docs/14-MESSAGES-INBOX.md +309 -0
  51. package/templates/nextblock-template/docs/README.md +42 -41
  52. package/templates/nextblock-template/docs/assets/lighthouse-scores.png +0 -0
  53. package/templates/nextblock-template/lib/blocks/blockColors.test.ts +22 -2
  54. package/templates/nextblock-template/lib/blocks/blockColors.ts +44 -1
  55. package/templates/nextblock-template/lib/blocks/blockRegistry.ts +761 -753
  56. package/templates/nextblock-template/lib/cms/contact-reminder.ts +64 -0
  57. package/templates/nextblock-template/lib/cms/payments-reminder.ts +98 -0
  58. package/templates/nextblock-template/lib/cms/unread-messages.ts +42 -0
  59. package/templates/nextblock-template/lib/commerce/seller-contact.ts +162 -0
  60. package/templates/nextblock-template/lib/config/email-settings.ts +323 -254
  61. package/templates/nextblock-template/lib/config/email-tls.test.ts +57 -0
  62. package/templates/nextblock-template/lib/email/placeholder-address.test.ts +59 -0
  63. package/templates/nextblock-template/lib/email/placeholder-address.ts +39 -0
  64. package/templates/nextblock-template/lib/messages/thread-reference.test.ts +70 -0
  65. package/templates/nextblock-template/lib/messages/thread-token.test.ts +93 -0
  66. package/templates/nextblock-template/lib/messages/thread-token.ts +157 -0
  67. package/templates/nextblock-template/lib/messages/threads.ts +579 -0
  68. package/templates/nextblock-template/lib/setup/migrations-bundle.ts +20 -0
  69. package/templates/nextblock-template/lib/site-url.test.ts +89 -0
  70. package/templates/nextblock-template/lib/site-url.ts +102 -48
  71. package/templates/nextblock-template/package.json +1 -1
@@ -1,396 +1,489 @@
1
- "use server";
2
-
3
- import { createClient, getServiceRoleSupabaseClient, getProfileWithRoleServerSide } from "@nextblock-cms/db/server";
4
- import { revalidatePath } from "next/cache";
5
- import { cookies, headers } from "next/headers";
6
- import { sendEmail } from "./email";
7
-
8
- export interface SubmitInteractionInput {
9
- type: "review" | "comment";
10
- content: string;
11
- rating?: number;
12
- productId?: string;
13
- postId?: number;
14
- }
15
-
16
- /**
17
- * Submits a new comment or review. Default status is 'pending' for moderation.
18
- */
19
- export async function submitInteraction(input: SubmitInteractionInput) {
20
- const supabase = createClient();
21
-
22
- // 1. Authenticate user
23
- const { data: { user }, error: authError } = await supabase.auth.getUser();
24
- if (authError || !user) {
25
- return { error: "You must be logged in to submit a review or comment." };
26
- }
27
-
28
- // 2. Validate inputs
29
- if (!input.content || input.content.trim().length < 5) {
30
- return { error: "Content must be at least 5 characters long." };
31
- }
32
-
33
- if (input.type === "review") {
34
- if (!input.productId) {
35
- return { error: "Product ID is required for a review." };
36
- }
37
- if (!input.rating || input.rating < 1 || input.rating > 5) {
38
- return { error: "Rating must be between 1 and 5 stars." };
39
- }
40
- } else if (input.type === "comment") {
41
- if (!input.postId) {
42
- return { error: "Post ID is required for a comment." };
43
- }
44
- } else {
45
- return { error: "Invalid interaction type." };
46
- }
47
-
48
- try {
49
- // 3. Insert interaction
50
- const { data, error } = await supabase
51
- .from("cms_interactions" as any)
52
- .insert({
53
- type: input.type,
54
- status: "pending",
55
- content: input.content.trim(),
56
- rating: input.type === "review" ? input.rating : null,
57
- user_id: user.id,
58
- product_id: input.type === "review" ? input.productId : null,
59
- post_id: input.type === "comment" ? input.postId : null,
60
- reactions: {},
61
- })
62
- .select()
63
- .single();
64
-
65
- if (error) {
66
- console.error("Error inserting interaction:", error);
67
- return { error: `Failed to submit: ${error.message}` };
68
- }
69
-
70
- // 4. Revalidate moderation panel path
71
- revalidatePath("/cms/interactions");
72
-
73
- // 5. Send email notification asynchronously if emails are configured
74
- try {
75
- const admin = getServiceRoleSupabaseClient();
76
- if (admin) {
77
- const { data: config } = await admin
78
- .from("site_settings")
79
- .select("value")
80
- .eq("key", "interactions_notification_emails")
81
- .maybeSingle();
82
-
83
- const emailsString = (config?.value as any)?.emails || "";
84
-
85
- if (emailsString) {
86
- const host = (await headers()).get("host");
87
- const protocol = host?.includes("localhost") || host?.includes("127.0.0.1") ? "http" : "https";
88
- const origin = `${protocol}://${host}`;
89
-
90
- const capitalizedType = input.type.charAt(0).toUpperCase() + input.type.slice(1);
91
- const subject = `New Pending ${capitalizedType} Submitted`;
92
-
93
- const html = `
94
- <div style="font-family: sans-serif; padding: 20px; color: #333; max-width: 600px; margin: 0 auto; border: 1px solid #eee; border-radius: 8px;">
95
- {{brand_header}}
96
- <h2 style="color: #6366f1; margin-top: 0;">New Pending ${capitalizedType} Submitted</h2>
97
- <p>Hello,</p>
98
- <p>A new content interaction has been submitted and is currently <strong>pending moderation</strong>.</p>
99
- <hr style="border: 0; border-top: 1px solid #eee; margin: 20px 0;" />
100
- <div style="background-color: #f9fafb; padding: 15px; border-radius: 6px; margin-bottom: 20px;">
101
- <p style="margin: 0 0 8px 0;"><strong>Type:</strong> ${capitalizedType}</p>
102
- ${input.type === "review" && input.rating ? `<p style="margin: 0 0 8px 0;"><strong>Rating:</strong> ${input.rating} / 5</p>` : ""}
103
- <p style="margin: 0 0 8px 0;"><strong>Content:</strong></p>
104
- <blockquote style="margin: 0; padding-left: 10px; border-left: 3px solid #6366f1; color: #555; font-style: italic;">
105
- ${input.content.trim()}
106
- </blockquote>
107
- </div>
108
- <p>Please log in to the moderation dashboard to approve or deny this interaction:</p>
109
- <p style="margin-top: 20px;">
110
- <a href="${origin}/cms/interactions" style="background-color: #6366f1; color: white; padding: 10px 20px; text-decoration: none; border-radius: 6px; font-weight: bold; display: inline-block;">
111
- Go to Moderation Dashboard
112
- </a>
113
- </p>
114
- <hr style="border: 0; border-top: 1px solid #eee; margin: 20px 0;" />
115
- <p style="font-size: 11px; color: #888;">This is an automated notification from your CMS.</p>
116
- </div>
117
- `;
118
-
119
- const text = `
120
- New Pending ${capitalizedType} Submitted
121
-
122
- A new ${input.type} has been submitted and is currently pending moderation.
123
-
124
- Type: ${capitalizedType}
125
- ${input.type === "review" && input.rating ? `Rating: ${input.rating} / 5\n` : ""}
126
- Content: "${input.content.trim()}"
127
-
128
- Moderation Dashboard: ${origin}/cms/interactions
129
- `;
130
-
131
- sendEmail({
132
- to: emailsString,
133
- subject,
134
- text,
135
- html,
136
- }).catch((err) =>
137
- console.error("Failed to send pending interaction email notification:", err)
138
- );
139
- }
140
- }
141
- } catch (emailErr) {
142
- console.error("Failed to process email notifications:", emailErr);
143
- }
144
-
145
- return { success: true, data };
146
- } catch (err: any) {
147
- console.error("Submit interaction failed:", err);
148
- return { error: err.message || "An unexpected error occurred." };
149
- }
150
- }
151
-
152
- /**
153
- * Toggles a reaction (like) on a comment or review. Rate-limited and validated using cookies.
154
- */
155
- export async function toggleReaction(interactionId: string, reactionType = "likes") {
156
- if (!interactionId) return { error: "Interaction ID is required." };
157
-
158
- try {
159
- // Rate limit / duplicate prevention using cookies
160
- const cookieStore = await cookies();
161
- const reactedCookie = cookieStore.get("reacted_interactions")?.value;
162
- let reactedList: string[] = [];
163
-
164
- try {
165
- if (reactedCookie) {
166
- reactedList = JSON.parse(reactedCookie);
167
- }
168
- } catch {
169
- reactedList = [];
170
- }
171
-
172
- const hasReacted = reactedList.includes(interactionId);
173
-
174
- // Call service role client since visitors don't have update RLS policies
175
- const admin = getServiceRoleSupabaseClient();
176
-
177
- // Fetch current reactions
178
- const { data: interaction, error: fetchError } = await admin
179
- .from("cms_interactions")
180
- .select("reactions, type, product_id, post_id, products(slug), posts(slug)")
181
- .eq("id", interactionId)
182
- .single();
183
-
184
- if (fetchError || !interaction) {
185
- return { error: "Interaction not found." };
186
- }
187
-
188
- const reactions = (interaction.reactions as Record<string, number>) || {};
189
- const currentCount = reactions[reactionType] || 0;
190
- const newCount = hasReacted ? Math.max(0, currentCount - 1) : currentCount + 1;
191
- reactions[reactionType] = newCount;
192
-
193
- // Save back to db
194
- const { error: updateError } = await admin
195
- .from("cms_interactions")
196
- .update({ reactions })
197
- .eq("id", interactionId);
198
-
199
- if (updateError) {
200
- console.error("Error updating reactions:", updateError);
201
- return { error: "Failed to update reaction." };
202
- }
203
-
204
- // Update the cookie
205
- if (hasReacted) {
206
- reactedList = reactedList.filter(id => id !== interactionId);
207
- } else {
208
- reactedList.push(interactionId);
209
- }
210
-
211
- cookieStore.set("reacted_interactions", JSON.stringify(reactedList), {
212
- maxAge: 60 * 60 * 24 * 365, // 1 year
213
- httpOnly: true,
214
- path: "/",
215
- sameSite: "lax",
216
- });
217
-
218
- // Revalidate paths to reflect reaction count updates
219
- const resolvedProduct = interaction.products as any;
220
- const resolvedPost = interaction.posts as any;
221
-
222
- if (interaction.product_id && resolvedProduct?.slug) {
223
- revalidatePath(`/product/${resolvedProduct.slug}`);
224
- } else if (interaction.post_id && resolvedPost?.slug) {
225
- revalidatePath(`/article/${resolvedPost.slug}`);
226
- }
227
- revalidatePath("/cms/interactions");
228
-
229
- return { success: true, count: newCount, hasReacted: !hasReacted };
230
- } catch (err: any) {
231
- console.error("Toggle reaction failed:", err);
232
- return { error: err.message || "An unexpected error occurred." };
233
- }
234
- }
235
-
236
- /**
237
- * Updates an interaction's status (approved or denied). Admin/Moderator only.
238
- */
239
- export async function updateInteractionStatus(interactionId: string, status: "approved" | "denied") {
240
- const supabase = createClient();
241
-
242
- // 1. Authenticate user
243
- const { data: { user }, error: authError } = await supabase.auth.getUser();
244
- if (authError || !user) {
245
- return { error: "Not authenticated" };
246
- }
247
-
248
- // 2. Authorize as Admin or Writer
249
- const profile = await getProfileWithRoleServerSide(user.id);
250
- if (!profile || (profile.role !== "ADMIN" && profile.role !== "WRITER")) {
251
- return { error: "Unauthorized. Admin or Writer permissions required." };
252
- }
253
-
254
- // 3. Admin-only rule for denying/approving if strict
255
- if (profile.role !== "ADMIN") {
256
- // If writers are not allowed to moderate, block it. The spec says:
257
- // "Admin-only permission action to switch states between approved or denied."
258
- // So let's enforce STRICT Admin only for status updates.
259
- return { error: "Unauthorized. Admin permissions required to moderate." };
260
- }
261
-
262
- try {
263
- const admin = getServiceRoleSupabaseClient();
264
-
265
- // Fetch interaction details for path revalidation
266
- const { data: interaction, error: fetchError } = await admin
267
- .from("cms_interactions")
268
- .select("product_id, post_id, products(slug), posts(slug)")
269
- .eq("id", interactionId)
270
- .single();
271
-
272
- if (fetchError || !interaction) {
273
- return { error: "Interaction not found." };
274
- }
275
-
276
- // 4. Update status
277
- const { error: updateError } = await admin
278
- .from("cms_interactions")
279
- .update({ status })
280
- .eq("id", interactionId);
281
-
282
- if (updateError) {
283
- console.error("Error updating status:", updateError);
284
- return { error: `Failed to update status: ${updateError.message}` };
285
- }
286
-
287
- // 5. Revalidate paths
288
- const resolvedProduct = interaction.products as any;
289
- const resolvedPost = interaction.posts as any;
290
-
291
- if (interaction.product_id && resolvedProduct?.slug) {
292
- revalidatePath(`/product/${resolvedProduct.slug}`);
293
- } else if (interaction.post_id && resolvedPost?.slug) {
294
- revalidatePath(`/article/${resolvedPost.slug}`);
295
- }
296
- revalidatePath("/cms/interactions");
297
-
298
- return { success: true };
299
- } catch (err: any) {
300
- console.error("Update interaction status failed:", err);
301
- return { error: err.message || "An unexpected error occurred." };
302
- }
303
- }
304
-
305
- /**
306
- * Fetches the interactions notification emails from site_settings.
307
- */
308
- export async function getNotificationEmails() {
309
- const supabase = createClient();
310
-
311
- // Authenticate & authorize
312
- const { data: { user } } = await supabase.auth.getUser();
313
- if (!user) return { error: "Not authenticated" };
314
-
315
- const profile = await getProfileWithRoleServerSide(user.id);
316
- if (!profile || profile.role !== "ADMIN") {
317
- return { error: "Unauthorized. Admin role required." };
318
- }
319
-
320
- try {
321
- const { data, error } = await supabase
322
- .from("site_settings")
323
- .select("value")
324
- .eq("key", "interactions_notification_emails")
325
- .maybeSingle();
326
-
327
- if (error) throw error;
328
-
329
- return { success: true, emails: (data?.value as any)?.emails || "" };
330
- } catch (err: any) {
331
- console.error("Failed to fetch notification emails:", err);
332
- return { error: err.message || "Failed to fetch settings." };
333
- }
334
- }
335
-
336
- /**
337
- * Saves the interactions notification emails to site_settings.
338
- */
339
- export async function saveNotificationEmails(emails: string) {
340
- const supabase = createClient();
341
-
342
- // Authenticate & authorize
343
- const { data: { user } } = await supabase.auth.getUser();
344
- if (!user) return { error: "Not authenticated" };
345
-
346
- const profile = await getProfileWithRoleServerSide(user.id);
347
- if (!profile || profile.role !== "ADMIN") {
348
- return { error: "Unauthorized. Admin role required." };
349
- }
350
-
351
- // Validate every address, dedupe (case-insensitive), and normalize to lowercase.
352
- // Mirrors the client-side check so a crafted/legacy payload can't persist junk.
353
- const emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
354
- const tokens = emails
355
- .split(",")
356
- .map((e) => e.trim())
357
- .filter(Boolean);
358
-
359
- const seen = new Set<string>();
360
- const valid: string[] = [];
361
- const invalid: string[] = [];
362
- for (const token of tokens) {
363
- const lower = token.toLowerCase();
364
- if (!emailRe.test(lower)) {
365
- invalid.push(token);
366
- continue;
367
- }
368
- if (seen.has(lower)) continue;
369
- seen.add(lower);
370
- valid.push(lower);
371
- }
372
-
373
- if (invalid.length > 0) {
374
- return {
375
- error: `Invalid email address${invalid.length > 1 ? "es" : ""}: ${invalid.join(", ")}`,
376
- };
377
- }
378
-
379
- const cleaned = valid.join(", ");
380
-
381
- try {
382
- const { error } = await supabase
383
- .from("site_settings")
384
- .upsert({
385
- key: "interactions_notification_emails",
386
- value: { emails: cleaned },
387
- });
388
-
389
- if (error) throw error;
390
-
391
- return { success: true, emails: cleaned };
392
- } catch (err: any) {
393
- console.error("Failed to save notification emails:", err);
394
- return { error: err.message || "Failed to save settings." };
395
- }
396
- }
1
+ "use server";
2
+
3
+ import { createClient, getServiceRoleSupabaseClient, getProfileWithRoleServerSide } from "@nextblock-cms/db/server";
4
+ import { revalidatePath } from "next/cache";
5
+ import { cookies, headers } from "next/headers";
6
+ import { sendEmail } from "./email";
7
+
8
+ export interface SubmitInteractionInput {
9
+ type: "review" | "comment";
10
+ content: string;
11
+ rating?: number;
12
+ productId?: string;
13
+ postId?: number;
14
+ }
15
+
16
+ /**
17
+ * Submits a new comment or review. Default status is 'pending' for moderation.
18
+ */
19
+ export async function submitInteraction(input: SubmitInteractionInput) {
20
+ const supabase = createClient();
21
+
22
+ // 1. Authenticate user
23
+ const { data: { user }, error: authError } = await supabase.auth.getUser();
24
+ if (authError || !user) {
25
+ return { error: "You must be logged in to submit a review or comment." };
26
+ }
27
+
28
+ // 2. Validate inputs
29
+ if (!input.content || input.content.trim().length < 5) {
30
+ return { error: "Content must be at least 5 characters long." };
31
+ }
32
+
33
+ if (input.type === "review") {
34
+ if (!input.productId) {
35
+ return { error: "Product ID is required for a review." };
36
+ }
37
+ if (!input.rating || input.rating < 1 || input.rating > 5) {
38
+ return { error: "Rating must be between 1 and 5 stars." };
39
+ }
40
+ } else if (input.type === "comment") {
41
+ if (!input.postId) {
42
+ return { error: "Post ID is required for a comment." };
43
+ }
44
+ } else {
45
+ return { error: "Invalid interaction type." };
46
+ }
47
+
48
+ try {
49
+ // 3. Insert interaction
50
+ const { data, error } = await supabase
51
+ .from("cms_interactions" as any)
52
+ .insert({
53
+ type: input.type,
54
+ status: "pending",
55
+ content: input.content.trim(),
56
+ rating: input.type === "review" ? input.rating : null,
57
+ user_id: user.id,
58
+ product_id: input.type === "review" ? input.productId : null,
59
+ post_id: input.type === "comment" ? input.postId : null,
60
+ reactions: {},
61
+ })
62
+ .select()
63
+ .single();
64
+
65
+ if (error) {
66
+ console.error("Error inserting interaction:", error);
67
+ return { error: `Failed to submit: ${error.message}` };
68
+ }
69
+
70
+ // 4. Revalidate moderation panel path
71
+ revalidatePath("/cms/interactions");
72
+
73
+ // 5. Send email notification asynchronously if emails are configured
74
+ try {
75
+ const admin = getServiceRoleSupabaseClient();
76
+ if (admin) {
77
+ const { data: config } = await admin
78
+ .from("site_settings")
79
+ .select("value")
80
+ .eq("key", "interactions_notification_emails")
81
+ .maybeSingle();
82
+
83
+ const emailsString = (config?.value as any)?.emails || "";
84
+
85
+ if (emailsString) {
86
+ const host = (await headers()).get("host");
87
+ const protocol = host?.includes("localhost") || host?.includes("127.0.0.1") ? "http" : "https";
88
+ const origin = `${protocol}://${host}`;
89
+
90
+ const capitalizedType = input.type.charAt(0).toUpperCase() + input.type.slice(1);
91
+ const subject = `New Pending ${capitalizedType} Submitted`;
92
+
93
+ const html = `
94
+ <div style="font-family: sans-serif; padding: 20px; color: #333; max-width: 600px; margin: 0 auto; border: 1px solid #eee; border-radius: 8px;">
95
+ {{brand_header}}
96
+ <h2 style="color: #6366f1; margin-top: 0;">New Pending ${capitalizedType} Submitted</h2>
97
+ <p>Hello,</p>
98
+ <p>A new content interaction has been submitted and is currently <strong>pending moderation</strong>.</p>
99
+ <hr style="border: 0; border-top: 1px solid #eee; margin: 20px 0;" />
100
+ <div style="background-color: #f9fafb; padding: 15px; border-radius: 6px; margin-bottom: 20px;">
101
+ <p style="margin: 0 0 8px 0;"><strong>Type:</strong> ${capitalizedType}</p>
102
+ ${input.type === "review" && input.rating ? `<p style="margin: 0 0 8px 0;"><strong>Rating:</strong> ${input.rating} / 5</p>` : ""}
103
+ <p style="margin: 0 0 8px 0;"><strong>Content:</strong></p>
104
+ <blockquote style="margin: 0; padding-left: 10px; border-left: 3px solid #6366f1; color: #555; font-style: italic;">
105
+ ${escapeHtml(input.content.trim())}
106
+ </blockquote>
107
+ </div>
108
+ <p>Please log in to the moderation dashboard to approve or deny this interaction:</p>
109
+ <p style="margin-top: 20px;">
110
+ <a href="${origin}/cms/interactions" style="background-color: #6366f1; color: white; padding: 10px 20px; text-decoration: none; border-radius: 6px; font-weight: bold; display: inline-block;">
111
+ Go to Moderation Dashboard
112
+ </a>
113
+ </p>
114
+ <hr style="border: 0; border-top: 1px solid #eee; margin: 20px 0;" />
115
+ <p style="font-size: 11px; color: #888;">This is an automated notification from your CMS.</p>
116
+ </div>
117
+ `;
118
+
119
+ const text = `
120
+ New Pending ${capitalizedType} Submitted
121
+
122
+ A new ${input.type} has been submitted and is currently pending moderation.
123
+
124
+ Type: ${capitalizedType}
125
+ ${input.type === "review" && input.rating ? `Rating: ${input.rating} / 5\n` : ""}
126
+ Content: "${input.content.trim()}"
127
+
128
+ Moderation Dashboard: ${origin}/cms/interactions
129
+ `;
130
+
131
+ sendEmail({
132
+ to: emailsString,
133
+ subject,
134
+ text,
135
+ html,
136
+ }).catch((err) =>
137
+ console.error("Failed to send pending interaction email notification:", err)
138
+ );
139
+ }
140
+ }
141
+ } catch (emailErr) {
142
+ console.error("Failed to process email notifications:", emailErr);
143
+ }
144
+
145
+ return { success: true, data };
146
+ } catch (err: any) {
147
+ console.error("Submit interaction failed:", err);
148
+ return { error: err.message || "An unexpected error occurred." };
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Toggles a reaction (like) on a comment or review. Rate-limited and validated using cookies.
154
+ */
155
+ export async function toggleReaction(interactionId: string, reactionType = "likes") {
156
+ if (!interactionId) return { error: "Interaction ID is required." };
157
+
158
+ try {
159
+ // Rate limit / duplicate prevention using cookies
160
+ const cookieStore = await cookies();
161
+ const reactedCookie = cookieStore.get("reacted_interactions")?.value;
162
+ let reactedList: string[] = [];
163
+
164
+ try {
165
+ if (reactedCookie) {
166
+ reactedList = JSON.parse(reactedCookie);
167
+ }
168
+ } catch {
169
+ reactedList = [];
170
+ }
171
+
172
+ const hasReacted = reactedList.includes(interactionId);
173
+
174
+ // Call service role client since visitors don't have update RLS policies
175
+ const admin = getServiceRoleSupabaseClient();
176
+
177
+ // Fetch current reactions
178
+ const { data: interaction, error: fetchError } = await admin
179
+ .from("cms_interactions")
180
+ .select("reactions, type, product_id, post_id, products(slug), posts(slug)")
181
+ .eq("id", interactionId)
182
+ .single();
183
+
184
+ if (fetchError || !interaction) {
185
+ return { error: "Interaction not found." };
186
+ }
187
+
188
+ const reactions = (interaction.reactions as Record<string, number>) || {};
189
+ const currentCount = reactions[reactionType] || 0;
190
+ const newCount = hasReacted ? Math.max(0, currentCount - 1) : currentCount + 1;
191
+ reactions[reactionType] = newCount;
192
+
193
+ // Save back to db
194
+ const { error: updateError } = await admin
195
+ .from("cms_interactions")
196
+ .update({ reactions })
197
+ .eq("id", interactionId);
198
+
199
+ if (updateError) {
200
+ console.error("Error updating reactions:", updateError);
201
+ return { error: "Failed to update reaction." };
202
+ }
203
+
204
+ // Update the cookie
205
+ if (hasReacted) {
206
+ reactedList = reactedList.filter(id => id !== interactionId);
207
+ } else {
208
+ reactedList.push(interactionId);
209
+ }
210
+
211
+ cookieStore.set("reacted_interactions", JSON.stringify(reactedList), {
212
+ maxAge: 60 * 60 * 24 * 365, // 1 year
213
+ httpOnly: true,
214
+ path: "/",
215
+ sameSite: "lax",
216
+ });
217
+
218
+ // Revalidate paths to reflect reaction count updates
219
+ const resolvedProduct = interaction.products as any;
220
+ const resolvedPost = interaction.posts as any;
221
+
222
+ if (interaction.product_id && resolvedProduct?.slug) {
223
+ revalidatePath(`/product/${resolvedProduct.slug}`);
224
+ } else if (interaction.post_id && resolvedPost?.slug) {
225
+ revalidatePath(`/article/${resolvedPost.slug}`);
226
+ }
227
+ revalidatePath("/cms/interactions");
228
+
229
+ return { success: true, count: newCount, hasReacted: !hasReacted };
230
+ } catch (err: any) {
231
+ console.error("Toggle reaction failed:", err);
232
+ return { error: err.message || "An unexpected error occurred." };
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Updates an interaction's status (approved or denied). Admin/Moderator only.
238
+ */
239
+ /**
240
+ * Visitor text goes straight into an HTML mail body below. Without this a review
241
+ * containing markup renders as live HTML inside the moderator's mail client.
242
+ */
243
+ function escapeHtml(value: string): string {
244
+ return value
245
+ .replace(/&/g, '&amp;')
246
+ .replace(/</g, '&lt;')
247
+ .replace(/>/g, '&gt;')
248
+ .replace(/"/g, '&quot;')
249
+ .replace(/'/g, '&#39;');
250
+ }
251
+
252
+ /**
253
+ * Publish a staff answer under a review or a post comment.
254
+ *
255
+ * This is the PUBLIC lane of the Messages inbox, and it is a different shape from a
256
+ * private thread on purpose: the parent is already visible on the site and already
257
+ * belongs to a registered account, so the reply is published content, not a private
258
+ * message. Putting it behind a token link would hide a public answer.
259
+ *
260
+ * A reply is stored as `type: 'comment'` with a NULL rating, carrying the PARENT's
261
+ * target. That is not a shortcut — it is the only shape the existing constraints admit
262
+ * (`check_rating_only_for_review` is an exhaustive OR over 'review' and 'comment'), and
263
+ * it is what keeps the reply out of `update_product_ratings`, which aggregates only
264
+ * `type='review' AND status='approved'`. A reply must never move a product's stars.
265
+ */
266
+ export async function replyToInteraction(parentId: string, content: string) {
267
+ const supabase = createClient();
268
+
269
+ const { data: { user }, error: authError } = await supabase.auth.getUser();
270
+ if (authError || !user) {
271
+ return { error: "Not authenticated" };
272
+ }
273
+
274
+ const profile = await getProfileWithRoleServerSide(user.id);
275
+ if (!profile || profile.role !== "ADMIN") {
276
+ return { error: "Unauthorized. Admin permissions required to reply." };
277
+ }
278
+
279
+ const body = content.trim().slice(0, 5000);
280
+ if (body.length < 2) {
281
+ return { error: "Write a reply before sending." };
282
+ }
283
+
284
+ try {
285
+ const admin = getServiceRoleSupabaseClient();
286
+
287
+ const { data: parent, error: parentError } = await admin
288
+ .from("cms_interactions")
289
+ .select("id, product_id, post_id, parent_id, products(slug), posts(slug)")
290
+ .eq("id", parentId)
291
+ .single();
292
+
293
+ if (parentError || !parent) {
294
+ return { error: "That review or comment no longer exists." };
295
+ }
296
+
297
+ // One level only. The schema enforces this too; failing here gives a better message.
298
+ if (parent.parent_id) {
299
+ return { error: "You can only reply to a top-level review or comment." };
300
+ }
301
+
302
+ const { error: insertError } = await admin.from("cms_interactions").insert({
303
+ type: "comment",
304
+ status: "approved",
305
+ content: body,
306
+ rating: null,
307
+ user_id: user.id,
308
+ parent_id: parent.id,
309
+ product_id: parent.product_id,
310
+ post_id: parent.post_id,
311
+ });
312
+
313
+ if (insertError) {
314
+ console.error("Error publishing reply:", insertError.message);
315
+ return { error: "Could not publish that reply." };
316
+ }
317
+
318
+ const productSlug = (parent.products as { slug?: string } | null)?.slug;
319
+ const postSlug = (parent.posts as { slug?: string } | null)?.slug;
320
+ if (productSlug) revalidatePath(`/product/${productSlug}`);
321
+ if (postSlug) revalidatePath(`/article/${postSlug}`);
322
+ revalidatePath("/cms/messages");
323
+ revalidatePath("/cms/interactions");
324
+
325
+ return { success: true };
326
+ } catch (error) {
327
+ console.error("replyToInteraction failed:", error);
328
+ return { error: "Could not publish that reply." };
329
+ }
330
+ }
331
+
332
+ export async function updateInteractionStatus(interactionId: string, status: "approved" | "denied") {
333
+ const supabase = createClient();
334
+
335
+ // 1. Authenticate user
336
+ const { data: { user }, error: authError } = await supabase.auth.getUser();
337
+ if (authError || !user) {
338
+ return { error: "Not authenticated" };
339
+ }
340
+
341
+ // 2. Authorize as Admin or Writer
342
+ const profile = await getProfileWithRoleServerSide(user.id);
343
+ if (!profile || (profile.role !== "ADMIN" && profile.role !== "WRITER")) {
344
+ return { error: "Unauthorized. Admin or Writer permissions required." };
345
+ }
346
+
347
+ // 3. Admin-only rule for denying/approving if strict
348
+ if (profile.role !== "ADMIN") {
349
+ // If writers are not allowed to moderate, block it. The spec says:
350
+ // "Admin-only permission action to switch states between approved or denied."
351
+ // So let's enforce STRICT Admin only for status updates.
352
+ return { error: "Unauthorized. Admin permissions required to moderate." };
353
+ }
354
+
355
+ try {
356
+ const admin = getServiceRoleSupabaseClient();
357
+
358
+ // Fetch interaction details for path revalidation
359
+ const { data: interaction, error: fetchError } = await admin
360
+ .from("cms_interactions")
361
+ .select("product_id, post_id, products(slug), posts(slug)")
362
+ .eq("id", interactionId)
363
+ .single();
364
+
365
+ if (fetchError || !interaction) {
366
+ return { error: "Interaction not found." };
367
+ }
368
+
369
+ // 4. Update status
370
+ const { error: updateError } = await admin
371
+ .from("cms_interactions")
372
+ .update({ status })
373
+ .eq("id", interactionId);
374
+
375
+ if (updateError) {
376
+ console.error("Error updating status:", updateError);
377
+ return { error: `Failed to update status: ${updateError.message}` };
378
+ }
379
+
380
+ // 5. Revalidate paths
381
+ const resolvedProduct = interaction.products as any;
382
+ const resolvedPost = interaction.posts as any;
383
+
384
+ if (interaction.product_id && resolvedProduct?.slug) {
385
+ revalidatePath(`/product/${resolvedProduct.slug}`);
386
+ } else if (interaction.post_id && resolvedPost?.slug) {
387
+ revalidatePath(`/article/${resolvedPost.slug}`);
388
+ }
389
+ revalidatePath("/cms/interactions");
390
+
391
+ return { success: true };
392
+ } catch (err: any) {
393
+ console.error("Update interaction status failed:", err);
394
+ return { error: err.message || "An unexpected error occurred." };
395
+ }
396
+ }
397
+
398
+ /**
399
+ * Fetches the interactions notification emails from site_settings.
400
+ */
401
+ export async function getNotificationEmails() {
402
+ const supabase = createClient();
403
+
404
+ // Authenticate & authorize
405
+ const { data: { user } } = await supabase.auth.getUser();
406
+ if (!user) return { error: "Not authenticated" };
407
+
408
+ const profile = await getProfileWithRoleServerSide(user.id);
409
+ if (!profile || profile.role !== "ADMIN") {
410
+ return { error: "Unauthorized. Admin role required." };
411
+ }
412
+
413
+ try {
414
+ const { data, error } = await supabase
415
+ .from("site_settings")
416
+ .select("value")
417
+ .eq("key", "interactions_notification_emails")
418
+ .maybeSingle();
419
+
420
+ if (error) throw error;
421
+
422
+ return { success: true, emails: (data?.value as any)?.emails || "" };
423
+ } catch (err: any) {
424
+ console.error("Failed to fetch notification emails:", err);
425
+ return { error: err.message || "Failed to fetch settings." };
426
+ }
427
+ }
428
+
429
+ /**
430
+ * Saves the interactions notification emails to site_settings.
431
+ */
432
+ export async function saveNotificationEmails(emails: string) {
433
+ const supabase = createClient();
434
+
435
+ // Authenticate & authorize
436
+ const { data: { user } } = await supabase.auth.getUser();
437
+ if (!user) return { error: "Not authenticated" };
438
+
439
+ const profile = await getProfileWithRoleServerSide(user.id);
440
+ if (!profile || profile.role !== "ADMIN") {
441
+ return { error: "Unauthorized. Admin role required." };
442
+ }
443
+
444
+ // Validate every address, dedupe (case-insensitive), and normalize to lowercase.
445
+ // Mirrors the client-side check so a crafted/legacy payload can't persist junk.
446
+ const emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
447
+ const tokens = emails
448
+ .split(",")
449
+ .map((e) => e.trim())
450
+ .filter(Boolean);
451
+
452
+ const seen = new Set<string>();
453
+ const valid: string[] = [];
454
+ const invalid: string[] = [];
455
+ for (const token of tokens) {
456
+ const lower = token.toLowerCase();
457
+ if (!emailRe.test(lower)) {
458
+ invalid.push(token);
459
+ continue;
460
+ }
461
+ if (seen.has(lower)) continue;
462
+ seen.add(lower);
463
+ valid.push(lower);
464
+ }
465
+
466
+ if (invalid.length > 0) {
467
+ return {
468
+ error: `Invalid email address${invalid.length > 1 ? "es" : ""}: ${invalid.join(", ")}`,
469
+ };
470
+ }
471
+
472
+ const cleaned = valid.join(", ");
473
+
474
+ try {
475
+ const { error } = await supabase
476
+ .from("site_settings")
477
+ .upsert({
478
+ key: "interactions_notification_emails",
479
+ value: { emails: cleaned },
480
+ });
481
+
482
+ if (error) throw error;
483
+
484
+ return { success: true, emails: cleaned };
485
+ } catch (err: any) {
486
+ console.error("Failed to save notification emails:", err);
487
+ return { error: err.message || "Failed to save settings." };
488
+ }
489
+ }