From 680dc4448659d328b6eceeb1c831248cb7861900 Mon Sep 17 00:00:00 2001 From: imperosol Date: Thu, 5 Mar 2026 19:08:16 +0100 Subject: [PATCH] adapt products export to new price system --- .../bundled/counter/product-list-index.ts | 28 ++++++++++++------- counter/templates/counter/product_list.jinja | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/counter/static/bundled/counter/product-list-index.ts b/counter/static/bundled/counter/product-list-index.ts index 5c5abec6..30d26b9a 100644 --- a/counter/static/bundled/counter/product-list-index.ts +++ b/counter/static/bundled/counter/product-list-index.ts @@ -1,13 +1,9 @@ import { showSaveFilePicker } from "native-file-system-adapter"; import type TomSelect from "tom-select"; -import { paginated } from "#core:utils/api.ts"; -import { csv } from "#core:utils/csv.ts"; -import { - getCurrentUrlParams, - History, - updateQueryString, -} from "#core:utils/history.ts"; -import type { NestedKeyOf } from "#core:utils/types.ts"; +import { paginated } from "#core:utils/api"; +import { csv } from "#core:utils/csv"; +import { getCurrentUrlParams, History, updateQueryString } from "#core:utils/history"; +import type { NestedKeyOf } from "#core:utils/types"; import { type ProductSchema, type ProductSearchProductsDetailedData, @@ -20,6 +16,9 @@ type GroupedProducts = Record; const defaultPageSize = 100; const defaultPage = 1; +// biome-ignore lint/style/useNamingConvention: api is snake case +type ProductWithPriceSchema = ProductSchema & { selling_price: string }; + /** * Keys of the properties to include in the CSV. */ @@ -34,7 +33,7 @@ const csvColumns = [ "purchase_price", "selling_price", "archived", -] as NestedKeyOf[]; +] as NestedKeyOf[]; /** * Title of the csv columns. @@ -175,7 +174,16 @@ document.addEventListener("alpine:init", () => { this.nbPages > 1 ? await paginated(productSearchProductsDetailed, this.getQueryParams()) : Object.values(this.products).flat(); - const content = csv.stringify(products, { + // CSV cannot represent nested data + // so we create a row for each price of each product. + const productsWithPrice: ProductWithPriceSchema[] = products.flatMap( + (product: ProductSchema) => + product.prices.map((price) => + // biome-ignore lint/style/useNamingConvention: API is snake_case + Object.assign(product, { selling_price: price.amount }), + ), + ); + const content = csv.stringify(productsWithPrice, { columns: csvColumns, titleRow: csvColumnTitles, }); diff --git a/counter/templates/counter/product_list.jinja b/counter/templates/counter/product_list.jinja index 617aeaa5..ca2e4c4d 100644 --- a/counter/templates/counter/product_list.jinja +++ b/counter/templates/counter/product_list.jinja @@ -108,7 +108,7 @@ -

+