feat(cms): add seed-forms route — creates birthday + group-visits forms and links to globals
This commit is contained in:
parent
7f28be23f8
commit
65498e2da5
1 changed files with 163 additions and 0 deletions
163
src/app/api/admin/seed-forms/route.ts
Normal file
163
src/app/api/admin/seed-forms/route.ts
Normal file
|
|
@ -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<NextResponse> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue