@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,18 +1,21 @@
1
- import { COLORS } from '@jsb188/app/constants/app.ts';
2
- import { intersection } from '@jsb188/app/utils/object.ts';
1
+ import type { SupportedLogLanguage } from '@jsb188/app/constants/app.ts';
2
+ import { COLORS, isSupportedLogLanguage } from '@jsb188/app/constants/app.ts';
3
3
  import { shortenToSpace } from '@jsb188/app/utils/string.ts';
4
4
  import { DEFAULT_TIMEZONE } from '@jsb188/app/utils/timeZone.ts';
5
- import { FEATURES_BY_OPERATION } from '../constants/product.ts';
5
+ import { ORG_CONTACTS } from '../constants/organization.ts';
6
6
  import type {
7
- ACLPermissionEnum,
8
- MergedOrgContact,
9
- OrgContact,
10
- OrganizationFeatureEnum,
11
- OrganizationOperationEnum,
12
- OrganizationRelData,
13
- OrganizationRelGQL,
14
- OrganizationRoleEnum,
15
- OrganizationSettingsObj
7
+ ACLPermissionEnum,
8
+ MergedOrgContact,
9
+ OrganizationChildData,
10
+ OrganizationChildGQL,
11
+ OrganizationContactData,
12
+ OrganizationDepartmentEnum,
13
+ OrganizationOperationEnum,
14
+ OrganizationRelData,
15
+ OrganizationRelGQL,
16
+ OrganizationRoleEnum,
17
+ OrganizationSettingsObj,
18
+ OrgContact,
16
19
  } from '../types/organization.d.ts';
17
20
 
18
21
  // Placeholder to match Server import
@@ -20,19 +23,37 @@ type ViewerOrganization = any;
20
23
 
21
24
  type OperationPermissionTuple = readonly [PermissionCheckFor, ACLPermissionEnum, string];
22
25
  type OperationPermissionsMap = Record<string, OperationPermissionTuple>;
26
+ type OrganizationChildPreferredContacts = OrgContact[] | Record<string, Partial<OrgContact> | null | undefined> | null | undefined;
27
+ type OrganizationChildContactSource = {
28
+ organization?: {
29
+ id?: string | number | bigint | null;
30
+ readableId?: string | null;
31
+ directory?: OrgContact[] | null;
32
+ name?: string | null;
33
+ operation?: OrganizationOperationEnum | string | null;
34
+ } | null;
35
+ preferredContacts?: OrganizationChildPreferredContacts;
36
+ };
23
37
 
24
38
  export const OPERATION_PERMISSIONS = {
25
- chat: ['viewData', 'READ', 'You do not have permission to chat with AI in this organization.'],
26
- general_read: ['viewData', 'READ', 'You do not have permission to view this organization\'s data.'],
27
- general_write: ['viewData', 'WRITE', 'You do not have permission to write data in this organization.'],
28
- integrations_update: ['integrations', 'MANAGE', 'You do not have permission to manage this organization\'s app connections.'],
29
- ai_inbox_management: ['orgManagement', 'MANAGE', 'You do not have permission to manage this organization\'s AI inbox.'],
30
- ai_inbox_read: ['orgManagement', 'READ', 'You do not have permission to read this organization\'s AI inbox.'],
31
- logs_write: ['logs', 'WRITE', 'You do not have permission to write logs in this organization.'],
32
- org_directory: ['logs', 'WRITE', 'You do not have permission to manage this organization\'s directory book.'],
33
- org_management: ['orgManagement', 'WRITE', 'You do not have permission to edit this organization\'s data.'],
39
+ billing_read: ['billing', 'READ', "You do not have permission to view this organization's billing usage."],
40
+ billing_manage: ['billing', 'MANAGE', "You do not have permission to manage this organization's billing."],
41
+ chat: ['viewData', 'READ', 'You do not have permission to chat with AI in this organization.'],
42
+ campaign_edit: ['products', 'WRITE', 'You do not have permission to edit campaigns in this organization.'],
43
+ general_read: ['viewData', 'READ', "You do not have permission to view this organization's data."],
44
+ general_write: ['viewData', 'WRITE', 'You do not have permission to write data in this organization.'],
45
+ integrations_update: ['integrations', 'MANAGE', "You do not have permission to manage this organization's app connections."],
46
+ ai_inbox_management: ['orgManagement', 'MANAGE', "You do not have permission to manage this organization's AI inbox."],
47
+ ai_inbox_read: ['orgManagement', 'READ', "You do not have permission to read this organization's AI inbox."],
48
+ logs_write: ['logs', 'WRITE', 'You do not have permission to write logs in this organization.'],
49
+ member_sessions_manage: ['members', 'MANAGE', 'You do not have permission to manage member sessions in this organization.'],
50
+ org_directory: ['logs', 'WRITE', "You do not have permission to manage this organization's directory book."],
51
+ agent_notes_manage: ['viewData', 'MANAGE', 'You do not have permission to manage AI notes in this organization.'],
52
+ org_management: ['orgManagement', 'WRITE', "You do not have permission to edit this organization's data."],
53
+ org_settings_manage: ['settings', 'MANAGE', "You do not have permission to manage this organization's settings."],
34
54
  org_sites_update: ['logs', 'WRITE', 'You do not have permission to update organization sites.'],
35
- data_tables_edit: ['viewData', 'MANAGE', 'You do not have permission to edit dataTables in this organization.'],
55
+ schedule_edit: ['viewData', 'MANAGE', 'You do not have permission to edit schedules in this organization.'],
56
+ sheets_edit: ['viewData', 'MANAGE', 'You do not have permission to edit sheets & data tables in this organization.'],
36
57
  } as const satisfies OperationPermissionsMap;
37
58
 
38
59
  export type OperationName = keyof typeof OPERATION_PERMISSIONS;
@@ -88,11 +109,7 @@ export function sanitizeOrganizationInboundEmailHandleInput(value?: string | nul
88
109
  export function isValidOrganizationInboundEmailHandle(value?: string | null): boolean {
89
110
  const handle = String(value || '');
90
111
 
91
- return !!(
92
- handle &&
93
- handle.length <= ORGANIZATION_INBOUND_EMAIL_MAX_LENGTH &&
94
- ORGANIZATION_INBOUND_EMAIL_ALLOWED_REGEX.test(handle)
95
- );
112
+ return !!(handle && handle.length <= ORGANIZATION_INBOUND_EMAIL_MAX_LENGTH && ORGANIZATION_INBOUND_EMAIL_ALLOWED_REGEX.test(handle));
96
113
  }
97
114
 
98
115
  const INT_TO_PERMISSION = Object.keys(PERMISSION_TO_INT);
@@ -103,13 +120,7 @@ const INT_TO_PERMISSION = Object.keys(PERMISSION_TO_INT);
103
120
  */
104
121
 
105
122
  export function getRoleValue(role: OrganizationRoleEnum): number {
106
- return [
107
- 'GUEST',
108
- 'MEMBER',
109
- 'MANAGER',
110
- 'ADMIN',
111
- 'OWNER',
112
- ].indexOf(role);
123
+ return ['GUEST', 'MEMBER', 'MANAGER', 'ADMIN', 'OWNER'].indexOf(role);
113
124
  }
114
125
 
115
126
  /**
@@ -135,8 +146,21 @@ export function getDefaultPermissionsByRole(orgRel: OrganizationRelGQL | Organiz
135
146
  // 0 = "NONE"
136
147
 
137
148
  switch (role) {
138
- case 'ADMIN':
139
149
  case 'OWNER':
150
+ return {
151
+ billing: 'MANAGE',
152
+ compliance: 'MANAGE',
153
+ digests: 'MANAGE',
154
+ finances: 'MANAGE',
155
+ integrations: 'MANAGE',
156
+ logs: 'MANAGE',
157
+ members: 'MANAGE',
158
+ orgManagement: 'MANAGE',
159
+ products: 'MANAGE',
160
+ settings: 'MANAGE',
161
+ viewData: 'MANAGE',
162
+ };
163
+ case 'ADMIN':
140
164
  return {
141
165
  billing: acl.billing || 'MANAGE',
142
166
  compliance: acl.compliance || 'MANAGE',
@@ -146,7 +170,7 @@ export function getDefaultPermissionsByRole(orgRel: OrganizationRelGQL | Organiz
146
170
  logs: acl.logs || 'MANAGE', // "READ" for logs does nothing
147
171
  members: acl.members || 'MANAGE',
148
172
  orgManagement: acl.orgManagement || 'MANAGE',
149
- products: acl.products || 'MANAGE', // products = reports
173
+ products: acl.products || 'MANAGE', // products = field docs
150
174
  settings: acl.settings || 'MANAGE',
151
175
  viewData: acl.viewData || 'MANAGE', // This blocks access from users being able to read other people's logs
152
176
  };
@@ -174,7 +198,7 @@ export function getDefaultPermissionsByRole(orgRel: OrganizationRelGQL | Organiz
174
198
  logs: acl.logs || 'NONE',
175
199
  members: acl.members || 'NONE',
176
200
  orgManagement: acl.orgManagement || 'NONE',
177
- products: acl.products || 'READ', // products = reports
201
+ products: acl.products || 'READ', // products = field docs
178
202
  settings: acl.settings || 'NONE',
179
203
  viewData: acl.viewData || 'READ',
180
204
  };
@@ -191,7 +215,7 @@ export function getDefaultPermissionsByRole(orgRel: OrganizationRelGQL | Organiz
191
215
  logs: acl.logs || 'WRITE', // "READ" for logs does nothing, also "WRITE" is required for directory/site management
192
216
  members: acl.members || 'READ',
193
217
  orgManagement: acl.orgManagement || 'READ',
194
- products: acl.products || 'READ', // products = reports
218
+ products: acl.products || 'READ', // products = field docs
195
219
  settings: acl.settings || 'READ',
196
220
  viewData: acl.viewData || 'WRITE',
197
221
  };
@@ -227,14 +251,14 @@ export function checkACLPermission(
227
251
 
228
252
  export function checkACL(
229
253
  orgRel: OrganizationRelGQL | OrganizationRelData | ViewerOrganization | null | undefined,
230
- opName: OperationName,
254
+ opName: OperationName,
231
255
  ) {
232
- const [permissionCheckFor, requiredPermission] = OPERATION_PERMISSIONS[opName] || [];
233
- if (!permissionCheckFor || !requiredPermission) {
234
- console.warn(`Operation ${opName} not found in permissions map.`);
235
- return null;
236
- }
237
- return checkACLPermission(orgRel, permissionCheckFor, requiredPermission);
256
+ const [permissionCheckFor, requiredPermission] = OPERATION_PERMISSIONS[opName] || [];
257
+ if (!permissionCheckFor || !requiredPermission) {
258
+ console.warn(`Operation ${opName} not found in permissions map.`);
259
+ return null;
260
+ }
261
+ return checkACLPermission(orgRel, permissionCheckFor, requiredPermission);
238
262
  }
239
263
 
240
264
  /**
@@ -242,9 +266,7 @@ export function checkACL(
242
266
  * @returns true if at least one ACL permission differs from default role ACL
243
267
  */
244
268
 
245
- export function isCustomACLForRole(
246
- orgRel: OrganizationRelGQL | OrganizationRelData | ViewerOrganization | null | undefined,
247
- ): boolean {
269
+ export function isCustomACLForRole(orgRel: OrganizationRelGQL | OrganizationRelData | ViewerOrganization | null | undefined): boolean {
248
270
  if (!orgRel) {
249
271
  return false;
250
272
  }
@@ -275,54 +297,105 @@ export function isCustomACLForRole(
275
297
 
276
298
  export function getDefaultOrganizationSettings(
277
299
  timeZone: string | null,
278
- contacts?: OrgContact[],
279
300
  // do params here for priority service + manage roles
280
301
  ): Partial<OrganizationSettingsObj> {
281
- const orgSettings: Record<string, any> = {
302
+ return {
282
303
  timeZone: timeZone || DEFAULT_TIMEZONE,
304
+ displayTranslatedLogs: true,
305
+ translateLanguage: 'en',
283
306
  color: COLORS[Math.floor(Math.random() * COLORS.length)],
284
307
  };
308
+ }
285
309
 
286
- if (contacts?.length) {
287
- orgSettings.directory = contacts.map((contact) => {
288
- return {
289
- ...contact,
290
- department: contact.department || 'OTHER',
291
- };
292
- }).filter((contact) => contact.emailAddress || contact.phoneNumber);
310
+ /*
311
+ * Return one effective organization log-language setting while preserving an explicit disabled value.
312
+ */
313
+ export function getOrganizationLogLanguageSetting(
314
+ settings: Partial<OrganizationSettingsObj> | Record<string, unknown> | null | undefined,
315
+ settingName: 'translateLanguage',
316
+ ): SupportedLogLanguage | null {
317
+ const value = settings?.[settingName];
318
+
319
+ if (value === null) {
320
+ return null;
293
321
  }
294
322
 
295
- return orgSettings;
323
+ return isSupportedLogLanguage(value) ? value : 'en';
296
324
  }
297
325
 
298
- /**
299
- * Get the enabled features for the organization;
300
- * If none or empty, return the default features based on operation
326
+ /*
327
+ * Return whether organization log views should prefer available translated text.
301
328
  */
329
+ export function getOrganizationDisplayTranslatedLogsSetting(
330
+ settings: Partial<OrganizationSettingsObj> | Record<string, unknown> | null | undefined,
331
+ ): boolean {
332
+ return typeof settings?.displayTranslatedLogs === 'boolean' ? settings.displayTranslatedLogs : true;
333
+ }
302
334
 
303
- export function getOrganizationFeatures(
304
- operation: OrganizationOperationEnum,
305
- enabledFeatures?: OrganizationFeatureEnum[] | null,
306
- ): OrganizationFeatureEnum[] {
307
- // @ts-expect-error - Allow operation as string
308
- const allowedFeatures = FEATURES_BY_OPERATION[operation] || [];
309
- const filteredFeatures = intersection(enabledFeatures || [], allowedFeatures);
310
- return filteredFeatures.length ? filteredFeatures : allowedFeatures[0] ? [allowedFeatures[0]] : [];
335
+ /*
336
+ * Resolve the translation locale used by read-only log views, or null when
337
+ * the organization prefers original text or has translation disabled.
338
+ */
339
+ export function getOrganizationLogDisplayLanguage(
340
+ settings: Partial<OrganizationSettingsObj> | Record<string, unknown> | null | undefined,
341
+ ): SupportedLogLanguage | null {
342
+ return getOrganizationDisplayTranslatedLogsSetting(settings)
343
+ ? getOrganizationLogLanguageSetting(settings, 'translateLanguage')
344
+ : null;
311
345
  }
312
346
 
313
- /**
314
- * Map organization feature to a color (used for workflows without logType)
315
- * @param feature - The feature of the workflow
316
- * @returns The color associated with the feature
347
+ /*
348
+ * Map organization_contacts rows to plain directory contacts, ordered by department.
349
+ */
350
+ export function mapOrgContactRowsToContacts(rows?: Array<Partial<OrganizationContactData>> | null): OrgContact[] {
351
+ return (rows || [])
352
+ .filter((row) => row.department)
353
+ .map((row) => ({
354
+ department: row.department as OrganizationDepartmentEnum,
355
+ name: row.name || null,
356
+ phoneNumber: row.phoneNumber || null,
357
+ emailAddress: row.emailAddress || null,
358
+ }))
359
+ .sort((a, b) => ORG_CONTACTS.indexOf(a.department) - ORG_CONTACTS.indexOf(b.department));
360
+ }
361
+
362
+ /*
363
+ * Map parent-override organization_contacts rows to the department-keyed preferred contacts shape.
317
364
  */
365
+ export function mapOrgContactRowsToPreferredContactsMap(
366
+ rows?: Array<Partial<OrganizationContactData>> | null,
367
+ ): Record<string, OrgContact> {
368
+ return mapOrgContactRowsToContacts(rows).reduce(
369
+ (acc, contact) => {
370
+ acc[contact.department] = contact;
371
+ return acc;
372
+ },
373
+ {} as Record<string, OrgContact>,
374
+ );
375
+ }
318
376
 
319
- export function getOrganizationFeatureColor(feature?: OrganizationFeatureEnum | null) {
320
- const featureToColor = {
321
- FOOD_SAFETY: 'sky',
322
- GLOBAL_GAP: 'brown',
323
- } as Record<OrganizationFeatureEnum, string>;
377
+ /*
378
+ * Split one org's contact rows into own-directory rows and one parent's override rows.
379
+ */
380
+ export function splitOrgContactRowsByScope(
381
+ rows: Array<Partial<OrganizationContactData>>,
382
+ ownerOrgId?: number | bigint | null,
383
+ ): {
384
+ ownRows: Array<Partial<OrganizationContactData>>;
385
+ overrideRows: Array<Partial<OrganizationContactData>>;
386
+ } {
387
+ const ownRows: Array<Partial<OrganizationContactData>> = [];
388
+ const overrideRows: Array<Partial<OrganizationContactData>> = [];
389
+
390
+ for (const row of rows) {
391
+ if (row.ownerOrgId == null) {
392
+ ownRows.push(row);
393
+ } else if (ownerOrgId != null && row.ownerOrgId == ownerOrgId) {
394
+ overrideRows.push(row);
395
+ }
396
+ }
324
397
 
325
- return (feature && featureToColor[feature]) || 'stone';
398
+ return { ownRows, overrideRows };
326
399
  }
327
400
 
328
401
  /**
@@ -335,11 +408,9 @@ function hasPreferredOverride(dirContact: OrgContact, preferred?: OrgContact) {
335
408
  }
336
409
 
337
410
  return (
338
- preferred.name !== undefined && preferred.name !== dirContact.name
339
- ) || (
340
- preferred.phoneNumber !== undefined && preferred.phoneNumber !== dirContact.phoneNumber
341
- ) || (
342
- preferred.emailAddress !== undefined && preferred.emailAddress !== dirContact.emailAddress
411
+ (preferred.name !== undefined && preferred.name !== dirContact.name) ||
412
+ (preferred.phoneNumber !== undefined && preferred.phoneNumber !== dirContact.phoneNumber) ||
413
+ (preferred.emailAddress !== undefined && preferred.emailAddress !== dirContact.emailAddress)
343
414
  );
344
415
  }
345
416
 
@@ -414,3 +485,185 @@ export function mergeOrgDirectory(
414
485
 
415
486
  return merged;
416
487
  }
488
+
489
+ /*
490
+ * Upsert one preferred contact by department without mutating the source list.
491
+ */
492
+ export function upsertOrgPreferredContactByDepartment(
493
+ preferredContacts: OrgContact[] | undefined,
494
+ department: OrgContact['department'],
495
+ values: Partial<Pick<OrgContact, 'emailAddress' | 'name' | 'phoneNumber'>>,
496
+ ): OrgContact[] {
497
+ const source = preferredContacts || [];
498
+ const nextContact: OrgContact = {
499
+ department,
500
+ name: values.name || null,
501
+ phoneNumber: values.phoneNumber || null,
502
+ emailAddress: values.emailAddress || null,
503
+ };
504
+
505
+ if (!source.some((contact) => contact.department === department)) {
506
+ return [...source, nextContact];
507
+ }
508
+
509
+ return source.map((contact) => (contact.department === department ? nextContact : contact));
510
+ }
511
+
512
+ /*
513
+ * Remove one preferred contact department without mutating the source list.
514
+ */
515
+ export function removeOrgPreferredContactByDepartment(
516
+ preferredContacts: OrgContact[] | undefined,
517
+ department: OrgContact['department'],
518
+ ): OrgContact[] {
519
+ return (preferredContacts || []).filter((contact) => contact.department !== department);
520
+ }
521
+
522
+ /*
523
+ * Restore one preferred contact department from a saved contact snapshot.
524
+ */
525
+ export function restoreOrgPreferredContactByDepartment(
526
+ currentPreferredContacts: OrgContact[] | undefined,
527
+ savedPreferredContacts: OrgContact[] | undefined,
528
+ department: OrgContact['department'],
529
+ ): OrgContact[] {
530
+ const savedContact = (savedPreferredContacts || []).find((contact) => contact.department === department);
531
+ const withoutDepartment = (currentPreferredContacts || []).filter((contact) => contact.department !== department);
532
+
533
+ return savedContact ? [...withoutDepartment, savedContact] : withoutDepartment;
534
+ }
535
+
536
+ /*
537
+ * Find the editable version of a merged contact department.
538
+ */
539
+ export function getEditableOrgContactByDepartment(
540
+ list: MergedOrgContact[],
541
+ department: OrgContact['department'],
542
+ ): MergedOrgContact | null {
543
+ return list.find((item) => item.department === department && !item.locked) || null;
544
+ }
545
+
546
+ /*
547
+ * Compare preferred-contact lists by normalized department and value pairs.
548
+ */
549
+ export function areOrgPreferredContactsEqual(a?: OrgContact[] | null, b?: OrgContact[] | null) {
550
+ const sourceA = a || [];
551
+ const sourceB = b || [];
552
+
553
+ if (sourceA.length !== sourceB.length) {
554
+ return false;
555
+ }
556
+
557
+ const normalizedA = [...sourceA].sort((x, y) => x.department.localeCompare(y.department));
558
+ const normalizedB = [...sourceB].sort((x, y) => x.department.localeCompare(y.department));
559
+
560
+ return normalizedA.every((item, index) => {
561
+ const other = normalizedB[index];
562
+
563
+ return (
564
+ item.department === other.department &&
565
+ (item.name || null) === (other.name || null) &&
566
+ (item.phoneNumber || null) === (other.phoneNumber || null) &&
567
+ (item.emailAddress || null) === (other.emailAddress || null)
568
+ );
569
+ });
570
+ }
571
+
572
+ /*
573
+ * Return one preferred contact with a department supplied from the map key when needed.
574
+ */
575
+ function normalizeOrganizationChildPreferredContact(department: string, contact?: Partial<OrgContact> | null): OrgContact | null {
576
+ if (!contact) {
577
+ return null;
578
+ }
579
+
580
+ const contactDepartment = contact.department || (department as OrganizationDepartmentEnum);
581
+
582
+ if (!contactDepartment) {
583
+ return null;
584
+ }
585
+
586
+ return {
587
+ department: contactDepartment,
588
+ name: contact.name,
589
+ phoneNumber: contact.phoneNumber,
590
+ emailAddress: contact.emailAddress,
591
+ };
592
+ }
593
+
594
+ /*
595
+ * Return preferred child-organization contacts in the array shape expected by directory helpers.
596
+ */
597
+ export function getOrganizationChildPreferredContacts(childOrganization?: OrganizationChildContactSource | null): OrgContact[] {
598
+ const preferredContacts = childOrganization?.preferredContacts;
599
+
600
+ if (Array.isArray(preferredContacts)) {
601
+ return preferredContacts
602
+ .map((contact) => normalizeOrganizationChildPreferredContact(contact.department, contact))
603
+ .filter(Boolean) as OrgContact[];
604
+ }
605
+
606
+ return Object.entries(preferredContacts || {})
607
+ .map(([department, contact]) => normalizeOrganizationChildPreferredContact(department, contact))
608
+ .filter(Boolean) as OrgContact[];
609
+ }
610
+
611
+ /*
612
+ * Return the child organization's directory with parent-specific preferred contacts applied.
613
+ */
614
+ export function getOrganizationChildMergedDirectory(childOrganization?: OrganizationChildContactSource | null): MergedOrgContact[] {
615
+ const organization = childOrganization?.organization;
616
+
617
+ return mergeOrgDirectory(organization?.directory || [], getOrganizationChildPreferredContacts(childOrganization));
618
+ }
619
+
620
+ /*
621
+ * Return the primary contact shown for one child organization relationship.
622
+ */
623
+ export function getOrganizationChildPrimaryContact(
624
+ childOrganization?: OrganizationChildContactSource | null,
625
+ ): MergedOrgContact | null {
626
+ const directory = getOrganizationChildMergedDirectory(childOrganization);
627
+
628
+ return directory.find((contact) => contact.department === 'PRIMARY_CONTACT') || directory[0] || null;
629
+ }
630
+
631
+ /*
632
+ * Return whether one child-organization list cell should open the organization profile.
633
+ */
634
+ export function isOrganizationChildProfileLinkCellKey(cellKey: string) {
635
+ return cellKey === 'organizationId' || cellKey === 'vendorName' || cellKey === 'name';
636
+ }
637
+
638
+ /*
639
+ * Return the display text used by child-organization lists.
640
+ */
641
+ export function getOrganizationChildListCellValue(
642
+ childOrganization: OrganizationChildData | OrganizationChildGQL | OrganizationChildContactSource | null | undefined,
643
+ cellKey: string,
644
+ ): string {
645
+ const organization = childOrganization?.organization;
646
+ const primaryContact = getOrganizationChildPrimaryContact(childOrganization);
647
+
648
+ switch (cellKey) {
649
+ case 'organizationId': {
650
+ const organizationReadableId = organization && 'readableId' in organization ? organization.readableId : null;
651
+ const organizationId = organization && 'id' in organization ? organization.id : null;
652
+ const childOrganizationId = childOrganization && 'childId' in childOrganization ? childOrganization.childId : null;
653
+
654
+ return (
655
+ organizationReadableId || (organizationId ? String(organizationId) : childOrganizationId ? String(childOrganizationId) : '')
656
+ );
657
+ }
658
+ case 'name':
659
+ return organization?.name || '';
660
+ case 'phone':
661
+ return primaryContact?.phoneNumber || '';
662
+ case 'email':
663
+ return primaryContact?.emailAddress || '';
664
+ case 'type':
665
+ return organization?.operation || '';
666
+ default:
667
+ return '';
668
+ }
669
+ }
@@ -0,0 +1,71 @@
1
+ import {
2
+ PAGE_ROUTE_MAX_COLUMNS,
3
+ PAGE_ROUTE_QUERY_ENUMS,
4
+ PAGE_ROUTE_RESERVED_SLUGS,
5
+ PAGE_ROUTE_SLUG_MAX_LENGTH,
6
+ PAGE_ROUTE_SLUG_MIN_LENGTH,
7
+ } from '../constants/pageRoute.ts';
8
+ import { isValidTableRouteColumnSettingsKey } from './table-route.ts';
9
+
10
+ /*
11
+ * Normalize free-form input into a page route slug candidate.
12
+ */
13
+ export function normalizePageRouteSlug(value?: string | null) {
14
+ return String(value || '')
15
+ .trim()
16
+ .toLowerCase();
17
+ }
18
+
19
+ /*
20
+ * Return whether a normalized page route slug is valid and not reserved.
21
+ */
22
+ export function isValidPageRouteSlug(slug?: string | null) {
23
+ return (
24
+ !!slug &&
25
+ slug.length >= PAGE_ROUTE_SLUG_MIN_LENGTH &&
26
+ slug.length <= PAGE_ROUTE_SLUG_MAX_LENGTH &&
27
+ /^[a-z0-9_-]+$/.test(slug) &&
28
+ !PAGE_ROUTE_RESERVED_SLUGS.includes(slug)
29
+ );
30
+ }
31
+
32
+ /*
33
+ * Return whether a value is a supported page route query name.
34
+ */
35
+ export function isValidPageRouteQuery(value?: string | null) {
36
+ return !!value && PAGE_ROUTE_QUERY_ENUMS.includes(value);
37
+ }
38
+
39
+ /*
40
+ * Return a unique, charset-validated page route column key list; null when any key is invalid.
41
+ */
42
+ export function getPageRouteColumnsFromInput(columns?: string[] | null) {
43
+ if (!Array.isArray(columns)) {
44
+ return null;
45
+ }
46
+
47
+ const seenColumnKeys = new Set<string>();
48
+ const columnKeys = columns.reduce((acc, value) => {
49
+ const columnKey = String(value || '');
50
+
51
+ if (
52
+ !isValidTableRouteColumnSettingsKey(columnKey) ||
53
+ seenColumnKeys.has(columnKey)
54
+ ) {
55
+ return acc;
56
+ }
57
+
58
+ seenColumnKeys.add(columnKey);
59
+ acc.push(columnKey);
60
+ return acc;
61
+ }, [] as string[]);
62
+
63
+ if (
64
+ columnKeys.length !== columns.length ||
65
+ columnKeys.length > PAGE_ROUTE_MAX_COLUMNS
66
+ ) {
67
+ return null;
68
+ }
69
+
70
+ return columnKeys;
71
+ }