diff --git a/app/api/listings/route.ts b/app/api/listings/route.ts index f8eb5f4..a97b968 100644 --- a/app/api/listings/route.ts +++ b/app/api/listings/route.ts @@ -208,10 +208,6 @@ export async function POST(req: Request) { return NextResponse.json({ error: 'Missing slug' }, { status: 400 }); } - if (!saveDraft && (!country || !region || !city || !contactEmail || !contactName)) { - return NextResponse.json({ error: 'Missing required fields' }, { status: 400 }); - } - const maxGuests = body.maxGuests === undefined || body.maxGuests === null || body.maxGuests === '' ? null : Number(body.maxGuests); const bedrooms = body.bedrooms === undefined || body.bedrooms === null || body.bedrooms === '' ? null : Number(body.bedrooms); const beds = body.beds === undefined || body.beds === null || body.beds === '' ? null : Number(body.beds); @@ -247,7 +243,7 @@ export async function POST(req: Request) { }); } - if (!translationsInput.length) { + if (!translationsInput.length && !saveDraft) { return NextResponse.json({ error: 'Missing translation fields (title/description)' }, { status: 400 }); } @@ -311,11 +307,20 @@ export async function POST(req: Request) { } if (!saveDraft) { - if (!country || !region || !city || !contactEmail || !contactName || !maxGuests || !bedrooms || !beds || !bathrooms) { - return NextResponse.json({ error: 'Missing required fields for publish' }, { status: 400 }); - } - if (!parsedImages.length) { - return NextResponse.json({ error: 'At least one image is required to publish' }, { status: 400 }); + const missingFields: string[] = []; + if (!country) missingFields.push('country'); + if (!region) missingFields.push('region'); + if (!city) missingFields.push('city'); + if (!contactEmail) missingFields.push('contactEmail'); + if (!contactName) missingFields.push('contactName'); + if (!maxGuests) missingFields.push('maxGuests'); + if (!bedrooms && bedrooms !== 0) missingFields.push('bedrooms'); + if (!beds) missingFields.push('beds'); + if (!bathrooms) missingFields.push('bathrooms'); + if (!translationsInput.length) missingFields.push('translations'); + if (!parsedImages.length) missingFields.push('images'); + if (missingFields.length) { + return NextResponse.json({ error: `Missing required fields: ${missingFields.join(', ')}` }, { status: 400 }); } } @@ -362,15 +367,17 @@ export async function POST(req: Request) { externalUrl: body.externalUrl ?? null, published: status === ListingStatus.PUBLISHED, isSample, - translations: { - create: translationsInput.map((t: TranslationInput) => ({ - locale: t.locale, - slug: t.slug || slug, - title: t.title, - description: t.description, - teaser: t.teaser ?? null, - })), - }, + translations: translationsInput.length + ? { + create: translationsInput.map((t: TranslationInput) => ({ + locale: t.locale, + slug: t.slug || slug, + title: t.title, + description: t.description, + teaser: t.teaser ?? null, + })), + } + : undefined, images: parsedImages.length ? { create: parsedImages, diff --git a/app/listings/new/page.tsx b/app/listings/new/page.tsx index ec64cd3..a0eb341 100644 --- a/app/listings/new/page.tsx +++ b/app/listings/new/page.tsx @@ -315,14 +315,18 @@ export default function NewListingPage() { teaser: translations[loc].teaser.trim(), })).filter((t) => t.title && t.description); - if (translationEntries.length === 0) { - setError(t('translationMissing')); - setLoading(false); - return; - } - - if (!saveDraft && selectedImages.length === 0) { - setError(t('imagesRequired')); + const missing: string[] = []; + if (!slug.trim()) missing.push(t('slugLabel')); + if (!saveDraft && translationEntries.length === 0) missing.push(t('translationMissing')); + if (!saveDraft && !country.trim()) missing.push(t('countryLabel')); + if (!saveDraft && !region.trim()) missing.push(t('regionLabel')); + if (!saveDraft && !city.trim()) missing.push(t('cityLabel')); + if (!saveDraft && !streetAddress.trim()) missing.push(t('streetAddressLabel')); + if (!saveDraft && !contactName.trim()) missing.push(t('contactNameLabel')); + if (!saveDraft && !contactEmail.trim()) missing.push(t('contactEmailLabel')); + if (!saveDraft && selectedImages.length === 0) missing.push(t('imagesLabel')); + if (missing.length) { + setError(t('missingFields', { fields: missing.join(', ') })); setLoading(false); return; } @@ -471,11 +475,11 @@ export default function NewListingPage() {