@jsb188/mday 1.1.7 → 1.1.10

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.
Files changed (45) hide show
  1. package/package.json +39 -20
  2. package/src/constants/dataTable.ts +22 -0
  3. package/src/constants/email.ts +28 -0
  4. package/src/constants/inboundContact.ts +8 -0
  5. package/src/constants/integration.ts +14 -0
  6. package/src/constants/log-activityTexts.ts +54 -28
  7. package/src/constants/log.ts +82 -38
  8. package/src/constants/organization.ts +11 -2
  9. package/src/constants/product.ts +9 -33
  10. package/src/constants/report.ts +29 -10
  11. package/src/constants/sheet.ts +20 -0
  12. package/src/constants/square.ts +16 -0
  13. package/src/constants/storage.ts +12 -0
  14. package/src/schemas/editLogEntry.ts +90 -44
  15. package/src/schemas/inboundContact.ts +72 -0
  16. package/src/schemas/logsQuery.ts +7 -272
  17. package/src/schemas/organization.ts +11 -174
  18. package/src/schemas/productsQuery.ts +2 -94
  19. package/src/schemas/reportsQuery.ts +8 -95
  20. package/src/types/account.d.ts +78 -0
  21. package/src/types/dataTable.d.ts +447 -0
  22. package/src/types/email.d.ts +71 -0
  23. package/src/types/inboundContact.d.ts +56 -0
  24. package/src/types/integration.d.ts +42 -0
  25. package/src/types/log.d.ts +289 -178
  26. package/src/types/organization.d.ts +106 -50
  27. package/src/types/product.d.ts +7 -96
  28. package/src/types/report.d.ts +239 -152
  29. package/src/types/sheet.d.ts +298 -0
  30. package/src/types/storage.d.ts +161 -0
  31. package/src/types/workflow.d.ts +174 -0
  32. package/src/utils/dataTable.ts +834 -0
  33. package/src/utils/email.ts +113 -0
  34. package/src/utils/log.ts +30 -628
  35. package/src/utils/organization.ts +227 -44
  36. package/src/utils/product-calEvent.ts +7 -60
  37. package/src/utils/report.ts +158 -0
  38. package/src/utils/sheet.ts +170 -0
  39. package/src/utils/square.ts +22 -0
  40. package/src/utils/table-route.ts +146 -0
  41. package/src/utils/workflow.ts +212 -0
  42. package/tsconfig.json +12 -0
  43. package/src/schemas/childOrganizationsQuery.ts +0 -44
  44. package/src/types/action.d.ts +0 -42
  45. package/src/utils/route-helpers.ts +0 -583
@@ -0,0 +1,834 @@
1
+ import { formatCalDateInTimeZone, parseDateInTimezone } from '@jsb188/app/utils/timeZone.ts';
2
+ import {
3
+ DATA_TABLE_COLUMN_MAX_WIDTH,
4
+ DATA_TABLE_COLUMN_MIN_WIDTH,
5
+ } from '../constants/dataTable.ts';
6
+ import type {
7
+ DataTableDesignCellObj,
8
+ DataTableDesignObj,
9
+ DataTableDesignViewColumnObj,
10
+ DataTableDesignViewGeneratorDimensionObj,
11
+ DataTableDesignViewObj,
12
+ DataTableFieldTypeEnum,
13
+ DataTableRecordValue,
14
+ } from '../types/dataTable.d.ts';
15
+
16
+ export type GeneratedDataTableViewRowDefinition = {
17
+ viewRowKey: string;
18
+ position: number;
19
+ cellValues: Record<string, string | number>;
20
+ criteria: {
21
+ date?: {
22
+ key: string;
23
+ value: string;
24
+ grain: 'DAY' | 'WEEK';
25
+ sourceCellKey: string;
26
+ };
27
+ dimensions: Array<{
28
+ key: string;
29
+ value: string;
30
+ sourceCellKey?: string | null;
31
+ }>;
32
+ };
33
+ };
34
+
35
+ /*
36
+ * Keep a user-resized dataTable column width inside the usable spreadsheet range.
37
+ */
38
+
39
+ export function clampDataTableColumnWidth(width: number) {
40
+ return Math.min(DATA_TABLE_COLUMN_MAX_WIDTH, Math.max(DATA_TABLE_COLUMN_MIN_WIDTH, Math.round(width)));
41
+ }
42
+
43
+ /*
44
+ * Check whether a value is a non-array object.
45
+ */
46
+
47
+ function isPlainObject(value: any): value is Record<string, any> {
48
+ return !!value && typeof value === 'object' && !(value instanceof Date) && !Array.isArray(value);
49
+ }
50
+
51
+ /*
52
+ * Return whether one dataTable view column reads from a master dataTable cell.
53
+ */
54
+
55
+ export function isDataTableViewMasterCellColumn(column: DataTableDesignViewColumnObj | null | undefined) {
56
+ return column?.source?.type === 'MASTER_CELL' && typeof column.source.cellKey === 'string' && !!column.source.cellKey;
57
+ }
58
+
59
+ /*
60
+ * Return whether one dataTable field type stores and compares values as numbers.
61
+ */
62
+
63
+ export function isDataTableNumberLikeFieldType(fieldType: DataTableFieldTypeEnum | null | undefined) {
64
+ return fieldType === 'NUMBER' || fieldType === 'PRICE';
65
+ }
66
+
67
+ /*
68
+ * Return whether one dataTable field type stores and compares values as dates.
69
+ */
70
+
71
+ export function isDataTableDateLikeFieldType(fieldType: DataTableFieldTypeEnum | null | undefined) {
72
+ return fieldType === 'DATE' || fieldType === 'WEEK_OF_MON' || fieldType === 'WEEK_OF_SUN';
73
+ }
74
+
75
+ /*
76
+ * Return whether one dataTable field type displays a calendar week range.
77
+ */
78
+
79
+ export function isDataTableWeekFieldType(fieldType: DataTableFieldTypeEnum | null | undefined) {
80
+ return fieldType === 'WEEK_OF_MON' || fieldType === 'WEEK_OF_SUN';
81
+ }
82
+
83
+ /*
84
+ * Return the UTC day-of-week index used as the start for a week field type.
85
+ */
86
+
87
+ function getDataTableWeekStartDay(fieldType: DataTableFieldTypeEnum | null | undefined) {
88
+ return fieldType === 'WEEK_OF_SUN' ? 0 : 1;
89
+ }
90
+
91
+ /*
92
+ * Return the scalar value from a dataTable record-like object input.
93
+ */
94
+
95
+ function getDataTableRecordScalarValue(value: DataTableRecordValue | Date) {
96
+ if (isPlainObject(value) && 'value' in value) {
97
+ return value.value as DataTableRecordValue;
98
+ }
99
+
100
+ return value;
101
+ }
102
+
103
+ /*
104
+ * Return whether one value is a valid finite number input for a dataTable cell.
105
+ */
106
+
107
+ function isValidDataTableNumberValue(value: DataTableRecordValue) {
108
+ const numberValue = typeof value === 'number' ? value : Number(value);
109
+
110
+ return Number.isFinite(numberValue);
111
+ }
112
+
113
+ /*
114
+ * Return whether a date string points at a real calendar date.
115
+ */
116
+
117
+ function isValidDataTableDateKey(value: string) {
118
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
119
+ return false;
120
+ }
121
+
122
+ const [year, month, day] = value.split('-').map(Number);
123
+ const date = new Date(Date.UTC(year, month - 1, day));
124
+
125
+ return date.getUTCFullYear() === year &&
126
+ date.getUTCMonth() === month - 1 &&
127
+ date.getUTCDate() === day;
128
+ }
129
+
130
+ /*
131
+ * Return a UTC date parsed from a YYYY-MM-DD string.
132
+ */
133
+
134
+ function parseDateKey(value: string | null | undefined) {
135
+ if (!value || !/^\d{4}-\d{2}-\d{2}$/.test(value)) {
136
+ return null;
137
+ }
138
+
139
+ const [year, month, day] = value.split('-').map(Number);
140
+ const date = new Date(Date.UTC(year, month - 1, day));
141
+
142
+ if (
143
+ date.getUTCFullYear() !== year ||
144
+ date.getUTCMonth() !== month - 1 ||
145
+ date.getUTCDate() !== day
146
+ ) {
147
+ return null;
148
+ }
149
+
150
+ return date;
151
+ }
152
+
153
+ /*
154
+ * Return a YYYY-MM-DD string for one date using UTC calendar fields.
155
+ */
156
+
157
+ function formatDateKey(date: Date) {
158
+ const year = date.getUTCFullYear();
159
+ const month = String(date.getUTCMonth() + 1).padStart(2, '0');
160
+ const day = String(date.getUTCDate()).padStart(2, '0');
161
+
162
+ return `${year}-${month}-${day}`;
163
+ }
164
+
165
+ /*
166
+ * Return a new UTC date offset by a number of days.
167
+ */
168
+
169
+ function addDays(date: Date, days: number) {
170
+ const nextDate = new Date(date.getTime());
171
+ nextDate.setUTCDate(nextDate.getUTCDate() + days);
172
+
173
+ return nextDate;
174
+ }
175
+
176
+ /*
177
+ * Return whether one value can be stored as a dataTable date cell.
178
+ */
179
+
180
+ function isValidDataTableDateValue(value: DataTableRecordValue | Date) {
181
+ const scalarValue = getDataTableRecordScalarValue(value);
182
+
183
+ if (scalarValue instanceof Date) {
184
+ return Number.isFinite(scalarValue.getTime());
185
+ }
186
+
187
+ if (isValidDataTableDateKey(String(scalarValue).split('T')[0])) {
188
+ return true;
189
+ }
190
+
191
+ return Number.isFinite(new Date(String(scalarValue)).getTime());
192
+ }
193
+
194
+ /*
195
+ * Return the calendar date key represented by one date value in a timezone.
196
+ */
197
+
198
+ function getDataTableDateValueCalendarKey(value: DataTableRecordValue | Date, timeZone?: string | null) {
199
+ const scalarValue = getDataTableRecordScalarValue(value);
200
+
201
+ if (scalarValue instanceof Date) {
202
+ return formatCalDateInTimeZone(scalarValue, timeZone || null);
203
+ }
204
+
205
+ const stringValue = String(scalarValue);
206
+ if (isValidDataTableDateKey(stringValue)) {
207
+ return stringValue;
208
+ }
209
+
210
+ const parsedDate = parseDateInTimezone(stringValue, timeZone, 0, stringValue.includes('T'));
211
+ if (Number.isFinite(parsedDate.getTime())) {
212
+ return formatCalDateInTimeZone(parsedDate, timeZone || null);
213
+ }
214
+
215
+ return stringValue.split('T')[0];
216
+ }
217
+
218
+ /*
219
+ * Return the canonical date key for a dataTable date-like value.
220
+ */
221
+
222
+ export function getDataTableDateValueKey(value: DataTableRecordValue | Date) {
223
+ const scalarValue = getDataTableRecordScalarValue(value);
224
+
225
+ return scalarValue instanceof Date ? scalarValue.toISOString().split('T')[0] : String(scalarValue).split('T')[0];
226
+ }
227
+
228
+ /*
229
+ * Return a date string normalized to the configured dataTable week start.
230
+ */
231
+
232
+ export function normalizeDataTableWeekDateValue(value: DataTableRecordValue | Date, fieldType: DataTableFieldTypeEnum, timeZone?: string | null) {
233
+ const dateKey = getDataTableDateValueCalendarKey(value, timeZone);
234
+ const date = parseDateKey(dateKey);
235
+ if (!date || !isDataTableWeekFieldType(fieldType)) {
236
+ return dateKey;
237
+ }
238
+
239
+ const startDay = getDataTableWeekStartDay(fieldType);
240
+ const offset = (date.getUTCDay() - startDay + 7) % 7;
241
+
242
+ return formatDateKey(addDays(date, -offset));
243
+ }
244
+
245
+ /*
246
+ * Return a date string normalized for storage by one date-like dataTable field.
247
+ */
248
+
249
+ export function normalizeDataTableDateLikeValue(value: DataTableRecordValue | Date, fieldType: DataTableFieldTypeEnum, timeZone?: string | null) {
250
+ return isDataTableWeekFieldType(fieldType) ? normalizeDataTableWeekDateValue(value, fieldType, timeZone) : getDataTableDateValueCalendarKey(value, timeZone);
251
+ }
252
+
253
+ /*
254
+ * Return the canonical ISO datetime string for a dataTable datetime value in one timezone.
255
+ */
256
+
257
+ export function normalizeDataTableDateTimeValue(value: DataTableRecordValue | Date, timeZone?: string | null) {
258
+ const scalarValue = getDataTableRecordScalarValue(value);
259
+
260
+ return parseDateInTimezone(
261
+ scalarValue instanceof Date ? scalarValue : String(scalarValue),
262
+ timeZone,
263
+ 0,
264
+ typeof scalarValue === 'string',
265
+ ).toISOString();
266
+ }
267
+
268
+ /*
269
+ * Return whether one value can be stored as a dataTable datetime cell.
270
+ */
271
+
272
+ function isValidDataTableDateTimeValue(value: DataTableRecordValue) {
273
+ const scalarValue = getDataTableRecordScalarValue(value);
274
+ const date = scalarValue instanceof Date ? scalarValue : new Date(String(scalarValue));
275
+
276
+ return Number.isFinite(date.getTime());
277
+ }
278
+
279
+ /*
280
+ * Return whether one value is included in a dataTable cell's saved options.
281
+ */
282
+
283
+ function isDataTableOptionValue(cell: DataTableDesignCellObj, value: unknown) {
284
+ const validValues = new Set((cell.options || []).map((option) => String(option.value)));
285
+
286
+ return validValues.has(String(value));
287
+ }
288
+
289
+ /*
290
+ * Return the normalized string used for case-insensitive dataTable option matching.
291
+ */
292
+
293
+ function getNormalizedDataTableOptionText(value: unknown) {
294
+ return String(value).toLowerCase();
295
+ }
296
+
297
+ /*
298
+ * Return whether one dataTable design cell should normalize select option values.
299
+ */
300
+
301
+ function isDataTableSelectOptionNormalizeField(cell: DataTableDesignCellObj) {
302
+ return cell.fieldType === 'SELECT' ||
303
+ cell.fieldType === 'SELECT_OR_TEXT' ||
304
+ cell.humanFieldType === 'SELECT' ||
305
+ cell.humanFieldType === 'SELECT_OR_TEXT';
306
+ }
307
+
308
+ /*
309
+ * Return the saved option value when one select-style cell input matches an option value or label.
310
+ */
311
+
312
+ export function normalizeDataTableSelectOptionValue(cell: DataTableDesignCellObj, value: DataTableRecordValue) {
313
+ if (value === null || !isDataTableSelectOptionNormalizeField(cell)) {
314
+ return value;
315
+ }
316
+
317
+ if (isDataTableOptionValue(cell, value)) {
318
+ return value;
319
+ }
320
+
321
+ const normalizedValue = getNormalizedDataTableOptionText(value);
322
+ const option = (cell.options || []).find((item) => (
323
+ getNormalizedDataTableOptionText(item.value) === normalizedValue ||
324
+ getNormalizedDataTableOptionText(item.label) === normalizedValue
325
+ ));
326
+
327
+ return option ? option.value : value;
328
+ }
329
+
330
+ /*
331
+ * Return a user-readable validation error for an invalid dataTable cell value.
332
+ */
333
+
334
+ export function getDataTableCellValueValidationError(
335
+ cell: DataTableDesignCellObj,
336
+ value: DataTableRecordValue,
337
+ ) {
338
+ if (value === null) {
339
+ return null;
340
+ }
341
+
342
+ if (isDataTableNumberLikeFieldType(cell.fieldType) && !isValidDataTableNumberValue(value)) {
343
+ return `${cell.key} must be a valid number.`;
344
+ }
345
+
346
+ if (isDataTableDateLikeFieldType(cell.fieldType) && !isValidDataTableDateValue(value)) {
347
+ return `${cell.key} must be a valid date.`;
348
+ }
349
+
350
+ if (cell.fieldType === 'DATETIME' && !isValidDataTableDateTimeValue(value)) {
351
+ return `${cell.key} must be a valid datetime.`;
352
+ }
353
+
354
+ if (cell.fieldType === 'SELECT' && cell.options?.length && !isDataTableOptionValue(cell, value)) {
355
+ return `${cell.key} must be one of: ${(cell.options || []).map((option) => option.value).join(', ')}`;
356
+ }
357
+
358
+ if (cell.fieldType === 'SELECT_OR_TEXT' && !isDataTableOptionValue(cell, value) && typeof value !== 'string') {
359
+ const optionsText = (cell.options || []).map((option) => option.value).join(', ');
360
+ return optionsText ? `${cell.key} must be one of: ${optionsText}, or a text string.` : `${cell.key} must be a text string.`;
361
+ }
362
+
363
+ if (cell.fieldType === 'MULTI_SELECT') {
364
+ if (!Array.isArray(value)) {
365
+ return `${cell.key} must be an array of option values.`;
366
+ }
367
+
368
+ if (cell.options?.length) {
369
+ const invalidValues = value.filter((item) => !isDataTableOptionValue(cell, item));
370
+ if (invalidValues.length) {
371
+ return `${cell.key} contains invalid option values: ${invalidValues.map((item) => String(item)).join(', ')}. ` +
372
+ `Allowed values: ${(cell.options || []).map((option) => option.value).join(', ')}`;
373
+ }
374
+ }
375
+ }
376
+
377
+ return null;
378
+ }
379
+
380
+ /*
381
+ * Return a compact month-day string for dataTable week display labels.
382
+ */
383
+
384
+ function getDataTableWeekDisplayMonthDay(date: Date, showMonth: boolean) {
385
+ const day = String(date.getUTCDate());
386
+ if (!showMonth) {
387
+ return day;
388
+ }
389
+
390
+ const month = date.toLocaleString('en-US', {
391
+ month: 'short',
392
+ timeZone: 'UTC',
393
+ });
394
+
395
+ return `${month} ${day}`;
396
+ }
397
+
398
+ /*
399
+ * Return the display text for one date as a dataTable week range.
400
+ */
401
+
402
+ export function formatDataTableWeekDateRange(value: DataTableRecordValue | Date, fieldType: DataTableFieldTypeEnum) {
403
+ if (!isDataTableWeekFieldType(fieldType)) {
404
+ return '';
405
+ }
406
+
407
+ const start = parseDateKey(normalizeDataTableWeekDateValue(value, fieldType));
408
+ if (!start) {
409
+ return '';
410
+ }
411
+
412
+ const end = addDays(start, 6);
413
+ const startText = getDataTableWeekDisplayMonthDay(start, true);
414
+ const endText = getDataTableWeekDisplayMonthDay(end, start.getUTCMonth() !== end.getUTCMonth());
415
+
416
+ return `${startText} - ${endText}`;
417
+ }
418
+
419
+ /*
420
+ * Return whether one saved view has a usable generated grouped-row model.
421
+ */
422
+
423
+ export function isDataTableViewGeneratedRowsView(view: DataTableDesignViewObj | null | undefined) {
424
+ return view?.rowModel?.type === 'GROUPED_ROWS' &&
425
+ typeof view.rowModel.generator?.keyPrefix === 'string' &&
426
+ !!view.rowModel.generator.keyPrefix;
427
+ }
428
+
429
+ /*
430
+ * Return validated stored views from a dataTable design object.
431
+ */
432
+
433
+ export function getDataTableDesignViews(design: DataTableDesignObj | null | undefined) {
434
+ const views = design?.views;
435
+ if (!Array.isArray(views)) {
436
+ return [];
437
+ }
438
+
439
+ return views.filter((view) => {
440
+ return (
441
+ isPlainObject(view) &&
442
+ typeof view.id === 'string' &&
443
+ !!view.id &&
444
+ typeof view.name === 'string' &&
445
+ !!view.name &&
446
+ view.layout === 'GRID' &&
447
+ Array.isArray(view.columns)
448
+ );
449
+ }) as DataTableDesignViewObj[];
450
+ }
451
+
452
+ /*
453
+ * Return the stored views in the order that dataTable tabs should display them.
454
+ */
455
+
456
+ export function getOrderedDataTableDesignViews(design: DataTableDesignObj | null | undefined) {
457
+ const views = getDataTableDesignViews(design);
458
+ const viewsById = new Map(views.map((view) => [view.id, view]));
459
+ const orderedViews = (design?.viewsOrder || [])
460
+ .map((viewId) => viewsById.get(viewId))
461
+ .filter(Boolean) as DataTableDesignViewObj[];
462
+ const orderedIds = new Set(orderedViews.map((view) => view.id));
463
+ const remainingViews = views.filter((view) => !orderedIds.has(view.id));
464
+
465
+ return orderedViews.concat(remainingViews);
466
+ }
467
+
468
+ /*
469
+ * Return the displayable columns for one dataTable view in saved order.
470
+ */
471
+
472
+ export function getOrderedDataTableDesignViewColumns(view: DataTableDesignViewObj | null | undefined) {
473
+ const columns = Array.isArray(view?.columns) ? view.columns : [];
474
+ const columnsByKey = new Map(columns.map((column) => [column.key, column]));
475
+ const orderedColumns = (view?.columnsOrder || [])
476
+ .map((columnKey) => columnsByKey.get(columnKey))
477
+ .filter(Boolean) as DataTableDesignViewColumnObj[];
478
+ const orderedKeys = new Set(orderedColumns.map((column) => column.key));
479
+ const remainingColumns = columns.filter((column) => !orderedKeys.has(column.key));
480
+
481
+ return orderedColumns.concat(remainingColumns);
482
+ }
483
+
484
+ /*
485
+ * Return every column key once, honoring a saved order before appending missing keys.
486
+ */
487
+
488
+ export function getCompleteDataTableColumnOrder(
489
+ columnKeys: string[],
490
+ savedOrder?: string[] | null,
491
+ ) {
492
+ const validKeys = new Set(columnKeys);
493
+ const orderedKeys: string[] = [];
494
+ const seenKeys = new Set<string>();
495
+
496
+ (savedOrder || []).forEach((key) => {
497
+ if (!validKeys.has(key) || seenKeys.has(key)) {
498
+ return;
499
+ }
500
+
501
+ orderedKeys.push(key);
502
+ seenKeys.add(key);
503
+ });
504
+
505
+ columnKeys.forEach((key) => {
506
+ if (seenKeys.has(key)) {
507
+ return;
508
+ }
509
+
510
+ orderedKeys.push(key);
511
+ seenKeys.add(key);
512
+ });
513
+
514
+ return orderedKeys;
515
+ }
516
+
517
+ /*
518
+ * Move one visible dataTable column while preserving hidden or unrendered key slots.
519
+ */
520
+
521
+ export function moveVisibleDataTableColumnKeyInOrder(params: {
522
+ allColumnKeys: string[];
523
+ fromKey: string;
524
+ savedOrder?: string[] | null;
525
+ toVisibleIndex: number;
526
+ visibleColumnKeys: string[];
527
+ }) {
528
+ const fullOrder = getCompleteDataTableColumnOrder(params.allColumnKeys, params.savedOrder);
529
+ const visibleKeySet = new Set(params.visibleColumnKeys);
530
+ const visibleOrder = fullOrder.filter((key) => visibleKeySet.has(key));
531
+ const fromIndex = visibleOrder.indexOf(params.fromKey);
532
+
533
+ if (fromIndex < 0) {
534
+ return fullOrder;
535
+ }
536
+
537
+ const nextVisibleOrder = visibleOrder.slice(0);
538
+ const [movedKey] = nextVisibleOrder.splice(fromIndex, 1);
539
+ const toIndex = Math.max(0, Math.min(params.toVisibleIndex, nextVisibleOrder.length));
540
+
541
+ if (!movedKey) {
542
+ return fullOrder;
543
+ }
544
+
545
+ nextVisibleOrder.splice(toIndex, 0, movedKey);
546
+
547
+ let visibleIndex = 0;
548
+ return fullOrder.map((key) => {
549
+ if (!visibleKeySet.has(key)) {
550
+ return key;
551
+ }
552
+
553
+ const nextKey = nextVisibleOrder[visibleIndex] || key;
554
+ visibleIndex += 1;
555
+
556
+ return nextKey;
557
+ });
558
+ }
559
+
560
+ /*
561
+ * Return the inclusive start and end dates for one generated date series.
562
+ */
563
+
564
+ function getDataTableViewDateSeriesRange(
565
+ dateSeries: NonNullable<NonNullable<DataTableDesignViewObj['rowModel']>['generator']>['dateSeries'],
566
+ referenceDate: Date,
567
+ ) {
568
+ if (!dateSeries?.range) {
569
+ return null;
570
+ }
571
+
572
+ if (dateSeries.range.type === 'FIXED') {
573
+ const start = parseDateKey(dateSeries.range.start);
574
+ const end = parseDateKey(dateSeries.range.end);
575
+
576
+ return start && end && start.getTime() <= end.getTime() ? { start, end } : null;
577
+ }
578
+
579
+ if (dateSeries.range.type === 'CURRENT_MONTH') {
580
+ const start = new Date(Date.UTC(referenceDate.getUTCFullYear(), referenceDate.getUTCMonth(), 1));
581
+ const end = new Date(Date.UTC(referenceDate.getUTCFullYear(), referenceDate.getUTCMonth() + 1, 0));
582
+
583
+ return { start, end };
584
+ }
585
+
586
+ return null;
587
+ }
588
+
589
+ /*
590
+ * Return generated date points for one date series.
591
+ */
592
+
593
+ function getDataTableViewDateSeriesValues(
594
+ dateSeries: NonNullable<NonNullable<DataTableDesignViewObj['rowModel']>['generator']>['dateSeries'],
595
+ referenceDate: Date,
596
+ ) {
597
+ const range = getDataTableViewDateSeriesRange(dateSeries, referenceDate);
598
+ if (!dateSeries?.key || !range) {
599
+ return [];
600
+ }
601
+
602
+ const values: Array<{
603
+ key: string;
604
+ value: string;
605
+ label: string;
606
+ grain: 'DAY' | 'WEEK';
607
+ sourceCellKey: string;
608
+ }> = [];
609
+ let current = range.start;
610
+
611
+ while (current.getTime() <= range.end.getTime()) {
612
+ if (dateSeries.grain === 'DAY' || (dateSeries.grain === 'WEEK' && current.getUTCDay() === 1)) {
613
+ const value = formatDateKey(current);
614
+ values.push({
615
+ key: dateSeries.key,
616
+ value,
617
+ label: value,
618
+ grain: dateSeries.grain,
619
+ sourceCellKey: dateSeries.sourceCellKey || dateSeries.key,
620
+ });
621
+ }
622
+
623
+ current = addDays(current, 1);
624
+ }
625
+
626
+ return values;
627
+ }
628
+
629
+ /*
630
+ * Return generated dimension values from static values or master cell options.
631
+ */
632
+
633
+ function getDataTableViewGeneratorDimensionValues(
634
+ dimension: DataTableDesignViewGeneratorDimensionObj,
635
+ masterCellsByKey: Map<string, DataTableDesignCellObj>,
636
+ ) {
637
+ if (!dimension?.key || !dimension.source) {
638
+ return [];
639
+ }
640
+
641
+ if (dimension.source.type === 'STATIC_VALUES') {
642
+ return (dimension.source.values || [])
643
+ .filter((value) => typeof value?.value === 'string' && !!value.value)
644
+ .map((value) => ({
645
+ key: dimension.key,
646
+ value: value.value,
647
+ label: value.label || value.value,
648
+ sourceCellKey: null,
649
+ }));
650
+ }
651
+
652
+ if (dimension.source.type === 'MASTER_CELL_OPTIONS') {
653
+ const cellKey = dimension.source.cellKey;
654
+ const cell = masterCellsByKey.get(cellKey);
655
+
656
+ return (cell?.options || [])
657
+ .filter((option) => typeof option?.value === 'string' && !!option.value)
658
+ .map((option) => ({
659
+ key: dimension.key,
660
+ value: option.value,
661
+ label: option.label || option.value,
662
+ sourceCellKey: cellKey,
663
+ }));
664
+ }
665
+
666
+ return [];
667
+ }
668
+
669
+ /*
670
+ * Return all dimension value groups for a generated grouped-row view.
671
+ */
672
+
673
+ function getDataTableViewGeneratorValueGroups(
674
+ view: DataTableDesignViewObj,
675
+ masterCells: DataTableDesignCellObj[],
676
+ referenceDate: Date,
677
+ ) {
678
+ const generator = view.rowModel?.generator;
679
+ const masterCellsByKey = new Map(masterCells.map((cell) => [cell.key, cell]));
680
+ const groups: Array<Array<{
681
+ key: string;
682
+ value: string;
683
+ label: string;
684
+ grain?: 'DAY' | 'WEEK';
685
+ sourceCellKey?: string | null;
686
+ }>> = [];
687
+
688
+ const dateValues = getDataTableViewDateSeriesValues(generator?.dateSeries, referenceDate);
689
+ if (dateValues.length) {
690
+ groups.push(dateValues);
691
+ }
692
+
693
+ (generator?.dimensions || []).forEach((dimension) => {
694
+ const values = getDataTableViewGeneratorDimensionValues(dimension, masterCellsByKey);
695
+ if (values.length) {
696
+ groups.push(values);
697
+ }
698
+ });
699
+
700
+ return groups;
701
+ }
702
+
703
+ /*
704
+ * Return every cartesian combination of generated date and dimension values.
705
+ */
706
+
707
+ function getDataTableViewGeneratorCombinations(
708
+ groups: Array<Array<{
709
+ key: string;
710
+ value: string;
711
+ label: string;
712
+ grain?: 'DAY' | 'WEEK';
713
+ sourceCellKey?: string | null;
714
+ }>>,
715
+ ) {
716
+ return groups.reduce((combinations, group) => {
717
+ if (!combinations.length) {
718
+ return group.map((value) => [value]);
719
+ }
720
+
721
+ return combinations.flatMap((combination) => group.map((value) => combination.concat(value)));
722
+ }, [] as Array<Array<{
723
+ key: string;
724
+ value: string;
725
+ label: string;
726
+ grain?: 'DAY' | 'WEEK';
727
+ sourceCellKey?: string | null;
728
+ }>>);
729
+ }
730
+
731
+ /*
732
+ * Return the stable row key for one generated row combination.
733
+ */
734
+
735
+ function getDataTableViewGeneratedRowKey(
736
+ keyPrefix: string,
737
+ combination: Array<{
738
+ key: string;
739
+ value: string;
740
+ }>,
741
+ ) {
742
+ const segments = combination.map((value) => `${value.key}=${encodeURIComponent(value.value)}`);
743
+
744
+ return `${keyPrefix}:${segments.join(':')}`;
745
+ }
746
+
747
+ /*
748
+ * Return generated row definitions for one grouped-row view generator.
749
+ */
750
+
751
+ export function getDataTableViewGeneratedRowDefinitions(
752
+ view: DataTableDesignViewObj,
753
+ masterCells: DataTableDesignCellObj[],
754
+ referenceDate: Date = new Date(),
755
+ ) {
756
+ if (!isDataTableViewGeneratedRowsView(view)) {
757
+ return [];
758
+ }
759
+
760
+ const generator = view.rowModel!.generator!;
761
+ const groups = getDataTableViewGeneratorValueGroups(view, masterCells, referenceDate);
762
+ const combinations = getDataTableViewGeneratorCombinations(groups);
763
+
764
+ return combinations.map((combination, index) => {
765
+ const cellValues = combination.reduce((values, value) => {
766
+ values[value.key] = value.label;
767
+ return values;
768
+ }, {} as Record<string, string | number>);
769
+ const dateValue = combination.find((value) => value.key === generator.dateSeries?.key);
770
+ const dimensionValues = combination.filter((value) => value.key !== generator.dateSeries?.key);
771
+
772
+ return {
773
+ viewRowKey: getDataTableViewGeneratedRowKey(generator.keyPrefix, combination),
774
+ position: index + 1,
775
+ cellValues,
776
+ criteria: {
777
+ date: dateValue && dateValue.grain
778
+ ? {
779
+ key: dateValue.key,
780
+ value: dateValue.value,
781
+ grain: dateValue.grain,
782
+ sourceCellKey: dateValue.sourceCellKey || dateValue.key,
783
+ }
784
+ : undefined,
785
+ dimensions: dimensionValues.map((value) => ({
786
+ key: value.key,
787
+ value: value.value,
788
+ sourceCellKey: value.sourceCellKey,
789
+ })),
790
+ },
791
+ } satisfies GeneratedDataTableViewRowDefinition;
792
+ });
793
+ }
794
+
795
+ /*
796
+ * Convert a view column into the same design-cell shape used by the grid renderer.
797
+ */
798
+
799
+ export function mapDataTableDesignViewColumnToCell(
800
+ column: DataTableDesignViewColumnObj,
801
+ masterCellsByKey: Map<string, DataTableDesignCellObj>,
802
+ ) {
803
+ const masterCell = isDataTableViewMasterCellColumn(column)
804
+ ? masterCellsByKey.get(column.source!.cellKey!)
805
+ : null;
806
+
807
+ return {
808
+ key: column.key,
809
+ label: column.label || masterCell?.label || column.key,
810
+ humanLabel: column.humanLabel ?? null,
811
+ iconName: column.iconName ?? masterCell?.iconName ?? null,
812
+ fieldType: masterCell?.fieldType || column.fieldType || column.humanFieldType as DataTableFieldTypeEnum,
813
+ humanFieldType: column.humanFieldType,
814
+ format: masterCell?.format ?? null,
815
+ source: column.source?.type === 'RELATED_RECORD' && column.source.path && column.source.table
816
+ ? {
817
+ path: column.source.path,
818
+ table: column.source.table,
819
+ }
820
+ : masterCell?.source || null,
821
+ options: column.options || masterCell?.options || [],
822
+ openLink: column.openLink ?? masterCell?.openLink,
823
+ humansOnly: masterCell?.humansOnly,
824
+ humansCannotEdit: column.humansCannotEdit ?? masterCell?.humansCannotEdit ?? (
825
+ column.source?.type !== 'MASTER_CELL' &&
826
+ column.source?.type !== 'CUSTOM'
827
+ ),
828
+ indexed: masterCell?.indexed,
829
+ width: column.width ?? null,
830
+ viewSource: column.source || null,
831
+ } as DataTableDesignCellObj & {
832
+ viewSource?: DataTableDesignViewColumnObj['source'] | null;
833
+ };
834
+ }