diff --git a/index.ts b/index.ts index 7d48e50..909eee4 100644 --- a/index.ts +++ b/index.ts @@ -2,8 +2,7 @@ import PicnicClient from "picnic-api" import type { ApiConfig, Decorator, - PriceDecorator, - Delivery, + Delivery } from "picnic-api/lib/types/picnic-api.js" import type { ParsedArgs } from 'minimist' import { DateTime } from "luxon" @@ -12,10 +11,11 @@ import { jsonOut, csvOut, sum, runCommands, getXdgConfigHome } from "./util.js" import path from "node:path" import repl from "node:repl" -type QuantityDecorator = { - type: "QUANTITY" - quantity: number -} +function getDecorator( + thing: { decorators?: Decorator[] }, + name: "PROMO", + fallback?: string +): string function getDecorator( thing: { decorators?: Decorator[] }, name: "QUANTITY", @@ -39,6 +39,8 @@ function getDecorator( return l[0].quantity case "PRICE": return l[0].display_price + case "PROMO": + return l[0].text } } return fallback @@ -53,16 +55,7 @@ function deliveryToCSV(delv: Delivery) { "yyyy-MM-dd HH:mm" ) for (const orderline of order.items) { - const totalqty = sum( - orderline.items.map(x => getDecorator(x, "QUANTITY")) - ) - // todo orderline decorators - const real_price = getDecorator( - orderline as any, - "PRICE", - orderline.display_price - ) - const display_price = orderline.display_price + const real_price = getDecorator(orderline, "PRICE", orderline.display_price) const totalprice = sum(orderline.items.map(x => x.price)) const promo = real_price / totalprice for (const orderarticle of orderline.items) { @@ -134,6 +127,44 @@ const commands = (argv: ParsedArgs) => { } } }, + async article(id: string) { + jsonOut(await p.getArticle(id)) + }, + async "cart.list"() { + const cart = await p.getShoppingCart() + if (argv.json) { + jsonOut(cart) + } else { + const out: any[] = [] + cart.items.map(orderLine => { + const isGroup = orderLine.items.length > 1 + if (isGroup) { + out.push({ + name: ``, + price: getDecorator(orderLine, "PRICE", orderLine.price) + }) + } + for (const item of orderLine.items) { + out.push({ + id: item.id, + name: item.name, + quantity: getDecorator(item, "QUANTITY", 1), + price: item.price + }) + } + if (isGroup) out.push({name: ""}) + }) + console.table(out); + } + }, + async "cart.add"(product: string, count: string = "1") { + if (isNaN(+count)) throw new TypeError("count must be a number") + await p.addProductToShoppingCart(product, +count) + }, + async "cart.del"(product: string, count: string = "1") { + if (isNaN(+count)) throw new TypeError("count must be a number") + await p.removeProductFromShoppingCart(product, +count) + }, repl() { repl.start().context.p = p } diff --git a/package.json b/package.json index d97a30e..b9a69e1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "dependencies": { "luxon": "^3.2.1", "minimist": "^1.2.7", - "picnic-api": "^2.0.1" + "picnic-api": "yorickvp/picnic-api#getArticle" }, "bin": { "picnic": "./bin/picnic.js" diff --git a/yarn.lock b/yarn.lock index 626fd30..31a7604 100644 --- a/yarn.lock +++ b/yarn.lock @@ -598,10 +598,9 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== -picnic-api@^2.0.1: +picnic-api@yorickvp/picnic-api#getArticle: version "2.0.1" - resolved "https://registry.yarnpkg.com/picnic-api/-/picnic-api-2.0.1.tgz#0a67c3423ad20098a3c38a78f785af522e970ab5" - integrity sha512-6F1eumsFgUvkGWzQOOhDQwuiDIbbeedCdzTr4j6fkNrmud85TX4EZgEC7AQtRJPfBFA40G0bGi4eCUrROJEpjA== + resolved "https://codeload.github.com/yorickvp/picnic-api/tar.gz/338b2ba6ab8a2c54f5778cfbe8ab71eaffcd411b" dependencies: axios "^0.26.0" crypto-js "^4.1.1"