@composable-os/pack-shop 0.0.0-stage → 0.1.0-beta.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +109 -0
- package/dist/dashboards.d.ts +5 -0
- package/dist/dashboards.js +97 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +576 -0
- package/dist/tools.d.ts +19 -0
- package/dist/tools.js +294 -0
- package/package.json +31 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
> This licence covers the repository except the directories listed in NOTICE. In particular, the
|
|
4
|
+
> assistant packages `packages/agent-*` (agent-core, agent-a2ui, agent-voice, agent-plugins) are
|
|
5
|
+
> licensed under the MIT License; see the LICENSE file in each of those directories (ADR 0350).
|
|
6
|
+
|
|
7
|
+
## Abbreviation
|
|
8
|
+
|
|
9
|
+
FSL-1.1-ALv2
|
|
10
|
+
|
|
11
|
+
## Notice
|
|
12
|
+
|
|
13
|
+
Copyright 2026 Tim O'Connell
|
|
14
|
+
|
|
15
|
+
## Terms and Conditions
|
|
16
|
+
|
|
17
|
+
### Licensor ("We")
|
|
18
|
+
|
|
19
|
+
The party offering the Software under these Terms and Conditions.
|
|
20
|
+
|
|
21
|
+
### The Software
|
|
22
|
+
|
|
23
|
+
The "Software" is each version of the software that we make available under
|
|
24
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
25
|
+
Conditions with the Software.
|
|
26
|
+
|
|
27
|
+
### License Grant
|
|
28
|
+
|
|
29
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
30
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
31
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
32
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
33
|
+
|
|
34
|
+
### Permitted Purpose
|
|
35
|
+
|
|
36
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
37
|
+
means making the Software available to others in a commercial product or
|
|
38
|
+
service that:
|
|
39
|
+
|
|
40
|
+
1. substitutes for the Software;
|
|
41
|
+
|
|
42
|
+
2. substitutes for any other product or service we offer using the Software
|
|
43
|
+
that exists as of the date we make the Software available; or
|
|
44
|
+
|
|
45
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
46
|
+
|
|
47
|
+
Permitted Purposes specifically include using the Software:
|
|
48
|
+
|
|
49
|
+
1. for your internal use and access;
|
|
50
|
+
|
|
51
|
+
2. for non-commercial education;
|
|
52
|
+
|
|
53
|
+
3. for non-commercial research; and
|
|
54
|
+
|
|
55
|
+
4. in connection with professional services that you provide to a licensee
|
|
56
|
+
using the Software in accordance with these Terms and Conditions.
|
|
57
|
+
|
|
58
|
+
### Patents
|
|
59
|
+
|
|
60
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
61
|
+
patents, the license grant above includes a license under our patents. If you
|
|
62
|
+
make a claim against any party that the Software infringes or contributes to
|
|
63
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
64
|
+
immediately.
|
|
65
|
+
|
|
66
|
+
### Redistribution
|
|
67
|
+
|
|
68
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
69
|
+
the Software.
|
|
70
|
+
|
|
71
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
72
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
73
|
+
remove any copyright notices provided in or with the Software.
|
|
74
|
+
|
|
75
|
+
### Disclaimer
|
|
76
|
+
|
|
77
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
78
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
79
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
80
|
+
|
|
81
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
82
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
83
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
84
|
+
|
|
85
|
+
### Trademarks
|
|
86
|
+
|
|
87
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
88
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
89
|
+
trademarks, trade names, service marks or product names.
|
|
90
|
+
|
|
91
|
+
## Grant of Future License
|
|
92
|
+
|
|
93
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
94
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
95
|
+
the date we make the Software available. On or after that date, you may use the
|
|
96
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
97
|
+
will apply:
|
|
98
|
+
|
|
99
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
100
|
+
this file except in compliance with the License.
|
|
101
|
+
|
|
102
|
+
You may obtain a copy of the License at
|
|
103
|
+
|
|
104
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
105
|
+
|
|
106
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
107
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
108
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
109
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const LOW = "v.state = 'active' AND v.reorder_at IS NOT NULL AND v._stk_on_hand <= v.reorder_at";
|
|
2
|
+
const SOLD = `FROM rec_order_line l JOIN rec_order o ON o.id = l.order_id
|
|
3
|
+
JOIN rec_x_shop_variant v ON v.id = l.record_id
|
|
4
|
+
LEFT JOIN rec_x_shop_item i ON i.id = v.item
|
|
5
|
+
WHERE l.object_type = 'shop.variant' AND o.state = 'paid'
|
|
6
|
+
AND o.paid_at >= :paid_from AND o.paid_at < :paid_to`;
|
|
7
|
+
export const shopOverview = {
|
|
8
|
+
title: "Shop",
|
|
9
|
+
description: "What is low and who supplies it, what is waiting to be packed or collected, and what sold.",
|
|
10
|
+
filters: [{ type: "dateRange", id: "paid", label: "Paid", default: "this_month" }],
|
|
11
|
+
widgets: [
|
|
12
|
+
{
|
|
13
|
+
id: "low",
|
|
14
|
+
type: "kpi",
|
|
15
|
+
title: "Low on stock",
|
|
16
|
+
description: "Active variants with on hand at or below their reorder level.",
|
|
17
|
+
sql: `SELECT count(*) AS value FROM rec_x_shop_variant v WHERE ${LOW}`,
|
|
18
|
+
mapping: { value: "value" },
|
|
19
|
+
options: { compare: false },
|
|
20
|
+
layout: { w: 3, h: 1 },
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "to-pack",
|
|
24
|
+
type: "kpi",
|
|
25
|
+
title: "To pack",
|
|
26
|
+
description: "Shipments of paid orders not yet packed.",
|
|
27
|
+
sql: "SELECT count(*) AS value FROM rec_x_shop_shipment s WHERE s.state = 'to_pack'",
|
|
28
|
+
mapping: { value: "value" },
|
|
29
|
+
options: { compare: false },
|
|
30
|
+
clickThrough: {
|
|
31
|
+
object: "shop.shipment",
|
|
32
|
+
filter: { field: "state", op: "eq", value: "to_pack" },
|
|
33
|
+
},
|
|
34
|
+
layout: { w: 3, h: 1 },
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
id: "ready",
|
|
38
|
+
type: "kpi",
|
|
39
|
+
title: "Ready to collect",
|
|
40
|
+
description: "Shipments waiting for their buyer to collect them.",
|
|
41
|
+
sql: "SELECT count(*) AS value FROM rec_x_shop_shipment s WHERE s.state = 'ready'",
|
|
42
|
+
mapping: { value: "value" },
|
|
43
|
+
options: { compare: false },
|
|
44
|
+
clickThrough: {
|
|
45
|
+
object: "shop.shipment",
|
|
46
|
+
filter: { field: "state", op: "eq", value: "ready" },
|
|
47
|
+
},
|
|
48
|
+
layout: { w: 3, h: 1 },
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "sales",
|
|
52
|
+
type: "kpi",
|
|
53
|
+
title: "Shop sales",
|
|
54
|
+
description: "Line totals of shop goods on orders paid in the period (postage left out).",
|
|
55
|
+
sql: `SELECT coalesce(sum(l.line_total), 0) / 100.0 AS value ${SOLD}`,
|
|
56
|
+
mapping: { value: "value" },
|
|
57
|
+
options: { compare: true, format: "currency" },
|
|
58
|
+
layout: { w: 3, h: 1 },
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: "low-by-supplier",
|
|
62
|
+
type: "table",
|
|
63
|
+
title: "Low stock by supplier",
|
|
64
|
+
description: "Each active variant at or below its reorder level, its usual supplier and how many to " +
|
|
65
|
+
"order: ready to become a purchase order.",
|
|
66
|
+
sql: `SELECT v.id AS id, coalesce(o.name, 'No supplier') AS supplier, v.name AS variant,
|
|
67
|
+
v._stk_on_hand AS on_hand, v._stk_held AS held, v.reorder_at AS reorder_at,
|
|
68
|
+
v.reorder_qty AS reorder_qty
|
|
69
|
+
FROM rec_x_shop_variant v LEFT JOIN rec_organisation o ON o.id = v.supplier
|
|
70
|
+
WHERE ${LOW}
|
|
71
|
+
ORDER BY supplier, v._stk_on_hand LIMIT 100`,
|
|
72
|
+
mapping: {
|
|
73
|
+
columns: ["supplier", "variant", "on_hand", "held", "reorder_at", "reorder_qty"],
|
|
74
|
+
},
|
|
75
|
+
clickThrough: {
|
|
76
|
+
object: "shop.variant",
|
|
77
|
+
filter: { field: "id", op: "eq", value: { $row: "id" } },
|
|
78
|
+
},
|
|
79
|
+
layout: { w: 12, h: 2 },
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
id: "best-sellers",
|
|
83
|
+
type: "bar",
|
|
84
|
+
title: "Sold by item",
|
|
85
|
+
description: "Units of each item sold on orders paid in the period.",
|
|
86
|
+
sql: `SELECT i.id AS item_id, coalesce(i.name, v.name) AS item, sum(l.quantity) AS units ${SOLD}
|
|
87
|
+
GROUP BY i.id, item ORDER BY units DESC LIMIT 20`,
|
|
88
|
+
mapping: { x: "item", y: ["units"] },
|
|
89
|
+
clickThrough: {
|
|
90
|
+
object: "shop.variant",
|
|
91
|
+
filter: { field: "item", op: "eq", value: { $row: "item_id" } },
|
|
92
|
+
},
|
|
93
|
+
layout: { w: 12, h: 2 },
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
export const shopDashboards = { shopOverview };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
// Shop (stage 37, step 5): one list of the things a business sells (drinks, clothing,
|
|
2
|
+
// supplements, spare parts), each with its versions, prices and pictures, marked for where it is
|
|
3
|
+
// sold; how many of each there are; the ways an online order reaches its buyer; and the shipments
|
|
4
|
+
// staff pack and send or hand over. The till (drafted separately) and, later, Jobs sell and use
|
|
5
|
+
// the same variants: one catalogue and one count.
|
|
6
|
+
//
|
|
7
|
+
// Vocabulary (the spec): an **item** is a thing in the shop ("Club hoodie"); a **variant** is one
|
|
8
|
+
// version of it that is bought and counted ("Club hoodie, large, black"). An item with one
|
|
9
|
+
// version still has one variant.
|
|
10
|
+
//
|
|
11
|
+
// Everything that is a rule is the host's, declared here as behaviours (ADR 0404):
|
|
12
|
+
// - a variant is `sellable` goods (ADRs 0435, 0682): its price is read by the host, it is sold
|
|
13
|
+
// where its item says (`goods.soldIn: "item.whereSold"`), and a line not sold in the order's
|
|
14
|
+
// channel (online for post and collect, in person for "take it now") is refused on every path;
|
|
15
|
+
// - a variant has `stock` (ADR 0680): on hand is kept by the host from a ledger nobody edits,
|
|
16
|
+
// held while an order is paid for, sold in the paid batch, never sold past what is there; a
|
|
17
|
+
// refund does not restock. Staff record opening stock and corrections with the host's
|
|
18
|
+
// `stock.opening` and `stock.adjust`; receipts, counts and write-offs (step 6) move it through
|
|
19
|
+
// `stockMoves`. Falling to the reorder level tells the holders of `shop:stock` once (ADR 0681);
|
|
20
|
+
// - a delivery option is `sellable.delivery` (ADRs 0682, 0685): post (an address in its
|
|
21
|
+
// countries), collect, or take it now; the host prices it, free over an amount, and a paid post
|
|
22
|
+
// or collect line grants a shipment; take it now grants nothing;
|
|
23
|
+
// - the public reads published items, and the active variants of published items, and the
|
|
24
|
+
// delivery options (`public`, ADRs 0438, 0442, 0689); a buyer reads their own shipments
|
|
25
|
+
// (`customer`, ADR 0402). Cost price, supplier, SKU, barcode, reorder levels and the counts
|
|
26
|
+
// stay staff-only: outsiders are told stock only as `stock.get` and `order.quote` allow;
|
|
27
|
+
// - prices, postage, the countries an option posts to and whether it is on are a person's to
|
|
28
|
+
// change: an AI's or an automation's write of them waits for a person (`personApproves`).
|
|
29
|
+
// The tools in tools.ts compose the host's operations and hold no rule of their own.
|
|
30
|
+
import { defineExtension } from "@composable-os/sdk";
|
|
31
|
+
import { shopDashboards } from "./dashboards.js";
|
|
32
|
+
import { markCollected, markPacked, markReady, markSent, shipmentsToSend, stockLevels, } from "./tools.js";
|
|
33
|
+
const WHERE_SOLD = [
|
|
34
|
+
{ value: "both", label: "Online and in person" },
|
|
35
|
+
{ value: "online", label: "Online only" },
|
|
36
|
+
{ value: "in_person", label: "In person only" },
|
|
37
|
+
];
|
|
38
|
+
const DELIVERY_KINDS = [
|
|
39
|
+
{ value: "post", label: "Post" },
|
|
40
|
+
{ value: "collect", label: "Collect" },
|
|
41
|
+
{ value: "here", label: "Take it now" },
|
|
42
|
+
];
|
|
43
|
+
/**
|
|
44
|
+
* Tax at checkout (ADR 0479), only once a human admin turns it on: the provider's general code
|
|
45
|
+
* for physical goods, and its code for shipping. Whether goods and postage are taxed alike is
|
|
46
|
+
* the business's accountant's call (decision 8); an admin may change either in Settings → Orders.
|
|
47
|
+
*/
|
|
48
|
+
const GOODS_TAX = { code: "txcd_99999999" };
|
|
49
|
+
const POSTAGE_TAX = { code: "txcd_92010001" };
|
|
50
|
+
export default defineExtension({
|
|
51
|
+
id: "shop",
|
|
52
|
+
version: "0.1.0",
|
|
53
|
+
contractVersion: 1,
|
|
54
|
+
label: "Shop",
|
|
55
|
+
description: "One list of the things the business sells, in their versions, sold online, in person or " +
|
|
56
|
+
"both, with stock the host keeps, ways to get an order to its buyer, and the shipments staff " +
|
|
57
|
+
"pack and send or hand over.",
|
|
58
|
+
permissions: {
|
|
59
|
+
// person.get (read person): the buyer's name on a shipment, for the staff list of what to
|
|
60
|
+
// send. query.sql: the stock levels and the dashboard's lists over the rec_* views.
|
|
61
|
+
read: ["person"],
|
|
62
|
+
operations: ["query.sql"],
|
|
63
|
+
modules: ["ops"],
|
|
64
|
+
},
|
|
65
|
+
// A supplier is an Organisation with the Shop's fields (no second address book). Purchase
|
|
66
|
+
// orders to them are step 6's.
|
|
67
|
+
fields: {
|
|
68
|
+
organisation: {
|
|
69
|
+
supplier: {
|
|
70
|
+
type: "boolean",
|
|
71
|
+
label: "Supplier",
|
|
72
|
+
description: "We buy stock from this organisation.",
|
|
73
|
+
},
|
|
74
|
+
supplierAccount: { type: "text", label: "Our account number with them" },
|
|
75
|
+
supplierEmail: {
|
|
76
|
+
type: "email",
|
|
77
|
+
label: "Email for orders",
|
|
78
|
+
description: "Where purchase orders to this supplier are sent.",
|
|
79
|
+
},
|
|
80
|
+
supplierLeadDays: {
|
|
81
|
+
type: "number",
|
|
82
|
+
label: "Lead time (days)",
|
|
83
|
+
description: "How many days an order from this supplier usually takes to arrive.",
|
|
84
|
+
min: 0,
|
|
85
|
+
step: 1,
|
|
86
|
+
},
|
|
87
|
+
supplierNotes: { type: "longText", label: "Supplier notes" },
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
objects: {
|
|
91
|
+
item: {
|
|
92
|
+
label: "Item",
|
|
93
|
+
plural: "Items",
|
|
94
|
+
icon: "shopping-bag",
|
|
95
|
+
titleField: "name",
|
|
96
|
+
description: "A thing in the shop (a club hoodie, a protein bar), with its pictures and where it is " +
|
|
97
|
+
"sold. What is bought and counted is its variants: an item with one version has one.",
|
|
98
|
+
navigation: "main",
|
|
99
|
+
navigationOrder: 10,
|
|
100
|
+
fields: {
|
|
101
|
+
name: { type: "text", label: "Name", required: true },
|
|
102
|
+
slug: { type: "slug", label: "Address" },
|
|
103
|
+
description: { type: "richText", label: "Description" },
|
|
104
|
+
image: { type: "image", label: "Picture" },
|
|
105
|
+
image2: { type: "image", label: "Second picture" },
|
|
106
|
+
image3: { type: "image", label: "Third picture" },
|
|
107
|
+
category: {
|
|
108
|
+
type: "text",
|
|
109
|
+
label: "Category",
|
|
110
|
+
description: "Drinks, clothing, supplements: how the shop and the stock list group it.",
|
|
111
|
+
},
|
|
112
|
+
// Where it is sold (decision 3): the host refuses a line not sold in the order's channel.
|
|
113
|
+
// Not content: a change takes effect at once, published or not.
|
|
114
|
+
whereSold: {
|
|
115
|
+
type: "select",
|
|
116
|
+
label: "Where sold",
|
|
117
|
+
required: true,
|
|
118
|
+
default: "both",
|
|
119
|
+
options: WHERE_SOLD,
|
|
120
|
+
// It decides where the item may be sold, so an AI's change of it waits for a person.
|
|
121
|
+
personApproves: true,
|
|
122
|
+
track: "timeline",
|
|
123
|
+
description: "Online (posted or collected), in person (bought at the gym with Take it now), or " +
|
|
124
|
+
"both. A fridge drink is in person only.",
|
|
125
|
+
},
|
|
126
|
+
option1Name: {
|
|
127
|
+
type: "text",
|
|
128
|
+
label: "First option",
|
|
129
|
+
description: "What the variants' first option is called, e.g. Size.",
|
|
130
|
+
},
|
|
131
|
+
option2Name: { type: "text", label: "Second option", description: "e.g. Colour." },
|
|
132
|
+
option3Name: { type: "text", label: "Third option", description: "e.g. Flavour." },
|
|
133
|
+
weight: {
|
|
134
|
+
type: "number",
|
|
135
|
+
label: "Weight (grams)",
|
|
136
|
+
description: "Each variant's weight unless it has its own. Not used for postage yet.",
|
|
137
|
+
min: 0,
|
|
138
|
+
step: 1,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
// Drafts and published versions (ADRs 0442, 0446): only a published item is public, and
|
|
142
|
+
// publishing is a person's (an AI's request waits for one, ADR 0480).
|
|
143
|
+
publishable: {
|
|
144
|
+
content: [
|
|
145
|
+
"name",
|
|
146
|
+
"description",
|
|
147
|
+
"image",
|
|
148
|
+
"image2",
|
|
149
|
+
"image3",
|
|
150
|
+
"category",
|
|
151
|
+
"option1Name",
|
|
152
|
+
"option2Name",
|
|
153
|
+
"option3Name",
|
|
154
|
+
],
|
|
155
|
+
slug: "slug",
|
|
156
|
+
},
|
|
157
|
+
public: {
|
|
158
|
+
read: [
|
|
159
|
+
"name",
|
|
160
|
+
"slug",
|
|
161
|
+
"description",
|
|
162
|
+
"image",
|
|
163
|
+
"image2",
|
|
164
|
+
"image3",
|
|
165
|
+
"category",
|
|
166
|
+
"whereSold",
|
|
167
|
+
"option1Name",
|
|
168
|
+
"option2Name",
|
|
169
|
+
"option3Name",
|
|
170
|
+
"publishedAt",
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
indexes: [["category"], ["whereSold"]],
|
|
174
|
+
},
|
|
175
|
+
variant: {
|
|
176
|
+
label: "Variant",
|
|
177
|
+
plural: "Variants",
|
|
178
|
+
icon: "tag",
|
|
179
|
+
titleField: "name",
|
|
180
|
+
description: "One version of an item that is bought and counted (large, black), with its price, " +
|
|
181
|
+
"stock, reorder level and usual supplier.",
|
|
182
|
+
navigation: "main",
|
|
183
|
+
navigationOrder: 11,
|
|
184
|
+
fields: {
|
|
185
|
+
// Pointing a variant at another item changes where it is sold and whether the public
|
|
186
|
+
// sees it (its item's published state), so an AI's change of it waits for a person.
|
|
187
|
+
item: {
|
|
188
|
+
type: "relation",
|
|
189
|
+
label: "Item",
|
|
190
|
+
to: "shop.item",
|
|
191
|
+
required: true,
|
|
192
|
+
personApproves: true,
|
|
193
|
+
},
|
|
194
|
+
// The order line's title is the record's: the whole name, so a receipt reads well.
|
|
195
|
+
name: {
|
|
196
|
+
type: "text",
|
|
197
|
+
label: "Name",
|
|
198
|
+
required: true,
|
|
199
|
+
description: "As the buyer's order shows it, e.g. Club hoodie, Large / Black.",
|
|
200
|
+
},
|
|
201
|
+
option1: { type: "text", label: "First option value", description: "e.g. Large." },
|
|
202
|
+
option2: { type: "text", label: "Second option value", description: "e.g. Black." },
|
|
203
|
+
option3: { type: "text", label: "Third option value" },
|
|
204
|
+
sku: { type: "text", label: "SKU" },
|
|
205
|
+
barcode: { type: "text", label: "Barcode" },
|
|
206
|
+
price: {
|
|
207
|
+
type: "currency",
|
|
208
|
+
label: "Price",
|
|
209
|
+
required: true,
|
|
210
|
+
personApproves: true,
|
|
211
|
+
description: "Including tax. An AI's change of it waits for a person.",
|
|
212
|
+
},
|
|
213
|
+
costPrice: {
|
|
214
|
+
type: "currency",
|
|
215
|
+
label: "Cost price",
|
|
216
|
+
readOnly: true,
|
|
217
|
+
description: "What one cost when it was last received from a supplier.",
|
|
218
|
+
},
|
|
219
|
+
maxPerOrder: {
|
|
220
|
+
type: "number",
|
|
221
|
+
label: "Most per order",
|
|
222
|
+
description: "The most one order may hold. Empty: 10.",
|
|
223
|
+
min: 1,
|
|
224
|
+
step: 1,
|
|
225
|
+
},
|
|
226
|
+
weight: {
|
|
227
|
+
type: "number",
|
|
228
|
+
label: "Weight (grams)",
|
|
229
|
+
description: "Overrides the item's weight.",
|
|
230
|
+
min: 0,
|
|
231
|
+
step: 1,
|
|
232
|
+
},
|
|
233
|
+
reorderAt: {
|
|
234
|
+
type: "number",
|
|
235
|
+
label: "Reorder level",
|
|
236
|
+
description: "At or below this many on hand, the stock is low and staff are told once.",
|
|
237
|
+
min: 0,
|
|
238
|
+
step: 1,
|
|
239
|
+
},
|
|
240
|
+
reorderQty: {
|
|
241
|
+
type: "number",
|
|
242
|
+
label: "Reorder quantity",
|
|
243
|
+
description: "How many to order when it is low.",
|
|
244
|
+
min: 1,
|
|
245
|
+
step: 1,
|
|
246
|
+
},
|
|
247
|
+
supplier: {
|
|
248
|
+
type: "relation",
|
|
249
|
+
label: "Usual supplier",
|
|
250
|
+
to: "organisation",
|
|
251
|
+
},
|
|
252
|
+
state: {
|
|
253
|
+
type: "select",
|
|
254
|
+
label: "State",
|
|
255
|
+
track: "timeline",
|
|
256
|
+
options: [
|
|
257
|
+
{ value: "active", label: "Active" },
|
|
258
|
+
{ value: "retired", label: "Retired" },
|
|
259
|
+
],
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
states: {
|
|
263
|
+
field: "state",
|
|
264
|
+
initial: "active",
|
|
265
|
+
moves: [
|
|
266
|
+
{ from: "active", to: "retired", by: ["staff"] },
|
|
267
|
+
{ from: "retired", to: "active", by: ["staff"] },
|
|
268
|
+
],
|
|
269
|
+
},
|
|
270
|
+
sellable: {
|
|
271
|
+
price: "price",
|
|
272
|
+
maxPerOrder: "maxPerOrder",
|
|
273
|
+
// Sold where its item is (decision 3): the host reads the item's mark on every order.
|
|
274
|
+
goods: { soldIn: "item.whereSold" },
|
|
275
|
+
tax: GOODS_TAX,
|
|
276
|
+
finance: { category: { key: "shop", label: "Shop" }, earned: "at_sale" },
|
|
277
|
+
},
|
|
278
|
+
// Outsiders are told "In stock" or "Sold out" (the default, `availability`), never a count.
|
|
279
|
+
stock: { reorderAt: "reorderAt", reorderQty: "reorderQty", notify: "shop:stock" },
|
|
280
|
+
// Where agreement may be needed (stage 44, ADR 0641): nothing is attached by default.
|
|
281
|
+
agreements: { point: "buy", label: "Buying from the shop", inlineStep: true },
|
|
282
|
+
// An active variant of a published item (ADR 0689): what a shop page shows and a buyer may
|
|
283
|
+
// put on an order. A retired one, or one of a draft item, is not found.
|
|
284
|
+
public: {
|
|
285
|
+
read: ["item", "name", "option1", "option2", "option3", "price", "maxPerOrder"],
|
|
286
|
+
when: [{ field: "state", in: ["active"] }, { via: "item" }],
|
|
287
|
+
},
|
|
288
|
+
indexes: [["item"], ["sku"], ["barcode"], ["supplier"], ["state"]],
|
|
289
|
+
},
|
|
290
|
+
delivery: {
|
|
291
|
+
label: "Delivery option",
|
|
292
|
+
plural: "Delivery options",
|
|
293
|
+
icon: "truck",
|
|
294
|
+
titleField: "name",
|
|
295
|
+
description: "A way an order's goods reach the buyer: posted, collected, or taken now at the gym. Its " +
|
|
296
|
+
"price is the host's postage; a paid post or collect order gets a shipment to pack.",
|
|
297
|
+
navigation: "settings",
|
|
298
|
+
fields: {
|
|
299
|
+
name: { type: "text", label: "Name", required: true },
|
|
300
|
+
kind: {
|
|
301
|
+
type: "select",
|
|
302
|
+
label: "Kind",
|
|
303
|
+
required: true,
|
|
304
|
+
options: DELIVERY_KINDS,
|
|
305
|
+
// Post, collect or take it now decides the order's channel and whether an address is
|
|
306
|
+
// asked for: an AI's change of it waits for a person, as the price does.
|
|
307
|
+
personApproves: true,
|
|
308
|
+
description: "Post (an address in its countries), collect (no address), or take it now (bought " +
|
|
309
|
+
"in person: nothing to pack).",
|
|
310
|
+
},
|
|
311
|
+
price: {
|
|
312
|
+
type: "currency",
|
|
313
|
+
label: "Price",
|
|
314
|
+
personApproves: true,
|
|
315
|
+
description: "Postage, including tax. 0 is free. A post option without one cannot be chosen.",
|
|
316
|
+
},
|
|
317
|
+
freeOver: {
|
|
318
|
+
type: "currency",
|
|
319
|
+
label: "Free over",
|
|
320
|
+
personApproves: true,
|
|
321
|
+
description: "No postage when the order's goods come to at least this. Empty: never free.",
|
|
322
|
+
},
|
|
323
|
+
countries: {
|
|
324
|
+
type: "text",
|
|
325
|
+
label: "Countries",
|
|
326
|
+
personApproves: true,
|
|
327
|
+
defaultSetting: "country",
|
|
328
|
+
description: "Post only: two-letter codes separated by spaces (AU NZ), or * for everywhere no " +
|
|
329
|
+
"other post option names. Empty: it posts nowhere.",
|
|
330
|
+
},
|
|
331
|
+
collectAt: {
|
|
332
|
+
type: "text",
|
|
333
|
+
label: "Where to collect",
|
|
334
|
+
description: "Collect only: where the buyer picks it up, e.g. the front desk.",
|
|
335
|
+
},
|
|
336
|
+
active: {
|
|
337
|
+
type: "boolean",
|
|
338
|
+
label: "Active",
|
|
339
|
+
default: true,
|
|
340
|
+
personApproves: true,
|
|
341
|
+
description: "Off: buyers cannot choose it.",
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
sellable: {
|
|
345
|
+
price: "price",
|
|
346
|
+
delivery: { kind: "kind", freeOver: "freeOver", countries: "countries", active: "active" },
|
|
347
|
+
tax: POSTAGE_TAX,
|
|
348
|
+
// A paid post or collect line gives staff a shipment to pack; take it now gives nothing.
|
|
349
|
+
grants: {
|
|
350
|
+
object: "shipment",
|
|
351
|
+
set: {
|
|
352
|
+
name: "line.title",
|
|
353
|
+
order: "order",
|
|
354
|
+
buyer: "order.person",
|
|
355
|
+
option: "item",
|
|
356
|
+
kind: "item.kind",
|
|
357
|
+
collectAt: "item.collectAt",
|
|
358
|
+
},
|
|
359
|
+
void: "void",
|
|
360
|
+
},
|
|
361
|
+
finance: { category: { key: "postage", label: "Postage" }, earned: "at_sale" },
|
|
362
|
+
},
|
|
363
|
+
// The checkout's choice (ADR 0682): every option, take it now included, since a member buys
|
|
364
|
+
// at the gym on their own phone through the website, signed in or as a guest (decision 13,
|
|
365
|
+
// follow-up 2). An option that is off is refused by the host.
|
|
366
|
+
public: {
|
|
367
|
+
read: ["name", "kind", "price", "freeOver", "countries", "collectAt", "active"],
|
|
368
|
+
},
|
|
369
|
+
// A new shop (follow-up 1): collect and take it now at no charge; posting at home and to
|
|
370
|
+
// everywhere else, each off until the business sets its price (and the home option its
|
|
371
|
+
// country, which no seed can know: new options take it from the `country` setting).
|
|
372
|
+
seed: {
|
|
373
|
+
collect: {
|
|
374
|
+
name: "Collect at the gym",
|
|
375
|
+
kind: "collect",
|
|
376
|
+
price: 0,
|
|
377
|
+
collectAt: "The front desk",
|
|
378
|
+
active: true,
|
|
379
|
+
},
|
|
380
|
+
here: { name: "Take it now", kind: "here", price: 0, active: true },
|
|
381
|
+
post: { name: "Standard post", kind: "post", active: false },
|
|
382
|
+
international: { name: "International post", kind: "post", countries: "*", active: false },
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
shipment: {
|
|
386
|
+
label: "Shipment",
|
|
387
|
+
plural: "Shipments",
|
|
388
|
+
icon: "package",
|
|
389
|
+
titleField: "name",
|
|
390
|
+
description: "What a paid order to post or collect gives staff to do: pack it, then send it or have it " +
|
|
391
|
+
"ready to collect. Made by the host when the order is paid; the buyer sees their own.",
|
|
392
|
+
navigation: "main",
|
|
393
|
+
navigationOrder: 12,
|
|
394
|
+
fields: {
|
|
395
|
+
name: { type: "text", label: "Name", required: true },
|
|
396
|
+
order: {
|
|
397
|
+
type: "text",
|
|
398
|
+
label: "Order",
|
|
399
|
+
readOnly: true,
|
|
400
|
+
description: "The order that granted it, set by the host.",
|
|
401
|
+
},
|
|
402
|
+
// Not required: erasing the buyer clears it, and the shipment stays (spec, Privacy).
|
|
403
|
+
buyer: { type: "relation", label: "Buyer", to: "person", readOnly: true },
|
|
404
|
+
option: {
|
|
405
|
+
type: "relation",
|
|
406
|
+
label: "Delivery option",
|
|
407
|
+
to: "shop.delivery",
|
|
408
|
+
readOnly: true,
|
|
409
|
+
},
|
|
410
|
+
kind: {
|
|
411
|
+
type: "select",
|
|
412
|
+
label: "Kind",
|
|
413
|
+
readOnly: true,
|
|
414
|
+
options: [
|
|
415
|
+
{ value: "post", label: "Post" },
|
|
416
|
+
{ value: "collect", label: "Collect" },
|
|
417
|
+
],
|
|
418
|
+
},
|
|
419
|
+
collectAt: { type: "text", label: "Where to collect", readOnly: true },
|
|
420
|
+
tracking: { type: "text", label: "Tracking reference" },
|
|
421
|
+
state: {
|
|
422
|
+
type: "select",
|
|
423
|
+
label: "State",
|
|
424
|
+
track: "timeline",
|
|
425
|
+
options: [
|
|
426
|
+
{ value: "to_pack", label: "To pack" },
|
|
427
|
+
{ value: "packed", label: "Packed" },
|
|
428
|
+
{ value: "sent", label: "Sent" },
|
|
429
|
+
{ value: "ready", label: "Ready to collect" },
|
|
430
|
+
{ value: "collected", label: "Collected" },
|
|
431
|
+
{ value: "void", label: "Void" },
|
|
432
|
+
],
|
|
433
|
+
},
|
|
434
|
+
packedAt: { type: "datetime", label: "Packed at", readOnly: true },
|
|
435
|
+
sentAt: { type: "datetime", label: "Sent at", readOnly: true },
|
|
436
|
+
readyAt: { type: "datetime", label: "Ready at", readOnly: true },
|
|
437
|
+
collectedAt: { type: "datetime", label: "Collected at", readOnly: true },
|
|
438
|
+
},
|
|
439
|
+
states: {
|
|
440
|
+
field: "state",
|
|
441
|
+
initial: "to_pack",
|
|
442
|
+
moves: [
|
|
443
|
+
{ from: "to_pack", to: "packed", by: ["staff"] },
|
|
444
|
+
{ from: "packed", to: "to_pack", by: ["staff"] },
|
|
445
|
+
{ from: ["to_pack", "packed"], to: "sent", by: ["staff"], final: true },
|
|
446
|
+
{ from: ["to_pack", "packed"], to: "ready", by: ["staff"] },
|
|
447
|
+
{ from: "ready", to: "collected", by: ["staff"], final: true },
|
|
448
|
+
// The order refunded in full (ADR 0435): the host's, before it went.
|
|
449
|
+
{ from: ["to_pack", "packed", "ready"], to: "void", by: ["host"], final: true },
|
|
450
|
+
],
|
|
451
|
+
},
|
|
452
|
+
// The buyer reads their own (My account's Deliveries, step 8) and changes nothing.
|
|
453
|
+
customer: {
|
|
454
|
+
field: "buyer",
|
|
455
|
+
read: [
|
|
456
|
+
"name",
|
|
457
|
+
"order",
|
|
458
|
+
"option",
|
|
459
|
+
"kind",
|
|
460
|
+
"collectAt",
|
|
461
|
+
"tracking",
|
|
462
|
+
"state",
|
|
463
|
+
"sentAt",
|
|
464
|
+
"readyAt",
|
|
465
|
+
"collectedAt",
|
|
466
|
+
],
|
|
467
|
+
write: [],
|
|
468
|
+
},
|
|
469
|
+
timelineLinks: ["buyer"],
|
|
470
|
+
indexes: [["state"], ["order"], ["buyer"]],
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
views: {
|
|
474
|
+
onlineShop: {
|
|
475
|
+
label: "Sold online",
|
|
476
|
+
object: "shop.item",
|
|
477
|
+
filter: { field: "whereSold", op: "in", value: ["online", "both"] },
|
|
478
|
+
sort: { field: "name", direction: "asc" },
|
|
479
|
+
columns: ["name", "category", "whereSold", "publishState"],
|
|
480
|
+
},
|
|
481
|
+
atTheGym: {
|
|
482
|
+
label: "Sold in person",
|
|
483
|
+
object: "shop.item",
|
|
484
|
+
filter: { field: "whereSold", op: "in", value: ["in_person", "both"] },
|
|
485
|
+
sort: { field: "name", direction: "asc" },
|
|
486
|
+
columns: ["name", "category", "whereSold", "publishState"],
|
|
487
|
+
},
|
|
488
|
+
stock: {
|
|
489
|
+
label: "Stock",
|
|
490
|
+
object: "shop.variant",
|
|
491
|
+
filter: { field: "state", op: "eq", value: "active" },
|
|
492
|
+
sort: { field: "available", direction: "asc" },
|
|
493
|
+
columns: ["name", "item", "onHand", "held", "available", "reorderAt", "supplier"],
|
|
494
|
+
},
|
|
495
|
+
toPack: {
|
|
496
|
+
label: "To pack and send",
|
|
497
|
+
object: "shop.shipment",
|
|
498
|
+
filter: { field: "state", op: "in", value: ["to_pack", "packed"] },
|
|
499
|
+
sort: { field: "createdAt", direction: "asc" },
|
|
500
|
+
columns: ["name", "kind", "buyer", "state", "createdAt"],
|
|
501
|
+
},
|
|
502
|
+
readyToCollect: {
|
|
503
|
+
label: "Ready to collect",
|
|
504
|
+
object: "shop.shipment",
|
|
505
|
+
filter: { field: "state", op: "eq", value: "ready" },
|
|
506
|
+
sort: { field: "readyAt", direction: "asc" },
|
|
507
|
+
columns: ["name", "buyer", "collectAt", "readyAt"],
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
dashboards: shopDashboards,
|
|
511
|
+
recordTabs: {
|
|
512
|
+
itemVariants: {
|
|
513
|
+
label: "Variants",
|
|
514
|
+
on: "shop.item",
|
|
515
|
+
view: {
|
|
516
|
+
type: "table",
|
|
517
|
+
object: "shop.variant",
|
|
518
|
+
relation: "item",
|
|
519
|
+
columns: ["name", "price", "onHand", "available", "reorderAt", "state"],
|
|
520
|
+
sort: { field: "name", direction: "asc" },
|
|
521
|
+
},
|
|
522
|
+
order: 10,
|
|
523
|
+
},
|
|
524
|
+
personShipments: {
|
|
525
|
+
label: "Shipments",
|
|
526
|
+
on: "person",
|
|
527
|
+
view: {
|
|
528
|
+
type: "table",
|
|
529
|
+
object: "shop.shipment",
|
|
530
|
+
relation: "buyer",
|
|
531
|
+
columns: ["name", "kind", "state", "tracking", "createdAt"],
|
|
532
|
+
sort: { field: "createdAt", direction: "desc" },
|
|
533
|
+
},
|
|
534
|
+
order: 40,
|
|
535
|
+
},
|
|
536
|
+
supplierVariants: {
|
|
537
|
+
label: "Supplies",
|
|
538
|
+
on: "organisation",
|
|
539
|
+
view: {
|
|
540
|
+
type: "table",
|
|
541
|
+
object: "shop.variant",
|
|
542
|
+
relation: "supplier",
|
|
543
|
+
columns: ["name", "item", "onHand", "reorderAt", "reorderQty"],
|
|
544
|
+
sort: { field: "name", direction: "asc" },
|
|
545
|
+
},
|
|
546
|
+
order: 40,
|
|
547
|
+
},
|
|
548
|
+
},
|
|
549
|
+
settings: {
|
|
550
|
+
// The host keeps no home country (ADR 0682): a new post option posts to this one.
|
|
551
|
+
country: {
|
|
552
|
+
type: "text",
|
|
553
|
+
label: "Your country",
|
|
554
|
+
description: "Two letters, e.g. AU. New post options post here unless you list other countries. Set " +
|
|
555
|
+
"it, then give the seeded Standard post option this country and a price, and turn it on.",
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
roles: {
|
|
559
|
+
stockroom: {
|
|
560
|
+
label: "Stockroom",
|
|
561
|
+
description: "Sees the items, the variants and their stock levels, for someone who only receives " +
|
|
562
|
+
"and counts stock. Nothing else: no people, orders, prices to change or settings.",
|
|
563
|
+
permissions: ["shop:stock"],
|
|
564
|
+
read: ["shop.item", "shop.variant"],
|
|
565
|
+
},
|
|
566
|
+
},
|
|
567
|
+
// For a coach or desk account (ADR 0600): reading the stock levels, and handing over or
|
|
568
|
+
// sending what was bought. The stockroom role's permission is its own and not granted one by one.
|
|
569
|
+
grantable: ["shop:read", "shop:fulfil"],
|
|
570
|
+
grantableLabels: {
|
|
571
|
+
"shop:read": "Shop stock: see every variant's stock, what is held by orders being paid and what is low.",
|
|
572
|
+
"shop:fulfil": "Shop orders: see what is to pack, send or hand over, and mark it packed, sent, ready or " +
|
|
573
|
+
"collected.",
|
|
574
|
+
},
|
|
575
|
+
tools: { stockLevels, shipmentsToSend, markPacked, markSent, markReady, markCollected },
|
|
576
|
+
});
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const ITEM = "shop.item";
|
|
2
|
+
export declare const VARIANT = "shop.variant";
|
|
3
|
+
export declare const SHIPMENT = "shop.shipment";
|
|
4
|
+
/** A record as record.get and record.list return it. */
|
|
5
|
+
export interface HostRecord {
|
|
6
|
+
id: string;
|
|
7
|
+
title: string | null;
|
|
8
|
+
values: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
/** The most variants one call lists. */
|
|
11
|
+
export declare const STOCK_LEVELS_LIMIT = 200;
|
|
12
|
+
export declare const stockLevels: any;
|
|
13
|
+
/** The most shipments one call lists. */
|
|
14
|
+
export declare const SHIPMENTS_LIMIT = 100;
|
|
15
|
+
export declare const shipmentsToSend: any;
|
|
16
|
+
export declare const markPacked: any;
|
|
17
|
+
export declare const markSent: any;
|
|
18
|
+
export declare const markReady: any;
|
|
19
|
+
export declare const markCollected: any;
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// The Shop's staff tools (stage 37, step 5). They become operations x.shop.<tool>, run in the
|
|
2
|
+
// sandbox, and reach the host only through ctx.ops. None of them holds a rule: prices, where a
|
|
3
|
+
// thing is sold, stock, postage and which moves a shipment may make are all the host's. A tool
|
|
4
|
+
// asks, and reports what the host answered.
|
|
5
|
+
import { defineTool, OpsCallError, z } from "@composable-os/sdk";
|
|
6
|
+
export const ITEM = "shop.item";
|
|
7
|
+
export const VARIANT = "shop.variant";
|
|
8
|
+
export const SHIPMENT = "shop.shipment";
|
|
9
|
+
const text = (value) => value === null || value === undefined || value === "" ? null : String(value);
|
|
10
|
+
const num = (value) => value === null || value === undefined ? null : Number(value);
|
|
11
|
+
// --- Stock levels --------------------------------------------------------------------------------
|
|
12
|
+
const stockRow = z.object({
|
|
13
|
+
variantId: z.string(),
|
|
14
|
+
variant: z.string().nullable(),
|
|
15
|
+
itemId: z.string().nullable(),
|
|
16
|
+
item: z.string().nullable(),
|
|
17
|
+
category: z.string().nullable(),
|
|
18
|
+
onHand: z.number().describe("On the shelf now: opening, received and returned, less sold"),
|
|
19
|
+
held: z.number().describe("Held by orders being paid for"),
|
|
20
|
+
available: z.number().describe("On hand less held: what can still be sold"),
|
|
21
|
+
reorderAt: z.number().nullable(),
|
|
22
|
+
reorderQty: z.number().nullable(),
|
|
23
|
+
low: z.boolean().describe("On hand is at or below the reorder level"),
|
|
24
|
+
supplierId: z.string().nullable(),
|
|
25
|
+
supplier: z.string().nullable(),
|
|
26
|
+
});
|
|
27
|
+
/** The most variants one call lists. */
|
|
28
|
+
export const STOCK_LEVELS_LIMIT = 200;
|
|
29
|
+
export const stockLevels = defineTool({
|
|
30
|
+
description: "The stock of the shop's active variants: on hand, held by orders being paid for, available, " +
|
|
31
|
+
"the reorder level and quantity, and the usual supplier, lowest first. Use lowOnly for " +
|
|
32
|
+
"'what's low?' (on hand at or below the reorder level), and category or supplierId to narrow " +
|
|
33
|
+
"it. Numbers are the host's, kept from the stock ledger; this changes nothing. Read-only.",
|
|
34
|
+
input: z.object({
|
|
35
|
+
lowOnly: z.boolean().optional().describe("Only variants at or below their reorder level"),
|
|
36
|
+
category: z.string().max(80).optional().describe("Only items of this category"),
|
|
37
|
+
supplierId: z.string().max(64).optional().describe("Only variants this organisation supplies"),
|
|
38
|
+
}),
|
|
39
|
+
output: z.object({ variants: z.array(stockRow), truncated: z.boolean() }),
|
|
40
|
+
risk: "read",
|
|
41
|
+
// Staff who read the stock (a grant for a desk or coach account) and the stockroom role.
|
|
42
|
+
permission: ["shop:read", "shop:stock"],
|
|
43
|
+
// The stockroom role holds no limit (today's records, a team) that a grant could lift.
|
|
44
|
+
grantLiftsRoleLimits: true,
|
|
45
|
+
async handler(input, ctx) {
|
|
46
|
+
const where = ["v.state = 'active'"];
|
|
47
|
+
const params = [];
|
|
48
|
+
if (input.lowOnly)
|
|
49
|
+
where.push("v.reorder_at IS NOT NULL AND v._stk_on_hand <= v.reorder_at");
|
|
50
|
+
if (input.category) {
|
|
51
|
+
where.push("i.category = ?");
|
|
52
|
+
params.push(input.category);
|
|
53
|
+
}
|
|
54
|
+
if (input.supplierId) {
|
|
55
|
+
where.push("v.supplier = ?");
|
|
56
|
+
params.push(input.supplierId);
|
|
57
|
+
}
|
|
58
|
+
const result = await ctx.ops.call("query.sql", {
|
|
59
|
+
sql: "SELECT v.id AS variant_id, v.name AS variant, i.id AS item_id, i.name AS item, " +
|
|
60
|
+
"i.category AS category, v._stk_on_hand AS on_hand, v._stk_held AS held, " +
|
|
61
|
+
"v._stk_available AS available, v.reorder_at AS reorder_at, v.reorder_qty AS reorder_qty, " +
|
|
62
|
+
"o.id AS supplier_id, o.name AS supplier " +
|
|
63
|
+
"FROM rec_x_shop_variant v LEFT JOIN rec_x_shop_item i ON i.id = v.item " +
|
|
64
|
+
"LEFT JOIN rec_organisation o ON o.id = v.supplier " +
|
|
65
|
+
`WHERE ${where.join(" AND ")} ` +
|
|
66
|
+
"ORDER BY (v.reorder_at IS NOT NULL AND v._stk_on_hand <= v.reorder_at) DESC, " +
|
|
67
|
+
`v._stk_available ASC, v.name ASC LIMIT ${STOCK_LEVELS_LIMIT + 1}`,
|
|
68
|
+
params,
|
|
69
|
+
});
|
|
70
|
+
const rows = result.rows.slice(0, STOCK_LEVELS_LIMIT);
|
|
71
|
+
return {
|
|
72
|
+
truncated: result.rows.length > STOCK_LEVELS_LIMIT,
|
|
73
|
+
variants: rows.map((row) => {
|
|
74
|
+
const onHand = Number(row.on_hand ?? 0);
|
|
75
|
+
const reorderAt = num(row.reorder_at);
|
|
76
|
+
return {
|
|
77
|
+
variantId: String(row.variant_id),
|
|
78
|
+
variant: text(row.variant),
|
|
79
|
+
itemId: text(row.item_id),
|
|
80
|
+
item: text(row.item),
|
|
81
|
+
category: text(row.category),
|
|
82
|
+
onHand,
|
|
83
|
+
held: Number(row.held ?? 0),
|
|
84
|
+
available: Number(row.available ?? 0),
|
|
85
|
+
reorderAt,
|
|
86
|
+
reorderQty: num(row.reorder_qty),
|
|
87
|
+
low: reorderAt !== null && onHand <= reorderAt,
|
|
88
|
+
supplierId: text(row.supplier_id),
|
|
89
|
+
supplier: text(row.supplier),
|
|
90
|
+
};
|
|
91
|
+
}),
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
// --- Shipments -----------------------------------------------------------------------------------
|
|
96
|
+
const shipmentOutput = z.object({
|
|
97
|
+
id: z.string(),
|
|
98
|
+
name: z.string().nullable(),
|
|
99
|
+
orderId: z.string().nullable(),
|
|
100
|
+
kind: z.string().nullable().describe("post or collect"),
|
|
101
|
+
state: z.string().nullable(),
|
|
102
|
+
tracking: z.string().nullable(),
|
|
103
|
+
collectAt: z.string().nullable(),
|
|
104
|
+
buyerId: z.string().nullable(),
|
|
105
|
+
buyer: z.string().nullable().describe("The buyer's name, if they are a Person"),
|
|
106
|
+
createdAt: z.string().nullable(),
|
|
107
|
+
});
|
|
108
|
+
async function personName(ctx, id) {
|
|
109
|
+
if (!id)
|
|
110
|
+
return null;
|
|
111
|
+
try {
|
|
112
|
+
const person = await ctx.ops.call("person.get", { id });
|
|
113
|
+
return text(person.displayName ?? person.name);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (error instanceof OpsCallError && error.code === "not_found")
|
|
117
|
+
return null;
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function describe(record, buyer) {
|
|
122
|
+
const v = record.values;
|
|
123
|
+
return {
|
|
124
|
+
id: record.id,
|
|
125
|
+
name: text(v.name) ?? record.title,
|
|
126
|
+
orderId: text(v.order),
|
|
127
|
+
kind: text(v.kind),
|
|
128
|
+
state: text(v.state),
|
|
129
|
+
tracking: text(v.tracking),
|
|
130
|
+
collectAt: text(v.collectAt),
|
|
131
|
+
buyerId: text(v.buyer),
|
|
132
|
+
buyer,
|
|
133
|
+
createdAt: text(v.createdAt),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** The most shipments one call lists. */
|
|
137
|
+
export const SHIPMENTS_LIMIT = 100;
|
|
138
|
+
export const shipmentsToSend = defineTool({
|
|
139
|
+
description: "What paid online orders have left to do: shipments to pack, packed ones to send or have " +
|
|
140
|
+
"ready, and those waiting to be collected, oldest first, with the buyer's name, the kind " +
|
|
141
|
+
"(post or collect), where to collect and the tracking reference. Read-only.",
|
|
142
|
+
input: z.object({
|
|
143
|
+
state: z
|
|
144
|
+
.enum(["to_pack", "packed", "ready"])
|
|
145
|
+
.optional()
|
|
146
|
+
.describe("Only shipments in this state; left out, all three"),
|
|
147
|
+
}),
|
|
148
|
+
output: z.object({ shipments: z.array(shipmentOutput) }),
|
|
149
|
+
risk: "read",
|
|
150
|
+
permission: "shop:fulfil",
|
|
151
|
+
async handler(input, ctx) {
|
|
152
|
+
const states = input.state ? [input.state] : ["to_pack", "packed", "ready"];
|
|
153
|
+
const page = await ctx.ops.call("record.list", {
|
|
154
|
+
object: SHIPMENT,
|
|
155
|
+
filter: { field: "state", op: "in", value: states },
|
|
156
|
+
sort: { field: "createdAt", direction: "asc" },
|
|
157
|
+
limit: SHIPMENTS_LIMIT,
|
|
158
|
+
});
|
|
159
|
+
const names = new Map();
|
|
160
|
+
for (const record of page.items) {
|
|
161
|
+
const buyer = text(record.values.buyer);
|
|
162
|
+
if (buyer && !names.has(buyer))
|
|
163
|
+
names.set(buyer, await personName(ctx, buyer));
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
shipments: page.items.map((record) => {
|
|
167
|
+
const buyer = text(record.values.buyer);
|
|
168
|
+
return describe(record, buyer ? (names.get(buyer) ?? null) : null);
|
|
169
|
+
}),
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
const moveOutput = z.object({
|
|
174
|
+
moved: z.boolean(),
|
|
175
|
+
message: z.string(),
|
|
176
|
+
shipment: shipmentOutput,
|
|
177
|
+
});
|
|
178
|
+
/**
|
|
179
|
+
* Moves a shipment from the state it is in now to `to`, with `values`. The move is made from a
|
|
180
|
+
* current read (ADR 0437): `from` is what was read, so two people marking the same shipment at
|
|
181
|
+
* once make it once and the other is told. Which moves exist is the host's (the shipment's
|
|
182
|
+
* states); a move it does not declare is refused there, not here.
|
|
183
|
+
*/
|
|
184
|
+
async function move(ctx, id, to, values, done) {
|
|
185
|
+
const current = await ctx.ops.call("record.get", { object: SHIPMENT, id });
|
|
186
|
+
const from = text(current.values.state);
|
|
187
|
+
if (from === to) {
|
|
188
|
+
return {
|
|
189
|
+
moved: false,
|
|
190
|
+
message: `Already ${label(to)}.`,
|
|
191
|
+
shipment: describe(current, await personName(ctx, text(current.values.buyer))),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const after = await ctx.ops.call("record.update", {
|
|
196
|
+
object: SHIPMENT,
|
|
197
|
+
id,
|
|
198
|
+
...(from ? { from } : {}),
|
|
199
|
+
values: { ...values, state: to },
|
|
200
|
+
});
|
|
201
|
+
return {
|
|
202
|
+
moved: true,
|
|
203
|
+
message: done,
|
|
204
|
+
shipment: describe(after, await personName(ctx, text(after.values.buyer))),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
// Moved by someone else meanwhile, or a move the shipment's states do not allow from here.
|
|
209
|
+
if (!(error instanceof OpsCallError) || !["conflict", "invalid_input"].includes(error.code)) {
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
const now = await ctx.ops.call("record.get", { object: SHIPMENT, id });
|
|
213
|
+
return {
|
|
214
|
+
moved: false,
|
|
215
|
+
message: `Not changed: the shipment is ${label(text(now.values.state))}. ${error.message}`,
|
|
216
|
+
shipment: describe(now, await personName(ctx, text(now.values.buyer))),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function label(state) {
|
|
221
|
+
switch (state) {
|
|
222
|
+
case "to_pack":
|
|
223
|
+
return "to pack";
|
|
224
|
+
case "ready":
|
|
225
|
+
return "ready to collect";
|
|
226
|
+
default:
|
|
227
|
+
return state ?? "unknown";
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const shipmentId = z.string().min(1).max(64).describe("The shipment's id");
|
|
231
|
+
export const markPacked = defineTool({
|
|
232
|
+
description: "Mark a shipment packed: its goods are boxed and waiting to be sent or collected. Safe to " +
|
|
233
|
+
"call twice; two people at once: one marks it, the other is told.",
|
|
234
|
+
input: z.object({ shipmentId }),
|
|
235
|
+
output: moveOutput,
|
|
236
|
+
risk: "write",
|
|
237
|
+
permission: "shop:fulfil",
|
|
238
|
+
recordAction: {
|
|
239
|
+
object: SHIPMENT,
|
|
240
|
+
label: "Mark packed",
|
|
241
|
+
idInput: "shipmentId",
|
|
242
|
+
hideWhen: { field: "state", values: ["packed", "sent", "ready", "collected", "void"] },
|
|
243
|
+
},
|
|
244
|
+
handler: (input, ctx) => move(ctx, input.shipmentId, "packed", { packedAt: new Date().toISOString() }, "Packed."),
|
|
245
|
+
});
|
|
246
|
+
export const markSent = defineTool({
|
|
247
|
+
description: "Mark a posted shipment sent, with the carrier's tracking reference if there is one. It " +
|
|
248
|
+
"cannot be undone. The buyer sees the state and the reference on their own shipments.",
|
|
249
|
+
input: z.object({
|
|
250
|
+
shipmentId,
|
|
251
|
+
tracking: z.string().max(200).optional().describe("The carrier's tracking reference"),
|
|
252
|
+
}),
|
|
253
|
+
output: moveOutput,
|
|
254
|
+
risk: "write",
|
|
255
|
+
permission: "shop:fulfil",
|
|
256
|
+
recordAction: {
|
|
257
|
+
object: SHIPMENT,
|
|
258
|
+
label: "Mark sent",
|
|
259
|
+
idInput: "shipmentId",
|
|
260
|
+
hideWhen: { field: "kind", values: ["collect"] },
|
|
261
|
+
},
|
|
262
|
+
handler: (input, ctx) => move(ctx, input.shipmentId, "sent", {
|
|
263
|
+
sentAt: new Date().toISOString(),
|
|
264
|
+
...(input.tracking ? { tracking: input.tracking } : {}),
|
|
265
|
+
}, "Sent."),
|
|
266
|
+
});
|
|
267
|
+
export const markReady = defineTool({
|
|
268
|
+
description: "Mark a shipment to collect as ready: its goods are waiting where the buyer collects them.",
|
|
269
|
+
input: z.object({ shipmentId }),
|
|
270
|
+
output: moveOutput,
|
|
271
|
+
risk: "write",
|
|
272
|
+
permission: "shop:fulfil",
|
|
273
|
+
recordAction: {
|
|
274
|
+
object: SHIPMENT,
|
|
275
|
+
label: "Ready to collect",
|
|
276
|
+
idInput: "shipmentId",
|
|
277
|
+
hideWhen: { field: "kind", values: ["post"] },
|
|
278
|
+
},
|
|
279
|
+
handler: (input, ctx) => move(ctx, input.shipmentId, "ready", { readyAt: new Date().toISOString() }, "Ready to collect."),
|
|
280
|
+
});
|
|
281
|
+
export const markCollected = defineTool({
|
|
282
|
+
description: "Mark a ready shipment collected: the buyer has picked it up. It cannot be undone.",
|
|
283
|
+
input: z.object({ shipmentId }),
|
|
284
|
+
output: moveOutput,
|
|
285
|
+
risk: "write",
|
|
286
|
+
permission: "shop:fulfil",
|
|
287
|
+
recordAction: {
|
|
288
|
+
object: SHIPMENT,
|
|
289
|
+
label: "Mark collected",
|
|
290
|
+
idInput: "shipmentId",
|
|
291
|
+
hideWhen: { field: "state", values: ["to_pack", "packed", "sent", "collected", "void"] },
|
|
292
|
+
},
|
|
293
|
+
handler: (input, ctx) => move(ctx, input.shipmentId, "collected", { collectedAt: new Date().toISOString() }, "Collected."),
|
|
294
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composable-os/pack-shop",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.12",
|
|
4
|
+
"license": "FSL-1.1-ALv2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/timoconnellaus/composable-os.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://composableos.dev",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"provenance": true
|
|
14
|
+
},
|
|
15
|
+
"description": "Shop: the things a business sells (items and their variants, sold online, in person or both), the stock the host keeps of each, delivery options and the shipments staff pack and send (stage 37). Bundled as source into cos-runner.",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"composableOs": {
|
|
26
|
+
"optional": true,
|
|
27
|
+
"label": "Shop",
|
|
28
|
+
"description": "One list of what the business sells, in its sizes and flavours, sold online and at the gym, with stock kept by the host, posting or collecting, and the shipments to pack and send."
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@composable-os/sdk": "0.1.0-beta.12"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/README.md
DELETED