Files
lunaqrmenu/app/admin/actions.ts

192 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use server';
import { prisma } from '@/app/lib/prisma';
import { revalidatePath } from 'next/cache';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import bcrypt from 'bcryptjs';
import { encrypt } from '@/app/lib/auth';
/* ────── Auth Actions ────── */
export async function authenticate(formData: FormData) {
const username = formData.get('username') as string;
const password = formData.get('password') as string;
const user = await (prisma as any).user.findUnique({ where: { username } });
if (!user || !(await bcrypt.compare(password, user.password))) {
return { error: 'Geçersiz kullanıcı adı veya şifre' };
}
// Create session
const expires = new Date(Date.now() + 2 * 60 * 60 * 1000); // 2 hours
const session = await encrypt({ userId: user.id, username: user.username, expires });
(await cookies()).set('session', session, { expires, httpOnly: true });
redirect('/admin');
}
export async function signout() {
(await cookies()).set('session', '', { expires: new Date(0) });
redirect('/login');
}
/* ────── User Actions ────── */
export async function createAdmin(formData: FormData) {
const username = formData.get('username') as string;
const password = formData.get('password') as string;
const hashedPassword = await bcrypt.hash(password, 10);
await (prisma as any).user.create({
data: {
username,
password: hashedPassword,
},
});
revalidatePath('/admin/users');
}
export async function updateAdminPassword(id: string, formData: FormData) {
const password = formData.get('password') as string;
const hashedPassword = await bcrypt.hash(password, 10);
await (prisma as any).user.update({
where: { id },
data: {
password: hashedPassword,
},
});
revalidatePath('/admin/users');
}
export async function deleteAdmin(id: string) {
const session = await encrypt({ expires: new Date(0) }); // placeholder check
// Don't allow deleting self if we had current user id here,
// but for now simple delete
await (prisma as any).user.delete({ where: { id } });
revalidatePath('/admin/users');
}
export async function createCategory(formData: FormData) {
const title = formData.get('title') as string;
const externalId = formData.get('externalId') as string;
await prisma.category.create({
data: {
title,
externalId: externalId || null,
restaurantId: (await prisma.restaurant.findFirst())?.id || '',
},
});
revalidatePath('/admin/categories');
revalidatePath('/');
}
export async function updateCategory(id: string, formData: FormData) {
const title = formData.get('title') as string;
const externalId = formData.get('externalId') as string;
await prisma.category.update({
where: { id },
data: {
title,
externalId: externalId || null,
},
});
revalidatePath('/admin/categories');
revalidatePath('/');
}
export async function deleteCategory(id: string) {
// Optional: check if there are items first, or let prisma handles cascade if configured
await prisma.item.deleteMany({ where: { categoryId: id } });
await prisma.category.delete({ where: { id } });
revalidatePath('/admin/categories');
revalidatePath('/');
}
/* ────── Item Actions ────── */
export async function createItem(formData: FormData) {
const name = formData.get('name') as string;
const categoryId = formData.get('categoryId') as string;
const ingredients = formData.get('ingredients') as string;
const tasteProfile = formData.get('tasteProfile') as string;
const grapeVariety = formData.get('grapeVariety') as string;
const priceInput = formData.get('price') as string;
let price;
try {
// Try to parse as JSON if it looks like an object, otherwise keep as string
price = (priceInput.startsWith('{') || priceInput.startsWith('['))
? JSON.parse(priceInput)
: priceInput;
} catch (e) {
price = priceInput;
}
await prisma.item.create({
data: {
name,
categoryId,
ingredients: ingredients || null,
tasteProfile: tasteProfile || null,
grapeVariety: grapeVariety || null,
price,
},
});
revalidatePath('/admin/items');
revalidatePath('/');
}
export async function updateItem(id: string, formData: FormData) {
const name = formData.get('name') as string;
const categoryId = formData.get('categoryId') as string;
const ingredients = formData.get('ingredients') as string;
const tasteProfile = formData.get('tasteProfile') as string;
const grapeVariety = formData.get('grapeVariety') as string;
const priceInput = formData.get('price') as string;
let price;
try {
price = (priceInput.startsWith('{') || priceInput.startsWith('['))
? JSON.parse(priceInput)
: priceInput;
} catch (e) {
price = priceInput;
}
await prisma.item.update({
where: { id },
data: {
name,
categoryId,
ingredients: ingredients || null,
tasteProfile: tasteProfile || null,
grapeVariety: grapeVariety || null,
price,
},
});
revalidatePath('/admin/items');
revalidatePath('/');
}
export async function deleteItem(id: string) {
await prisma.item.delete({ where: { id } });
revalidatePath('/admin/items');
revalidatePath('/');
}