diff --git a/src/app/api/admin/connections/test/route.ts b/src/app/api/admin/connections/test/route.ts new file mode 100644 index 0000000..5413748 --- /dev/null +++ b/src/app/api/admin/connections/test/route.ts @@ -0,0 +1,45 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getServerSession } from 'next-auth' +import { authOptions } from '@/lib/auth' + +async function testShopify(): Promise<{ ok: boolean; message: string }> { + const domain = process.env.SHOPIFY_STORE_DOMAIN + const token = process.env.SHOPIFY_ADMIN_API_TOKEN + if (!domain || !token) return { ok: false, message: 'Variables manquantes' } + try { + const res = await fetch(`https://${domain}/admin/api/2024-01/shop.json`, { + headers: { 'X-Shopify-Access-Token': token }, + }) + if (!res.ok) return { ok: false, message: `HTTP ${res.status}` } + const data = await res.json() + return { ok: true, message: `Boutique : ${data.shop?.name ?? domain}` } + } catch (e) { + return { ok: false, message: e instanceof Error ? e.message : 'Erreur réseau' } + } +} + +async function testSheets(): Promise<{ ok: boolean; message: string }> { + const apiKey = process.env.GOOGLE_API_KEY + const sheetId = process.env.GOOGLE_SHEETS_ID + if (!apiKey || !sheetId) return { ok: false, message: 'Variables manquantes' } + try { + const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}?key=${apiKey}&fields=properties.title` + const res = await fetch(url) + if (!res.ok) return { ok: false, message: `HTTP ${res.status}` } + const data = await res.json() + return { ok: true, message: `Feuille : ${data.properties?.title ?? sheetId}` } + } catch (e) { + return { ok: false, message: e instanceof Error ? e.message : 'Erreur réseau' } + } +} + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export async function GET(_req: NextRequest) { + const session = await getServerSession(authOptions) + if (!session || session.user.role !== 'admin') { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) + } + + const [shopify, sheets] = await Promise.all([testShopify(), testSheets()]) + return NextResponse.json({ shopify, sheets }) +}