diff options
Diffstat (limited to 'src/cron.ts')
| -rw-r--r-- | src/cron.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/cron.ts b/src/cron.ts new file mode 100644 index 0000000..fb016a3 --- /dev/null +++ b/src/cron.ts @@ -0,0 +1,56 @@ +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<boolean> { + // 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<ProductInfo | null> { + switch (product.companyName) { + case "Coles": + return await fetchColesProduct(product.productId) + case "Woolies": + return await fetchWooliesProduct(product.productId) + default: + throw Error("Invalid store name") + } +}
\ No newline at end of file |
