233 lines
7.0 KiB
TypeScript
233 lines
7.0 KiB
TypeScript
'use server'
|
||
|
||
import { mockDb } from '@/lib/mockDb'
|
||
import { uploadToOpeninary } from '@/lib/openinary'
|
||
import { revalidatePath } from 'next/cache'
|
||
|
||
export async function submitBusinessAction(formData: FormData) {
|
||
const businessName = formData.get('businessName') as string
|
||
const categoryId = formData.get('categoryId') as string
|
||
const neighborhoodId = formData.get('neighborhoodId') as string
|
||
const address = formData.get('address') as string
|
||
const phone = formData.get('phone') as string
|
||
const whatsapp = formData.get('whatsapp') as string
|
||
const description = formData.get('description') as string
|
||
const contactName = formData.get('contactName') as string
|
||
const contactEmail = formData.get('contactEmail') as string
|
||
const imageFile = formData.get('imageFile') as File | null
|
||
|
||
if (!businessName || !categoryId || !neighborhoodId || !address || !description || !contactName || !contactEmail) {
|
||
return { error: 'Lütfen tüm zorunlu alanları doldurun.' }
|
||
}
|
||
|
||
let imageUrl = null
|
||
if (imageFile && imageFile.size > 0) {
|
||
try {
|
||
imageUrl = await uploadToOpeninary(imageFile, 'submissions')
|
||
} catch (e: any) {
|
||
console.error('Openinary upload error:', e)
|
||
return { error: `Görsel yüklenemedi: ${e.message}` }
|
||
}
|
||
}
|
||
|
||
await mockDb.createSubmission({
|
||
businessName,
|
||
categoryId,
|
||
neighborhoodId,
|
||
address,
|
||
phone,
|
||
whatsapp,
|
||
description,
|
||
contactName,
|
||
contactEmail,
|
||
imageUrl
|
||
})
|
||
|
||
revalidatePath('/admin/submissions')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function submitContactMessageAction(formData: FormData) {
|
||
const name = formData.get('name') as string
|
||
const email = formData.get('email') as string
|
||
const subject = formData.get('subject') as string
|
||
const message = formData.get('message') as string
|
||
|
||
if (!name || !email || !subject || !message) {
|
||
return { error: 'Lütfen tüm alanları doldurun.' }
|
||
}
|
||
|
||
await mockDb.createMessage({
|
||
name,
|
||
email,
|
||
subject,
|
||
message
|
||
})
|
||
|
||
revalidatePath('/admin/messages')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function approveSubmissionAction(id: string) {
|
||
const submission = await mockDb.getSubmissionById(id)
|
||
if (!submission) return { error: 'Başvuru bulunamadı' }
|
||
|
||
const slug = submission.businessName.toLowerCase()
|
||
.replace(/[^a-z0-9\s-]/g, '')
|
||
.replace(/\s+/g, '-')
|
||
|
||
await mockDb.createListing({
|
||
slug,
|
||
categoryId: submission.categoryId,
|
||
neighborhoodId: submission.neighborhoodId,
|
||
city: 'marmaris',
|
||
nameTr: submission.businessName,
|
||
nameEn: submission.businessName,
|
||
nameRu: submission.businessName,
|
||
descriptionTr: submission.description,
|
||
descriptionEn: submission.description,
|
||
descriptionRu: submission.description,
|
||
address: submission.address,
|
||
phone: submission.phone,
|
||
whatsapp: submission.whatsapp,
|
||
priceRange: 2,
|
||
rating: 5.0,
|
||
isLocalApproved: false,
|
||
images: submission.imageUrl ? [submission.imageUrl] : []
|
||
})
|
||
|
||
await mockDb.updateSubmissionStatus(id, 'APPROVED')
|
||
|
||
revalidatePath('/admin/submissions')
|
||
revalidatePath('/restoranlar')
|
||
revalidatePath('/apartlar')
|
||
revalidatePath('/isletmeler')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function rejectSubmissionAction(id: string) {
|
||
await mockDb.updateSubmissionStatus(id, 'REJECTED')
|
||
revalidatePath('/admin/submissions')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function deleteListingAction(id: string) {
|
||
await mockDb.deleteListing(id)
|
||
revalidatePath('/admin/listings')
|
||
revalidatePath('/restoranlar')
|
||
revalidatePath('/apartlar')
|
||
revalidatePath('/isletmeler')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function markMessageReadAction(id: string) {
|
||
await mockDb.markMessageAsRead(id)
|
||
revalidatePath('/admin/messages')
|
||
return { success: true }
|
||
}
|
||
|
||
export async function createOrUpdateListingAction(formData: FormData) {
|
||
const id = formData.get('id') as string | null
|
||
const slug = formData.get('slug') as string
|
||
const categoryId = formData.get('categoryId') as string
|
||
const neighborhoodId = formData.get('neighborhoodId') as string
|
||
|
||
const nameTr = formData.get('nameTr') as string
|
||
const nameEn = formData.get('nameEn') as string
|
||
const nameRu = formData.get('nameRu') as string
|
||
|
||
const descriptionTr = formData.get('descriptionTr') as string
|
||
const descriptionEn = formData.get('descriptionEn') as string
|
||
const descriptionRu = formData.get('descriptionRu') as string
|
||
|
||
const address = formData.get('address') as string
|
||
const phone = formData.get('phone') as string || null
|
||
const whatsapp = formData.get('whatsapp') as string || null
|
||
const website = formData.get('website') as string || null
|
||
const instagram = formData.get('instagram') as string || null
|
||
|
||
const priceRange = parseInt(formData.get('priceRange') as string)
|
||
const rating = formData.get('rating') ? parseFloat(formData.get('rating') as string) : null
|
||
const isLocalApproved = formData.get('isLocalApproved') === 'true'
|
||
|
||
const latitude = formData.get('latitude') ? parseFloat(formData.get('latitude') as string) : null
|
||
const longitude = formData.get('longitude') ? parseFloat(formData.get('longitude') as string) : null
|
||
|
||
const openingHoursStr = formData.get('openingHours') as string
|
||
const openingHours = openingHoursStr ? { all: openingHoursStr } : null
|
||
|
||
// Handle images
|
||
const images: string[] = []
|
||
|
||
const imageFile1 = formData.get('imageFile1') as File | null
|
||
const imageUrl1 = formData.get('imageUrl1') as string | null
|
||
const imageFile2 = formData.get('imageFile2') as File | null
|
||
const imageUrl2 = formData.get('imageUrl2') as string | null
|
||
|
||
// Process first image
|
||
if (imageFile1 && imageFile1.size > 0) {
|
||
try {
|
||
const url = await uploadToOpeninary(imageFile1, `listings/${slug}`)
|
||
images.push(url)
|
||
} catch (e: any) {
|
||
console.error('Image 1 upload error:', e)
|
||
return { success: false, error: `1. Görsel yüklenemedi: ${e.message}` }
|
||
}
|
||
} else if (imageUrl1) {
|
||
images.push(imageUrl1)
|
||
}
|
||
|
||
// Process second image
|
||
if (imageFile2 && imageFile2.size > 0) {
|
||
try {
|
||
const url = await uploadToOpeninary(imageFile2, `listings/${slug}`)
|
||
images.push(url)
|
||
} catch (e: any) {
|
||
console.error('Image 2 upload error:', e)
|
||
return { success: false, error: `2. Görsel yüklenemedi: ${e.message}` }
|
||
}
|
||
} else if (imageUrl2) {
|
||
images.push(imageUrl2)
|
||
}
|
||
|
||
const data = {
|
||
slug,
|
||
categoryId,
|
||
neighborhoodId,
|
||
city: 'marmaris',
|
||
nameTr,
|
||
nameEn,
|
||
nameRu,
|
||
descriptionTr,
|
||
descriptionEn,
|
||
descriptionRu,
|
||
address,
|
||
phone,
|
||
whatsapp,
|
||
website,
|
||
instagram,
|
||
priceRange,
|
||
rating,
|
||
isLocalApproved,
|
||
latitude,
|
||
longitude,
|
||
openingHours,
|
||
images
|
||
}
|
||
|
||
try {
|
||
if (id && id !== 'new') {
|
||
await mockDb.updateListing(id, data)
|
||
} else {
|
||
await mockDb.createListing(data)
|
||
}
|
||
revalidatePath('/admin/listings')
|
||
revalidatePath('/restoranlar')
|
||
revalidatePath('/apartlar')
|
||
revalidatePath('/isletmeler')
|
||
return { success: true }
|
||
} catch (err: any) {
|
||
return { success: false, error: err.message || 'Mekan kaydedilemedi.' }
|
||
}
|
||
}
|