Skip to content

Commit

Permalink
add order table and order api
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhagarwal1 committed Nov 9, 2024
1 parent c2026fc commit 32cc7dd
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
27 changes: 27 additions & 0 deletions alimento-nextjs/actions/customer-order/ GetOrderStatusandETA.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use server';

import prismadb from '@/lib/prismadb';

export async function getOrderStatusAndETA({
orderId,
}: {
orderId: string;
}): Promise<{ success: boolean; data?: any; error?: string }> {
try {
const order = await prismadb.order.findUnique({
where: { id: orderId },
include: {
dish: true, // Include dish details
},
});

if (!order) {
return { success: false, error: 'Order not found' };
}

return { success: true, data: { status: order.status, eta: order.estimatedTimeOfArrival } };
} catch (error) {
console.error('[GET_ORDER_STATUS_ETA_ERROR]', error);
return { success: false, error: 'Error fetching order status and ETA' };
}
}
28 changes: 28 additions & 0 deletions alimento-nextjs/actions/customer-order/addorder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use server';

import prismadb from '@/lib/prismadb';

export async function createOrder({
customerId,
dishId,
estimatedTimeOfArrival,
}: {
customerId: string;
dishId: string;
estimatedTimeOfArrival: Date;
}): Promise<{ success: boolean; error?: string }> {
try {
await prismadb.order.create({
data: {
customerId,
dishId,
estimatedTimeOfArrival,
},
});

return { success: true };
} catch (error) {
console.error('[CREATE_ORDER_ERROR]', error);
return { success: false, error: 'Error creating order' };
}
}
Empty file.
29 changes: 29 additions & 0 deletions alimento-nextjs/actions/customer-order/getorder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use server';

import prismadb from '@/lib/prismadb';

export async function getCustomerCurrentOrder({
customerId,
}: {
customerId: string;
}): Promise<{ success: boolean; data?: any; error?: string }> {
try {
const order = await prismadb.order.findFirst({
where: {
customerId,
status: {
in: ['PENDING', 'IN_PROGRESS'], // Fetch only active orders (pending or in progress)
},
},
include: {
dish: true, // Include details of the ordered dish
customer: true, // Include customer details
},
});

return { success: true, data: order };
} catch (error) {
console.error('[GET_CUSTOMER_CURRENT_ORDER_ERROR]', error);
return { success: false, error: 'Error fetching current order' };
}
}
23 changes: 23 additions & 0 deletions alimento-nextjs/actions/customer-order/updateorder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use server';

import prismadb from '@/lib/prismadb';

export async function updateOrderStatus({
orderId,
status,
}: {
orderId: string;
status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED';
}): Promise<{ success: boolean; error?: string }> {
try {
await prismadb.order.update({
where: { id: orderId },
data: { status },
});

return { success: true };
} catch (error) {
console.error('[UPDATE_ORDER_STATUS_ERROR]', error);
return { success: false, error: 'Error updating order status' };
}
}

0 comments on commit 32cc7dd

Please sign in to comment.