From 65498e2da5aca8c8eb66f5b50032d7228fef1865 Mon Sep 17 00:00:00 2001 From: Vadym Samoilenko Date: Wed, 3 Jun 2026 13:56:58 +0100 Subject: [PATCH] =?UTF-8?q?feat(cms):=20add=20seed-forms=20route=20?= =?UTF-8?q?=E2=80=94=20creates=20birthday=20+=20group-visits=20forms=20and?= =?UTF-8?q?=20links=20to=20globals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/admin/seed-forms/route.ts | 163 ++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/app/api/admin/seed-forms/route.ts diff --git a/src/app/api/admin/seed-forms/route.ts b/src/app/api/admin/seed-forms/route.ts new file mode 100644 index 0000000..1e137d5 --- /dev/null +++ b/src/app/api/admin/seed-forms/route.ts @@ -0,0 +1,163 @@ +import type { NextRequest } from 'next/server' +import { NextResponse } from 'next/server' +import { getPayload } from 'payload' +import config from '@payload-config' + +export async function POST(req: NextRequest): Promise { + const secret = req.nextUrl.searchParams.get('secret') + if (!secret || secret !== process.env['SYNC_SECRET']) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } + + const payload = await getPayload({ config }) + + // Idempotency check + const existing = await payload.find({ + collection: 'forms', + where: { title: { in: ['Дні народження', 'Групові відвідування'] } }, + limit: 2, + }) + if (existing.totalDocs >= 2) { + return NextResponse.json({ message: 'Forms already seeded', count: existing.totalDocs }) + } + + // ── 1. Birthday form ────────────────────────────────────────────── + const birthdayForm = await payload.create({ + collection: 'forms', + data: { + title: 'Дні народження', + fields: [ + { + blockType: 'text', + name: 'name', + label: "Ваше ім'я", + required: true, + placeholder: 'Іван Іванов', + }, + { + blockType: 'text', + name: 'phone', + label: 'Телефон', + required: true, + placeholder: '+38 (0__) ___-__-__', + }, + { + blockType: 'email', + name: 'email', + label: 'Email', + placeholder: 'your@email.com', + }, + { + blockType: 'number', + name: 'childAge', + label: 'Вік іменинника', + placeholder: '7', + }, + { + blockType: 'number', + name: 'guestCount', + label: 'Кількість гостей', + placeholder: '15', + }, + { + blockType: 'date', + name: 'preferredDate', + label: 'Бажана дата', + required: true, + width: 100, + }, + { + blockType: 'textarea', + name: 'wishes', + label: 'Побажання', + placeholder: 'Тема свята, улюблені герої, особливі побажання...', + }, + ], + submitButtonLabel: 'Замовити святкування', + confirmationType: 'message', + } as never, + overrideAccess: true, + }) + + // ── 2. Group visits form ────────────────────────────────────────── + const groupForm = await payload.create({ + collection: 'forms', + data: { + title: 'Групові відвідування', + fields: [ + { + blockType: 'text', + name: 'name', + label: "Ваше ім'я", + required: true, + placeholder: 'Іван Іванов', + }, + { + blockType: 'text', + name: 'phone', + label: 'Телефон', + required: true, + placeholder: '+38 (0__) ___-__-__', + }, + { + blockType: 'email', + name: 'email', + label: 'Email', + placeholder: 'your@email.com', + }, + { + blockType: 'number', + name: 'groupSize', + label: 'Кількість учасників', + required: true, + placeholder: '30', + }, + { + blockType: 'date', + name: 'preferredDate', + label: 'Бажана дата', + width: 100, + }, + { + blockType: 'select', + name: 'groupType', + label: 'Тип групи', + options: [ + { label: 'Шкільна екскурсія', value: 'school' }, + { label: 'Дитячий садок', value: 'kindergarten' }, + { label: 'Корпоратив', value: 'corporate' }, + { label: 'Інше', value: 'other' }, + ], + }, + { + blockType: 'textarea', + name: 'message', + label: 'Повідомлення', + placeholder: 'Додаткові побажання або запитання...', + }, + ], + submitButtonLabel: 'Надіслати заявку', + confirmationType: 'message', + } as never, + overrideAccess: true, + }) + + // ── 3. Link forms to globals ────────────────────────────────────── + await payload.updateGlobal({ + slug: 'birthday-page', + data: { form: birthdayForm.id } as never, + overrideAccess: true, + }) + + await payload.updateGlobal({ + slug: 'group-visits-page', + data: { form: groupForm.id } as never, + overrideAccess: true, + }) + + return NextResponse.json({ + ok: true, + birthdayFormId: birthdayForm.id, + groupFormId: groupForm.id, + }) +}