@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
package/src/utils/log.ts CHANGED
@@ -1,91 +1,264 @@
1
1
  import i18n from '@jsb188/app/i18n/index.ts';
2
+ import { normalizeLogLanguage } from '@jsb188/app/utils/language.ts';
2
3
  import { formatCurrency, formatDecimal } from '@jsb188/app/utils/number.ts';
3
- import { formatReferenceNumber, joinReadable, textWithBrackets, ucFirst } from '@jsb188/app/utils/string.ts';
4
+ import { joinReadable, textWithBrackets, ucFirst } from '@jsb188/app/utils/string.ts';
4
5
  import {
5
- ARABLE_ACTIVITIES_GROUPED,
6
- FARMERS_MARKET_ACTIVITIES_GROUPED,
7
- GROWER_NETWORK_ACTIVITIES_GROUPED,
8
- LIVESTOCK_ACTIVITIES_GROUPED,
9
- LOG_TYPES_BY_OPERATION
6
+ APP_INTERACTIONS_LOG_GROUP,
7
+ LOG_ACTIVITIES_BY_OPERATION,
8
+ LOG_TYPES_BY_OPERATION,
9
+ PURCHASE_ACTIVITIES,
10
+ SALE_ACTIVITIES,
10
11
  } from '../constants/log.ts';
11
- import type { LogContentName, LogEntryData, LogTypeEnum } from '../types/log.d.ts';
12
+ import type {
13
+ LogContentName,
14
+ LogDisplayText,
15
+ LogEntryDetailsFieldGQL,
16
+ LogTranslationBundle,
17
+ LogTranslationBundleGQL,
18
+ LogTranslationLocaleText,
19
+ LogTranslationSourceTexts,
20
+ LogTypeEnum,
21
+ } from '../types/log.d.ts';
12
22
  import type { OrganizationOperationEnum } from '../types/organization.d.ts';
23
+ import { getLogEntryDetailsFieldUnitByKey, getLogEntryDetailsFieldValueByKey, humanizeRegistryKey } from './logTaxonomyDisplay.ts';
13
24
 
14
- /**
15
- * Get content type for log by activity
25
+ const FILE_CONSTANTS = {
26
+ accountingActivities: new Set([...PURCHASE_ACTIVITIES, ...SALE_ACTIVITIES] as string[]),
27
+ nonTranslatableFieldKeys: new Set(['flagColor', 'location', 'otherParty', 'unit']),
28
+ };
29
+
30
+ type LogTextSource = {
31
+ summary?: string | null;
32
+ translation?: LogTranslationBundle | LogTranslationBundleGQL | null;
33
+ details?: {
34
+ item?: string | null;
35
+ notes?: string | null;
36
+ translation?: LogTranslationBundle | LogTranslationBundleGQL | null;
37
+ values?: Array<Record<string, any>> | null;
38
+ fields?: Array<Partial<LogEntryDetailsFieldGQL> & { fieldDefKey?: string | null }> | null;
39
+ metadata?: Record<string, any> | null;
40
+ } | null;
41
+ };
42
+
43
+ /*
44
+ * Read the original translatable details from either server-native metadata or
45
+ * the flattened GraphQL details representation.
16
46
  */
47
+ function getOriginalLogTextParts(log: LogTextSource) {
48
+ const details = log.details || {};
49
+ const metadata = details.metadata || {};
17
50
 
18
- export function getLogContentName(activity: string): LogContentName {
19
- const value = {
20
- 'SALE_PRODUCE_ORDER': 'invoice',
21
- 'OTHER_SALE_ORDER': 'invoice',
22
- 'FEED_PURCHASE': 'invoice',
23
- 'OTHER_SUPPLY_PURCHASE_ACTIVITY': 'invoice',
24
- 'MARKET_CREDIT_RECEIPT': 'receipt',
25
- }[activity as string] || 'log';
26
-
27
- return value as LogContentName;
51
+ return {
52
+ summary: typeof log.summary === 'string' ? log.summary : null,
53
+ notes: typeof details.notes === 'string' ? details.notes : null,
54
+ item: typeof details.item === 'string' ? details.item : typeof metadata.item === 'string' ? metadata.item : null,
55
+ values: Array.isArray(details.values) ? details.values : Array.isArray(metadata.values) ? metadata.values : null,
56
+ fields: Array.isArray(details.fields) ? details.fields : null,
57
+ };
58
+ }
59
+
60
+ /*
61
+ * Return the persisted translation bundle from either the widened log row or
62
+ * its reconstructed server details.
63
+ */
64
+ function getLogTranslationBundle(log: LogTextSource) {
65
+ if (log.translation !== undefined) {
66
+ return log.translation;
67
+ }
68
+
69
+ return log.details?.translation ?? null;
70
+ }
71
+
72
+ /*
73
+ * Resolve one locale entry from the JSONB record or its GraphQL list shape.
74
+ */
75
+ function getLogLocaleTranslation(
76
+ translation: LogTranslationBundle | LogTranslationBundleGQL | null,
77
+ language: string,
78
+ ): LogTranslationLocaleText | null {
79
+ if (!translation?.texts) {
80
+ return null;
81
+ }
82
+
83
+ if (Array.isArray(translation.texts)) {
84
+ return translation.texts.find((text) => text.language === language) || null;
85
+ }
86
+
87
+ return translation.texts[language] || null;
88
+ }
89
+
90
+ /*
91
+ * Normalize one translated string while retaining valid empty strings.
92
+ */
93
+ function getTranslatedText(value: unknown, fallback: string | null) {
94
+ return typeof value === 'string' ? value : fallback;
95
+ }
96
+
97
+ /*
98
+ * Clone original item lines and replace only labels translated for their
99
+ * stable source ordinal.
100
+ */
101
+ function getTranslatedLogValues(values: Array<Record<string, any>> | null, translation: LogTranslationLocaleText | null) {
102
+ if (!values) {
103
+ return null;
104
+ }
105
+
106
+ const labelsByOrdinal = new Map(
107
+ (translation?.values || [])
108
+ .filter((value) => Number.isInteger(value.ordinal) && typeof value.label === 'string')
109
+ .map((value) => [value.ordinal, value.label]),
110
+ );
111
+
112
+ return values.map((value, ordinal) => ({
113
+ ...value,
114
+ label: labelsByOrdinal.has(ordinal) ? labelsByOrdinal.get(ordinal) : value.label,
115
+ }));
116
+ }
117
+
118
+ /*
119
+ * Clone original custom fields and replace only keyed text values present in
120
+ * the selected locale.
121
+ */
122
+ function getTranslatedLogFields(
123
+ fields: Array<Partial<LogEntryDetailsFieldGQL> & { fieldDefKey?: string | null }> | null,
124
+ translation: LogTranslationLocaleText | null,
125
+ ) {
126
+ if (!fields) {
127
+ return null;
128
+ }
129
+
130
+ const textByKey = new Map(
131
+ (translation?.fields || [])
132
+ .filter((field) => typeof field.key === 'string' && typeof field.textValue === 'string')
133
+ .map((field) => [field.key, field.textValue]),
134
+ );
135
+
136
+ return fields.map((field) => {
137
+ const key = field.key || field.fieldDefKey || '';
138
+
139
+ return {
140
+ ...field,
141
+ textValue: textByKey.has(key) ? textByKey.get(key)! : (field.textValue ?? null),
142
+ } as LogEntryDetailsFieldGQL;
143
+ });
144
+ }
145
+
146
+ /*
147
+ * Build the stable source payload shared by language detection, translation,
148
+ * and source hashing.
149
+ */
150
+ export function getLogTranslationSourceTexts(log: LogTextSource): LogTranslationSourceTexts {
151
+ const source = getOriginalLogTextParts(log);
152
+ const values = (source.values || []).flatMap((value, ordinal) =>
153
+ typeof value.label === 'string' ? [{ ordinal, label: value.label }] : [],
154
+ );
155
+ const fields = (source.fields || [])
156
+ .flatMap((field) => {
157
+ const key = field.key || field.fieldDefKey;
158
+
159
+ return typeof key === 'string' &&
160
+ field.fieldType === 'TEXT' &&
161
+ !FILE_CONSTANTS.nonTranslatableFieldKeys.has(key) &&
162
+ typeof field.textValue === 'string'
163
+ ? [{ key, textValue: field.textValue }]
164
+ : [];
165
+ })
166
+ .sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
167
+
168
+ return {
169
+ summary: source.summary,
170
+ notes: source.notes,
171
+ item: source.item,
172
+ values,
173
+ fields,
174
+ };
175
+ }
176
+
177
+ /*
178
+ * Select display-ready log text for one locale without mutating source data.
179
+ */
180
+ export function getLogDisplayText(log: LogTextSource, displayLanguage?: string | null): LogDisplayText {
181
+ const source = getOriginalLogTextParts(log);
182
+ const translationBundle = getLogTranslationBundle(log);
183
+ const normalizedLanguage = normalizeLogLanguage(displayLanguage) || displayLanguage?.trim().toLowerCase() || null;
184
+ const localeTranslation = normalizedLanguage ? getLogLocaleTranslation(translationBundle, normalizedLanguage) : null;
185
+
186
+ return {
187
+ summary: getTranslatedText(localeTranslation?.summary, source.summary) || '',
188
+ notes: getTranslatedText(localeTranslation?.notes, source.notes),
189
+ item: getTranslatedText(localeTranslation?.item, source.item),
190
+ values: getTranslatedLogValues(source.values, localeTranslation) as LogDisplayText['values'],
191
+ fields: getTranslatedLogFields(source.fields, localeTranslation),
192
+ };
193
+ }
194
+
195
+ /*
196
+ * Flatten every display-language text field into deterministic embedding and
197
+ * search input while omitting null and empty values.
198
+ */
199
+ export function getTextFormatDisplayLog(log: LogTextSource, displayLanguage?: string | null) {
200
+ const displayText = getLogDisplayText(log, displayLanguage);
201
+ const sourceTexts = getLogTranslationSourceTexts({
202
+ summary: displayText.summary,
203
+ details: displayText,
204
+ });
205
+
206
+ return [
207
+ sourceTexts.summary,
208
+ sourceTexts.item,
209
+ sourceTexts.notes,
210
+ ...sourceTexts.values.map((value) => value.label),
211
+ ...sourceTexts.fields.map((field) => field.textValue),
212
+ ]
213
+ .filter((value): value is string => typeof value === 'string' && !!value.trim())
214
+ .map((value) => value.trim())
215
+ .join('\n');
216
+ }
217
+
218
+ /*
219
+ * Flatten every original log text field into deterministic embedding and
220
+ * search input while omitting null and empty values.
221
+ */
222
+ export function getTextFormatOriginalLog(log: LogTextSource) {
223
+ const sourceTexts = getLogTranslationSourceTexts(log);
224
+
225
+ return [
226
+ sourceTexts.summary,
227
+ sourceTexts.item,
228
+ sourceTexts.notes,
229
+ ...sourceTexts.values.map((value) => value.label),
230
+ ...sourceTexts.fields.map((field) => field.textValue),
231
+ ]
232
+ .filter((value): value is string => typeof value === 'string' && !!value.trim())
233
+ .map((value) => value.trim())
234
+ .join('\n');
235
+ }
236
+
237
+ /*
238
+ * Return whether one log activity uses the shared accounting metadata fields.
239
+ */
240
+ export function isAccountingLogActivity(activity?: string | null) {
241
+ return FILE_CONSTANTS.accountingActivities.has(activity || '');
242
+ }
243
+
244
+ /*
245
+ * Return whether a hydrated log belongs to the system-only app interactions
246
+ * group.
247
+ */
248
+ export function isAppInteractionLog(log: { details?: { logType?: string | null } | null } | null | undefined): boolean {
249
+ return log?.details?.logType === APP_INTERACTIONS_LOG_GROUP;
28
250
  }
29
251
 
30
252
  /**
31
- * Map log type to a color
32
- * @param type - The type of the log entry
33
- * @returns The color associated with the journal type
253
+ * Get content type for log by activity
34
254
  */
35
255
 
36
- export function getLogCategoryColor(type: LogTypeEnum) {
37
- // Do switch operation here
38
- // switch (operation) {
39
- // case 'ARABLE':
40
- // case 'LogArable':
41
- // // ..
42
- // break;
43
- // case 'LIVESTOCK':
44
- // case 'LogLivestock':
45
- // // ..
46
- // break;
47
- // default:
48
- // }
49
-
50
- const logTypeToColor = {
51
- // #### ARABLE
52
- SEED: 'brown',
53
- PLANTING: 'amber',
54
- FIELD: 'lime',
55
- HARVEST: 'emerald',
56
- POST_HARVEST: 'cyan',
57
- SALES: 'blue',
58
- WATER: 'violet',
59
- DELETED: 'medium',
60
-
61
- // #### ARABLE - Food Safety
62
- HYGIENE: 'teal',
63
- SANITATION: 'sky',
64
- EQUIPMENT: 'indigo',
65
- BIOSECURITY: 'green',
66
- EMPLOYEE: 'yellow',
67
-
68
- // #### FARMERS_MARKET
69
- MARKET_RECEIPT: 'blue',
70
- MARKET_OPERATION: 'rose',
71
-
72
- // #### GROWER_NETWORK
73
- WORKER_PRACTICE: 'purple',
74
- PRODUCTION_INPUT: 'sky',
75
- OPERATION: 'yellow',
76
-
77
- // #### LIVESTOCK
78
- SUPPLY_PURCHASE: 'brown',
79
- LIVESTOCK_LIFE_CYCLE: 'amber',
80
- LIVESTOCK_TRACKING: 'lime',
81
- PASTURE_LAND_MANAGEMENT: 'green',
82
- LIVESTOCK_HEALTHCARE: 'teal',
83
- LIVESTOCK_SALE: 'blue',
84
- } as Record<LogTypeEnum, string>;
85
-
86
- // Default to zinc if type is not found
87
- const color = logTypeToColor[type] || 'zinc';
88
- return color;
256
+ export function getLogContentName(activity: string): LogContentName {
257
+ if (activity === 'MARKET_CREDIT_RECEIPT') {
258
+ return 'receipt';
259
+ }
260
+
261
+ return isAccountingLogActivity(activity) ? 'invoice' : 'log';
89
262
  }
90
263
 
91
264
  /**
@@ -96,7 +269,7 @@ export function getLogCategoryColor(type: LogTypeEnum) {
96
269
 
97
270
  export function getLogTypesForOperation(operation: OrganizationOperationEnum): LogTypeEnum[] {
98
271
  const logTypes = (LOG_TYPES_BY_OPERATION[operation] || []) as LogTypeEnum[];
99
- return logTypes;
272
+ return logTypes;
100
273
  }
101
274
 
102
275
  /**
@@ -107,30 +280,9 @@ export function getLogTypesForOperation(operation: OrganizationOperationEnum): L
107
280
  */
108
281
 
109
282
  export function getLogTypeFromActivity(operation: OrganizationOperationEnum | string, activity: any): LogTypeEnum | null {
110
- let logGroup;
111
- switch (operation) {
112
- case 'ARABLE':
113
- case 'LogArable':
114
- logGroup = ARABLE_ACTIVITIES_GROUPED.find((group: any) => group[1].includes(activity));
115
- break;
116
- case 'LIVESTOCK':
117
- case 'LogLivestock':
118
- logGroup = LIVESTOCK_ACTIVITIES_GROUPED.find((group: any) => group[1].includes(activity));
119
- break;
120
- case 'FARMERS_MARKET':
121
- case 'LogFarmersMarket':
122
- logGroup = FARMERS_MARKET_ACTIVITIES_GROUPED.find((group: any) => group[1].includes(activity));
123
- break;
124
- case 'GROWER_NETWORK':
125
- case 'LogGrowerNetwork':
126
- logGroup = GROWER_NETWORK_ACTIVITIES_GROUPED.find((group: any) => group[1].includes(activity));
127
- break;
128
- default:
129
- console.warn('(!1) Cannot get log type from unknown operation type:', operation);
130
- return null;
131
- }
283
+ const logGroup = (LOG_ACTIVITIES_BY_OPERATION[operation] || []).find((group: any) => group[1].includes(activity));
132
284
 
133
- return logGroup?.[0] as LogTypeEnum || null;
285
+ return (logGroup?.[0] as LogTypeEnum) || null;
134
286
  }
135
287
 
136
288
  /**
@@ -143,34 +295,41 @@ export function getActivityText(activity: any, tryShortForm?: boolean): string {
143
295
  if (tryShortForm && i18n.has(`log.activity_short.${activity}`)) {
144
296
  return i18n.t(`log.activity_short.${activity}`);
145
297
  }
146
- return i18n.t(`log.activity.${activity}`);
298
+ return i18n.has(`log.activity.${activity}`) ? i18n.t(`log.activity.${activity}`) : humanizeRegistryKey(activity);
147
299
  }
148
300
 
149
301
  /**
150
- * Get the title for log entry for each Union interface
151
- * @param details - The details Union interface for log entry
302
+ * Get the title for a generic log entry.
303
+ * @param details - The generic details interface for the log entry
152
304
  * @param isServer - True if used in server
153
- * @param logType - Use this param if this function is used in server; {d.type} will be undefined unless manually set
305
+ * @param logType - Optional group-key override for partial or form details
154
306
  * @param iface - In unique cases (such as client-side form), you can use this for interface name
155
307
  * @return The title for the log entry
156
308
  */
157
309
 
158
- export function getLogEntryTitle(d: any, isServer?: boolean, logType_?: string, iface?: string): string {
159
- const { __typename, __table, type } = d;
160
- const logType = logType_ || type;
161
- const md = isServer ? (d.metadata || {}) : d;
310
+ export function getLogEntryTitle(d: any, isServer?: boolean, logType_?: string | null, iface?: string): string {
311
+ const { __typename, __table, type, logType: storedLogType } = d;
312
+ const logType = logType_ || type || storedLogType;
313
+ const md = isServer ? d.metadata || {} : d;
162
314
 
163
315
  switch (iface || __typename || __table) {
164
- case 'ARABLE':
165
- case 'logs_arable':
166
- case 'LogArable': {
316
+ case 'log_entry_details':
317
+ case 'LogEntryDetails': {
167
318
  const quantityText = joinReadable([formatDecimal(md.quantity, true, true), md.unit], ' ', ' ');
168
319
 
169
320
  let titleItem = ucFirst(md.item);
170
321
  let logSpecificText = '';
171
322
 
172
323
  if (logType === 'WATER') {
173
- logSpecificText = joinReadable([md.concentration, md.concentration && md.concentrationUnit], ' ', ' ');
324
+ const concentration = getLogEntryDetailsFieldValueByKey(md, 'concentration');
325
+ logSpecificText = joinReadable(
326
+ [
327
+ concentration === null ? null : String(concentration),
328
+ concentration === null ? null : getLogEntryDetailsFieldUnitByKey(md, 'concentration'),
329
+ ],
330
+ ' ',
331
+ ' ',
332
+ );
174
333
  } else if (['SEED', 'SALES'].includes(logType)) {
175
334
  if (!titleItem) {
176
335
  titleItem = i18n.t('log.unknown_crop');
@@ -182,92 +341,17 @@ export function getLogEntryTitle(d: any, isServer?: boolean, logType_?: string,
182
341
  // console.log(md);
183
342
  // console.log('titleItem', titleItem);
184
343
 
185
- return textWithBrackets(
186
- titleItem,
187
- [
188
- logSpecificText + (logSpecificText && quantityText ? ',' : ''),
189
- quantityText,
190
- ],
191
- );
192
- }
193
- case 'FARMERS_MARKET':
194
- case 'logs_farmers_market':
195
- case 'LogFarmersMarket': {
196
- let creditsText = '';
197
- if (Array.isArray(md.values) && md.values.length) {
198
- creditsText = md.values.map((item: any) => {
199
- return `${formatCurrency(item.value, false)} ${item.label}`;
200
- }).join(', ');
201
-
344
+ const priceText = md.price ? formatCurrency(md.price, false) : '';
345
+ if (logType === 'MARKET_RECEIPT' && Array.isArray(md.values) && md.values.length) {
346
+ const creditsText = md.values.map((item: any) => `${formatCurrency(item.value, false)} ${item.label}`).join(', ');
202
347
  const totalAmount = md.values.reduce((sum: number, item: any) => sum + Number(item.value || 0), 0);
203
- creditsText += ` = ${i18n.t('form.total')}: ${formatCurrency(totalAmount, false)}`;
348
+ const voidText = isServer && md.voided ? i18n.t('c.void').toUpperCase() : '';
349
+ return `${voidText ? `(${voidText}) ` : ''}${creditsText} = ${i18n.t('c.total')}: ${formatCurrency(totalAmount, false)}`;
204
350
  }
205
351
 
206
- const voidText = isServer && md.voided ? i18n.t('form.void').toUpperCase() : '';
207
- return `${creditsText && voidText ? '(' + voidText + ')' : voidText} ${creditsText}`.trim();
208
- }
209
- case 'GROWER_NETWORK':
210
- case 'logs_grower_network':
211
- case 'LogGrowerNetwork':
212
- return textWithBrackets(
213
- ucFirst(md.item),
214
- md.location,
215
- [' - ', ''],
216
- );
217
- case 'LIVESTOCK':
218
- case 'logs_livestock':
219
- case 'LogLivestock': {
220
- if (['SUPPLY_PURCHASE', 'LIVESTOCK_SALE'].includes(logType)) {
221
- return joinReadable(
222
- [
223
- formatReferenceNumber(md?.referenceNumber),
224
- formatCurrency((md?.values || []).reduce((sum: number, item: any) => sum + Number(item.value || 0), 0), false),
225
- ],
226
- ' - ',
227
- ' - ',
228
- );
229
- }
230
-
231
- if (isServer) {
232
- let livestockText = '';
233
- if (md.livestock) {
234
- livestockText += 'Species: ' + ucFirst(md.livestock) + '\n';
235
- }
236
- if (d.livestockGroup) {
237
- livestockText += 'Group: ' + d.livestockGroup + '\n';
238
- }
239
- if (Array.isArray(d.livestockIdentifiers) && d.livestockIdentifiers.length) {
240
- livestockText += 'Animal ID(s): ' + d.livestockIdentifiers.join(', ') + '\n';
241
- }
242
- if (d.damIdentifier) {
243
- livestockText += 'Dam ID: ' + d.damIdentifier + '\n';
244
- }
245
- return livestockText.trim();
246
- }
247
-
248
- return textWithBrackets(
249
- ucFirst(md.livestock),
250
- (d.livestockGroup ? [d.livestockGroup] : []).concat(
251
- (d.livestockIdentifiers || []).map((id: string) => `#${id}`),
252
- ).join(', ') + (
253
- d.damIdentifier ? ` (Dam: #${d.damIdentifier})` : ''
254
- ),
255
- [' - ', ''],
256
- );
352
+ return textWithBrackets(titleItem, [logSpecificText || priceText + (priceText && quantityText ? ',' : ''), quantityText]);
257
353
  }
258
354
  default:
259
355
  }
260
356
  return '..';
261
357
  }
262
-
263
- /**
264
- * Get the text format for this log
265
- * @param log - Log entry object from database
266
- * @return Log in text format
267
- */
268
-
269
- export function getTextFormatLog(log: LogEntryData) {
270
- const { notes } = log.details;
271
- const title = getLogEntryTitle(log.details || {}, true);
272
- return (title + '\n\n' + (notes + '')).trim();
273
- }