|
1 | 1 | import { Action } from '@stacksjs/actions/runtime' |
2 | 2 | import { config } from '@stacksjs/config' |
3 | | -import { Category, Customer, Manufacturer, Product, TaxRate } from '@stacksjs/orm' |
4 | 3 | import { dashboardOperationalError } from '../dashboard-response' |
5 | | -import { |
6 | | - isCommercePosCustomerActive, |
7 | | - normalizeCommercePosCustomer, |
8 | | - normalizeCommercePosProduct, |
9 | | - selectCommercePosTaxRate, |
10 | | -} from './commerce-pos' |
11 | | -import { |
12 | | - normalizeCommerceProductRecord, |
13 | | - normalizeCommerceCurrency, |
14 | | - normalizeManufacturerOption, |
15 | | - normalizeProductOption, |
16 | | -} from './commerce-product-records' |
| 4 | +import { readPosSettings } from './commerce-pos-catalog' |
| 5 | +import { normalizeCommerceCurrency } from './commerce-product-records' |
17 | 6 |
|
| 7 | +/** |
| 8 | + * `GET /api/dashboard/commerce/pos` - the point of sale's settings: the |
| 9 | + * categories in use, the tax rate, the currency, the payment methods and the |
| 10 | + * catalogue summary, counted over every product (commerce-pos-catalog.ts, |
| 11 | + * stacksjs/stacks#3218). |
| 12 | + * |
| 13 | + * The catalogue itself is GET /commerce/pos/products, searched and paged, and |
| 14 | + * the checkout searches customers through GET /commerce/customer-choices. |
| 15 | + * This used to send the first 500 products, categories, manufacturers, |
| 16 | + * customers and tax rates. |
| 17 | + */ |
18 | 18 | export default new Action({ |
19 | 19 | name: 'CommercePosAction', |
20 | | - description: 'Returns persisted products, customers, and tax configuration for the native point of sale.', |
| 20 | + description: 'Returns the native point of sale settings and catalogue summary.', |
21 | 21 | method: 'GET', |
22 | 22 | apiResponse: true, |
23 | 23 |
|
24 | 24 | async handle() { |
25 | 25 | try { |
26 | | - const [products, categories, manufacturers, customers, taxRates] = await Promise.all([ |
27 | | - Product.orderBy('name', 'asc').limit(500).get(), |
28 | | - Category.orderBy('name', 'asc').limit(500).get(), |
29 | | - Manufacturer.orderBy('manufacturer', 'asc').limit(500).get(), |
30 | | - Customer.orderBy('name', 'asc').limit(500).get(), |
31 | | - TaxRate.orderBy('id', 'asc').limit(500).get(), |
32 | | - ]) |
33 | | - const categoryOptions = categories.map(normalizeProductOption) |
34 | | - const manufacturerOptions = manufacturers.map(normalizeManufacturerOption) |
35 | | - const categoryMap = new Map(categoryOptions.map(option => [option.id, option.label])) |
36 | | - const manufacturerMap = new Map(manufacturerOptions.map(option => [option.id, option.label])) |
37 | | - const emptyCounts = new Map<string, number>() |
38 | | - const records = products.map(product => normalizeCommercePosProduct(normalizeCommerceProductRecord( |
39 | | - product, |
40 | | - categoryMap, |
41 | | - manufacturerMap, |
42 | | - emptyCounts, |
43 | | - emptyCounts, |
44 | | - emptyCounts, |
45 | | - ))) |
46 | | - const sellable = records.filter(record => record.isAvailable && record.inventoryCount > 0) |
47 | | - const usedCategoryIds = new Set(records.map(record => record.categoryId).filter(Boolean)) |
48 | | - |
| 26 | + const settings = await readPosSettings() |
49 | 27 | return { |
50 | | - products: records, |
51 | | - categories: categoryOptions.filter(category => usedCategoryIds.has(category.id)), |
52 | | - customers: customers |
53 | | - .filter(isCommercePosCustomerActive) |
54 | | - .map(normalizeCommercePosCustomer), |
55 | | - taxRate: selectCommercePosTaxRate(taxRates), |
| 28 | + categories: settings.categories, |
| 29 | + taxRate: settings.taxRate, |
56 | 30 | defaultCurrency: normalizeCommerceCurrency(config.commerce?.currency), |
57 | 31 | paymentMethods: [{ id: 'cash', label: 'Cash' }], |
58 | | - summary: { |
59 | | - products: records.length, |
60 | | - sellable: sellable.length, |
61 | | - inventory: records.reduce((sum, record) => sum + record.inventoryCount, 0), |
62 | | - }, |
| 32 | + summary: settings.summary, |
63 | 33 | } |
64 | 34 | } |
65 | 35 | catch (error) { |
|
0 commit comments