feat: add description field to orders and support marking menu items as sold out
This commit is contained in:
+31
-5
@@ -237,6 +237,7 @@ export async function getMyOrders(creator_id: string) {
|
||||
export async function createOrder(data: {
|
||||
creator_id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
date: Date;
|
||||
allow_custom: boolean;
|
||||
available_items: string[];
|
||||
@@ -245,6 +246,7 @@ export async function createOrder(data: {
|
||||
const order = await prisma.order.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
description: data.description || null,
|
||||
date: data.date,
|
||||
allow_custom: data.allow_custom,
|
||||
creator_id: data.creator_id,
|
||||
@@ -273,12 +275,13 @@ export async function duplicateOrder(order_id: string) {
|
||||
const newOrder = await prisma.order.create({
|
||||
data: {
|
||||
title: oldOrder.title + ' (Copy)',
|
||||
description: oldOrder.description,
|
||||
date: new Date(),
|
||||
allow_custom: oldOrder.allow_custom,
|
||||
creator_id: oldOrder.creator_id,
|
||||
status: 'DRAFT',
|
||||
available_items: {
|
||||
create: oldOrder.available_items.map(ai => ({ name: ai.name }))
|
||||
create: oldOrder.available_items.map(ai => ({ name: ai.name, is_sold_out: ai.is_sold_out }))
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -291,8 +294,9 @@ export async function duplicateOrder(order_id: string) {
|
||||
|
||||
export async function updateOrder(order_id: string, data: {
|
||||
title: string;
|
||||
description?: string;
|
||||
allow_custom: boolean;
|
||||
available_items: string[];
|
||||
available_items: { name: string; is_sold_out: boolean }[];
|
||||
}) {
|
||||
try {
|
||||
const order = await prisma.order.findUnique({
|
||||
@@ -312,9 +316,10 @@ export async function updateOrder(order_id: string, data: {
|
||||
where: { id: order_id },
|
||||
data: {
|
||||
title: data.title,
|
||||
description: data.description || null,
|
||||
allow_custom: data.allow_custom,
|
||||
available_items: {
|
||||
create: data.available_items.map(name => ({ name }))
|
||||
create: data.available_items.map(item => ({ name: item.name, is_sold_out: item.is_sold_out }))
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -323,8 +328,9 @@ export async function updateOrder(order_id: string, data: {
|
||||
revalidatePath(`/my-orders/${order_id}`)
|
||||
revalidatePath('/')
|
||||
return { success: true, order: updatedOrder }
|
||||
} catch (error) {
|
||||
return { success: false, error: 'Gagal memperbarui order.' }
|
||||
} catch (error: any) {
|
||||
console.error("Update Order Error:", error)
|
||||
return { success: false, error: 'Gagal memperbarui order: ' + error?.message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -697,3 +703,23 @@ export async function getBalancesAsSubmittor(user_id: string) {
|
||||
|
||||
return Array.from(balanceMap.values()).filter(b => b.amount !== 0)
|
||||
}
|
||||
|
||||
export async function deleteSubmission(submission_id: string, user_id: string) {
|
||||
try {
|
||||
const submission = await prisma.submission.findUnique({
|
||||
where: { id: submission_id },
|
||||
include: { order: true }
|
||||
});
|
||||
|
||||
if (!submission) return { success: false, error: 'Pesanan tidak ditemukan' };
|
||||
if (submission.user_id !== user_id) return { success: false, error: 'Tidak ada akses' };
|
||||
if (submission.order.status !== 'OPEN') return { success: false, error: 'Hanya bisa membatalkan PO yang OPEN' };
|
||||
|
||||
await prisma.submission.delete({
|
||||
where: { id: submission_id }
|
||||
});
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: 'Gagal membatalkan pesanan' };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user