@jsb188/mday 1.1.2

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 (202) hide show
  1. package/package.json +142 -0
  2. package/src/constants/agentMemory.ts +166 -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 +22 -0
  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 +146 -0
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/inboundContact.ts +8 -0
  17. package/src/constants/integration.ts +13 -0
  18. package/src/constants/log.ts +21 -0
  19. package/src/constants/logDisplay/README.md +88 -0
  20. package/src/constants/logDisplay/defaults.ts +39 -0
  21. package/src/constants/logDisplay/index.ts +27 -0
  22. package/src/constants/logDisplay/operations/arable.ts +16 -0
  23. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  24. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  25. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  26. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  27. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  28. package/src/constants/logTaxonomy.generated.ts +2199 -0
  29. package/src/constants/noteDoc.ts +77 -0
  30. package/src/constants/organization.ts +60 -0
  31. package/src/constants/pageRoute.ts +23 -0
  32. package/src/constants/qrCode.ts +30 -0
  33. package/src/constants/sheet.ts +132 -0
  34. package/src/constants/sms.ts +18 -0
  35. package/src/constants/square.ts +16 -0
  36. package/src/constants/storage.ts +85 -0
  37. package/src/constants/usage.ts +82 -0
  38. package/src/constants/workflowSteps.ts +60 -0
  39. package/src/schemas/editLogEntry.ts +879 -0
  40. package/src/schemas/inboundContact.ts +77 -0
  41. package/src/schemas/logsQuery.ts +93 -0
  42. package/src/types/account.d.ts +81 -0
  43. package/src/types/agentMemory.d.ts +100 -0
  44. package/src/types/agentNote.d.ts +441 -0
  45. package/src/types/billing.d.ts +537 -0
  46. package/src/types/campaign.d.ts +1533 -0
  47. package/src/types/contact.d.ts +15 -0
  48. package/src/types/csvExport.d.ts +213 -0
  49. package/src/types/dataAnalysis.d.ts +44 -0
  50. package/src/types/dataTable.d.ts +159 -0
  51. package/src/types/definitionElement.d.ts +35 -0
  52. package/src/types/docAccess.d.ts +96 -0
  53. package/src/types/dt.d.ts +257 -0
  54. package/src/types/email.d.ts +231 -0
  55. package/src/types/emailReplyReport.d.ts +76 -0
  56. package/src/types/fieldDoc.d.ts +702 -0
  57. package/src/types/fieldDocContract.d.ts +90 -0
  58. package/src/types/inboundContact.d.ts +65 -0
  59. package/src/types/integration.d.ts +42 -0
  60. package/src/types/log.d.ts +338 -0
  61. package/src/types/logDisplay.types.ts +85 -0
  62. package/src/types/organization.d.ts +350 -0
  63. package/src/types/pageRoute.d.ts +48 -0
  64. package/src/types/qrCode.d.ts +96 -0
  65. package/src/types/richText.d.ts +81 -0
  66. package/src/types/sheet.d.ts +847 -0
  67. package/src/types/sms.d.ts +38 -0
  68. package/src/types/storage.d.ts +481 -0
  69. package/src/types/usage.d.ts +106 -0
  70. package/src/types/workflow.d.ts +435 -0
  71. package/src/utils/agentMemory.ts +15 -0
  72. package/src/utils/agentNote.ts +850 -0
  73. package/src/utils/aiChat.ts +43 -0
  74. package/src/utils/aiChatWork.ts +64 -0
  75. package/src/utils/billing.ts +37 -0
  76. package/src/utils/campaign.ts +1173 -0
  77. package/src/utils/campaignAI.ts +72 -0
  78. package/src/utils/campaignAccess.ts +239 -0
  79. package/src/utils/campaignEmail.ts +601 -0
  80. package/src/utils/campaignEmailCompiler.ts +372 -0
  81. package/src/utils/campaignEmailImages.ts +60 -0
  82. package/src/utils/campaignEmailStyles.ts +544 -0
  83. package/src/utils/campaignLifecycle.ts +104 -0
  84. package/src/utils/campaignPreview.ts +285 -0
  85. package/src/utils/contact.ts +50 -0
  86. package/src/utils/csvExport.ts +19 -0
  87. package/src/utils/dataTable.ts +311 -0
  88. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  89. package/src/utils/dataTableRelativeDate.ts +447 -0
  90. package/src/utils/definitionElement.ts +84 -0
  91. package/src/utils/docAccess.ts +383 -0
  92. package/src/utils/documentName.ts +53 -0
  93. package/src/utils/email.ts +379 -0
  94. package/src/utils/emailReplyReport.test.ts +187 -0
  95. package/src/utils/emailReplyReport.ts +143 -0
  96. package/src/utils/fieldDoc.ts +157 -0
  97. package/src/utils/fieldDocAnswers.ts +128 -0
  98. package/src/utils/fieldDocDefaults.ts +36 -0
  99. package/src/utils/fieldDocProgram.ts +36 -0
  100. package/src/utils/fieldDocSummary.ts +30 -0
  101. package/src/utils/log.ts +357 -0
  102. package/src/utils/logDisplay.helpers.ts +177 -0
  103. package/src/utils/logDisplay.ts +87 -0
  104. package/src/utils/logRegistryValue.ts +90 -0
  105. package/src/utils/logStoreRead.ts +262 -0
  106. package/src/utils/logTaxonomy.helpers.ts +17 -0
  107. package/src/utils/logTaxonomy.ts +76 -0
  108. package/src/utils/logTaxonomyDisplay.ts +395 -0
  109. package/src/utils/noteDoc.ts +609 -0
  110. package/src/utils/organization.ts +669 -0
  111. package/src/utils/pageRoute.ts +71 -0
  112. package/src/utils/qrCode.helpers.ts +189 -0
  113. package/src/utils/qrCode.test.ts +519 -0
  114. package/src/utils/qrCode.ts +273 -0
  115. package/src/utils/richTextHTML.ts +385 -0
  116. package/src/utils/sheet.ts +262 -0
  117. package/src/utils/sheetAxisMove.ts +616 -0
  118. package/src/utils/sheetBootstrap.ts +1402 -0
  119. package/src/utils/sheetCellLiteral.ts +101 -0
  120. package/src/utils/sheetCommand.ts +358 -0
  121. package/src/utils/sheetDesign.ts +705 -0
  122. package/src/utils/sheetExport.ts +186 -0
  123. package/src/utils/sheetFeatureReference.ts +183 -0
  124. package/src/utils/sheetFormulaArgs.ts +168 -0
  125. package/src/utils/sheetFormulaCompile.ts +475 -0
  126. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  127. package/src/utils/sheetFormulaCriteria.ts +656 -0
  128. package/src/utils/sheetFormulaDate.ts +444 -0
  129. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  130. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  131. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  132. package/src/utils/sheetFormulaLogical.ts +116 -0
  133. package/src/utils/sheetFormulaLookup.ts +488 -0
  134. package/src/utils/sheetFormulaMath.ts +48 -0
  135. package/src/utils/sheetFormulaParser.ts +729 -0
  136. package/src/utils/sheetFormulaRange.ts +330 -0
  137. package/src/utils/sheetFormulaReference.ts +703 -0
  138. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  139. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  140. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  141. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  142. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  143. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  144. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  145. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  146. package/src/utils/sheetFormulaText.ts +430 -0
  147. package/src/utils/sheetJournal.ts +200 -0
  148. package/src/utils/sheetNumberFormat.ts +1012 -0
  149. package/src/utils/sheetPreview.ts +68 -0
  150. package/src/utils/sheetSequence.ts +118 -0
  151. package/src/utils/sheetSnapshot.ts +739 -0
  152. package/src/utils/sheetStructureEdit.ts +914 -0
  153. package/src/utils/sheetStructureReferences.ts +389 -0
  154. package/src/utils/square.ts +32 -0
  155. package/src/utils/storage.ts +198 -0
  156. package/src/utils/table-route.ts +262 -0
  157. package/src/utils/time.ts +12 -0
  158. package/src/utils/workflow.ts +506 -0
  159. package/src/utils/workflowLogFingerprint.ts +302 -0
  160. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  161. package/src/utils/workflowProgress.ts +280 -0
  162. package/src/utils/workflowTriggers.ts +61 -0
  163. package/tests/agentNote.test.ts +395 -0
  164. package/tests/campaignAI.test.ts +119 -0
  165. package/tests/campaignAccess.test.ts +239 -0
  166. package/tests/campaignEmail.test.ts +441 -0
  167. package/tests/campaignLifecycle.test.ts +134 -0
  168. package/tests/campaignPublication.test.ts +309 -0
  169. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  170. package/tests/dataTableRelativeDate.test.ts +412 -0
  171. package/tests/docAccess.test.ts +525 -0
  172. package/tests/editLogEntry.test.ts +112 -0
  173. package/tests/email.test.ts +39 -0
  174. package/tests/fieldDocProgram.test.ts +114 -0
  175. package/tests/logDisplay.fixtures.ts +67 -0
  176. package/tests/logDisplay.test.ts +227 -0
  177. package/tests/noteDoc.test.ts +514 -0
  178. package/tests/richTextHTML.test.ts +185 -0
  179. package/tests/sheetAxisMove.test.ts +352 -0
  180. package/tests/sheetBootstrap.test.ts +1967 -0
  181. package/tests/sheetFeatureReference.test.ts +22 -0
  182. package/tests/sheetFormulaCriteria.test.ts +269 -0
  183. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  184. package/tests/sheetFormulaFunctions.test.ts +538 -0
  185. package/tests/sheetFormulaLanguage.test.ts +361 -0
  186. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  187. package/tests/sheetFormulaParity.test.ts +188 -0
  188. package/tests/sheetFormulaRange.test.ts +167 -0
  189. package/tests/sheetFormulaReference.test.ts +78 -0
  190. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  191. package/tests/sheetFormulaTextDate.test.ts +512 -0
  192. package/tests/sheetSequence.test.ts +22 -0
  193. package/tests/sheetSnapshot.test.ts +134 -0
  194. package/tests/sheetStructureEdit.test.ts +537 -0
  195. package/tests/sheetStructureReferences.test.ts +589 -0
  196. package/tests/workflowLifecycleStatus.test.ts +60 -0
  197. package/tests/workflowLogFingerprint.test.ts +203 -0
  198. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  199. package/tests/workflowProgress.test.ts +180 -0
  200. package/tests/workflowTriggers.test.ts +29 -0
  201. package/tsconfig.json +12 -0
  202. package/vitest.config.ts +8 -0
@@ -0,0 +1,383 @@
1
+ import {
2
+ DOC_ACCESS_GRANTEE_TYPE_ENUMS,
3
+ DOC_GRANT_PERMISSION_ENUMS,
4
+ DOC_PERMISSION_ENUMS,
5
+ } from '../constants/docAccess.ts';
6
+ import type {
7
+ DocAccessBaseObj,
8
+ DocAccessGranteeTypeEnum,
9
+ DocAccessGrantObj,
10
+ DocAccessObj,
11
+ DocAccessScopeEnum,
12
+ DocAccessViewerCtx,
13
+ DocGrantPermissionEnum,
14
+ DocPermissionEnum,
15
+ } from '../types/docAccess.d.ts';
16
+
17
+ // Permission ordering used by the max/cap helpers ("EDIT" implies "VIEW")
18
+ const DOC_PERMISSION_RANKS: Record<DocPermissionEnum, number> = {
19
+ NONE: 0,
20
+ VIEW: 1,
21
+ EDIT: 2,
22
+ };
23
+
24
+ /**
25
+ * Normalize one document's access settings; a missing value means the
26
+ * default policy: every owner-org member can edit, the link is private,
27
+ * and network organizations have no access.
28
+ */
29
+
30
+ export function normalizeDocAccess(
31
+ access?: Partial<DocAccessObj> | null,
32
+ ): DocAccessObj {
33
+ return {
34
+ team: isValidDocPermission(access?.team) ? access.team : 'EDIT',
35
+ public: isValidDocPermission(access?.public) ? access.public : 'NONE',
36
+ network: isValidDocPermission(access?.network) ? access.network : 'NONE',
37
+ grants: access?.grants || [],
38
+ };
39
+ }
40
+
41
+ /**
42
+ * Check whether one organization member role always keeps full document
43
+ * access (document access settings never overrule admins and owners).
44
+ */
45
+
46
+ export function isDocAccessOverruledRole(role?: string | null): boolean {
47
+ return role === 'ADMIN' || role === 'OWNER';
48
+ }
49
+
50
+ /**
51
+ * Check whether one value is a valid document permission.
52
+ */
53
+
54
+ export function isValidDocPermission(
55
+ permission?: string | null,
56
+ ): permission is DocPermissionEnum {
57
+ return DOC_PERMISSION_ENUMS.includes(permission as DocPermissionEnum);
58
+ }
59
+
60
+ /**
61
+ * Check whether one value is a valid document grant permission.
62
+ */
63
+
64
+ export function isValidDocGrantPermission(
65
+ permission?: string | null,
66
+ ): permission is DocGrantPermissionEnum {
67
+ return DOC_GRANT_PERMISSION_ENUMS.includes(
68
+ permission as DocGrantPermissionEnum,
69
+ );
70
+ }
71
+
72
+ /**
73
+ * Check whether one value is a valid document access grantee type.
74
+ */
75
+
76
+ export function isValidDocAccessGranteeType(
77
+ granteeType?: string | null,
78
+ ): granteeType is DocAccessGranteeTypeEnum {
79
+ return DOC_ACCESS_GRANTEE_TYPE_ENUMS.includes(
80
+ granteeType as DocAccessGranteeTypeEnum,
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Return the higher of two document permissions.
86
+ */
87
+
88
+ export function maxDocPermission(
89
+ a: DocPermissionEnum,
90
+ b: DocPermissionEnum,
91
+ ): DocPermissionEnum {
92
+ return DOC_PERMISSION_RANKS[a] >= DOC_PERMISSION_RANKS[b] ? a : b;
93
+ }
94
+
95
+ /**
96
+ * Return the lower of two document permissions.
97
+ */
98
+
99
+ export function minDocPermission(
100
+ a: DocPermissionEnum,
101
+ b: DocPermissionEnum,
102
+ ): DocPermissionEnum {
103
+ return DOC_PERMISSION_RANKS[a] <= DOC_PERMISSION_RANKS[b] ? a : b;
104
+ }
105
+
106
+ /**
107
+ * Return the permission of the ACCOUNT grant targeting one viewer, or null
108
+ * when the viewer has no account grant (an explicit grant is an override,
109
+ * so "no grant" and "NONE grant" mean different things).
110
+ */
111
+
112
+ function findAccountGrantPermission(
113
+ grants: DocAccessGrantObj[] | null | undefined,
114
+ accountId: DocAccessViewerCtx['accountId'],
115
+ ): DocPermissionEnum | null {
116
+ if (!grants?.length || !accountId) {
117
+ return null;
118
+ }
119
+
120
+ const grant = grants.find(
121
+ (g) =>
122
+ g.granteeType === 'ACCOUNT' && String(g.granteeId) === String(accountId),
123
+ );
124
+
125
+ return grant ? grant.permission : null;
126
+ }
127
+
128
+ /**
129
+ * Return the highest permission the ORGANIZATION grants give one viewer
130
+ * across every organization the viewer belongs to, or null when none of the
131
+ * viewer's organizations has a grant (an explicit grant is an override, so
132
+ * "no grant" and "NONE grant" mean different things).
133
+ */
134
+
135
+ function findOrganizationGrantPermission(
136
+ grants: DocAccessGrantObj[] | null | undefined,
137
+ viewerOrganizationIds: DocAccessViewerCtx['viewerOrganizationIds'],
138
+ ): DocPermissionEnum | null {
139
+ if (!grants?.length || !viewerOrganizationIds?.length) {
140
+ return null;
141
+ }
142
+
143
+ const viewerOrgIdSet = new Set(viewerOrganizationIds.map((id) => String(id)));
144
+
145
+ return grants.reduce<DocPermissionEnum | null>((best, grant) => {
146
+ if (
147
+ grant.granteeType !== 'ORGANIZATION' ||
148
+ !viewerOrgIdSet.has(String(grant.granteeId))
149
+ ) {
150
+ return best;
151
+ }
152
+
153
+ return best === null
154
+ ? grant.permission
155
+ : maxDocPermission(best, grant.permission);
156
+ }, null);
157
+ }
158
+
159
+ /**
160
+ * Return one viewer's effective permission on one document. Grants are
161
+ * per-grantee overrides of the base policy: they can raise or lower access
162
+ * (including an explicit "NONE" block).
163
+ *
164
+ * Owner-org members:
165
+ * - ADMIN and OWNER roles always keep full access: document access settings
166
+ * never overrule them
167
+ * - a matching ACCOUNT grant overrides the team policy, including granting
168
+ * EDIT to a member without the org write ACL (a targeted grant is a
169
+ * deliberate elevation)
170
+ * - without a grant the team policy applies; team EDIT only grants EDIT to
171
+ * members with the org write ACL (read-only members cap at VIEW)
172
+ *
173
+ * Everyone else: a matching ACCOUNT grant wins; otherwise matching
174
+ * ORGANIZATION grants (best across the viewer's organizations) override the
175
+ * network policy; otherwise the network policy applies (network org members
176
+ * only).
177
+ *
178
+ * The public policy floors everyone's result (public EDIT needs a signed-in
179
+ * account): an override can never drop a viewer below what the public link
180
+ * already gives, since the link works without signing in.
181
+ */
182
+
183
+ export function getEffectiveDocPermission(
184
+ access: DocAccessObj | null | undefined,
185
+ viewerCtx: DocAccessViewerCtx,
186
+ ): DocPermissionEnum {
187
+ const normalized = normalizeDocAccess(access);
188
+ const publicFloor =
189
+ normalized.public === 'EDIT' && !viewerCtx.accountId
190
+ ? 'VIEW'
191
+ : normalized.public;
192
+ const accountGrant = findAccountGrantPermission(
193
+ normalized.grants,
194
+ viewerCtx.accountId,
195
+ );
196
+
197
+ if (viewerCtx.isOwnerOrgMember) {
198
+ if (viewerCtx.isAdminOrOwner) {
199
+ return 'EDIT';
200
+ }
201
+
202
+ const teamPermission = viewerCtx.hasWriteACL
203
+ ? normalized.team
204
+ : minDocPermission(normalized.team, 'VIEW');
205
+
206
+ return maxDocPermission(accountGrant ?? teamPermission, publicFloor);
207
+ }
208
+
209
+ const organizationGrant = findOrganizationGrantPermission(
210
+ normalized.grants,
211
+ viewerCtx.viewerOrganizationIds,
212
+ );
213
+ const networkPermission = viewerCtx.isNetworkOrgMember
214
+ ? normalized.network
215
+ : 'NONE';
216
+
217
+ return maxDocPermission(
218
+ accountGrant ?? organizationGrant ?? networkPermission,
219
+ publicFloor,
220
+ );
221
+ }
222
+
223
+ /**
224
+ * Check whether one viewer can view a document based on its access settings.
225
+ */
226
+
227
+ export function canViewDocByAccess(
228
+ access: DocAccessObj | null | undefined,
229
+ viewerCtx: DocAccessViewerCtx,
230
+ ): boolean {
231
+ return getEffectiveDocPermission(access, viewerCtx) !== 'NONE';
232
+ }
233
+
234
+ /**
235
+ * Check whether one viewer can edit a document based on its access settings.
236
+ */
237
+
238
+ export function canEditDocByAccess(
239
+ access: DocAccessObj | null | undefined,
240
+ viewerCtx: DocAccessViewerCtx,
241
+ ): boolean {
242
+ return getEffectiveDocPermission(access, viewerCtx) === 'EDIT';
243
+ }
244
+
245
+ /**
246
+ * Check whether one document is reachable through its public link.
247
+ */
248
+
249
+ export function isPublicDocAccess(access?: DocAccessBaseObj | null): boolean {
250
+ return (
251
+ !!access && access.public !== 'NONE' && isValidDocPermission(access.public)
252
+ );
253
+ }
254
+
255
+ /**
256
+ * Return the UI presentation scope for one document's general access policy
257
+ * beyond the owner team: the public link wins over the network policy, and
258
+ * a document with neither is invite-only (restricted).
259
+ */
260
+
261
+ export function getDocAccessScope(
262
+ access?: DocAccessBaseObj | null,
263
+ ): DocAccessScopeEnum {
264
+ const normalized = normalizeDocAccess(access);
265
+
266
+ if (normalized.public !== 'NONE') {
267
+ return 'PUBLIC';
268
+ }
269
+
270
+ return normalized.network !== 'NONE' ? 'NETWORK' : 'RESTRICTED';
271
+ }
272
+
273
+ /**
274
+ * Return the i18n key for one document access scope label.
275
+ */
276
+
277
+ export function getDocAccessScopeLabelKey(
278
+ scope?: DocAccessScopeEnum | null,
279
+ ): string {
280
+ switch (scope) {
281
+ case 'PUBLIC':
282
+ return 'sheet.access_anyone_with_link';
283
+ case 'NETWORK':
284
+ return 'sheet.access_network_orgs';
285
+ case 'RESTRICTED':
286
+ default:
287
+ return 'sheet.access_only_invites';
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Split one grants list into per-grantee permission maps keyed by grantee
293
+ * id, ready for UI pickers (one map per grantee type).
294
+ */
295
+
296
+ export function getDocAccessGrantMaps(grants?: DocAccessGrantObj[] | null): {
297
+ accountPermissions: Record<string, DocGrantPermissionEnum>;
298
+ organizationPermissions: Record<string, DocGrantPermissionEnum>;
299
+ } {
300
+ const accountPermissions: Record<string, DocGrantPermissionEnum> = {};
301
+ const organizationPermissions: Record<string, DocGrantPermissionEnum> = {};
302
+
303
+ for (const grant of grants || []) {
304
+ if (!isValidDocGrantPermission(grant?.permission)) {
305
+ continue;
306
+ }
307
+
308
+ if (grant.granteeType === 'ACCOUNT') {
309
+ accountPermissions[String(grant.granteeId)] = grant.permission;
310
+ } else if (grant.granteeType === 'ORGANIZATION') {
311
+ organizationPermissions[String(grant.granteeId)] = grant.permission;
312
+ }
313
+ }
314
+
315
+ return { accountPermissions, organizationPermissions };
316
+ }
317
+
318
+ /**
319
+ * Build one flat grants list from per-grantee permission maps (the inverse
320
+ * of getDocAccessGrantMaps).
321
+ */
322
+
323
+ export function buildDocAccessGrants(maps: {
324
+ accountPermissions: Record<string, DocGrantPermissionEnum>;
325
+ organizationPermissions: Record<string, DocGrantPermissionEnum>;
326
+ }): DocAccessGrantObj[] {
327
+ const accountGrants = Object.entries(maps.accountPermissions).map(
328
+ ([granteeId, permission]): DocAccessGrantObj => ({
329
+ granteeType: 'ACCOUNT',
330
+ granteeId,
331
+ permission,
332
+ }),
333
+ );
334
+ const organizationGrants = Object.entries(maps.organizationPermissions).map(
335
+ ([granteeId, permission]): DocAccessGrantObj => ({
336
+ granteeType: 'ORGANIZATION',
337
+ granteeId,
338
+ permission,
339
+ }),
340
+ );
341
+
342
+ return accountGrants.concat(organizationGrants);
343
+ }
344
+
345
+ /**
346
+ * Build the full access settings save payload from UI state: the team
347
+ * permission, the general access scope (public link is always view-only;
348
+ * the network scope carries its own permission), and the grant maps.
349
+ * Grants persist across every scope so switching scopes never silently
350
+ * drops them.
351
+ */
352
+
353
+ export function buildDocAccessSavePayload(p: {
354
+ scope: DocAccessScopeEnum;
355
+ teamPermission: DocPermissionEnum;
356
+ networkPermission: DocGrantPermissionEnum;
357
+ accountPermissions: Record<string, DocGrantPermissionEnum>;
358
+ organizationPermissions: Record<string, DocGrantPermissionEnum>;
359
+ }): DocAccessObj {
360
+ return {
361
+ team: p.teamPermission,
362
+ public: p.scope === 'PUBLIC' ? 'VIEW' : 'NONE',
363
+ network: p.scope === 'NETWORK' ? p.networkPermission : 'NONE',
364
+ grants: buildDocAccessGrants({
365
+ accountPermissions: p.accountPermissions,
366
+ organizationPermissions: p.organizationPermissions,
367
+ }),
368
+ };
369
+ }
370
+
371
+ /**
372
+ * Cycle one grantee's permission for UI pick rows: none -> VIEW -> EDIT -> none.
373
+ */
374
+
375
+ export function getNextDocAccessGrantPermission(
376
+ permission?: DocGrantPermissionEnum | null,
377
+ ): DocGrantPermissionEnum | null {
378
+ if (!permission) {
379
+ return 'VIEW';
380
+ }
381
+
382
+ return permission === 'VIEW' ? 'EDIT' : null;
383
+ }
@@ -0,0 +1,53 @@
1
+ const DOCUMENT_NAME_MAX_LENGTH = 80;
2
+ const RANDOM_NAME_SUFFIX_LENGTH = 5;
3
+ const RANDOM_NAME_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789';
4
+
5
+ /*
6
+ * Append one suffix to a document name while preserving the storage length limit.
7
+ */
8
+
9
+ export function appendDocumentNameSuffix(baseName: string, suffix: string) {
10
+ return `${baseName.substring(0, DOCUMENT_NAME_MAX_LENGTH - suffix.length)}${suffix}`;
11
+ }
12
+
13
+ /*
14
+ * Return numeric document name variants from `_1` through the requested count.
15
+ */
16
+
17
+ export function getDocumentNameNumericVariants(baseName: string, count = 10) {
18
+ return Array.from({ length: count }, (_item, index) => {
19
+ const suffix = `_${index + 1}`;
20
+ return appendDocumentNameSuffix(baseName, suffix);
21
+ });
22
+ }
23
+
24
+ /*
25
+ * Return the first numeric document name variant that is not already taken.
26
+ */
27
+
28
+ export function getFirstAvailableDocumentNameVariant(
29
+ baseName: string,
30
+ takenNames: Set<string>,
31
+ count = 10,
32
+ ) {
33
+ return (
34
+ getDocumentNameNumericVariants(baseName, count).find(
35
+ (candidate) => !takenNames.has(candidate.toLowerCase()),
36
+ ) || null
37
+ );
38
+ }
39
+
40
+ /*
41
+ * Return a document name with a random underscore-prefixed alphanumeric suffix.
42
+ */
43
+
44
+ export function buildRandomDocumentNameVariant(baseName: string) {
45
+ let suffix = '_';
46
+ for (let index = 0; index < RANDOM_NAME_SUFFIX_LENGTH; index += 1) {
47
+ suffix += RANDOM_NAME_CHARS.charAt(
48
+ Math.floor(Math.random() * RANDOM_NAME_CHARS.length),
49
+ );
50
+ }
51
+
52
+ return appendDocumentNameSuffix(baseName, suffix);
53
+ }