// Function to fetch prices from API
async function syncPrices() {
console.log("Syncing prices...");
const response = await fetch('https://api.example.com/prices');
if (!response.ok) throw new Error(Prices API responded with status: ${response.status});
const prices = await response.json();
// Insert prices into DB
const result = await db.insert(Price).values(
prices.map((price: any) => ({
id: price.id,
productId: price.productId,
amount: price.amount,
currency: price.currency,
// Map other fields
}))
);
return { count: result.length, type: 'prices' };
}
// Function to fetch stock levels from API
async function syncStock() {
console.log("Syncing stock levels...");
const response = await fetch('https://api.example.com/stock');
if (!response.ok) throw new Error(Stock API responded with status: ${response.status});
const stockItems = await response.json();
// Insert stock levels into DB
const result = await db.insert(Stock).values(
stockItems.map((item: any) => ({
id: item.id,
productId: item.productId,
sizeId: item.sizeId,
quantity: item.quantity,
// Map other fields
}))
);
return { count: result.length, type: 'stock' };
}
// Function to sync all data
async function syncAll() {
// Use Promise.all to run all sync operations in parallel
const results = await Promise.all([
syncProducts(),
syncCategories(),
syncPrices(),
syncStock()
]);
return results;
}
// Map of sync functions by type
const syncFunctions: Record<string, () => Promise<any>> = {
'products': syncProducts,
'categories': syncCategories,
'prices': syncPrices,
'stock': syncStock,
'all': syncAll
};