import { fetchColesProduct, fetchWooliesProduct, ProductInfo } from "./gather" import { storePriceRecord } from "./store" export interface ProductRequest { productId: string, companyName: string } export async function cacheAllPrices(db: D1Database, products: ProductRequest[]): Promise { // return getAllPrices(products).then().map(storePriceRecord) return (await getAllPrices(products)) .map(x => {console.log("getting prices", x); return x}) .filter(x => x != null) .map(product => storePriceRecord(db, product as ProductInfo)) .every(async x => await x === true) // for (let p of products) { // console.log(p) // const prices = await getPrice(p) // console.warn(prices) // } // let out = [] // for (let price of prices) { // if (price !== null) { // const result = await storePriceRecord(db, price) // console.log(price, result) // out.push(result) // } // } // return out.every(x => !!x) // return true } async function getAllPrices(products: ProductRequest[]): Promise<(ProductInfo | null)[]> { return await Promise.all(products.map(getPrice)) // let out = [] // for (let p of products) { // out.push(await getPrice(p)) // } // return out // return [await getPrice(products[0])] } async function getPrice(product: ProductRequest): Promise { switch (product.companyName) { case "Coles": return await fetchColesProduct(product.productId) case "Woolies": return await fetchWooliesProduct(product.productId) default: throw Error("Invalid store name") } }