62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
// app/api/devices/[id]/history/route.ts
|
|
import { NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
type RouteParams = { id: string };
|
|
// ⬇️ wie bei /api/devices/[id]
|
|
type RouteContext = { params: Promise<RouteParams> };
|
|
|
|
export async function GET(
|
|
_req: Request,
|
|
ctx: RouteContext,
|
|
) {
|
|
// params-Promise auflösen
|
|
const { id } = await ctx.params;
|
|
const inventoryNumber = decodeURIComponent(id);
|
|
|
|
try {
|
|
const history = await prisma.deviceHistory.findMany({
|
|
where: { deviceId: inventoryNumber },
|
|
include: { changedBy: true },
|
|
orderBy: { changedAt: 'desc' },
|
|
});
|
|
|
|
const payload = history.map((entry) => {
|
|
const snapshot = entry.snapshot as any;
|
|
|
|
const rawChanges: any[] = Array.isArray(snapshot?.changes)
|
|
? snapshot.changes
|
|
: [];
|
|
|
|
const changes = rawChanges.map((c) => ({
|
|
field: String(c.field),
|
|
from:
|
|
c.before === null || c.before === undefined
|
|
? null
|
|
: String(c.before),
|
|
to:
|
|
c.after === null || c.after === undefined
|
|
? null
|
|
: String(c.after),
|
|
}));
|
|
|
|
return {
|
|
id: entry.id,
|
|
changeType: entry.changeType, // 'CREATED' | 'UPDATED' | 'DELETED'
|
|
changedAt: entry.changedAt.toISOString(),
|
|
changedBy:
|
|
entry.changedBy?.name ??
|
|
entry.changedBy?.username ??
|
|
entry.changedBy?.email ??
|
|
null,
|
|
changes,
|
|
};
|
|
});
|
|
|
|
return NextResponse.json(payload);
|
|
} catch (err) {
|
|
console.error('[GET /api/devices/[id]/history]', err);
|
|
return NextResponse.json({ error: 'INTERNAL_ERROR' }, { status: 500 });
|
|
}
|
|
}
|