mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 14:35:23 +02:00
fix: merge history data into rows when products table is empty
This commit is contained in:
@@ -194,6 +194,35 @@ export async function getHistory(): Promise<HistoryEntry[]> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getHistoryDataForMerge(): Promise<Record<string, ExcelRow>> {
|
||||
try {
|
||||
const response = await safeFetch(
|
||||
`${SUPABASE_URL}/rest/v1/products_history?select=product_id,new_data&order=changed_at.desc`,
|
||||
{ cache: 'no-store' }
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('[getHistoryDataForMerge] Error:', response.status);
|
||||
return {};
|
||||
}
|
||||
const entries: Array<{ product_id: string; new_data: ExcelRow }> = await response.json();
|
||||
|
||||
const result: Record<string, ExcelRow> = {};
|
||||
const seen = new Set<string>();
|
||||
for (const entry of entries) {
|
||||
if (seen.has(entry.product_id)) continue;
|
||||
seen.add(entry.product_id);
|
||||
if (entry.new_data && entry.new_data.length > 0) {
|
||||
result[entry.product_id] = entry.new_data;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
console.error('[getHistoryDataForMerge] Exception:', error.message);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteHistoryEntry(id: string): Promise<boolean> {
|
||||
try {
|
||||
const response = await safeFetch(
|
||||
|
||||
Reference in New Issue
Block a user