63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import type { SyncProduct } from '@/types/sync'
|
|
|
|
function normalizeHandle(ref: string): string {
|
|
return ref
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
}
|
|
|
|
/**
|
|
* Parse des lignes brutes Google Sheets en SyncProduct[].
|
|
* Colonnes : A=Référence, B=Nom, C=Description, D=Prix, E=Stock, F=Statut
|
|
* Exporté pour les tests unitaires.
|
|
*/
|
|
export function parseSheetRows(rows: string[][]): SyncProduct[] {
|
|
return rows
|
|
.map((row): SyncProduct | null => {
|
|
const ref = row[0]?.trim()
|
|
if (!ref) return null
|
|
|
|
const rawPrice = (row[3] ?? '').replace(',', '.').trim()
|
|
const price = parseFloat(rawPrice || '0')
|
|
|
|
return {
|
|
ref,
|
|
handle: normalizeHandle(ref),
|
|
title: row[1]?.trim() || ref,
|
|
description: row[2]?.trim() || '',
|
|
price: isNaN(price) ? '0.00' : price.toFixed(2),
|
|
stock: parseInt(row[4] ?? '0', 10) || 0,
|
|
status: (row[5] ?? '').toLowerCase().includes('inac') ? 'draft' : 'active',
|
|
}
|
|
})
|
|
.filter((p): p is SyncProduct => p !== null)
|
|
}
|
|
|
|
/**
|
|
* Lit tous les produits depuis Google Sheets via l'API v4 (API Key publique).
|
|
* Le fichier Sheets doit être partagé en lecture ("Toute personne avec le lien").
|
|
*/
|
|
export async function readSheetProducts(): Promise<SyncProduct[]> {
|
|
const apiKey = process.env.GOOGLE_API_KEY
|
|
const sheetId = process.env.GOOGLE_SHEETS_ID
|
|
const sheetName = process.env.GOOGLE_SHEETS_TAB ?? 'Produits'
|
|
|
|
if (!apiKey || !sheetId) {
|
|
throw new Error('Variables GOOGLE_API_KEY et GOOGLE_SHEETS_ID requises')
|
|
}
|
|
|
|
const range = encodeURIComponent(`${sheetName}!A2:F`)
|
|
const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${range}?key=${apiKey}`
|
|
|
|
const res = await fetch(url)
|
|
if (!res.ok) {
|
|
const err = await res.text()
|
|
throw new Error(`Google Sheets API error: ${res.status} — ${err}`)
|
|
}
|
|
|
|
const data = await res.json()
|
|
const rows = (data.values ?? []) as string[][]
|
|
return parseSheetRows(rows)
|
|
}
|