mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 14:15:24 +02:00
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { ExcelRow } from '../types';
|
|||
|
|
|
||
|
|
const SUPABASE_URL = 'https://hwithddwaapyhnfwcesj.supabase.co';
|
||
|
|
const SUPABASE_KEY = 'sb_publishable_fGXkh0bSrAOqSk2jWKAzSg_NJD9YPCv';
|
||
|
|
|
||
|
|
export async function saveRowToSupabase(articleNo: string, rowData: ExcelRow) {
|
||
|
|
try {
|
||
|
|
const response = await fetch(`${SUPABASE_URL}/rest/v1/products_sync?id=eq.${encodeURIComponent(articleNo)}`, {
|
||
|
|
method: 'PATCH',
|
||
|
|
headers: {
|
||
|
|
'apikey': SUPABASE_KEY,
|
||
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`,
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Prefer': 'resolution=merge-duplicates'
|
||
|
|
},
|
||
|
|
body: JSON.stringify({
|
||
|
|
id: articleNo,
|
||
|
|
data: rowData,
|
||
|
|
updated_at: new Date().toISOString()
|
||
|
|
})
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response.status === 204 || response.ok) {
|
||
|
|
// If PATCH didn't find the record, try UPSERT
|
||
|
|
const upsertResponse = await fetch(`${SUPABASE_URL}/rest/v1/products_sync`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: {
|
||
|
|
'apikey': SUPABASE_KEY,
|
||
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`,
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Prefer': 'resolution=merge-duplicates'
|
||
|
|
},
|
||
|
|
body: JSON.stringify({
|
||
|
|
id: articleNo,
|
||
|
|
data: rowData,
|
||
|
|
updated_at: new Date().toISOString()
|
||
|
|
})
|
||
|
|
});
|
||
|
|
return upsertResponse.ok;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error saving to Supabase:', error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getAllSyncedRows(): Promise<Record<string, ExcelRow>> {
|
||
|
|
try {
|
||
|
|
const response = await fetch(`${SUPABASE_URL}/rest/v1/products_sync`, {
|
||
|
|
headers: {
|
||
|
|
'apikey': SUPABASE_KEY,
|
||
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) return {};
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
const result: Record<string, ExcelRow> = {};
|
||
|
|
data.forEach((item: any) => {
|
||
|
|
result[item.id] = item.data;
|
||
|
|
});
|
||
|
|
return result;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Error fetching from Supabase:', error);
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
}
|