2026-03-27 12:23:35 +01:00
|
|
|
const SUPABASE_URL = 'https://hwithddwaapyhnfwcesj.supabase.co';
|
|
|
|
|
const SUPABASE_KEY = 'sb_publishable_fGXkh0bSrAOqSk2jWKAzSg_NJD9YPCv';
|
|
|
|
|
|
2026-04-10 09:47:35 +02:00
|
|
|
export interface ExcelRow extends Array<any> {}
|
|
|
|
|
|
|
|
|
|
export interface SyncedRow {
|
|
|
|
|
data: ExcelRow;
|
|
|
|
|
status?: 'pending' | 'synced';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAllSyncedRows(): Promise<Record<string, SyncedRow>> {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${SUPABASE_URL}/rest/v1/products?select=product_id,data,status&order=updated_at.desc`,
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) return {};
|
|
|
|
|
const rows = await response.json();
|
|
|
|
|
const result: Record<string, SyncedRow> = {};
|
|
|
|
|
for (const row of rows) {
|
|
|
|
|
result[row.product_id] = { data: row.data, status: row.status };
|
2026-04-09 08:22:58 +02:00
|
|
|
}
|
2026-04-10 09:47:35 +02:00
|
|
|
return result;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching synced rows from Supabase:', error);
|
|
|
|
|
return {};
|
2026-04-09 08:22:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function saveRowToSupabase(articleNo: string, rowData: ExcelRow): Promise<boolean> {
|
2026-03-27 12:23:35 +01:00
|
|
|
try {
|
2026-04-10 09:47:35 +02:00
|
|
|
const response = await fetch(
|
|
|
|
|
`${SUPABASE_URL}/rest/v1/products?product_id=eq.${encodeURIComponent(articleNo)}`,
|
|
|
|
|
{
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`,
|
|
|
|
|
'Prefer': 'return=minimal'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
product_id: articleNo,
|
|
|
|
|
data: rowData,
|
|
|
|
|
status: 'synced',
|
|
|
|
|
updated_at: new Date().toISOString()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-03-27 12:23:35 +01:00
|
|
|
|
2026-04-10 09:47:35 +02:00
|
|
|
return response.ok;
|
2026-03-27 12:23:35 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error saving to Supabase:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 09:24:17 +02:00
|
|
|
export interface HistoryEntry {
|
2026-04-10 09:47:35 +02:00
|
|
|
id?: number;
|
2026-04-09 09:24:17 +02:00
|
|
|
product_id: string;
|
|
|
|
|
article_name: string;
|
|
|
|
|
old_data: ExcelRow;
|
|
|
|
|
new_data: ExcelRow;
|
|
|
|
|
changed_by: string;
|
2026-04-10 09:47:35 +02:00
|
|
|
changed_at: string;
|
2026-04-09 09:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function saveHistoryEntry(
|
2026-04-10 09:47:35 +02:00
|
|
|
productId: string,
|
2026-04-09 09:24:17 +02:00
|
|
|
articleName: string,
|
|
|
|
|
oldData: ExcelRow,
|
|
|
|
|
newData: ExcelRow,
|
2026-04-10 09:47:35 +02:00
|
|
|
changedBy: string
|
2026-04-09 09:24:17 +02:00
|
|
|
): Promise<boolean> {
|
|
|
|
|
try {
|
2026-04-10 09:47:35 +02:00
|
|
|
const response = await fetch(
|
|
|
|
|
`${SUPABASE_URL}/rest/v1/products_history`,
|
|
|
|
|
{
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`,
|
|
|
|
|
'Prefer': 'return=minimal'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
product_id: productId,
|
|
|
|
|
article_name: articleName,
|
|
|
|
|
old_data: oldData,
|
|
|
|
|
new_data: newData,
|
|
|
|
|
changed_by: changedBy,
|
|
|
|
|
changed_at: new Date().toISOString()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-04-09 09:24:17 +02:00
|
|
|
|
|
|
|
|
return response.ok;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error saving history to Supabase:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getHistory(): Promise<HistoryEntry[]> {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${SUPABASE_URL}/rest/v1/products_history?select=*&order=changed_at.desc&limit=100`,
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) return [];
|
|
|
|
|
return await response.json();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching history from Supabase:', error);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-10 09:47:35 +02:00
|
|
|
|
|
|
|
|
export async function deleteHistoryEntry(id: string): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${SUPABASE_URL}/rest/v1/products_history?id=eq.${encodeURIComponent(id)}`,
|
|
|
|
|
{
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
headers: {
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response.ok;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error deleting history from Supabase:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function resetAllPendingRows(): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${SUPABASE_URL}/rest/v1/products?status=eq.pending`,
|
|
|
|
|
{
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'apikey': SUPABASE_KEY,
|
|
|
|
|
'Authorization': `Bearer ${SUPABASE_KEY}`,
|
|
|
|
|
'Prefer': 'return=minimal'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ status: 'synced' })
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response.ok;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error resetting pending rows in Supabase:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|