|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { authActionClient } from '@/actions/safe-action'; |
| 4 | +import { db } from '@db'; |
| 5 | +import { revalidatePath } from 'next/cache'; |
| 6 | +import { z } from 'zod'; |
| 7 | +import { faqArraySchema } from '../types/faq'; |
| 8 | + |
| 9 | +const updateTrustPortalFaqsSchema = z.object({ |
| 10 | + faqs: faqArraySchema, |
| 11 | +}); |
| 12 | + |
| 13 | +export const updateTrustPortalFaqsAction = authActionClient |
| 14 | + .inputSchema(updateTrustPortalFaqsSchema) |
| 15 | + .metadata({ |
| 16 | + name: 'update-trust-portal-faqs', |
| 17 | + track: { |
| 18 | + event: 'update-trust-portal-faqs', |
| 19 | + channel: 'server', |
| 20 | + }, |
| 21 | + }) |
| 22 | + .action(async ({ parsedInput, ctx }) => { |
| 23 | + const { faqs } = parsedInput; |
| 24 | + const { activeOrganizationId } = ctx.session; |
| 25 | + |
| 26 | + if (!activeOrganizationId) { |
| 27 | + throw new Error('No active organization'); |
| 28 | + } |
| 29 | + |
| 30 | + // Normalize order values on the server to prevent gaps/duplicates and ensure stable rendering. |
| 31 | + const normalizedFaqs = faqs.map((faq, index) => ({ |
| 32 | + ...faq, |
| 33 | + order: index, |
| 34 | + })); |
| 35 | + |
| 36 | + try { |
| 37 | + await db.organization.update({ |
| 38 | + where: { |
| 39 | + id: activeOrganizationId, |
| 40 | + }, |
| 41 | + data: { |
| 42 | + trustPortalFaqs: |
| 43 | + normalizedFaqs.length > 0 |
| 44 | + ? (JSON.parse(JSON.stringify(normalizedFaqs)) as any) |
| 45 | + : (null as any), |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + revalidatePath(`/${activeOrganizationId}/trust/portal-settings`); |
| 50 | + |
| 51 | + return { |
| 52 | + success: true, |
| 53 | + }; |
| 54 | + } catch (error) { |
| 55 | + console.error('Error updating FAQs:', error); |
| 56 | + const errorMessage = |
| 57 | + error instanceof Error ? error.message : 'Failed to update FAQs'; |
| 58 | + throw new Error(errorMessage); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
0 commit comments