Fix save changes looping issue and authorize all Supabase API calls

This commit is contained in:
Christian Vidal Wolf
2026-04-10 12:03:12 +02:00
parent a92e136372
commit ee73b85100
3 changed files with 57 additions and 34 deletions
+13 -12
View File
@@ -8,14 +8,14 @@ export interface SyncedRow {
status?: 'pending' | 'synced';
}
export async function getAllSyncedRows(): Promise<Record<string, SyncedRow>> {
export async function getAllSyncedRows(token?: string): 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}`
'Authorization': `Bearer ${token || SUPABASE_KEY}`
}
}
);
@@ -33,7 +33,7 @@ export async function getAllSyncedRows(): Promise<Record<string, SyncedRow>> {
}
}
export async function saveRowToSupabase(articleNo: string, rowData: ExcelRow): Promise<boolean> {
export async function saveRowToSupabase(articleNo: string, rowData: ExcelRow, token?: string): Promise<boolean> {
try {
const response = await fetch(
`${SUPABASE_URL}/rest/v1/products`,
@@ -42,7 +42,7 @@ export async function saveRowToSupabase(articleNo: string, rowData: ExcelRow): P
headers: {
'Content-Type': 'application/json',
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`,
'Authorization': `Bearer ${token || SUPABASE_KEY}`,
'Prefer': 'resolution=merge-duplicates'
},
body: JSON.stringify({
@@ -76,7 +76,8 @@ export async function saveHistoryEntry(
articleName: string,
oldData: ExcelRow,
newData: ExcelRow,
changedBy: string
changedBy: string,
token?: string
): Promise<boolean> {
try {
const response = await fetch(
@@ -86,7 +87,7 @@ export async function saveHistoryEntry(
headers: {
'Content-Type': 'application/json',
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`,
'Authorization': `Bearer ${token || SUPABASE_KEY}`,
'Prefer': 'return=minimal'
},
body: JSON.stringify({
@@ -107,14 +108,14 @@ export async function saveHistoryEntry(
}
}
export async function getHistory(): Promise<HistoryEntry[]> {
export async function getHistory(token?: string): 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}`
'Authorization': `Bearer ${token || SUPABASE_KEY}`
}
}
);
@@ -127,7 +128,7 @@ export async function getHistory(): Promise<HistoryEntry[]> {
}
}
export async function deleteHistoryEntry(id: string): Promise<boolean> {
export async function deleteHistoryEntry(id: string, token?: string): Promise<boolean> {
try {
const response = await fetch(
`${SUPABASE_URL}/rest/v1/products_history?id=eq.${encodeURIComponent(id)}`,
@@ -135,7 +136,7 @@ export async function deleteHistoryEntry(id: string): Promise<boolean> {
method: 'DELETE',
headers: {
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`
'Authorization': `Bearer ${token || SUPABASE_KEY}`
}
}
);
@@ -147,7 +148,7 @@ export async function deleteHistoryEntry(id: string): Promise<boolean> {
}
}
export async function resetAllPendingRows(): Promise<boolean> {
export async function resetAllPendingRows(token?: string): Promise<boolean> {
try {
const response = await fetch(
`${SUPABASE_URL}/rest/v1/products?status=eq.pending`,
@@ -156,7 +157,7 @@ export async function resetAllPendingRows(): Promise<boolean> {
headers: {
'Content-Type': 'application/json',
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`,
'Authorization': `Bearer ${token || SUPABASE_KEY}`,
'Prefer': 'return=minimal'
},
body: JSON.stringify({ status: 'synced' })