diff options
| author | akiyamn | 2023-10-04 23:29:19 +1100 |
|---|---|---|
| committer | akiyamn | 2023-10-04 23:29:19 +1100 |
| commit | a2d1c179f88e056265321b426a51907a3941044a (patch) | |
| tree | 3804227ee072265a1cf1121cc59eb1fbdc74a3e2 /src/cron.ts | |
| parent | 4e87195739f2a5d9a05451b48773c8afdc680765 (diff) | |
| download | price-tracker-worker-master.tar.gz price-tracker-worker-master.zip | |
Kind of worksmaster
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 |
