@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
@@ -0,0 +1,76 @@
1
+ import {
2
+ LOG_ACTIVITIES_BY_OPERATION,
3
+ LOG_TAXONOMY_BY_OPERATION,
4
+ } from '../constants/log.ts';
5
+
6
+ export interface LogTaxonomyGroup {
7
+ key: string;
8
+ label: string;
9
+ ordinal: number;
10
+ }
11
+
12
+ export interface LogTaxonomyActivity {
13
+ key: string;
14
+ groupKey: string;
15
+ ordinal: number;
16
+ agentText: string;
17
+ financeKind: 'NONE' | 'PURCHASE' | 'SALE';
18
+ }
19
+
20
+ export interface LogTaxonomyOperation {
21
+ key: string;
22
+ groups: LogTaxonomyGroup[];
23
+ activities: LogTaxonomyActivity[];
24
+ }
25
+
26
+ /*
27
+ * Operations that carry at least one generated activity group.
28
+ */
29
+
30
+ export function getLogTaxonomyOperationKeys() {
31
+ return Object.keys(LOG_ACTIVITIES_BY_OPERATION);
32
+ }
33
+
34
+ /*
35
+ * Derive one operation's canonical taxonomy (groups + activities with agent
36
+ * texts and finance kinds) from the constants. This is the single source both
37
+ * the registry seed generator and the registry<->constants contract checker
38
+ * build on; a change here must keep the generated seed byte-identical or the
39
+ * recorded migration checksum will drift.
40
+ */
41
+
42
+ export function deriveLogTaxonomyOperation(
43
+ operation: string,
44
+ ): LogTaxonomyOperation {
45
+ const taxonomy =
46
+ LOG_TAXONOMY_BY_OPERATION[
47
+ operation as keyof typeof LOG_TAXONOMY_BY_OPERATION
48
+ ];
49
+ if (!taxonomy) {
50
+ throw new Error(`Unknown log taxonomy operation: ${operation}`);
51
+ }
52
+
53
+ return {
54
+ key: operation,
55
+ groups: taxonomy.groups.map((group) => ({
56
+ key: group.key,
57
+ label: group.label,
58
+ ordinal: group.ordinal,
59
+ })),
60
+ activities: taxonomy.activities.map((activity) => ({
61
+ key: activity.key,
62
+ groupKey: activity.groupKey,
63
+ ordinal: activity.ordinal,
64
+ agentText: activity.agentText,
65
+ financeKind: activity.financeKind,
66
+ })),
67
+ };
68
+ }
69
+
70
+ /*
71
+ * Derive the full canonical taxonomy for every operation that has one
72
+ */
73
+
74
+ export function deriveLogTaxonomyFromConstants(): LogTaxonomyOperation[] {
75
+ return getLogTaxonomyOperationKeys().map(deriveLogTaxonomyOperation);
76
+ }
@@ -0,0 +1,395 @@
1
+ import { isLogFieldApplicableToActivity } from './logTaxonomy.helpers.ts';
2
+ import i18n from '@jsb188/app/i18n/index.ts';
3
+ import { formatDisplayDate, getCalDate } from '@jsb188/app/utils/datetime.ts';
4
+ import { formatCurrency } from '@jsb188/app/utils/number.ts';
5
+ import { ucFirst } from '@jsb188/app/utils/string.ts';
6
+ import { parseDateInTimezone } from '@jsb188/app/utils/timeZone.ts';
7
+ import type {
8
+ LogEntryDetailsFieldGQL,
9
+ LogEntryDetailsGQL,
10
+ LogTaxonomyActivityGQL,
11
+ LogTaxonomyFieldDefGQL,
12
+ LogTaxonomyGQL,
13
+ } from '../types/log.d.ts';
14
+
15
+ export type LogTaxonomyFieldRequirement = 'REQUIRED' | 'OR_REQUIRED' | null;
16
+
17
+ /**
18
+ * Display helpers for registry-driven log operations (Phase 8b). Registry
19
+ * keys are data, not compile-time enums, so every label falls back from
20
+ * i18n → registry text → humanized key instead of assuming a translation
21
+ * exists. Kept separate from utils/logTaxonomy.ts, which is seed-critical
22
+ * (the frozen registry seed derives from it) and must stay dependency-free.
23
+ */
24
+
25
+ /*
26
+ * Humanize a registry key (SNAKE_CASE or camelCase) for display when no
27
+ * i18n label exists
28
+ */
29
+
30
+ export function humanizeRegistryKey(key?: string | null): string {
31
+ if (!key) {
32
+ return '';
33
+ }
34
+ const spaced = key
35
+ .replace(/[_-]+/g, ' ')
36
+ .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
37
+ .toLowerCase();
38
+ return ucFirst(spaced);
39
+ }
40
+
41
+ /*
42
+ * Display label for a registry activity key: i18n wins, then the registry
43
+ * agentText, then the humanized key
44
+ */
45
+
46
+ export function getTaxonomyActivityLabel(taxonomy: LogTaxonomyGQL | null | undefined, activityKey?: string | null): string {
47
+ if (!activityKey) {
48
+ return '';
49
+ }
50
+ if (i18n.has(`log.activity.${activityKey}`)) {
51
+ return i18n.t(`log.activity.${activityKey}`);
52
+ }
53
+ const agentText = taxonomy?.activities?.find((activity) => activity.key === activityKey)?.agentText;
54
+ return agentText ? ucFirst(agentText) : humanizeRegistryKey(activityKey);
55
+ }
56
+
57
+ /*
58
+ * Registry group key for an activity key
59
+ */
60
+
61
+ export function getTaxonomyGroupKey(
62
+ taxonomy: LogTaxonomyGQL | null | undefined,
63
+ activityKey?: string | null,
64
+ fallbackGroupKey?: string | null,
65
+ ): string | null {
66
+ if (!activityKey) {
67
+ return fallbackGroupKey ?? null;
68
+ }
69
+ return taxonomy?.activities?.find((activity) => activity.key === activityKey)?.groupKey ?? fallbackGroupKey ?? null;
70
+ }
71
+
72
+ /*
73
+ * Resolve one activity's effective required-field rule. Activity settings
74
+ * replace the group setting when present, including an explicit empty list.
75
+ */
76
+
77
+ export function getTaxonomyFieldRequirement(
78
+ taxonomy: LogTaxonomyGQL | null | undefined,
79
+ activityKey: string | null | undefined,
80
+ fieldKey: string,
81
+ fallbackGroupKey?: string | null,
82
+ ): LogTaxonomyFieldRequirement {
83
+ const activity = taxonomy?.activities.find((candidate) => candidate.key === activityKey);
84
+ const group = taxonomy?.groups.find((candidate) => candidate.key === (activity?.groupKey ?? fallbackGroupKey));
85
+ const requiredFields = activity?.requiredFields ?? group?.requiredFields ?? [];
86
+ const orRequiredFields = activity?.orRequiredFields ?? group?.orRequiredFields ?? [];
87
+
88
+ if (requiredFields.includes(fieldKey)) {
89
+ return 'REQUIRED';
90
+ }
91
+
92
+ return orRequiredFields.includes(fieldKey) ? 'OR_REQUIRED' : null;
93
+ }
94
+
95
+ /*
96
+ * Add the compact required-state marker used by registry-driven editor
97
+ * labels without assuming the label itself came from i18n.
98
+ */
99
+
100
+ export function formatLogTaxonomyFieldLabel(label: string, requirement: LogTaxonomyFieldRequirement): string {
101
+ if (requirement === 'REQUIRED') {
102
+ return `${label} *`;
103
+ }
104
+
105
+ return requirement === 'OR_REQUIRED' ? `${label} (${i18n.t('c.one_of_these_required')})` : label;
106
+ }
107
+
108
+ /*
109
+ * Display label for a registry group: i18n wins, then the registry label,
110
+ * then the humanized key
111
+ */
112
+
113
+ export function getTaxonomyGroupLabel(
114
+ taxonomy: LogTaxonomyGQL | null | undefined,
115
+ groupKey?: string | null,
116
+ operation?: string | null,
117
+ short = true,
118
+ ): string {
119
+ if (!groupKey) {
120
+ return '';
121
+ }
122
+ if (groupKey === 'MARKET_OPERATION' || (groupKey === 'OPERATION' && (operation || taxonomy?.operation) === 'FARMERS_MARKET')) {
123
+ return i18n.t(short ? 'log.type_short.MARKET_OPERATION' : 'log.market_operations');
124
+ }
125
+ const labelKey = `log.${short ? 'type_short' : 'type'}.${groupKey}`;
126
+ if (i18n.has(labelKey)) return i18n.t(labelKey);
127
+ const label = taxonomy?.groups?.find((group) => group.key === groupKey)?.label;
128
+ return label || humanizeRegistryKey(groupKey);
129
+ }
130
+
131
+ /*
132
+ * Group taxonomy activities in the taxonomy group's display order
133
+ */
134
+
135
+ export function getTaxonomyActivitiesGrouped(taxonomy: LogTaxonomyGQL): [string, LogTaxonomyActivityGQL[]][] {
136
+ const activitiesByGroup = new Map<string, LogTaxonomyActivityGQL[]>();
137
+ for (const activity of taxonomy.activities) {
138
+ if (!activity.groupKey) {
139
+ continue;
140
+ }
141
+
142
+ const activities = activitiesByGroup.get(activity.groupKey) || [];
143
+ activities.push(activity);
144
+ activitiesByGroup.set(activity.groupKey, activities);
145
+ }
146
+
147
+ return taxonomy.groups.map((group) => [group.key, activitiesByGroup.get(group.key) || []]);
148
+ }
149
+
150
+ /*
151
+ * Field defs that apply to one activity: activity-scoped defs match their
152
+ * activity only, group-scoped defs match the activity's group, unscoped defs
153
+ * apply operation-wide
154
+ */
155
+
156
+ export function getTaxonomyFieldDefsForActivity(
157
+ taxonomy: LogTaxonomyGQL | null | undefined,
158
+ activityKey?: string | null,
159
+ fallbackGroupKey?: string | null,
160
+ ): LogTaxonomyFieldDefGQL[] {
161
+ const groupKey = getTaxonomyGroupKey(taxonomy, activityKey, fallbackGroupKey);
162
+ return (taxonomy?.fieldDefs || []).filter((def) =>
163
+ isLogFieldApplicableToActivity(
164
+ {
165
+ ...def,
166
+ config: {
167
+ includedActivityKeys: def.includedActivityKeys ?? undefined,
168
+ excludedActivityKeys: def.excludedActivityKeys ?? undefined,
169
+ },
170
+ },
171
+ activityKey,
172
+ groupKey,
173
+ ),
174
+ );
175
+ }
176
+
177
+ /*
178
+ * Display label for a field def key
179
+ */
180
+
181
+ export function getLogEntryDetailsFieldLabel(key?: string | null): string {
182
+ if (!key) {
183
+ return '';
184
+ }
185
+ if (key === 'rackNumber') {
186
+ return i18n.t('c.rack');
187
+ }
188
+ if (i18n.has(`log.${key}`)) {
189
+ return i18n.t(`log.${key}`);
190
+ }
191
+ if (i18n.has(`c.${key}`)) {
192
+ return i18n.t(`c.${key}`);
193
+ }
194
+ return humanizeRegistryKey(key);
195
+ }
196
+
197
+ /*
198
+ * Preserve the domain label for envelope fields whose meaning is narrowed by
199
+ * the selected registry activity or group.
200
+ */
201
+
202
+ export function getLogEntryDetailsEnvelopeFieldLabel(key: string, activityKey?: string | null, groupKey?: string | null): string {
203
+ if (key === 'quantity' && activityKey === 'COLD_STORAGE_TEMPERATURE') {
204
+ return i18n.t('c.temperature');
205
+ }
206
+ if (key === 'quantity' && groupKey === 'WATER') {
207
+ return i18n.t('c.water_quantity');
208
+ }
209
+
210
+ return getLogEntryDetailsFieldLabel(key);
211
+ }
212
+
213
+ /*
214
+ * Return a canonical calendar-date value or null for an invalid value.
215
+ */
216
+
217
+ export function normalizeLogEntryDetailsDateValue(value?: string | null): string | null {
218
+ const trimmedValue = String(value || '').trim();
219
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmedValue)) {
220
+ return null;
221
+ }
222
+
223
+ const parsedDate = new Date(`${trimmedValue}T00:00:00.000Z`);
224
+ return Number.isFinite(parsedDate.getTime()) && parsedDate.toISOString().slice(0, 10) === trimmedValue ? trimmedValue : null;
225
+ }
226
+
227
+ /*
228
+ * Convert a canonical instant into the local value required by a
229
+ * datetime-local input, honoring the organization timezone when available.
230
+ */
231
+
232
+ export function getLogEntryDetailsDateTimeInputValue(value?: string | null, timeZone?: string | null): string | null {
233
+ const date = value ? new Date(value) : null;
234
+ if (!date || !Number.isFinite(date.getTime())) {
235
+ return null;
236
+ }
237
+
238
+ const { calDate, time } = getCalDate(date, timeZone);
239
+ return `${calDate}T${time.slice(0, 5)}`;
240
+ }
241
+
242
+ /*
243
+ * Convert a datetime-local input value into the canonical ISO instant used by
244
+ * the GraphQL mutation contract.
245
+ */
246
+
247
+ export function normalizeLogEntryDetailsDateTimeValue(value?: string | null, timeZone?: string | null): string | null {
248
+ const trimmedValue = String(value || '').trim();
249
+ if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/.test(trimmedValue)) {
250
+ return null;
251
+ }
252
+
253
+ const parsedDate = parseDateInTimezone(trimmedValue, timeZone, 0, true);
254
+ return Number.isFinite(parsedDate.getTime()) ? parsedDate.toISOString() : null;
255
+ }
256
+
257
+ /*
258
+ * Read one typed GraphQL value as a plain form scalar in display units.
259
+ * DATETIME is converted for its local input.
260
+ */
261
+
262
+ export function getLogEntryDetailsFieldValue(
263
+ field: LogEntryDetailsFieldGQL,
264
+ timeZone?: string | null,
265
+ ): string | number | boolean | null {
266
+ if (field.fieldType === 'DATE') {
267
+ return normalizeLogEntryDetailsDateValue(field.dateValue ?? field.textValue);
268
+ }
269
+ if (field.fieldType === 'DATETIME') {
270
+ return getLogEntryDetailsDateTimeInputValue(field.datetimeValue ?? field.textValue, timeZone);
271
+ }
272
+ return field.textValue ?? field.numberValue ?? field.booleanValue ?? null;
273
+ }
274
+
275
+ /*
276
+ * Read one registry field by key from either a GraphQL field array, an editor
277
+ * field map, or a materialized server metadata object.
278
+ */
279
+
280
+ export function getLogEntryDetailsFieldValueByKey(
281
+ details: Record<string, any> | null | undefined,
282
+ key: string,
283
+ timeZone?: string | null,
284
+ ): string | number | boolean | null {
285
+ const directValue = details?.[key];
286
+ if (directValue !== null && directValue !== undefined) {
287
+ return directValue;
288
+ }
289
+
290
+ const fields = details?.fields;
291
+ if (Array.isArray(fields)) {
292
+ const field = fields.find((candidate) => candidate?.key === key);
293
+ return field ? getLogEntryDetailsFieldValue(field, timeZone) : null;
294
+ }
295
+
296
+ if (fields && typeof fields === 'object') {
297
+ return fields[key] ?? null;
298
+ }
299
+
300
+ return null;
301
+ }
302
+
303
+ /*
304
+ * Read and validate one numeric registry field without turning an empty or
305
+ * invalid value into zero.
306
+ */
307
+
308
+ export function getLogEntryDetailsNumberFieldValue(details: Record<string, any> | null | undefined, key: string): number | null {
309
+ const value = getLogEntryDetailsFieldValueByKey(details, key);
310
+ if (value === '' || value === null || value === undefined) {
311
+ return null;
312
+ }
313
+
314
+ const numberValue = Number(value);
315
+ return Number.isFinite(numberValue) ? numberValue : null;
316
+ }
317
+
318
+ /*
319
+ * Format one rack position's temperature reading, preserving zero and negative values.
320
+ */
321
+
322
+ export function getLogTemperatureText(details: LogEntryDetailsGQL, position: 'top' | 'middle' | 'bottom'): string {
323
+ const unit = getLogEntryDetailsFieldValueByKey(details, 'temperatureUnit');
324
+ return formatLogTemperatureValue(
325
+ getLogEntryDetailsNumberFieldValue(details, `${position}Temperature`),
326
+ unit == null ? null : String(unit),
327
+ );
328
+ }
329
+
330
+ /*
331
+ * Format a measured temperature consistently across metadata and tables without
332
+ * inventing a missing unit or discarding an unfamiliar saved unit.
333
+ */
334
+
335
+ export function formatLogTemperatureValue(temperature: number | null, unit?: string | null): string {
336
+ if (temperature === null) return '';
337
+ return `${temperature}${unit === 'C' || unit === 'F' ? ` °${unit}` : unit ? ` ${unit}` : ''}`;
338
+ }
339
+
340
+ /*
341
+ * Read one QUANTITY registry field's unit from either GraphQL, editor, or
342
+ * materialized metadata details.
343
+ */
344
+
345
+ export function getLogEntryDetailsFieldUnitByKey(details: Record<string, any> | null | undefined, key: string): string | null {
346
+ const directUnit = details?.[`${key}Unit`];
347
+ if (directUnit !== null && directUnit !== undefined) {
348
+ return String(directUnit);
349
+ }
350
+
351
+ const fields = details?.fields;
352
+ if (Array.isArray(fields)) {
353
+ const unit = fields.find((candidate) => candidate?.key === key)?.unit;
354
+ return unit === null || unit === undefined ? null : String(unit);
355
+ }
356
+
357
+ const mappedUnit = details?.fieldUnits?.[key];
358
+ return mappedUnit === null || mappedUnit === undefined ? null : String(mappedUnit);
359
+ }
360
+
361
+ /*
362
+ * Format one typed field value for read-only display
363
+ */
364
+
365
+ export function formatLogEntryDetailsFieldValue(field: LogEntryDetailsFieldGQL, timeZone?: string | null): string {
366
+ const value = getLogEntryDetailsFieldValue(field, timeZone);
367
+ if (value === null || value === undefined) {
368
+ return '';
369
+ }
370
+ if (field.fieldType === 'MONEY') {
371
+ return formatCurrency(value as number, false);
372
+ }
373
+ if (field.fieldType === 'BOOLEAN') {
374
+ return i18n.t(value ? 'c.yes' : 'c.no');
375
+ }
376
+ if (field.fieldType === 'DATE') {
377
+ return formatDisplayDate(String(value), timeZone);
378
+ }
379
+ if (field.fieldType === 'DATETIME') {
380
+ const datetimeValue = field.datetimeValue ?? field.textValue;
381
+ const parsedDate = datetimeValue ? new Date(datetimeValue) : null;
382
+ return parsedDate && Number.isFinite(parsedDate.getTime()) ? formatDisplayDate(parsedDate, timeZone) : String(value);
383
+ }
384
+ const unitText = field.unit ? ` ${field.unit}` : '';
385
+ return `${value}${unitText}`;
386
+ }
387
+
388
+ /*
389
+ * Return whether a typed store value is a custom field that belongs in the
390
+ * generic details list. flagColor is a storage slot rendered elsewhere.
391
+ */
392
+
393
+ export function isLogEntryDetailsCustomField(field: LogEntryDetailsFieldGQL): boolean {
394
+ return !!field.key && field.key !== 'flagColor';
395
+ }