# Review package: 4b5b3b7..ae5ec76 ## Commits ae5ec76 PDF: trigger GitHub Action via repository_dispatch ## Files changed app/src/lib/server/pdf/dispatch.test.ts | 18 ++++++++++++++++++ app/src/lib/server/pdf/dispatch.ts | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) ## Diff diff --git a/app/src/lib/server/pdf/dispatch.test.ts b/app/src/lib/server/pdf/dispatch.test.ts new file mode 100644 index 0000000..c53d637 --- /dev/null +++ b/app/src/lib/server/pdf/dispatch.test.ts @@ -0,0 +1,18 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +vi.mock('$env/dynamic/private', () => ({ env: { GITHUB_DISPATCH_TOKEN: 'tok' } })); + +describe('triggerPdfGeneration', () => { + beforeEach(() => vi.restoreAllMocks()); + it('POSTa il repository_dispatch con il payload giusto', async () => { + const fetchMock = vi.fn().mockResolvedValue({ ok: true, status: 204 }); + vi.stubGlobal('fetch', fetchMock); + const { triggerPdfGeneration } = await import('./dispatch'); + await triggerPdfGeneration(467); + const [url, opts] = fetchMock.mock.calls[0]; + expect(url).toBe('https://api.github.com/repos/paoloalby/alleanza-dvr/dispatches'); + expect(opts.method).toBe('POST'); + expect(JSON.parse(opts.body)).toEqual({ event_type: 'generate-dvr-pdf', client_payload: { agenzia_id: 467 } }); + expect(opts.headers.Authorization).toBe('Bearer tok'); + }); +}); diff --git a/app/src/lib/server/pdf/dispatch.ts b/app/src/lib/server/pdf/dispatch.ts new file mode 100644 index 0000000..eeb9c9b --- /dev/null +++ b/app/src/lib/server/pdf/dispatch.ts @@ -0,0 +1,19 @@ +import { env } from '$env/dynamic/private'; + +const REPO = 'paoloalby/alleanza-dvr'; + +export async function triggerPdfGeneration(agenziaId: number): Promise { + const token = env.GITHUB_DISPATCH_TOKEN; + if (!token) throw new Error('GITHUB_DISPATCH_TOKEN mancante'); + const res = await fetch(`https://api.github.com/repos/${REPO}/dispatches`, { + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + Accept: 'application/vnd.github+json', + 'Content-Type': 'application/json', + 'User-Agent': 'alleanza-dvr' + }, + body: JSON.stringify({ event_type: 'generate-dvr-pdf', client_payload: { agenzia_id: agenziaId } }) + }); + if (!res.ok) throw new Error(`Dispatch fallito: ${res.status}`); +}