feat(plan4): API GET /api/admin/connections/test
This commit is contained in:
@@ -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 })
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user