fix: API Key Google Sheets (remplace service account) + signatures routes GET
This commit is contained in:
@@ -2,7 +2,9 @@ import { NextResponse } from 'next/server'
|
|||||||
import { prisma } from '@/lib/prisma'
|
import { prisma } from '@/lib/prisma'
|
||||||
import type { SyncHistoryEntry, SyncLogEntry } from '@/types/sync'
|
import type { SyncHistoryEntry, SyncLogEntry } from '@/types/sync'
|
||||||
|
|
||||||
export async function GET() {
|
import { NextRequest } from 'next/server'
|
||||||
|
|
||||||
|
export async function GET(_req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const runs = await prisma.syncRun.findMany({
|
const runs = await prisma.syncRun.findMany({
|
||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ import { readSheetProducts } from '@/lib/googleSheets'
|
|||||||
import { fetchAllShopifyProducts } from '@/lib/shopifySync'
|
import { fetchAllShopifyProducts } from '@/lib/shopifySync'
|
||||||
import { computeDiff } from '@/lib/syncDiff'
|
import { computeDiff } from '@/lib/syncDiff'
|
||||||
|
|
||||||
export async function GET() {
|
import { NextRequest } from 'next/server'
|
||||||
|
|
||||||
|
export async function GET(_req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const [sheetProducts, shopifyProducts] = await Promise.all([
|
const [sheetProducts, shopifyProducts] = await Promise.all([
|
||||||
readSheetProducts(),
|
readSheetProducts(),
|
||||||
|
|||||||
+14
-19
@@ -37,33 +37,28 @@ export function parseSheetRows(rows: string[][]): SyncProduct[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lit tous les produits depuis Google Sheets via l'API v4.
|
* Lit tous les produits depuis Google Sheets via l'API v4 (API Key publique).
|
||||||
* Utilise un service account (GOOGLE_SERVICE_ACCOUNT_EMAIL + GOOGLE_PRIVATE_KEY).
|
* Le fichier Sheets doit être partagé en lecture ("Toute personne avec le lien").
|
||||||
*/
|
*/
|
||||||
export async function readSheetProducts(): Promise<SyncProduct[]> {
|
export async function readSheetProducts(): Promise<SyncProduct[]> {
|
||||||
const email = process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL
|
const apiKey = process.env.GOOGLE_API_KEY
|
||||||
const key = process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n')
|
|
||||||
const sheetId = process.env.GOOGLE_SHEETS_ID
|
const sheetId = process.env.GOOGLE_SHEETS_ID
|
||||||
const sheetName = process.env.GOOGLE_SHEETS_TAB ?? 'Produits'
|
const sheetName = process.env.GOOGLE_SHEETS_TAB ?? 'Produits'
|
||||||
|
|
||||||
if (!email || !key || !sheetId) {
|
if (!apiKey || !sheetId) {
|
||||||
throw new Error('Variables GOOGLE_SERVICE_ACCOUNT_EMAIL, GOOGLE_PRIVATE_KEY et GOOGLE_SHEETS_ID requises')
|
throw new Error('Variables GOOGLE_API_KEY et GOOGLE_SHEETS_ID requises')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { google } = await import('googleapis')
|
const range = encodeURIComponent(`${sheetName}!A2:F`)
|
||||||
|
const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${range}?key=${apiKey}`
|
||||||
|
|
||||||
const auth = new google.auth.GoogleAuth({
|
const res = await fetch(url)
|
||||||
credentials: { client_email: email, private_key: key },
|
if (!res.ok) {
|
||||||
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'],
|
const err = await res.text()
|
||||||
})
|
throw new Error(`Google Sheets API error: ${res.status} — ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
const sheets = google.sheets({ version: API_VERSION, auth })
|
const data = await res.json()
|
||||||
|
const rows = (data.values ?? []) as string[][]
|
||||||
const response = await sheets.spreadsheets.values.get({
|
|
||||||
spreadsheetId: sheetId,
|
|
||||||
range: `${sheetName}!A2:F`, // A2 = skip header row
|
|
||||||
})
|
|
||||||
|
|
||||||
const rows = (response.data.values ?? []) as string[][]
|
|
||||||
return parseSheetRows(rows)
|
return parseSheetRows(rows)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user