feat: API route GET /api/sync/history (TDD)
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
import { GET } from '@/app/api/sync/history/route'
|
||||||
|
import { NextRequest } from 'next/server'
|
||||||
|
|
||||||
|
jest.mock('@/lib/prisma', () => ({
|
||||||
|
prisma: {
|
||||||
|
syncRun: {
|
||||||
|
findMany: jest.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
import { prisma } from '@/lib/prisma'
|
||||||
|
const mockFindMany = prisma.syncRun.findMany as jest.Mock
|
||||||
|
|
||||||
|
describe('GET /api/sync/history', () => {
|
||||||
|
beforeEach(() => jest.clearAllMocks())
|
||||||
|
|
||||||
|
it('retourne une liste vide si aucun SyncRun', async () => {
|
||||||
|
mockFindMany.mockResolvedValue([])
|
||||||
|
const req = new NextRequest('http://localhost/api/sync/history')
|
||||||
|
const res = await GET(req)
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
expect(await res.json()).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('retourne les syncs formatées', async () => {
|
||||||
|
const fakeRun = {
|
||||||
|
id: 'clid1',
|
||||||
|
createdAt: new Date('2026-06-08T10:00:00Z'),
|
||||||
|
status: 'success',
|
||||||
|
created: 3,
|
||||||
|
updated: 1,
|
||||||
|
deactivated: 0,
|
||||||
|
errors: 0,
|
||||||
|
log: JSON.stringify([{ ref: 'REF-1', action: 'create', status: 'success', message: '✓ REF-1 créé' }]),
|
||||||
|
user: { name: 'Mathieu' },
|
||||||
|
}
|
||||||
|
mockFindMany.mockResolvedValue([fakeRun])
|
||||||
|
|
||||||
|
const req = new NextRequest('http://localhost/api/sync/history')
|
||||||
|
const res = await GET(req)
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
const data = await res.json()
|
||||||
|
expect(data).toHaveLength(1)
|
||||||
|
expect(data[0].id).toBe('clid1')
|
||||||
|
expect(data[0].userName).toBe('Mathieu')
|
||||||
|
expect(data[0].status).toBe('success')
|
||||||
|
expect(data[0].created).toBe(3)
|
||||||
|
expect(data[0].log).toHaveLength(1)
|
||||||
|
expect(data[0].log[0].ref).toBe('REF-1')
|
||||||
|
expect(data[0].createdAt).toBe('2026-06-08T10:00:00.000Z')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('retourne 500 si Prisma échoue', async () => {
|
||||||
|
mockFindMany.mockRejectedValue(new Error('DB error'))
|
||||||
|
const req = new NextRequest('http://localhost/api/sync/history')
|
||||||
|
const res = await GET(req)
|
||||||
|
expect(res.status).toBe(500)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
|
import { prisma } from '@/lib/prisma'
|
||||||
|
import type { SyncHistoryEntry, SyncLogEntry } from '@/types/sync'
|
||||||
|
|
||||||
|
export async function GET(_req: NextRequest) {
|
||||||
|
try {
|
||||||
|
const runs = await prisma.syncRun.findMany({
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
take: 20,
|
||||||
|
include: { user: { select: { name: true } } },
|
||||||
|
})
|
||||||
|
|
||||||
|
const entries: SyncHistoryEntry[] = runs.map((run) => ({
|
||||||
|
id: run.id,
|
||||||
|
createdAt: run.createdAt.toISOString(),
|
||||||
|
userName: run.user.name,
|
||||||
|
status: run.status as SyncHistoryEntry['status'],
|
||||||
|
created: run.created,
|
||||||
|
updated: run.updated,
|
||||||
|
deactivated: run.deactivated,
|
||||||
|
errors: run.errors,
|
||||||
|
log: (() => {
|
||||||
|
try { return JSON.parse(run.log) as SyncLogEntry[] } catch { return [] }
|
||||||
|
})(),
|
||||||
|
}))
|
||||||
|
|
||||||
|
return NextResponse.json(entries)
|
||||||
|
} catch (err) {
|
||||||
|
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
||||||
|
return NextResponse.json({ error: message }, { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user