2026-03-27 12:23:35 +01:00
|
|
|
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}`,
|
2026-04-08 19:30:41 +02:00
|
|
|
'Content-Type': 'application/json'
|
2026-03-27 12:23:35 +01:00
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
id: articleNo,
|
|
|
|
|
data: rowData,
|
2026-04-08 19:30:41 +02:00
|
|
|
status_check: 'pending',
|
2026-03-27 12:23:35 +01:00
|
|
|
updated_at: new Date().toISOString()
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-08 19:30:41 +02:00
|
|
|
if (!response.ok) {
|
2026-03-27 12:23:35 +01:00
|
|
|
// 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,
|
2026-04-08 19:30:41 +02:00
|
|
|
status_check: 'pending',
|
2026-03-27 12:23:35 +01:00
|
|
|
updated_at: new Date().toISOString()
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
return upsertResponse.ok;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error saving to Supabase:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 19:30:41 +02:00
|
|
|
export async function getAllSyncedRows(): Promise<Record<string, { data: ExcelRow, status: string }>> {
|
2026-03-27 12:23:35 +01:00
|
|
|
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();
|
2026-04-08 19:30:41 +02:00
|
|
|
const result: Record<string, { data: ExcelRow, status: string }> = {};
|
2026-03-27 12:23:35 +01:00
|
|
|
data.forEach((item: any) => {
|
2026-04-08 19:30:41 +02:00
|
|
|
result[item.id] = {
|
|
|
|
|
data: item.data,
|
|
|
|
|
status: item.status_check || 'original'
|
|
|
|
|
};
|
2026-03-27 12:23:35 +01:00
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching from Supabase:', error);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-08 19:30:41 +02:00
|
|
|
|
|
|
|
|
export async function resetAllPendingRows() {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`${SUPABASE_URL}/rest/v1/products_sync?status_check=eq.pending`, {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: {
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
status_check: 'original',
|
|
|
|
|
updated_at: new Date().toISOString()
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
return response.ok;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error resetting statuses in Supabase:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|