86 lines
2.0 KiB
TypeScript
Raw Normal View History

Integrate NocoDB backend for admin portal with real data Phase 2 Backend Integration Complete: Backend Infrastructure: - Created NocoDB client abstraction layer (src/api/nocodbClient.ts) - Clean TypeScript API hiding NocoDB query syntax complexity - Helper methods for orders, stats, search, timeline, and filtering - Automatic date range handling and pagination support API Routes: - POST /api/admin/stats - Dashboard statistics with time range filtering - GET /api/admin/orders - List orders with search, filter, sort, pagination - GET /api/admin/orders/[id] - Single order details - PATCH /api/admin/orders/[id] - Update order fields - DELETE /api/admin/orders/[id] - Cancel order (soft delete) - GET /api/admin/orders/export - CSV/Excel export with filters Dashboard Updates: - Real-time data fetching from NocoDB - Time range selector (7d, 30d, 90d, all time) - Recharts line chart for orders timeline - Recharts pie chart for status distribution - Loading states and error handling - Dynamic stat cards with real numbers Dependencies Added: - papaparse - CSV export - xlsx - Excel export with styling - @types/papaparse - TypeScript support Data Types: - OrderRecord interface for NocoDB data structure - DashboardStats, TimelineData, OrderFilters interfaces - Full type safety across API and UI Environment Configuration: - NOCODB_BASE_URL, NOCODB_BASE_ID configured - NOCODB_API_KEY, NOCODB_ORDERS_TABLE_ID configured - All credentials stored securely in .env.local Ready for testing with sample data in NocoDB! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 10:40:25 +01:00
import { NextRequest, NextResponse } from 'next/server';
import { nocodbClient } from '@/api/nocodbClient';
/**
* GET /api/admin/orders/[id]
* Get single order by record ID
*/
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const order = await nocodbClient.getOrderById(params.id);
return NextResponse.json({
success: true,
data: order,
});
} catch (error) {
console.error('Error fetching order:', error);
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch order',
},
{ status: 500 }
);
}
}
/**
* PATCH /api/admin/orders/[id]
* Update order fields (commonly used for status updates)
*/
export async function PATCH(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const body = await request.json();
const updatedOrder = await nocodbClient.updateOrder(params.id, body);
return NextResponse.json({
success: true,
data: updatedOrder,
});
} catch (error) {
console.error('Error updating order:', error);
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Failed to update order',
},
{ status: 500 }
);
}
}
/**
* DELETE /api/admin/orders/[id]
* Delete/archive an order
*/
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// Instead of actually deleting, update status to "cancelled"
await nocodbClient.updateOrderStatus(params.id, 'cancelled');
return NextResponse.json({
success: true,
message: 'Order cancelled successfully',
});
} catch (error) {
console.error('Error cancelling order:', error);
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Failed to cancel order',
},
{ status: 500 }
);
}
}