@jsb188/mday 1.1.10 → 1.1.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.
Files changed (210) hide show
  1. package/package.json +140 -54
  2. package/src/constants/agentMemory.ts +123 -0
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +10 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +236 -0
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +21 -21
  10. package/src/constants/dataTableFieldTypes.generated.ts +59 -0
  11. package/src/constants/docAccess.ts +33 -0
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +125 -7
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/integration.ts +8 -9
  17. package/src/constants/log.ts +14 -408
  18. package/src/constants/logDisplay/README.md +88 -0
  19. package/src/constants/logDisplay/defaults.ts +39 -0
  20. package/src/constants/logDisplay/index.ts +27 -0
  21. package/src/constants/logDisplay/operations/arable.ts +16 -0
  22. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  23. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  24. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  25. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  26. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  27. package/src/constants/logTaxonomy.generated.ts +2207 -0
  28. package/src/constants/noteDoc.ts +77 -0
  29. package/src/constants/organization.ts +18 -21
  30. package/src/constants/pageRoute.ts +23 -0
  31. package/src/constants/qrCode.ts +30 -0
  32. package/src/constants/sheet.ts +126 -14
  33. package/src/constants/sms.ts +18 -0
  34. package/src/constants/square.ts +6 -6
  35. package/src/constants/storage.ts +80 -7
  36. package/src/constants/usage.ts +82 -0
  37. package/src/constants/workflowSteps.ts +60 -0
  38. package/src/schemas/editLogEntry.ts +836 -780
  39. package/src/schemas/inboundContact.ts +60 -55
  40. package/src/schemas/logsQuery.ts +55 -49
  41. package/src/types/account.d.ts +4 -1
  42. package/src/types/agentMemory.d.ts +98 -0
  43. package/src/types/agentNote.d.ts +443 -0
  44. package/src/types/billing.d.ts +537 -0
  45. package/src/types/campaign.d.ts +1533 -0
  46. package/src/types/contact.d.ts +15 -0
  47. package/src/types/csvExport.d.ts +213 -0
  48. package/src/types/dataAnalysis.d.ts +44 -0
  49. package/src/types/dataTable.d.ts +115 -403
  50. package/src/types/definitionElement.d.ts +35 -0
  51. package/src/types/docAccess.d.ts +96 -0
  52. package/src/types/dt.d.ts +257 -0
  53. package/src/types/email.d.ts +184 -24
  54. package/src/types/emailReplyReport.d.ts +76 -0
  55. package/src/types/fieldDoc.d.ts +702 -0
  56. package/src/types/fieldDocContract.d.ts +90 -0
  57. package/src/types/inboundContact.d.ts +10 -1
  58. package/src/types/log.d.ts +254 -295
  59. package/src/types/logDisplay.types.ts +85 -0
  60. package/src/types/organization.d.ts +91 -43
  61. package/src/types/pageRoute.d.ts +48 -0
  62. package/src/types/qrCode.d.ts +96 -0
  63. package/src/types/richText.d.ts +81 -0
  64. package/src/types/sheet.d.ts +678 -129
  65. package/src/types/sms.d.ts +38 -0
  66. package/src/types/storage.d.ts +449 -129
  67. package/src/types/usage.d.ts +106 -0
  68. package/src/types/workflow.d.ts +315 -54
  69. package/src/utils/agentMemory.ts +15 -0
  70. package/src/utils/agentNote.ts +850 -0
  71. package/src/utils/aiChat.ts +43 -0
  72. package/src/utils/aiChatWork.ts +64 -0
  73. package/src/utils/billing.ts +37 -0
  74. package/src/utils/campaign.ts +1174 -0
  75. package/src/utils/campaignAI.ts +72 -0
  76. package/src/utils/campaignAccess.ts +239 -0
  77. package/src/utils/campaignEmail.ts +601 -0
  78. package/src/utils/campaignEmailCompiler.ts +372 -0
  79. package/src/utils/campaignEmailImages.ts +60 -0
  80. package/src/utils/campaignEmailStyles.ts +544 -0
  81. package/src/utils/campaignLifecycle.ts +104 -0
  82. package/src/utils/campaignPreview.ts +285 -0
  83. package/src/utils/contact.ts +50 -0
  84. package/src/utils/csvExport.ts +19 -0
  85. package/src/utils/dataTable.ts +95 -618
  86. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  87. package/src/utils/dataTableRelativeDate.ts +447 -0
  88. package/src/utils/definitionElement.ts +84 -0
  89. package/src/utils/docAccess.ts +383 -0
  90. package/src/utils/documentName.ts +53 -0
  91. package/src/utils/email.ts +317 -51
  92. package/src/utils/emailReplyReport.test.ts +187 -0
  93. package/src/utils/emailReplyReport.ts +143 -0
  94. package/src/utils/fieldDoc.ts +157 -0
  95. package/src/utils/fieldDocAnswers.ts +128 -0
  96. package/src/utils/fieldDocDefaults.ts +36 -0
  97. package/src/utils/fieldDocProgram.ts +36 -0
  98. package/src/utils/fieldDocSummary.ts +30 -0
  99. package/src/utils/log.ts +276 -192
  100. package/src/utils/logDisplay.helpers.ts +177 -0
  101. package/src/utils/logDisplay.ts +87 -0
  102. package/src/utils/logRegistryValue.ts +90 -0
  103. package/src/utils/logStoreRead.ts +262 -0
  104. package/src/utils/logTaxonomy.helpers.ts +17 -0
  105. package/src/utils/logTaxonomy.ts +76 -0
  106. package/src/utils/logTaxonomyDisplay.ts +395 -0
  107. package/src/utils/noteDoc.ts +609 -0
  108. package/src/utils/organization.ts +337 -84
  109. package/src/utils/pageRoute.ts +71 -0
  110. package/src/utils/qrCode.helpers.ts +189 -0
  111. package/src/utils/qrCode.test.ts +519 -0
  112. package/src/utils/qrCode.ts +273 -0
  113. package/src/utils/richTextHTML.ts +385 -0
  114. package/src/utils/sheet.ts +199 -107
  115. package/src/utils/sheetAxisMove.ts +616 -0
  116. package/src/utils/sheetBootstrap.ts +1402 -0
  117. package/src/utils/sheetCellLiteral.ts +101 -0
  118. package/src/utils/sheetCommand.ts +358 -0
  119. package/src/utils/sheetDesign.ts +705 -0
  120. package/src/utils/sheetExport.ts +186 -0
  121. package/src/utils/sheetFeatureReference.ts +183 -0
  122. package/src/utils/sheetFormulaArgs.ts +168 -0
  123. package/src/utils/sheetFormulaCompile.ts +475 -0
  124. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  125. package/src/utils/sheetFormulaCriteria.ts +656 -0
  126. package/src/utils/sheetFormulaDate.ts +444 -0
  127. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  128. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  129. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  130. package/src/utils/sheetFormulaLogical.ts +116 -0
  131. package/src/utils/sheetFormulaLookup.ts +488 -0
  132. package/src/utils/sheetFormulaMath.ts +48 -0
  133. package/src/utils/sheetFormulaParser.ts +729 -0
  134. package/src/utils/sheetFormulaRange.ts +330 -0
  135. package/src/utils/sheetFormulaReference.ts +703 -0
  136. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  137. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  138. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  139. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  140. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  141. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  142. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  143. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  144. package/src/utils/sheetFormulaText.ts +430 -0
  145. package/src/utils/sheetJournal.ts +200 -0
  146. package/src/utils/sheetNumberFormat.ts +1012 -0
  147. package/src/utils/sheetPreview.ts +68 -0
  148. package/src/utils/sheetSequence.ts +118 -0
  149. package/src/utils/sheetSnapshot.ts +739 -0
  150. package/src/utils/sheetStructureEdit.ts +914 -0
  151. package/src/utils/sheetStructureReferences.ts +389 -0
  152. package/src/utils/square.ts +22 -12
  153. package/src/utils/storage.ts +198 -0
  154. package/src/utils/table-route.ts +127 -11
  155. package/src/utils/time.ts +12 -0
  156. package/src/utils/workflow.ts +334 -40
  157. package/src/utils/workflowLogFingerprint.ts +302 -0
  158. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  159. package/src/utils/workflowProgress.ts +280 -0
  160. package/src/utils/workflowTriggers.ts +61 -0
  161. package/tests/agentNote.test.ts +395 -0
  162. package/tests/campaignAI.test.ts +119 -0
  163. package/tests/campaignAccess.test.ts +239 -0
  164. package/tests/campaignEmail.test.ts +441 -0
  165. package/tests/campaignLifecycle.test.ts +134 -0
  166. package/tests/campaignPublication.test.ts +309 -0
  167. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  168. package/tests/dataTableRelativeDate.test.ts +412 -0
  169. package/tests/docAccess.test.ts +525 -0
  170. package/tests/editLogEntry.test.ts +112 -0
  171. package/tests/email.test.ts +39 -0
  172. package/tests/fieldDocProgram.test.ts +114 -0
  173. package/tests/logDisplay.fixtures.ts +67 -0
  174. package/tests/logDisplay.test.ts +227 -0
  175. package/tests/noteDoc.test.ts +514 -0
  176. package/tests/richTextHTML.test.ts +185 -0
  177. package/tests/sheetAxisMove.test.ts +352 -0
  178. package/tests/sheetBootstrap.test.ts +1967 -0
  179. package/tests/sheetFeatureReference.test.ts +22 -0
  180. package/tests/sheetFormulaCriteria.test.ts +269 -0
  181. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  182. package/tests/sheetFormulaFunctions.test.ts +538 -0
  183. package/tests/sheetFormulaLanguage.test.ts +361 -0
  184. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  185. package/tests/sheetFormulaParity.test.ts +188 -0
  186. package/tests/sheetFormulaRange.test.ts +167 -0
  187. package/tests/sheetFormulaReference.test.ts +78 -0
  188. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  189. package/tests/sheetFormulaTextDate.test.ts +512 -0
  190. package/tests/sheetSequence.test.ts +22 -0
  191. package/tests/sheetSnapshot.test.ts +134 -0
  192. package/tests/sheetStructureEdit.test.ts +537 -0
  193. package/tests/sheetStructureReferences.test.ts +589 -0
  194. package/tests/workflowLifecycleStatus.test.ts +60 -0
  195. package/tests/workflowLogFingerprint.test.ts +203 -0
  196. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  197. package/tests/workflowProgress.test.ts +180 -0
  198. package/tests/workflowTriggers.test.ts +29 -0
  199. package/tsconfig.json +10 -10
  200. package/vitest.config.ts +8 -0
  201. package/src/constants/log-activityTexts.ts +0 -235
  202. package/src/constants/product.ts +0 -42
  203. package/src/constants/report.ts +0 -44
  204. package/src/schemas/organization.ts +0 -65
  205. package/src/schemas/productsQuery.ts +0 -53
  206. package/src/schemas/reportsQuery.ts +0 -20
  207. package/src/types/product.d.ts +0 -84
  208. package/src/types/report.d.ts +0 -290
  209. package/src/utils/product-calEvent.ts +0 -455
  210. package/src/utils/report.ts +0 -158
@@ -1,44 +1,7 @@
1
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
- }
2
+ import { DATA_TABLE_FIELD_TYPE_SEMANTICS } from '../constants/dataTableFieldTypes.generated.ts';
3
+ import type { DataTableDesignCellObj, DataTableFieldTypeEnum, DataTableRecordValue } from '../types/dataTable.d.ts';
4
+ import type { DataTableFieldTypeSemanticsKey } from './dataTableFieldTypeRegistry.ts';
42
5
 
43
6
  /*
44
7
  * Check whether a value is a non-array object.
@@ -48,36 +11,51 @@ function isPlainObject(value: any): value is Record<string, any> {
48
11
  return !!value && typeof value === 'object' && !(value instanceof Date) && !Array.isArray(value);
49
12
  }
50
13
 
14
+ /* Field type keys whose registry semantics family is one of S, derived from
15
+ * the const-asserted registry snapshot so helper type predicates narrow to
16
+ * exactly the registry-bound keys. */
17
+ type DataTableFieldTypeOfSemantics<S extends DataTableFieldTypeSemanticsKey> = {
18
+ [K in keyof typeof DATA_TABLE_FIELD_TYPE_SEMANTICS]: (typeof DATA_TABLE_FIELD_TYPE_SEMANTICS)[K] extends S ? K : never;
19
+ }[keyof typeof DATA_TABLE_FIELD_TYPE_SEMANTICS];
20
+
51
21
  /*
52
- * Return whether one dataTable view column reads from a master dataTable cell.
22
+ * Return the registry semantics family one dataTable field type binds to.
53
23
  */
54
-
55
- export function isDataTableViewMasterCellColumn(column: DataTableDesignViewColumnObj | null | undefined) {
56
- return column?.source?.type === 'MASTER_CELL' && typeof column.source.cellKey === 'string' && !!column.source.cellKey;
24
+ function getDataTableFieldTypeSemanticsKey(
25
+ fieldType: DataTableFieldTypeEnum | null | undefined,
26
+ ): DataTableFieldTypeSemanticsKey | undefined {
27
+ return (DATA_TABLE_FIELD_TYPE_SEMANTICS as Record<string, DataTableFieldTypeSemanticsKey>)[fieldType || ''];
57
28
  }
58
29
 
59
30
  /*
60
31
  * Return whether one dataTable field type stores and compares values as numbers.
61
32
  */
62
33
 
63
- export function isDataTableNumberLikeFieldType(fieldType: DataTableFieldTypeEnum | null | undefined) {
64
- return fieldType === 'NUMBER' || fieldType === 'PRICE';
34
+ export function isDataTableNumberLikeFieldType(
35
+ fieldType: DataTableFieldTypeEnum | null | undefined,
36
+ ): fieldType is DataTableFieldTypeOfSemantics<'NUMBER'> {
37
+ return getDataTableFieldTypeSemanticsKey(fieldType) === 'NUMBER';
65
38
  }
66
39
 
67
40
  /*
68
41
  * Return whether one dataTable field type stores and compares values as dates.
69
42
  */
70
43
 
71
- export function isDataTableDateLikeFieldType(fieldType: DataTableFieldTypeEnum | null | undefined) {
72
- return fieldType === 'DATE' || fieldType === 'WEEK_OF_MON' || fieldType === 'WEEK_OF_SUN';
44
+ export function isDataTableDateLikeFieldType(
45
+ fieldType: DataTableFieldTypeEnum | null | undefined,
46
+ ): fieldType is DataTableFieldTypeOfSemantics<'DATE' | 'WEEK'> {
47
+ const semanticsKey = getDataTableFieldTypeSemanticsKey(fieldType);
48
+ return semanticsKey === 'DATE' || semanticsKey === 'WEEK';
73
49
  }
74
50
 
75
51
  /*
76
52
  * Return whether one dataTable field type displays a calendar week range.
77
53
  */
78
54
 
79
- export function isDataTableWeekFieldType(fieldType: DataTableFieldTypeEnum | null | undefined) {
80
- return fieldType === 'WEEK_OF_MON' || fieldType === 'WEEK_OF_SUN';
55
+ export function isDataTableWeekFieldType(
56
+ fieldType: DataTableFieldTypeEnum | null | undefined,
57
+ ): fieldType is DataTableFieldTypeOfSemantics<'WEEK'> {
58
+ return getDataTableFieldTypeSemanticsKey(fieldType) === 'WEEK';
81
59
  }
82
60
 
83
61
  /*
@@ -100,16 +78,6 @@ function getDataTableRecordScalarValue(value: DataTableRecordValue | Date) {
100
78
  return value;
101
79
  }
102
80
 
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
81
  /*
114
82
  * Return whether a date string points at a real calendar date.
115
83
  */
@@ -122,9 +90,7 @@ function isValidDataTableDateKey(value: string) {
122
90
  const [year, month, day] = value.split('-').map(Number);
123
91
  const date = new Date(Date.UTC(year, month - 1, day));
124
92
 
125
- return date.getUTCFullYear() === year &&
126
- date.getUTCMonth() === month - 1 &&
127
- date.getUTCDate() === day;
93
+ return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
128
94
  }
129
95
 
130
96
  /*
@@ -139,17 +105,53 @@ function parseDateKey(value: string | null | undefined) {
139
105
  const [year, month, day] = value.split('-').map(Number);
140
106
  const date = new Date(Date.UTC(year, month - 1, day));
141
107
 
142
- if (
143
- date.getUTCFullYear() !== year ||
144
- date.getUTCMonth() !== month - 1 ||
145
- date.getUTCDate() !== day
146
- ) {
108
+ if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) {
147
109
  return null;
148
110
  }
149
111
 
150
112
  return date;
151
113
  }
152
114
 
115
+ /*
116
+ * Return whether one date-like string includes an explicit time component.
117
+ */
118
+ function dataTableDateStringHasTimeComponent(value: string) {
119
+ return /(?:T|\s+\d{1,2}:\d{2}|\d{1,2}:\d{2}\s*(?:am|pm)\b)/i.test(value.trim());
120
+ }
121
+
122
+ /*
123
+ * Return a date key parsed from date-only strings without applying timezone conversion.
124
+ */
125
+ function getDataTableDateOnlyStringCalendarKey(value: string) {
126
+ const stringValue = value.trim();
127
+ if (!stringValue || dataTableDateStringHasTimeComponent(stringValue)) {
128
+ return null;
129
+ }
130
+
131
+ if (isValidDataTableDateKey(stringValue)) {
132
+ return stringValue;
133
+ }
134
+
135
+ const slashDate = stringValue.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
136
+ if (slashDate) {
137
+ const [, monthText, dayText, yearText] = slashDate;
138
+ const month = Number(monthText);
139
+ const day = Number(dayText);
140
+ const dateKey = `${yearText}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
141
+
142
+ return isValidDataTableDateKey(dateKey) ? dateKey : null;
143
+ }
144
+
145
+ const parsedDate = new Date(stringValue);
146
+ if (!Number.isFinite(parsedDate.getTime())) {
147
+ return null;
148
+ }
149
+
150
+ const dateKey = formatDateKey(parsedDate);
151
+
152
+ return isValidDataTableDateKey(dateKey) ? dateKey : null;
153
+ }
154
+
153
155
  /*
154
156
  * Return a YYYY-MM-DD string for one date using UTC calendar fields.
155
157
  */
@@ -173,24 +175,6 @@ function addDays(date: Date, days: number) {
173
175
  return nextDate;
174
176
  }
175
177
 
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
178
  /*
195
179
  * Return the calendar date key represented by one date value in a timezone.
196
180
  */
@@ -203,8 +187,9 @@ function getDataTableDateValueCalendarKey(value: DataTableRecordValue | Date, ti
203
187
  }
204
188
 
205
189
  const stringValue = String(scalarValue);
206
- if (isValidDataTableDateKey(stringValue)) {
207
- return stringValue;
190
+ const dateOnlyKey = getDataTableDateOnlyStringCalendarKey(stringValue);
191
+ if (dateOnlyKey) {
192
+ return dateOnlyKey;
208
193
  }
209
194
 
210
195
  const parsedDate = parseDateInTimezone(stringValue, timeZone, 0, stringValue.includes('T'));
@@ -229,7 +214,11 @@ export function getDataTableDateValueKey(value: DataTableRecordValue | Date) {
229
214
  * Return a date string normalized to the configured dataTable week start.
230
215
  */
231
216
 
232
- export function normalizeDataTableWeekDateValue(value: DataTableRecordValue | Date, fieldType: DataTableFieldTypeEnum, timeZone?: string | null) {
217
+ export function normalizeDataTableWeekDateValue(
218
+ value: DataTableRecordValue | Date,
219
+ fieldType: DataTableFieldTypeEnum,
220
+ timeZone?: string | null,
221
+ ) {
233
222
  const dateKey = getDataTableDateValueCalendarKey(value, timeZone);
234
223
  const date = parseDateKey(dateKey);
235
224
  if (!date || !isDataTableWeekFieldType(fieldType)) {
@@ -246,8 +235,14 @@ export function normalizeDataTableWeekDateValue(value: DataTableRecordValue | Da
246
235
  * Return a date string normalized for storage by one date-like dataTable field.
247
236
  */
248
237
 
249
- export function normalizeDataTableDateLikeValue(value: DataTableRecordValue | Date, fieldType: DataTableFieldTypeEnum, timeZone?: string | null) {
250
- return isDataTableWeekFieldType(fieldType) ? normalizeDataTableWeekDateValue(value, fieldType, timeZone) : getDataTableDateValueCalendarKey(value, timeZone);
238
+ export function normalizeDataTableDateLikeValue(
239
+ value: DataTableRecordValue | Date,
240
+ fieldType: DataTableFieldTypeEnum,
241
+ timeZone?: string | null,
242
+ ) {
243
+ return isDataTableWeekFieldType(fieldType)
244
+ ? normalizeDataTableWeekDateValue(value, fieldType, timeZone)
245
+ : getDataTableDateValueCalendarKey(value, timeZone);
251
246
  }
252
247
 
253
248
  /*
@@ -265,17 +260,6 @@ export function normalizeDataTableDateTimeValue(value: DataTableRecordValue | Da
265
260
  ).toISOString();
266
261
  }
267
262
 
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
263
  /*
280
264
  * Return whether one value is included in a dataTable cell's saved options.
281
265
  */
@@ -287,11 +271,12 @@ function isDataTableOptionValue(cell: DataTableDesignCellObj, value: unknown) {
287
271
  }
288
272
 
289
273
  /*
290
- * Return the normalized string used for case-insensitive dataTable option matching.
274
+ * Return the normalized string used for whitespace- and case-insensitive
275
+ * dataTable option matching without changing unmatched free text.
291
276
  */
292
277
 
293
278
  function getNormalizedDataTableOptionText(value: unknown) {
294
- return String(value).toLowerCase();
279
+ return String(value).trim().toLowerCase();
295
280
  }
296
281
 
297
282
  /*
@@ -299,10 +284,7 @@ function getNormalizedDataTableOptionText(value: unknown) {
299
284
  */
300
285
 
301
286
  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';
287
+ return cell.fieldType === 'SELECT' || cell.fieldType === 'SELECT_OR_TEXT';
306
288
  }
307
289
 
308
290
  /*
@@ -319,516 +301,11 @@ export function normalizeDataTableSelectOptionValue(cell: DataTableDesignCellObj
319
301
  }
320
302
 
321
303
  const normalizedValue = getNormalizedDataTableOptionText(value);
322
- const option = (cell.options || []).find((item) => (
323
- getNormalizedDataTableOptionText(item.value) === normalizedValue ||
324
- getNormalizedDataTableOptionText(item.label) === normalizedValue
325
- ));
304
+ const option = (cell.options || []).find(
305
+ (item) =>
306
+ getNormalizedDataTableOptionText(item.value) === normalizedValue ||
307
+ getNormalizedDataTableOptionText(item.label) === normalizedValue,
308
+ );
326
309
 
327
310
  return option ? option.value : value;
328
311
  }
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
- }