@stacksjs/defaults 0.75.618 → 0.75.621
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/ai/skills/stacks-actions/SKILL.md +1 -1
- package/ai/skills/stacks-auth/SKILL.md +12 -1
- package/ai/skills/stacks-auto-imports/SKILL.md +1 -1
- package/ai/skills/stacks-commerce/SKILL.md +78 -4
- package/ai/skills/stacks-dashboard/SKILL.md +2 -2
- package/ai/skills/stacks-database/SKILL.md +1 -1
- package/ai/skills/stacks-migrations/SKILL.md +10 -2
- package/ai/skills/stacks-mobile/SKILL.md +21 -2
- package/ai/skills/stacks-models/SKILL.md +2 -2
- package/ai/skills/stacks-native/CATALOG.md +11 -11
- package/ai/skills/stacks-orm/SKILL.md +2 -2
- package/app/Actions/Dashboard/Commerce/CommerceDiscountDestroyAction.ts +27 -0
- package/app/Actions/Dashboard/Commerce/CommerceDiscountStoreAction.ts +28 -0
- package/app/Actions/Dashboard/Commerce/CommerceDiscountUpdateAction.ts +30 -0
- package/app/Actions/Dashboard/Commerce/CommerceDiscountsAction.ts +42 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellDestroyAction.ts +31 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellDetailAction.ts +28 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellStoreAction.ts +26 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellUpdateAction.ts +29 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellsAction.ts +40 -0
- package/app/Actions/Dashboard/Commerce/commerce-discount-list.ts +204 -0
- package/app/Actions/Dashboard/Commerce/commerce-discount-write.ts +53 -0
- package/app/Actions/Dashboard/Commerce/commerce-discounts-upsells.test.ts +228 -0
- package/app/Actions/Dashboard/Commerce/commerce-upsell-list.ts +289 -0
- package/app/Actions/Dashboard/Commerce/discount-records.ts +392 -0
- package/app/Actions/Dashboard/Commerce/upsell-offer-records.ts +307 -0
- package/app/Actions/Dashboard/Models/ModelSaveAction.ts +6 -1
- package/app/Actions/Dashboard/Models/model-relations.ts +17 -7
- package/app/Actions/Dashboard/Models/model-row-writes.ts +9 -8
- package/app/Actions/Dashboard/Models/model-write.ts +51 -2
- package/app/Actions/Dashboard/Remote/RemoteCommandRunAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalCloseAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalInputAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalOpenAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalResizeAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalStreamAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/remote-commands.test.ts +2 -2
- package/app/Actions/Dashboard/Remote/remote-commands.ts +4 -4
- package/app/Actions/Dashboard/Remote/remote-terminal.test.ts +45 -25
- package/app/Actions/Dashboard/Remote/remote-terminal.ts +9 -9
- package/app/Actions/Storefront/AcceptOrderOfferAction.ts +68 -0
- package/app/Actions/Storefront/ApplyDiscountCodeAction.ts +83 -0
- package/app/Actions/Storefront/DeclineOrderOfferAction.ts +25 -0
- package/app/Actions/Storefront/PlaceOrderAction.ts +135 -72
- package/app/Gates.ts +4 -4
- package/app/Mail/OrderConfirmation.ts +7 -1
- package/app/Models/commerce/Cart.ts +14 -0
- package/app/Models/commerce/Discount.ts +255 -0
- package/app/Models/commerce/PostPurchaseOffer.ts +175 -0
- package/app/Models/commerce/PostPurchaseOfferResponse.ts +145 -0
- package/app/Policies/PostPolicy.ts +11 -11
- package/app/Storefront/DiscountCodeMessages.ts +12 -0
- package/app/Storefront/OrderKey.ts +29 -0
- package/app/Storefront/StorefrontPricing.ts +211 -0
- package/app/Storefront/UpsellCharge.ts +43 -0
- package/ide/vscode/package.json +1 -1
- package/package.json +7 -7
- package/resources/components/Dashboard/Commerce/CommerceDiscountBreakdown.stx +72 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountDeleteDialog.stx +19 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountDetailsDialog.stx +110 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountDialog.stx +353 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountsDashboard.stx +211 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountsTable.stx +105 -0
- package/resources/components/Dashboard/Commerce/CommerceProductPicker.stx +115 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellDeleteDialog.stx +21 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellDetailsDialog.stx +153 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellDialog.stx +191 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellsDashboard.stx +214 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellsTable.stx +91 -0
- package/resources/components/Dashboard/Email/InboxSettingsDashboard.stx +4 -19
- package/resources/components/Dashboard/Settings/SettingsDashboard.stx +5 -3
- package/resources/components/Dashboard/UI/SearchFilter.stx +7 -4
- package/resources/components/Storefront/OrderSummary.stx +17 -0
- package/resources/functions/dashboard/sidebar.ts +4 -1
- package/resources/views/cart.stx +50 -16
- package/resources/views/checkout/contact.stx +11 -5
- package/resources/views/checkout/payment.stx +11 -5
- package/resources/views/checkout/shipping.stx +11 -5
- package/resources/views/orders/[id].stx +227 -50
- package/routes/dashboard-api.ts +12 -0
- package/routes/dashboard.ts +2 -0
- package/routes/storefront.ts +8 -0
- package/views/dashboard/commerce/discounts/index.stx +10 -0
- package/views/dashboard/commerce/upsells/index.stx +10 -0
package/app/Gates.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { GateUser } from '@stacksjs/auth'
|
|
2
2
|
import { defineGates } from '@stacksjs/auth'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -29,18 +29,18 @@ export default defineGates({
|
|
|
29
29
|
*/
|
|
30
30
|
gates: {
|
|
31
31
|
/** Check if user can access admin area */
|
|
32
|
-
'access-admin': (user:
|
|
32
|
+
'access-admin': (user: GateUser | null) => {
|
|
33
33
|
return user?.email?.endsWith('@stacksjs.com') ?? false
|
|
34
34
|
},
|
|
35
35
|
|
|
36
36
|
/** Check if user can edit application settings */
|
|
37
|
-
'edit-settings': (user:
|
|
37
|
+
'edit-settings': (user: GateUser | null) => {
|
|
38
38
|
// Add your logic here
|
|
39
39
|
return user !== null
|
|
40
40
|
},
|
|
41
41
|
|
|
42
42
|
/** Check if user can view dashboard */
|
|
43
|
-
'view-dashboard': (user:
|
|
43
|
+
'view-dashboard': (user: GateUser | null) => {
|
|
44
44
|
return user !== null
|
|
45
45
|
},
|
|
46
46
|
|
|
@@ -17,10 +17,14 @@ export interface OrderConfirmationOptions {
|
|
|
17
17
|
/** The amounts below are integer minor units of this currency (1999 is $19.99). */
|
|
18
18
|
currency?: string
|
|
19
19
|
subtotal: number
|
|
20
|
+
/** Taken off the subtotal by discounts; the row is left out when 0. */
|
|
21
|
+
discount?: number
|
|
22
|
+
/** What the discount was ("BUY 4, GET 4 FREE"). */
|
|
23
|
+
discountTitle?: string
|
|
20
24
|
shipping: number
|
|
21
25
|
total: number
|
|
22
26
|
shippingAddress?: string
|
|
23
|
-
/** Absolute URL the receipt CTA should link to (e.g. `/orders/123
|
|
27
|
+
/** Absolute URL the receipt CTA should link to (e.g. `/orders/123?key=...`). */
|
|
24
28
|
orderUrl: string
|
|
25
29
|
}
|
|
26
30
|
|
|
@@ -55,6 +59,8 @@ export async function sendOrderConfirmation(options: OrderConfirmationOptions):
|
|
|
55
59
|
// never has to know the amounts are minor units or which currency.
|
|
56
60
|
subtotalLabel: formatCurrency(options.subtotal, currency),
|
|
57
61
|
shippingLabel: formatCurrency(options.shipping, currency),
|
|
62
|
+
discountLabel: options.discount ? `-${formatCurrency(options.discount, currency)}` : '',
|
|
63
|
+
discountTitle: options.discountTitle || '',
|
|
58
64
|
totalLabel: formatCurrency(options.total, currency),
|
|
59
65
|
shippingAddress: options.shippingAddress || '',
|
|
60
66
|
appName,
|
|
@@ -221,6 +221,20 @@ export default defineModel({
|
|
|
221
221
|
rule: schema.string().max(20),
|
|
222
222
|
},
|
|
223
223
|
},
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The discount code the shopper entered, as typed. Checked when it is
|
|
227
|
+
* applied and again when the order is placed, since a code can expire or
|
|
228
|
+
* run out in between. Matches a `Discount` code or a `Coupon` code.
|
|
229
|
+
*/
|
|
230
|
+
discountCode: {
|
|
231
|
+
order: 19,
|
|
232
|
+
fillable: true,
|
|
233
|
+
nullable: true,
|
|
234
|
+
validation: {
|
|
235
|
+
rule: schema.string().max(64),
|
|
236
|
+
},
|
|
237
|
+
},
|
|
224
238
|
},
|
|
225
239
|
|
|
226
240
|
dashboard: {
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { defineModel } from '@stacksjs/orm'
|
|
2
|
+
import { schema } from '@stacksjs/validation/runtime'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A discount the storefront applies by itself (automatic) or when a shopper
|
|
6
|
+
* enters its code.
|
|
7
|
+
*
|
|
8
|
+
* Coupons cover the simple case, a code worth a percentage or an amount.
|
|
9
|
+
* This covers the rest of what merchants run: buy X get Y ("buy 4, get 4
|
|
10
|
+
* free"), volume tiers ("5+ pairs 20% off"), free shipping, discounts limited
|
|
11
|
+
* to products, variants or categories, minimums, caps and the rules for which
|
|
12
|
+
* discounts combine. `commerce.discounts.price()` reads these rows and prices a
|
|
13
|
+
* basket with the pure engine in `@stacksjs/commerce/discounts/engine`.
|
|
14
|
+
*
|
|
15
|
+
* Amounts are integer minor units of the store's currency; percentages are
|
|
16
|
+
* plain numbers from 0 to 100.
|
|
17
|
+
*/
|
|
18
|
+
export default defineModel({
|
|
19
|
+
name: 'Discount',
|
|
20
|
+
table: 'discounts',
|
|
21
|
+
primaryKey: 'id',
|
|
22
|
+
autoIncrement: true,
|
|
23
|
+
|
|
24
|
+
// Store configuration: no row has a per-caller owner. Writes are an
|
|
25
|
+
// administrative concern gated by `middleware` (stacksjs/stacks#2375).
|
|
26
|
+
ownership: false,
|
|
27
|
+
|
|
28
|
+
traits: {
|
|
29
|
+
useUuid: true,
|
|
30
|
+
useTimestamps: true,
|
|
31
|
+
useSearch: {
|
|
32
|
+
displayable: ['id', 'title', 'code', 'type', 'value', 'isActive', 'usageCount', 'startsAt', 'endsAt'],
|
|
33
|
+
searchable: ['title', 'code'],
|
|
34
|
+
sortable: ['createdAt', 'updatedAt', 'title', 'usageCount', 'startsAt', 'endsAt'],
|
|
35
|
+
filterable: ['type', 'isActive'],
|
|
36
|
+
},
|
|
37
|
+
useSeeder: { count: 0 },
|
|
38
|
+
useApi: {
|
|
39
|
+
uri: 'discounts',
|
|
40
|
+
middleware: ['auth'],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
attributes: {
|
|
45
|
+
/** What the shopper sees beside the saving: "BUY 4, GET 4 FREE | 50% OFF". */
|
|
46
|
+
title: {
|
|
47
|
+
order: 1,
|
|
48
|
+
fillable: true,
|
|
49
|
+
required: true,
|
|
50
|
+
validation: { rule: schema.string().required().max(255) },
|
|
51
|
+
factory: () => 'BUY 4, GET 4 FREE',
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
/** The code a shopper types. Empty makes the discount automatic. */
|
|
55
|
+
code: {
|
|
56
|
+
order: 2,
|
|
57
|
+
fillable: true,
|
|
58
|
+
unique: true,
|
|
59
|
+
nullable: true,
|
|
60
|
+
validation: { rule: schema.string().max(64) },
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
type: {
|
|
64
|
+
order: 3,
|
|
65
|
+
fillable: true,
|
|
66
|
+
required: true,
|
|
67
|
+
validation: {
|
|
68
|
+
rule: schema.enum(['percentage', 'fixed_amount', 'buy_x_get_y', 'volume', 'free_shipping']).required(),
|
|
69
|
+
},
|
|
70
|
+
factory: () => 'percentage',
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
/** Whole percent off for `percentage`, minor units for `fixed_amount`. */
|
|
74
|
+
value: {
|
|
75
|
+
order: 4,
|
|
76
|
+
fillable: true,
|
|
77
|
+
default: 0,
|
|
78
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
79
|
+
factory: () => 10,
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
/** `product` or `order`, for percentage and fixed amounts. Empty: decided by whether a target is set. */
|
|
83
|
+
scope: {
|
|
84
|
+
order: 5,
|
|
85
|
+
fillable: true,
|
|
86
|
+
nullable: true,
|
|
87
|
+
validation: { rule: schema.enum(['product', 'order']) },
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
/** A fixed amount off every targeted unit rather than once. */
|
|
91
|
+
perItem: {
|
|
92
|
+
order: 6,
|
|
93
|
+
fillable: true,
|
|
94
|
+
default: false,
|
|
95
|
+
validation: { rule: schema.boolean() },
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
/** JSON `{ productIds, variantIds, categoryIds }`: the lines it reaches. Empty reaches every line. */
|
|
99
|
+
target: {
|
|
100
|
+
order: 7,
|
|
101
|
+
fillable: true,
|
|
102
|
+
nullable: true,
|
|
103
|
+
type: 'text',
|
|
104
|
+
validation: { rule: schema.string() },
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
buyQuantity: {
|
|
108
|
+
order: 8,
|
|
109
|
+
fillable: true,
|
|
110
|
+
nullable: true,
|
|
111
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
/** JSON target for what must be bought. Empty: `target`. */
|
|
115
|
+
buyTarget: {
|
|
116
|
+
order: 9,
|
|
117
|
+
fillable: true,
|
|
118
|
+
nullable: true,
|
|
119
|
+
type: 'text',
|
|
120
|
+
validation: { rule: schema.string() },
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
getQuantity: {
|
|
124
|
+
order: 10,
|
|
125
|
+
fillable: true,
|
|
126
|
+
nullable: true,
|
|
127
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
/** JSON target for what comes discounted. Empty: the buy target. */
|
|
131
|
+
getTarget: {
|
|
132
|
+
order: 11,
|
|
133
|
+
fillable: true,
|
|
134
|
+
nullable: true,
|
|
135
|
+
type: 'text',
|
|
136
|
+
validation: { rule: schema.string() },
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
/** Whole percent off each "get" unit: 100 is free. */
|
|
140
|
+
getPercent: {
|
|
141
|
+
order: 12,
|
|
142
|
+
fillable: true,
|
|
143
|
+
nullable: true,
|
|
144
|
+
validation: { rule: schema.number().integer().min(0).max(100) },
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
/** Times one order may use a buy X get Y. Empty: unlimited. */
|
|
148
|
+
maxApplications: {
|
|
149
|
+
order: 13,
|
|
150
|
+
fillable: true,
|
|
151
|
+
nullable: true,
|
|
152
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
/** JSON `[{ minQuantity, percent }]` for volume tiers. */
|
|
156
|
+
tiers: {
|
|
157
|
+
order: 14,
|
|
158
|
+
fillable: true,
|
|
159
|
+
nullable: true,
|
|
160
|
+
type: 'text',
|
|
161
|
+
validation: { rule: schema.string() },
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
/** Free shipping only applies to a rate at or below this. */
|
|
165
|
+
maxShipping: {
|
|
166
|
+
order: 15,
|
|
167
|
+
fillable: true,
|
|
168
|
+
nullable: true,
|
|
169
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
minimumSubtotal: {
|
|
173
|
+
order: 16,
|
|
174
|
+
fillable: true,
|
|
175
|
+
nullable: true,
|
|
176
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
minimumQuantity: {
|
|
180
|
+
order: 17,
|
|
181
|
+
fillable: true,
|
|
182
|
+
nullable: true,
|
|
183
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
/** The most it takes off one order. */
|
|
187
|
+
maxAmount: {
|
|
188
|
+
order: 18,
|
|
189
|
+
fillable: true,
|
|
190
|
+
nullable: true,
|
|
191
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
combinesWithProduct: {
|
|
195
|
+
order: 19,
|
|
196
|
+
fillable: true,
|
|
197
|
+
default: false,
|
|
198
|
+
validation: { rule: schema.boolean() },
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
combinesWithOrder: {
|
|
202
|
+
order: 20,
|
|
203
|
+
fillable: true,
|
|
204
|
+
default: false,
|
|
205
|
+
validation: { rule: schema.boolean() },
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
combinesWithShipping: {
|
|
209
|
+
order: 21,
|
|
210
|
+
fillable: true,
|
|
211
|
+
default: false,
|
|
212
|
+
validation: { rule: schema.boolean() },
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
usageLimit: {
|
|
216
|
+
order: 22,
|
|
217
|
+
fillable: true,
|
|
218
|
+
nullable: true,
|
|
219
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
usageCount: {
|
|
223
|
+
order: 23,
|
|
224
|
+
fillable: true,
|
|
225
|
+
default: 0,
|
|
226
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
isActive: {
|
|
230
|
+
order: 24,
|
|
231
|
+
fillable: true,
|
|
232
|
+
default: true,
|
|
233
|
+
validation: { rule: schema.boolean() },
|
|
234
|
+
factory: () => true,
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
startsAt: {
|
|
238
|
+
order: 25,
|
|
239
|
+
fillable: true,
|
|
240
|
+
nullable: true,
|
|
241
|
+
validation: { rule: schema.string() },
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
endsAt: {
|
|
245
|
+
order: 26,
|
|
246
|
+
fillable: true,
|
|
247
|
+
nullable: true,
|
|
248
|
+
validation: { rule: schema.string() },
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
dashboard: {
|
|
253
|
+
highlight: false,
|
|
254
|
+
},
|
|
255
|
+
} as const)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { defineModel } from '@stacksjs/orm'
|
|
2
|
+
import { schema } from '@stacksjs/validation/runtime'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A one-time offer shown after checkout, on the order confirmation page:
|
|
6
|
+
* "Thank you for your purchase! Grab 5+ pairs and get 60% off". The shopper
|
|
7
|
+
* picks a variant and a quantity and adds it to the order they just placed,
|
|
8
|
+
* charged to the payment method they already used, without checking out again.
|
|
9
|
+
*
|
|
10
|
+
* `commerce.upsells` decides which offers an order qualifies for, prices them,
|
|
11
|
+
* claims an acceptance once per order and product, takes the payment and adds
|
|
12
|
+
* the line to the order in one transaction. Responses (views, acceptances,
|
|
13
|
+
* declines) are `PostPurchaseOfferResponse` rows, which is where conversion
|
|
14
|
+
* and revenue come from.
|
|
15
|
+
*/
|
|
16
|
+
export default defineModel({
|
|
17
|
+
name: 'PostPurchaseOffer',
|
|
18
|
+
table: 'post_purchase_offers',
|
|
19
|
+
primaryKey: 'id',
|
|
20
|
+
autoIncrement: true,
|
|
21
|
+
|
|
22
|
+
// Store configuration, managed by staff through `middleware`.
|
|
23
|
+
ownership: false,
|
|
24
|
+
|
|
25
|
+
traits: {
|
|
26
|
+
useUuid: true,
|
|
27
|
+
useTimestamps: true,
|
|
28
|
+
useSearch: {
|
|
29
|
+
displayable: ['id', 'name', 'headline', 'discountPercent', 'minQuantity', 'isActive', 'priority'],
|
|
30
|
+
searchable: ['name', 'headline'],
|
|
31
|
+
sortable: ['createdAt', 'updatedAt', 'priority', 'name'],
|
|
32
|
+
filterable: ['isActive'],
|
|
33
|
+
},
|
|
34
|
+
useSeeder: { count: 0 },
|
|
35
|
+
useApi: {
|
|
36
|
+
uri: 'post-purchase-offers',
|
|
37
|
+
middleware: ['auth'],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
hasMany: ['PostPurchaseOfferResponse'],
|
|
42
|
+
|
|
43
|
+
attributes: {
|
|
44
|
+
/** For staff: which offer this is in the dashboard. */
|
|
45
|
+
name: {
|
|
46
|
+
order: 1,
|
|
47
|
+
fillable: true,
|
|
48
|
+
required: true,
|
|
49
|
+
validation: { rule: schema.string().required().max(255) },
|
|
50
|
+
factory: () => 'Sock bundle upsell',
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/** The heading over the offer on the confirmation page. */
|
|
54
|
+
headline: {
|
|
55
|
+
order: 2,
|
|
56
|
+
fillable: true,
|
|
57
|
+
nullable: true,
|
|
58
|
+
validation: { rule: schema.string().max(255) },
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/** The offer in the shopper's words: "Grab 5+ pairs and get 60% off". */
|
|
62
|
+
message: {
|
|
63
|
+
order: 3,
|
|
64
|
+
fillable: true,
|
|
65
|
+
nullable: true,
|
|
66
|
+
type: 'text',
|
|
67
|
+
validation: { rule: schema.string().max(2000) },
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
/** JSON array of the product ids offered, in the order they are shown. */
|
|
71
|
+
productIds: {
|
|
72
|
+
order: 4,
|
|
73
|
+
fillable: true,
|
|
74
|
+
required: true,
|
|
75
|
+
type: 'text',
|
|
76
|
+
validation: { rule: schema.string().required() },
|
|
77
|
+
factory: () => '[]',
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/** Whole percent off the offered products' prices, 0 to 100. */
|
|
81
|
+
discountPercent: {
|
|
82
|
+
order: 5,
|
|
83
|
+
fillable: true,
|
|
84
|
+
default: 0,
|
|
85
|
+
validation: { rule: schema.number().integer().min(0).max(100) },
|
|
86
|
+
factory: () => 60,
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
/** Units a shopper has to take for the offer price. */
|
|
90
|
+
minQuantity: {
|
|
91
|
+
order: 6,
|
|
92
|
+
fillable: true,
|
|
93
|
+
default: 1,
|
|
94
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
95
|
+
factory: () => 5,
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
/** What the quantity field starts at. */
|
|
99
|
+
defaultQuantity: {
|
|
100
|
+
order: 7,
|
|
101
|
+
fillable: true,
|
|
102
|
+
default: 1,
|
|
103
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
104
|
+
factory: () => 5,
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/** The most units one acceptance may take. Empty: no ceiling but stock. */
|
|
108
|
+
maxQuantity: {
|
|
109
|
+
order: 8,
|
|
110
|
+
fillable: true,
|
|
111
|
+
nullable: true,
|
|
112
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
/** JSON array of product ids, one of which the order must contain. Empty: any order. */
|
|
116
|
+
triggerProductIds: {
|
|
117
|
+
order: 9,
|
|
118
|
+
fillable: true,
|
|
119
|
+
nullable: true,
|
|
120
|
+
type: 'text',
|
|
121
|
+
validation: { rule: schema.string() },
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
/** The order total, in minor units, an order needs to see the offer. */
|
|
125
|
+
minOrderTotal: {
|
|
126
|
+
order: 10,
|
|
127
|
+
fillable: true,
|
|
128
|
+
nullable: true,
|
|
129
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
/** Minutes after the order is placed that the offer stays open. */
|
|
133
|
+
windowMinutes: {
|
|
134
|
+
order: 11,
|
|
135
|
+
fillable: true,
|
|
136
|
+
default: 30,
|
|
137
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
138
|
+
factory: () => 30,
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
/** Higher shows first when an order qualifies for several. */
|
|
142
|
+
priority: {
|
|
143
|
+
order: 12,
|
|
144
|
+
fillable: true,
|
|
145
|
+
default: 0,
|
|
146
|
+
validation: { rule: schema.number().integer() },
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
isActive: {
|
|
150
|
+
order: 13,
|
|
151
|
+
fillable: true,
|
|
152
|
+
default: true,
|
|
153
|
+
validation: { rule: schema.boolean() },
|
|
154
|
+
factory: () => true,
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
startsAt: {
|
|
158
|
+
order: 14,
|
|
159
|
+
fillable: true,
|
|
160
|
+
nullable: true,
|
|
161
|
+
validation: { rule: schema.string() },
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
endsAt: {
|
|
165
|
+
order: 15,
|
|
166
|
+
fillable: true,
|
|
167
|
+
nullable: true,
|
|
168
|
+
validation: { rule: schema.string() },
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
dashboard: {
|
|
173
|
+
highlight: false,
|
|
174
|
+
},
|
|
175
|
+
} as const)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { defineModel } from '@stacksjs/orm'
|
|
2
|
+
import { schema } from '@stacksjs/validation/runtime'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What one order did with one product of a post-purchase offer.
|
|
6
|
+
*
|
|
7
|
+
* Written when the offer is shown (`viewed`) and moved on from there:
|
|
8
|
+
* `pending` while its payment is being taken, then `accepted` (the line is on
|
|
9
|
+
* the order), `failed` (the payment did not go through; the shopper may try
|
|
10
|
+
* again while the offer is open) or `declined`. The unique index makes the
|
|
11
|
+
* offer one-time: an order can take each product of an offer once, however
|
|
12
|
+
* many tabs or double clicks race for it.
|
|
13
|
+
*
|
|
14
|
+
* The line is snapshotted here before the payment is taken, so a payment that
|
|
15
|
+
* needs the shopper to authenticate (3-D Secure) can be finished later by the
|
|
16
|
+
* payment webhook with exactly the price that was shown.
|
|
17
|
+
*/
|
|
18
|
+
export default defineModel({
|
|
19
|
+
name: 'PostPurchaseOfferResponse',
|
|
20
|
+
table: 'post_purchase_offer_responses',
|
|
21
|
+
primaryKey: 'id',
|
|
22
|
+
autoIncrement: true,
|
|
23
|
+
|
|
24
|
+
indexes: [
|
|
25
|
+
{
|
|
26
|
+
name: 'post_purchase_offer_responses_order_offer_product_unique',
|
|
27
|
+
columns: ['order_id', 'post_purchase_offer_id', 'product_id'],
|
|
28
|
+
unique: true,
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
|
|
32
|
+
// Written by `commerce.upsells` on the order's behalf, read by staff.
|
|
33
|
+
ownership: false,
|
|
34
|
+
|
|
35
|
+
belongsTo: ['PostPurchaseOffer', 'Order'],
|
|
36
|
+
|
|
37
|
+
traits: {
|
|
38
|
+
// No personal attributes of its own: the person is on the order it points at.
|
|
39
|
+
gdpr: { subject: { via: 'Order' }, erasure: 'keep', basis: 'legal_obligation', purpose: 'Order additions, kept for tax and accounting' },
|
|
40
|
+
useTimestamps: true,
|
|
41
|
+
useSeeder: { count: 0 },
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
attributes: {
|
|
45
|
+
status: {
|
|
46
|
+
order: 1,
|
|
47
|
+
fillable: true,
|
|
48
|
+
required: true,
|
|
49
|
+
validation: { rule: schema.enum(['viewed', 'pending', 'accepted', 'declined', 'failed']).required() },
|
|
50
|
+
factory: () => 'viewed',
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
productId: {
|
|
54
|
+
order: 2,
|
|
55
|
+
fillable: true,
|
|
56
|
+
required: true,
|
|
57
|
+
validation: { rule: schema.number().integer().required() },
|
|
58
|
+
factory: () => 1,
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
variantId: {
|
|
62
|
+
order: 3,
|
|
63
|
+
fillable: true,
|
|
64
|
+
nullable: true,
|
|
65
|
+
validation: { rule: schema.number().integer() },
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
quantity: {
|
|
69
|
+
order: 4,
|
|
70
|
+
fillable: true,
|
|
71
|
+
nullable: true,
|
|
72
|
+
validation: { rule: schema.number().integer().min(1) },
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
/** The offer price of one unit, in minor units. */
|
|
76
|
+
unitPrice: {
|
|
77
|
+
order: 5,
|
|
78
|
+
fillable: true,
|
|
79
|
+
nullable: true,
|
|
80
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
/** The regular price of one unit the offer was taken off, in minor units. */
|
|
84
|
+
compareAtPrice: {
|
|
85
|
+
order: 6,
|
|
86
|
+
fillable: true,
|
|
87
|
+
nullable: true,
|
|
88
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
/** `unitPrice * quantity`: what was added to the order. */
|
|
92
|
+
amount: {
|
|
93
|
+
order: 7,
|
|
94
|
+
fillable: true,
|
|
95
|
+
nullable: true,
|
|
96
|
+
validation: { rule: schema.number().integer().min(0) },
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
currency: {
|
|
100
|
+
order: 8,
|
|
101
|
+
fillable: true,
|
|
102
|
+
nullable: true,
|
|
103
|
+
validation: { rule: schema.string().max(3) },
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
/** The provider's id for the payment (a PaymentIntent), when one was taken. */
|
|
107
|
+
paymentReference: {
|
|
108
|
+
order: 9,
|
|
109
|
+
fillable: true,
|
|
110
|
+
nullable: true,
|
|
111
|
+
validation: { rule: schema.string().max(255) },
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
paymentProvider: {
|
|
115
|
+
order: 10,
|
|
116
|
+
fillable: true,
|
|
117
|
+
nullable: true,
|
|
118
|
+
validation: { rule: schema.string().max(64) },
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
failureReason: {
|
|
122
|
+
order: 11,
|
|
123
|
+
fillable: true,
|
|
124
|
+
nullable: true,
|
|
125
|
+
validation: { rule: schema.string().max(1000) },
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
/** The `order_items` row the acceptance created. */
|
|
129
|
+
orderItemId: {
|
|
130
|
+
order: 12,
|
|
131
|
+
fillable: true,
|
|
132
|
+
nullable: true,
|
|
133
|
+
validation: { rule: schema.number().integer() },
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
respondedAt: {
|
|
137
|
+
order: 13,
|
|
138
|
+
fillable: true,
|
|
139
|
+
nullable: true,
|
|
140
|
+
validation: { rule: schema.string() },
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
dashboard: { enabled: false },
|
|
145
|
+
} as const)
|