|
1
|
import { RelationshipFilterConfig } from "./relationship-filters" |
|
2
|
import { |
|
3
|
ColumnCategory, |
|
4
|
ColumnDataType, |
|
5
|
FilterOperator, |
|
6
|
RenderMode, |
|
7
|
} from "./render-mode-mapper" |
|
8
|
|
|
9
|
/** |
|
10
|
* Filter configuration for an injected column. |
|
11
|
*/ |
|
12
|
export interface ComputedColumnFilter { |
|
13
|
enabled: boolean |
|
14
|
/** |
|
15
|
* Useful for customizations where you want to control the operators that are available for the filter. |
|
16
|
*/ |
|
17
|
operators?: FilterOperator[] |
|
18
|
/** |
|
19
|
* Relationship dropdown filter. `filter_key` overrides the query-param key |
|
20
|
* the filter posts (e.g. `"location_id"`); options are fetched from |
|
21
|
* `endpoint`. Omit for a plain scalar/enum filter. |
|
22
|
*/ |
|
23
|
relationship?: RelationshipFilterConfig |
|
24
|
enumValues?: string[] |
|
25
|
} |
|
26
|
|
|
27
|
/** |
|
28
|
* Definition of a column injected for an entity, in addition to the columns |
|
29
|
* discovered from its schema. Can be a computed DISPLAY column (provide |
|
30
|
* `renderMode` + `requiredFields`), a FILTER-only column (set `context: |
|
31
|
* "filter"` + `filter`), or both. |
|
32
|
*/ |
|
33
|
export interface ComputedColumnDefinition { |
|
34
|
/** |
|
35
|
* Unique identifier for the column (e.g., "customer_display"). |
|
36
|
*/ |
|
37
|
id: string |
|
38
|
|
|
39
|
/** |
|
40
|
* Display name for the column. |
|
41
|
*/ |
|
42
|
name: string |
|
43
|
|
|
44
|
/** |
|
45
|
* Render mode/type for the column. Required for display columns; omit for |
|
46
|
* filter-only columns. |
|
47
|
*/ |
|
48
|
renderMode?: RenderMode |
|
49
|
|
|
50
|
/** |
|
51
|
* Fields required to render this column. Omit for filter-only columns. |
|
52
|
*/ |
|
53
|
requiredFields?: string[] |
|
54
|
|
|
55
|
/** |
|
56
|
* Optional fields that enhance the rendering. |
|
57
|
*/ |
|
58
|
optionalFields?: string[] |
|
59
|
|
|
60
|
/** |
|
61
|
* Whether this column should be visible by default. |
|
62
|
*/ |
|
63
|
defaultVisible?: boolean |
|
64
|
|
|
65
|
/** |
|
66
|
* Description of the column. |
|
67
|
*/ |
|
68
|
description?: string |
|
69
|
|
|
70
|
/** |
|
71
|
* Category for grouping columns (e.g., "relationship", "metadata", "computed"). |
|
72
|
*/ |
|
73
|
category?: ColumnCategory |
|
74
|
|
|
75
|
/** |
|
76
|
* Metadata for the column. |
|
77
|
*/ |
|
78
|
metadata?: Record<string, any> |
|
79
|
|
|
80
|
/** |
|
81
|
* Column context: "display" (default), "filter" (filter-only, not rendered), |
|
82
|
* or "both". |
|
83
|
*/ |
|
84
|
context?: "display" | "filter" | "both" |
|
85
|
|
|
86
|
/** |
|
87
|
* Filter configuration. When provided with `enabled: true`, the column is |
|
88
|
* filterable (used for injecting filters the schema doesn't expose, e.g. a |
|
89
|
* field inside an array relationship). |
|
90
|
*/ |
|
91
|
filter?: ComputedColumnFilter |
|
92
|
|
|
93
|
/** |
|
94
|
* Column data type (default "string"). |
|
95
|
*/ |
|
96
|
dataType?: ColumnDataType |
|
97
|
|
|
98
|
/** |
|
99
|
* Whether the column is sortable (default false). |
|
100
|
*/ |
|
101
|
sortable?: boolean |
|
102
|
} |
|
103
|
|
|
104
|
/** |
|
105
|
* Built-in computed columns, keyed by the entity they belong to. |
|
106
|
*/ |
|
107
|
export const BUILTIN_COMPUTED_COLUMNS: Record< |
|
108
|
string, |
|
109
|
ComputedColumnDefinition[] |
|
110
|
> = { |
|
111
|
Order: [ |
|
112
|
{ |
|
113
|
id: "customer_display", |
|
114
|
name: "Customer", |
|
115
|
renderMode: "name", |
|
116
|
requiredFields: [ |
|
117
|
"customer.first_name", |
|
118
|
"customer.last_name", |
|
119
|
"customer.email", |
|
120
|
], |
|
121
|
optionalFields: ["customer.phone"], |
|
122
|
defaultVisible: true, |
|
123
|
description: "Customer name and contact information", |
|
124
|
category: "relationship", |
|
125
|
metadata: { |
|
126
|
name_source: "customer", |
|
127
|
fallback_fields: ["customer.email", "customer.phone"], |
|
128
|
empty_label_key: "customers.guest", |
|
129
|
empty_label: "Guest", |
|
130
|
}, |
|
131
|
}, |
|
132
|
{ |
|
133
|
id: "shipping_address_display", |
|
134
|
name: "Shipping Address", |
|
135
|
renderMode: "address", |
|
136
|
requiredFields: [ |
|
137
|
"shipping_address.city", |
|
138
|
"shipping_address.country_code", |
|
139
|
], |
|
140
|
optionalFields: [ |
|
141
|
"shipping_address.address_1", |
|
142
|
"shipping_address.province", |
|
143
|
"shipping_address.postal_code", |
|
144
|
], |
|
145
|
metadata: { |
|
146
|
address_field: "shipping_address", |
|
147
|
}, |
|
148
|
defaultVisible: false, |
|
149
|
description: "Shipping address summary", |
|
150
|
category: "relationship", |
|
151
|
}, |
|
152
|
{ |
|
153
|
id: "billing_address_display", |
|
154
|
name: "Billing Address", |
|
155
|
renderMode: "address", |
|
156
|
requiredFields: ["billing_address.city", "billing_address.country_code"], |
|
157
|
optionalFields: [ |
|
158
|
"billing_address.address_1", |
|
159
|
"billing_address.province", |
|
160
|
"billing_address.postal_code", |
|
161
|
], |
|
162
|
metadata: { |
|
163
|
address_field: "billing_address", |
|
164
|
}, |
|
165
|
defaultVisible: false, |
|
166
|
description: "Billing address summary", |
|
167
|
category: "relationship", |
|
168
|
}, |
|
169
|
{ |
|
170
|
id: "order_shipping_country_display", |
|
171
|
name: "Country", |
|
172
|
renderMode: "country_code", |
|
173
|
requiredFields: ["shipping_address.country_code"], |
|
174
|
optionalFields: [], |
|
175
|
defaultVisible: true, |
|
176
|
description: "Shipping country", |
|
177
|
category: "metadata", |
|
178
|
metadata: { |
|
179
|
country_code_field: "shipping_address.country_code", |
|
180
|
}, |
|
181
|
}, |
|
182
|
], |
|
183
|
|
|
184
|
Product: [ |
|
185
|
{ |
|
186
|
id: "product_display", |
|
187
|
name: "Product", |
|
188
|
renderMode: "product_info", |
|
189
|
requiredFields: ["title", "thumbnail"], |
|
190
|
optionalFields: ["handle"], |
|
191
|
defaultVisible: true, |
|
192
|
description: "Product title and thumbnail", |
|
193
|
category: "computed", |
|
194
|
}, |
|
195
|
{ |
|
196
|
id: "variants_count", |
|
197
|
name: "Variants", |
|
198
|
renderMode: "count", |
|
199
|
requiredFields: ["variants.id"], |
|
200
|
optionalFields: [], |
|
201
|
defaultVisible: true, |
|
202
|
description: "Number of product variants", |
|
203
|
category: "metric", |
|
204
|
metadata: { |
|
205
|
list_field: "variants", |
|
206
|
}, |
|
207
|
}, |
|
208
|
{ |
|
209
|
id: "categories_display", |
|
210
|
name: "Categories", |
|
211
|
renderMode: "badges", |
|
212
|
requiredFields: ["categories.name"], |
|
213
|
optionalFields: [], |
|
214
|
defaultVisible: false, |
|
215
|
description: "Product categories", |
|
216
|
category: "relationship", |
|
217
|
metadata: { |
|
218
|
display_field: "name", |
|
219
|
list_field: "categories", |
|
220
|
}, |
|
221
|
}, |
|
222
|
{ |
|
223
|
id: "sales_channels_display", |
|
224
|
name: "Sales Channels", |
|
225
|
renderMode: "badges", |
|
226
|
requiredFields: ["sales_channels.name"], |
|
227
|
optionalFields: [], |
|
228
|
defaultVisible: true, |
|
229
|
description: "Sales channels the product is available in", |
|
230
|
category: "relationship", |
|
231
|
metadata: { |
|
232
|
list_field: "sales_channels", |
|
233
|
display_field: "name", |
|
234
|
}, |
|
235
|
}, |
|
236
|
], |
|
237
|
|
|
238
|
Customer: [ |
|
239
|
{ |
|
240
|
id: "customer_name", |
|
241
|
name: "Name", |
|
242
|
renderMode: "name", |
|
243
|
requiredFields: ["first_name", "last_name"], |
|
244
|
optionalFields: [], |
|
245
|
defaultVisible: true, |
|
246
|
description: "Customer's full name", |
|
247
|
category: "computed", |
|
248
|
}, |
|
249
|
], |
|
250
|
|
|
251
|
CustomerGroup: [ |
|
252
|
{ |
|
253
|
id: "customers_count", |
|
254
|
name: "Customers", |
|
255
|
renderMode: "count", |
|
256
|
requiredFields: ["customers.id"], |
|
257
|
optionalFields: [], |
|
258
|
defaultVisible: true, |
|
259
|
description: "Number of customers in the group", |
|
260
|
category: "metric", |
|
261
|
metadata: { |
|
262
|
list_field: "customers", |
|
263
|
}, |
|
264
|
}, |
|
265
|
], |
|
266
|
|
|
267
|
PriceList: [ |
|
268
|
{ |
|
269
|
id: "price_overrides", |
|
270
|
name: "Price overrides", |
|
271
|
renderMode: "price_overrides_count", |
|
272
|
requiredFields: [], |
|
273
|
optionalFields: [], |
|
274
|
defaultVisible: true, |
|
275
|
description: "Number of price overrides in the price list", |
|
276
|
category: "metric", |
|
277
|
}, |
|
278
|
], |
|
279
|
|
|
280
|
ProductCollection: [ |
|
281
|
{ |
|
282
|
id: "products_count", |
|
283
|
name: "Products", |
|
284
|
renderMode: "count", |
|
285
|
requiredFields: ["products.id"], |
|
286
|
optionalFields: [], |
|
287
|
defaultVisible: true, |
|
288
|
description: "Number of products in the collection", |
|
289
|
category: "metric", |
|
290
|
metadata: { |
|
291
|
list_field: "products", |
|
292
|
}, |
|
293
|
}, |
|
294
|
], |
|
295
|
|
|
296
|
ProductOption: [ |
|
297
|
{ |
|
298
|
id: "values_count", |
|
299
|
name: "Values", |
|
300
|
renderMode: "count", |
|
301
|
requiredFields: ["values.id"], |
|
302
|
optionalFields: [], |
|
303
|
defaultVisible: true, |
|
304
|
description: "Number of values for the option", |
|
305
|
category: "metric", |
|
306
|
metadata: { |
|
307
|
list_field: "values", |
|
308
|
}, |
|
309
|
}, |
|
310
|
], |
|
311
|
|
|
312
|
Promotion: [ |
|
313
|
{ |
|
314
|
id: "method", |
|
315
|
name: "Method", |
|
316
|
renderMode: "promotion_method", |
|
317
|
requiredFields: ["is_automatic"], |
|
318
|
optionalFields: [], |
|
319
|
defaultVisible: true, |
|
320
|
description: "Whether the promotion is automatic or code based", |
|
321
|
category: "computed", |
|
322
|
}, |
|
323
|
{ |
|
324
|
id: "status_display", |
|
325
|
name: "Status", |
|
326
|
renderMode: "status", |
|
327
|
requiredFields: [ |
|
328
|
"status", |
|
329
|
"campaign.starts_at", |
|
330
|
"campaign.ends_at", |
|
331
|
"campaign.budget.type", |
|
332
|
"campaign.budget.limit", |
|
333
|
"campaign.budget.used", |
|
334
|
], |
|
335
|
optionalFields: [], |
|
336
|
defaultVisible: true, |
|
337
|
description: "Promotion status (derived from status and campaign)", |
|
338
|
metadata: { resolver: "promotion_status" }, |
|
339
|
category: "computed", |
|
340
|
}, |
|
341
|
], |
|
342
|
|
|
343
|
Region: [ |
|
344
|
{ |
|
345
|
id: "region_country_display", |
|
346
|
name: "Countries", |
|
347
|
renderMode: "region_countries", |
|
348
|
requiredFields: ["countries.iso_2"], |
|
349
|
optionalFields: [], |
|
350
|
defaultVisible: true, |
|
351
|
description: "Countries in the region", |
|
352
|
category: "relationship", |
|
353
|
}, |
|
354
|
{ |
|
355
|
id: "payment_providers_display", |
|
356
|
name: "Payment providers", |
|
357
|
renderMode: "region_payment_providers", |
|
358
|
requiredFields: ["payment_providers.id"], |
|
359
|
optionalFields: [], |
|
360
|
defaultVisible: true, |
|
361
|
description: "Payment providers enabled for the region", |
|
362
|
category: "relationship", |
|
363
|
}, |
|
364
|
], |
|
365
|
|
|
366
|
StockLocation: [ |
|
367
|
{ |
|
368
|
id: "location_address_display", |
|
369
|
name: "Address", |
|
370
|
renderMode: "address", |
|
371
|
requiredFields: ["address.*"], |
|
372
|
optionalFields: [], |
|
373
|
metadata: { |
|
374
|
address_field: "address", |
|
375
|
}, |
|
376
|
defaultVisible: true, |
|
377
|
description: "Formatted stock location address", |
|
378
|
category: "relationship", |
|
379
|
}, |
|
380
|
{ |
|
381
|
id: "location_country_display", |
|
382
|
name: "Country", |
|
383
|
renderMode: "country_code", |
|
384
|
requiredFields: ["address.country_code"], |
|
385
|
optionalFields: [], |
|
386
|
defaultVisible: true, |
|
387
|
description: "Stock location country", |
|
388
|
category: "metadata", |
|
389
|
metadata: { |
|
390
|
country_code_field: "address.country_code", |
|
391
|
}, |
|
392
|
}, |
|
393
|
{ |
|
394
|
id: "shipping_fulfillment", |
|
395
|
name: "Shipping", |
|
396
|
renderMode: "stock_location_shipping", |
|
397
|
requiredFields: ["fulfillment_sets.type"], |
|
398
|
optionalFields: [], |
|
399
|
defaultVisible: true, |
|
400
|
description: "Whether shipping fulfillment is enabled", |
|
401
|
category: "metadata", |
|
402
|
}, |
|
403
|
{ |
|
404
|
id: "pickup_fulfillment", |
|
405
|
name: "Pickup", |
|
406
|
renderMode: "stock_location_pickup", |
|
407
|
requiredFields: ["fulfillment_sets.type"], |
|
408
|
optionalFields: [], |
|
409
|
defaultVisible: true, |
|
410
|
description: "Whether pickup fulfillment is enabled", |
|
411
|
category: "metadata", |
|
412
|
}, |
|
413
|
{ |
|
414
|
id: "location_sales_channels", |
|
415
|
name: "Connected sales channels", |
|
416
|
renderMode: "stock_location_sales_channels", |
|
417
|
requiredFields: ["sales_channels.name"], |
|
418
|
optionalFields: [], |
|
419
|
defaultVisible: true, |
|
420
|
description: "Sales channels served by the location", |
|
421
|
category: "relationship", |
|
422
|
}, |
|
423
|
], |
|
424
|
|
|
425
|
InventoryItem: [ |
|
426
|
{ |
|
427
|
// Filter-only injected column: inventory items reach a location through |
|
428
|
// the `location_levels` array relationship, which the generator can't |
|
429
|
// turn into a usable "location" filter. |
|
430
|
id: "inventory_location_filter", |
|
431
|
name: "Location", |
|
432
|
context: "filter", |
|
433
|
defaultVisible: false, |
|
434
|
description: "Stock location", |
|
435
|
category: "relationship", |
|
436
|
filter: { |
|
437
|
enabled: true, |
|
438
|
operators: ["in"], |
|
439
|
relationship: { |
|
440
|
entity: "StockLocation", |
|
441
|
value_field: "id", |
|
442
|
display_field: "name", |
|
443
|
multiple: true, |
|
444
|
endpoint: "/admin/stock-locations", |
|
445
|
filter_key: "location_levels.location_id", |
|
446
|
}, |
|
447
|
}, |
|
448
|
}, |
|
449
|
], |
|
450
|
|
|
451
|
ReservationItem: [ |
|
452
|
{ |
|
453
|
id: "reservation_location_filter", |
|
454
|
name: "Location", |
|
455
|
context: "filter", |
|
456
|
defaultVisible: false, |
|
457
|
description: "Stock location", |
|
458
|
category: "relationship", |
|
459
|
filter: { |
|
460
|
enabled: true, |
|
461
|
operators: ["in"], |
|
462
|
relationship: { |
|
463
|
entity: "StockLocation", |
|
464
|
value_field: "id", |
|
465
|
display_field: "name", |
|
466
|
multiple: true, |
|
467
|
endpoint: "/admin/stock-locations", |
|
468
|
filter_key: "location_id", |
|
469
|
}, |
|
470
|
}, |
|
471
|
}, |
|
472
|
], |
|
473
|
} |
|
474
|
|
|
475
|
/** |
|
476
|
* Registry for computed columns. |
|
477
|
* Allows registration of custom computed columns. |
|
478
|
*/ |
|
479
|
export class ComputedColumnRegistry { |
|
480
|
private columns: Map<string, ComputedColumnDefinition[]> = new Map() |
|
481
|
|
|
482
|
constructor() { |
|
483
|
// Register built-in columns |
|
484
|
for (const [entity, columns] of Object.entries(BUILTIN_COMPUTED_COLUMNS)) { |
|
485
|
this.register(entity, columns) |
|
486
|
} |
|
487
|
} |
|
488
|
|
|
489
|
/** |
|
490
|
* Register computed columns for an entity. Columns are appended to any |
|
491
|
* already registered for the entity; a column sharing an existing `id` |
|
492
|
* replaces it, keeping registration idempotent. |
|
493
|
*/ |
|
494
|
register(entity: string, columns: ComputedColumnDefinition[]): void { |
|
495
|
const byId = new Map(this.get(entity).map((column) => [column.id, column])) |
|
496
|
for (const column of columns) { |
|
497
|
byId.set(column.id, column) |
|
498
|
} |
|
499
|
this.columns.set(entity, Array.from(byId.values())) |
|
500
|
} |
|
501
|
|
|
502
|
/** |
|
503
|
* Get all computed columns for an entity. |
|
504
|
*/ |
|
505
|
get(entityName: string): ComputedColumnDefinition[] { |
|
506
|
return this.columns.get(entityName) ?? [] |
|
507
|
} |
|
508
|
} |
|
509
|
|
|
510
|
// Singleton instance |
|
511
|
let registryInstance: ComputedColumnRegistry | null = null |
|
512
|
|
|
513
|
/** |
|
514
|
* Get the computed column registry singleton. |
|
515
|
*/ |
|
516
|
export function getComputedColumnRegistry(): ComputedColumnRegistry { |
|
517
|
if (!registryInstance) { |
|
518
|
registryInstance = new ComputedColumnRegistry() |
|
519
|
} |
|
520
|
return registryInstance |
|
521
|
} |