Add error handling to stock page for Prisma queries
This commit is contained in:
@@ -4,24 +4,34 @@ import AccountsNav from '@/components/AccountsNav';
|
||||
import StockAdjustForm from '@/components/StockAdjustForm';
|
||||
|
||||
export default async function StockPage() {
|
||||
const [levels, productRows] = await Promise.all([
|
||||
prisma.stockLevel.findMany({
|
||||
include: { product: true },
|
||||
orderBy: [{ product: { name: 'asc' } }, { color: 'asc' }, { size: 'asc' }],
|
||||
}),
|
||||
prisma.product.findMany({ orderBy: { name: 'asc' } }),
|
||||
]);
|
||||
let levels = [];
|
||||
let productRows = [];
|
||||
|
||||
try {
|
||||
[levels, productRows] = await Promise.all([
|
||||
prisma.stockLevel.findMany({
|
||||
include: { product: true },
|
||||
}),
|
||||
prisma.product.findMany(),
|
||||
]);
|
||||
} catch (err) {
|
||||
console.error('Error fetching stock data:', err);
|
||||
}
|
||||
|
||||
const products = productRows.map((p) => ({
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
colors: JSON.parse(p.colors) as string[],
|
||||
sizes: p.sizes ? (JSON.parse(p.sizes) as string[]) : [],
|
||||
}));
|
||||
})).sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
||||
// Group rows under their product for a readable table.
|
||||
const grouped = new Map<string, typeof levels>();
|
||||
for (const level of levels) {
|
||||
for (const level of levels.sort((a, b) => {
|
||||
if (a.product.name !== b.product.name) return a.product.name.localeCompare(b.product.name);
|
||||
if (a.color !== b.color) return a.color.localeCompare(b.color);
|
||||
return (a.size || '').localeCompare(b.size || '');
|
||||
})) {
|
||||
const list = grouped.get(level.productId) ?? [];
|
||||
list.push(level);
|
||||
grouped.set(level.productId, list);
|
||||
|
||||
Reference in New Issue
Block a user