@open-mercato/documents 0.6.8-develop.7016.1.4ed7b1e49d

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 (602) hide show
  1. package/README.md +88 -0
  2. package/build.mjs +23 -0
  3. package/dist/index.js +5 -0
  4. package/dist/index.js.map +7 -0
  5. package/dist/modules/documents/acl.js +15 -0
  6. package/dist/modules/documents/acl.js.map +7 -0
  7. package/dist/modules/documents/api/[id]/archive/route.js +116 -0
  8. package/dist/modules/documents/api/[id]/archive/route.js.map +7 -0
  9. package/dist/modules/documents/api/[id]/attachments/[attachmentId]/route.js +155 -0
  10. package/dist/modules/documents/api/[id]/attachments/[attachmentId]/route.js.map +7 -0
  11. package/dist/modules/documents/api/[id]/attachments/route.js +153 -0
  12. package/dist/modules/documents/api/[id]/attachments/route.js.map +7 -0
  13. package/dist/modules/documents/api/[id]/collab-token/route.js +180 -0
  14. package/dist/modules/documents/api/[id]/collab-token/route.js.map +7 -0
  15. package/dist/modules/documents/api/[id]/comments/access-check/route.js +86 -0
  16. package/dist/modules/documents/api/[id]/comments/access-check/route.js.map +7 -0
  17. package/dist/modules/documents/api/[id]/comments/route.js +387 -0
  18. package/dist/modules/documents/api/[id]/comments/route.js.map +7 -0
  19. package/dist/modules/documents/api/[id]/content/route.js +143 -0
  20. package/dist/modules/documents/api/[id]/content/route.js.map +7 -0
  21. package/dist/modules/documents/api/[id]/duplicate/route.js +200 -0
  22. package/dist/modules/documents/api/[id]/duplicate/route.js.map +7 -0
  23. package/dist/modules/documents/api/[id]/export/route.js +421 -0
  24. package/dist/modules/documents/api/[id]/export/route.js.map +7 -0
  25. package/dist/modules/documents/api/[id]/favorite/route.js +96 -0
  26. package/dist/modules/documents/api/[id]/favorite/route.js.map +7 -0
  27. package/dist/modules/documents/api/[id]/links/[linkId]/route.js +85 -0
  28. package/dist/modules/documents/api/[id]/links/[linkId]/route.js.map +7 -0
  29. package/dist/modules/documents/api/[id]/links/route.js +257 -0
  30. package/dist/modules/documents/api/[id]/links/route.js.map +7 -0
  31. package/dist/modules/documents/api/[id]/principals/route.js +299 -0
  32. package/dist/modules/documents/api/[id]/principals/route.js.map +7 -0
  33. package/dist/modules/documents/api/[id]/route.js +246 -0
  34. package/dist/modules/documents/api/[id]/route.js.map +7 -0
  35. package/dist/modules/documents/api/[id]/shares/route.js +335 -0
  36. package/dist/modules/documents/api/[id]/shares/route.js.map +7 -0
  37. package/dist/modules/documents/api/[id]/unarchive/route.js +39 -0
  38. package/dist/modules/documents/api/[id]/unarchive/route.js.map +7 -0
  39. package/dist/modules/documents/api/[id]/versions/[versionId]/restore/route.js +133 -0
  40. package/dist/modules/documents/api/[id]/versions/[versionId]/restore/route.js.map +7 -0
  41. package/dist/modules/documents/api/[id]/versions/[versionId]/route.js +93 -0
  42. package/dist/modules/documents/api/[id]/versions/[versionId]/route.js.map +7 -0
  43. package/dist/modules/documents/api/[id]/versions/route.js +193 -0
  44. package/dist/modules/documents/api/[id]/versions/route.js.map +7 -0
  45. package/dist/modules/documents/api/[id]/watch/route.js +97 -0
  46. package/dist/modules/documents/api/[id]/watch/route.js.map +7 -0
  47. package/dist/modules/documents/api/_commands.js +34 -0
  48. package/dist/modules/documents/api/_commands.js.map +7 -0
  49. package/dist/modules/documents/api/_shared.js +490 -0
  50. package/dist/modules/documents/api/_shared.js.map +7 -0
  51. package/dist/modules/documents/api/folders/route.js +256 -0
  52. package/dist/modules/documents/api/folders/route.js.map +7 -0
  53. package/dist/modules/documents/api/instantiate/route.js +108 -0
  54. package/dist/modules/documents/api/instantiate/route.js.map +7 -0
  55. package/dist/modules/documents/api/route.js +322 -0
  56. package/dist/modules/documents/api/route.js.map +7 -0
  57. package/dist/modules/documents/api/templates/[templateId]/preview/route.js +94 -0
  58. package/dist/modules/documents/api/templates/[templateId]/preview/route.js.map +7 -0
  59. package/dist/modules/documents/api/templates/[templateId]/route.js +82 -0
  60. package/dist/modules/documents/api/templates/[templateId]/route.js.map +7 -0
  61. package/dist/modules/documents/api/templates/route.js +344 -0
  62. package/dist/modules/documents/api/templates/route.js.map +7 -0
  63. package/dist/modules/documents/backend/documents/DocumentsPageClient.js +160 -0
  64. package/dist/modules/documents/backend/documents/DocumentsPageClient.js.map +7 -0
  65. package/dist/modules/documents/backend/documents/DocumentsTable.js +143 -0
  66. package/dist/modules/documents/backend/documents/DocumentsTable.js.map +7 -0
  67. package/dist/modules/documents/backend/documents/FolderDialog.js +55 -0
  68. package/dist/modules/documents/backend/documents/FolderDialog.js.map +7 -0
  69. package/dist/modules/documents/backend/documents/FolderTree.js +40 -0
  70. package/dist/modules/documents/backend/documents/FolderTree.js.map +7 -0
  71. package/dist/modules/documents/backend/documents/MoveDocumentDialog.js +64 -0
  72. package/dist/modules/documents/backend/documents/MoveDocumentDialog.js.map +7 -0
  73. package/dist/modules/documents/backend/documents/[id]/CommentAnchorNavigation.js +77 -0
  74. package/dist/modules/documents/backend/documents/[id]/CommentAnchorNavigation.js.map +7 -0
  75. package/dist/modules/documents/backend/documents/[id]/CommentComposer.js +126 -0
  76. package/dist/modules/documents/backend/documents/[id]/CommentComposer.js.map +7 -0
  77. package/dist/modules/documents/backend/documents/[id]/CommentThreadList.js +88 -0
  78. package/dist/modules/documents/backend/documents/[id]/CommentThreadList.js.map +7 -0
  79. package/dist/modules/documents/backend/documents/[id]/CommentsRail.js +123 -0
  80. package/dist/modules/documents/backend/documents/[id]/CommentsRail.js.map +7 -0
  81. package/dist/modules/documents/backend/documents/[id]/DocumentCanvas.js +91 -0
  82. package/dist/modules/documents/backend/documents/[id]/DocumentCanvas.js.map +7 -0
  83. package/dist/modules/documents/backend/documents/[id]/DocumentEditorErrorBoundary.js +27 -0
  84. package/dist/modules/documents/backend/documents/[id]/DocumentEditorErrorBoundary.js.map +7 -0
  85. package/dist/modules/documents/backend/documents/[id]/DocumentEditorIsland.js +50 -0
  86. package/dist/modules/documents/backend/documents/[id]/DocumentEditorIsland.js.map +7 -0
  87. package/dist/modules/documents/backend/documents/[id]/DocumentEditorSurface.js +168 -0
  88. package/dist/modules/documents/backend/documents/[id]/DocumentEditorSurface.js.map +7 -0
  89. package/dist/modules/documents/backend/documents/[id]/DocumentNavigator.js +129 -0
  90. package/dist/modules/documents/backend/documents/[id]/DocumentNavigator.js.map +7 -0
  91. package/dist/modules/documents/backend/documents/[id]/DocumentPageClient.js +429 -0
  92. package/dist/modules/documents/backend/documents/[id]/DocumentPageClient.js.map +7 -0
  93. package/dist/modules/documents/backend/documents/[id]/EditorColorTools.js +68 -0
  94. package/dist/modules/documents/backend/documents/[id]/EditorColorTools.js.map +7 -0
  95. package/dist/modules/documents/backend/documents/[id]/EditorStatusPresence.js +69 -0
  96. package/dist/modules/documents/backend/documents/[id]/EditorStatusPresence.js.map +7 -0
  97. package/dist/modules/documents/backend/documents/[id]/EditorToolbar.js +151 -0
  98. package/dist/modules/documents/backend/documents/[id]/EditorToolbar.js.map +7 -0
  99. package/dist/modules/documents/backend/documents/[id]/ExportMenu.js +121 -0
  100. package/dist/modules/documents/backend/documents/[id]/ExportMenu.js.map +7 -0
  101. package/dist/modules/documents/backend/documents/[id]/MentionPicker.js +114 -0
  102. package/dist/modules/documents/backend/documents/[id]/MentionPicker.js.map +7 -0
  103. package/dist/modules/documents/backend/documents/[id]/OutlinePane.js +70 -0
  104. package/dist/modules/documents/backend/documents/[id]/OutlinePane.js.map +7 -0
  105. package/dist/modules/documents/backend/documents/[id]/RecordFieldsDialog.js +157 -0
  106. package/dist/modules/documents/backend/documents/[id]/RecordFieldsDialog.js.map +7 -0
  107. package/dist/modules/documents/backend/documents/[id]/RelatedRecordsPanel.js +266 -0
  108. package/dist/modules/documents/backend/documents/[id]/RelatedRecordsPanel.js.map +7 -0
  109. package/dist/modules/documents/backend/documents/[id]/VersionHistoryPanel.js +227 -0
  110. package/dist/modules/documents/backend/documents/[id]/VersionHistoryPanel.js.map +7 -0
  111. package/dist/modules/documents/backend/documents/[id]/VersionPreviewDialog.js +157 -0
  112. package/dist/modules/documents/backend/documents/[id]/VersionPreviewDialog.js.map +7 -0
  113. package/dist/modules/documents/backend/documents/[id]/commentTypes.js +150 -0
  114. package/dist/modules/documents/backend/documents/[id]/commentTypes.js.map +7 -0
  115. package/dist/modules/documents/backend/documents/[id]/componentCapabilities.js +13 -0
  116. package/dist/modules/documents/backend/documents/[id]/componentCapabilities.js.map +7 -0
  117. package/dist/modules/documents/backend/documents/[id]/documentPagination.js +295 -0
  118. package/dist/modules/documents/backend/documents/[id]/documentPagination.js.map +7 -0
  119. package/dist/modules/documents/backend/documents/[id]/editorPresenceStyles.js +81 -0
  120. package/dist/modules/documents/backend/documents/[id]/editorPresenceStyles.js.map +7 -0
  121. package/dist/modules/documents/backend/documents/[id]/editorTypes.js +52 -0
  122. package/dist/modules/documents/backend/documents/[id]/editorTypes.js.map +7 -0
  123. package/dist/modules/documents/backend/documents/[id]/page.js +10 -0
  124. package/dist/modules/documents/backend/documents/[id]/page.js.map +7 -0
  125. package/dist/modules/documents/backend/documents/[id]/page.meta.js +16 -0
  126. package/dist/modules/documents/backend/documents/[id]/page.meta.js.map +7 -0
  127. package/dist/modules/documents/backend/documents/[id]/relatedRecordModel.js +38 -0
  128. package/dist/modules/documents/backend/documents/[id]/relatedRecordModel.js.map +7 -0
  129. package/dist/modules/documents/backend/documents/[id]/restoreVersion.js +33 -0
  130. package/dist/modules/documents/backend/documents/[id]/restoreVersion.js.map +7 -0
  131. package/dist/modules/documents/backend/documents/[id]/useDocumentCollaboration.js +392 -0
  132. package/dist/modules/documents/backend/documents/[id]/useDocumentCollaboration.js.map +7 -0
  133. package/dist/modules/documents/backend/documents/[id]/useDocumentComments.js +253 -0
  134. package/dist/modules/documents/backend/documents/[id]/useDocumentComments.js.map +7 -0
  135. package/dist/modules/documents/backend/documents/[id]/useDocumentEditor.js +149 -0
  136. package/dist/modules/documents/backend/documents/[id]/useDocumentEditor.js.map +7 -0
  137. package/dist/modules/documents/backend/documents/[id]/useDocumentTitle.js +82 -0
  138. package/dist/modules/documents/backend/documents/[id]/useDocumentTitle.js.map +7 -0
  139. package/dist/modules/documents/backend/documents/[id]/useEditorInsertions.js +144 -0
  140. package/dist/modules/documents/backend/documents/[id]/useEditorInsertions.js.map +7 -0
  141. package/dist/modules/documents/backend/documents/[id]/useFallbackContentPersistence.js +237 -0
  142. package/dist/modules/documents/backend/documents/[id]/useFallbackContentPersistence.js.map +7 -0
  143. package/dist/modules/documents/backend/documents/[id]/useMentionPicker.js +216 -0
  144. package/dist/modules/documents/backend/documents/[id]/useMentionPicker.js.map +7 -0
  145. package/dist/modules/documents/backend/documents/[id]/versionHistoryModel.js +25 -0
  146. package/dist/modules/documents/backend/documents/[id]/versionHistoryModel.js.map +7 -0
  147. package/dist/modules/documents/backend/documents/components/EntityPicker.js +156 -0
  148. package/dist/modules/documents/backend/documents/components/EntityPicker.js.map +7 -0
  149. package/dist/modules/documents/backend/documents/components/EntityPickerResults.js +46 -0
  150. package/dist/modules/documents/backend/documents/components/EntityPickerResults.js.map +7 -0
  151. package/dist/modules/documents/backend/documents/components/NewFromTemplateDialog.js +127 -0
  152. package/dist/modules/documents/backend/documents/components/NewFromTemplateDialog.js.map +7 -0
  153. package/dist/modules/documents/backend/documents/components/PrincipalPicker.js +84 -0
  154. package/dist/modules/documents/backend/documents/components/PrincipalPicker.js.map +7 -0
  155. package/dist/modules/documents/backend/documents/components/ShareDialog.js +77 -0
  156. package/dist/modules/documents/backend/documents/components/ShareDialog.js.map +7 -0
  157. package/dist/modules/documents/backend/documents/components/ShareDialogAddForm.js +93 -0
  158. package/dist/modules/documents/backend/documents/components/ShareDialogAddForm.js.map +7 -0
  159. package/dist/modules/documents/backend/documents/components/ShareDialogList.js +82 -0
  160. package/dist/modules/documents/backend/documents/components/ShareDialogList.js.map +7 -0
  161. package/dist/modules/documents/backend/documents/components/TemplateBodyEditor.js +96 -0
  162. package/dist/modules/documents/backend/documents/components/TemplateBodyEditor.js.map +7 -0
  163. package/dist/modules/documents/backend/documents/components/TemplateEditorDialog.js +169 -0
  164. package/dist/modules/documents/backend/documents/components/TemplateEditorDialog.js.map +7 -0
  165. package/dist/modules/documents/backend/documents/components/TemplatePreview.js +38 -0
  166. package/dist/modules/documents/backend/documents/components/TemplatePreview.js.map +7 -0
  167. package/dist/modules/documents/backend/documents/components/TemplateSlotFields.js +69 -0
  168. package/dist/modules/documents/backend/documents/components/TemplateSlotFields.js.map +7 -0
  169. package/dist/modules/documents/backend/documents/components/TemplateSlotsEditor.js +71 -0
  170. package/dist/modules/documents/backend/documents/components/TemplateSlotsEditor.js.map +7 -0
  171. package/dist/modules/documents/backend/documents/components/principalPickerModel.js +63 -0
  172. package/dist/modules/documents/backend/documents/components/principalPickerModel.js.map +7 -0
  173. package/dist/modules/documents/backend/documents/components/shareDialogModel.js +68 -0
  174. package/dist/modules/documents/backend/documents/components/shareDialogModel.js.map +7 -0
  175. package/dist/modules/documents/backend/documents/components/templateUi.js +89 -0
  176. package/dist/modules/documents/backend/documents/components/templateUi.js.map +7 -0
  177. package/dist/modules/documents/backend/documents/components/useAvailableEntityRegistry.js +30 -0
  178. package/dist/modules/documents/backend/documents/components/useAvailableEntityRegistry.js.map +7 -0
  179. package/dist/modules/documents/backend/documents/components/useEntitySearch.js +191 -0
  180. package/dist/modules/documents/backend/documents/components/useEntitySearch.js.map +7 -0
  181. package/dist/modules/documents/backend/documents/components/usePrincipalPicker.js +221 -0
  182. package/dist/modules/documents/backend/documents/components/usePrincipalPicker.js.map +7 -0
  183. package/dist/modules/documents/backend/documents/components/useShareDialog.js +236 -0
  184. package/dist/modules/documents/backend/documents/components/useShareDialog.js.map +7 -0
  185. package/dist/modules/documents/backend/documents/components/useTemplateDetail.js +49 -0
  186. package/dist/modules/documents/backend/documents/components/useTemplateDetail.js.map +7 -0
  187. package/dist/modules/documents/backend/documents/components/useTemplateInstantiation.js +234 -0
  188. package/dist/modules/documents/backend/documents/components/useTemplateInstantiation.js.map +7 -0
  189. package/dist/modules/documents/backend/documents/documentUi.js +106 -0
  190. package/dist/modules/documents/backend/documents/documentUi.js.map +7 -0
  191. package/dist/modules/documents/backend/documents/documentsListTypes.js +132 -0
  192. package/dist/modules/documents/backend/documents/documentsListTypes.js.map +7 -0
  193. package/dist/modules/documents/backend/documents/page.js +9 -0
  194. package/dist/modules/documents/backend/documents/page.js.map +7 -0
  195. package/dist/modules/documents/backend/documents/page.meta.js +26 -0
  196. package/dist/modules/documents/backend/documents/page.meta.js.map +7 -0
  197. package/dist/modules/documents/backend/documents/templates/TemplatesPageClient.js +91 -0
  198. package/dist/modules/documents/backend/documents/templates/TemplatesPageClient.js.map +7 -0
  199. package/dist/modules/documents/backend/documents/templates/TemplatesTable.js +58 -0
  200. package/dist/modules/documents/backend/documents/templates/TemplatesTable.js.map +7 -0
  201. package/dist/modules/documents/backend/documents/templates/page.js +9 -0
  202. package/dist/modules/documents/backend/documents/templates/page.js.map +7 -0
  203. package/dist/modules/documents/backend/documents/templates/page.meta.js +34 -0
  204. package/dist/modules/documents/backend/documents/templates/page.meta.js.map +7 -0
  205. package/dist/modules/documents/backend/documents/templates/useTemplatesPage.js +139 -0
  206. package/dist/modules/documents/backend/documents/templates/useTemplatesPage.js.map +7 -0
  207. package/dist/modules/documents/backend/documents/useDocumentsList.js +314 -0
  208. package/dist/modules/documents/backend/documents/useDocumentsList.js.map +7 -0
  209. package/dist/modules/documents/ce.js +8 -0
  210. package/dist/modules/documents/ce.js.map +7 -0
  211. package/dist/modules/documents/commands/aggregate.js +99 -0
  212. package/dist/modules/documents/commands/aggregate.js.map +7 -0
  213. package/dist/modules/documents/commands/attachments.js +252 -0
  214. package/dist/modules/documents/commands/attachments.js.map +7 -0
  215. package/dist/modules/documents/commands/comments.js +782 -0
  216. package/dist/modules/documents/commands/comments.js.map +7 -0
  217. package/dist/modules/documents/commands/content.js +238 -0
  218. package/dist/modules/documents/commands/content.js.map +7 -0
  219. package/dist/modules/documents/commands/document-crud.js +505 -0
  220. package/dist/modules/documents/commands/document-crud.js.map +7 -0
  221. package/dist/modules/documents/commands/documents.js +473 -0
  222. package/dist/modules/documents/commands/documents.js.map +7 -0
  223. package/dist/modules/documents/commands/duplicate.js +640 -0
  224. package/dist/modules/documents/commands/duplicate.js.map +7 -0
  225. package/dist/modules/documents/commands/favorites.js +150 -0
  226. package/dist/modules/documents/commands/favorites.js.map +7 -0
  227. package/dist/modules/documents/commands/folders.js +420 -0
  228. package/dist/modules/documents/commands/folders.js.map +7 -0
  229. package/dist/modules/documents/commands/index.js +64 -0
  230. package/dist/modules/documents/commands/index.js.map +7 -0
  231. package/dist/modules/documents/commands/interceptors.js +300 -0
  232. package/dist/modules/documents/commands/interceptors.js.map +7 -0
  233. package/dist/modules/documents/commands/lifecycle.js +249 -0
  234. package/dist/modules/documents/commands/lifecycle.js.map +7 -0
  235. package/dist/modules/documents/commands/links.js +410 -0
  236. package/dist/modules/documents/commands/links.js.map +7 -0
  237. package/dist/modules/documents/commands/mutation-helpers.js +42 -0
  238. package/dist/modules/documents/commands/mutation-helpers.js.map +7 -0
  239. package/dist/modules/documents/commands/projection-types.js +1 -0
  240. package/dist/modules/documents/commands/projection-types.js.map +7 -0
  241. package/dist/modules/documents/commands/shared.js +143 -0
  242. package/dist/modules/documents/commands/shared.js.map +7 -0
  243. package/dist/modules/documents/commands/shares.js +527 -0
  244. package/dist/modules/documents/commands/shares.js.map +7 -0
  245. package/dist/modules/documents/commands/side-effects.js +103 -0
  246. package/dist/modules/documents/commands/side-effects.js.map +7 -0
  247. package/dist/modules/documents/commands/templates.js +461 -0
  248. package/dist/modules/documents/commands/templates.js.map +7 -0
  249. package/dist/modules/documents/commands/versions.js +432 -0
  250. package/dist/modules/documents/commands/versions.js.map +7 -0
  251. package/dist/modules/documents/commands/watchers.js +164 -0
  252. package/dist/modules/documents/commands/watchers.js.map +7 -0
  253. package/dist/modules/documents/data/entities.js +653 -0
  254. package/dist/modules/documents/data/entities.js.map +7 -0
  255. package/dist/modules/documents/data/extensions.js +44 -0
  256. package/dist/modules/documents/data/extensions.js.map +7 -0
  257. package/dist/modules/documents/data/validators.js +219 -0
  258. package/dist/modules/documents/data/validators.js.map +7 -0
  259. package/dist/modules/documents/di.js +35 -0
  260. package/dist/modules/documents/di.js.map +7 -0
  261. package/dist/modules/documents/encryption.js +12 -0
  262. package/dist/modules/documents/encryption.js.map +7 -0
  263. package/dist/modules/documents/events.js +27 -0
  264. package/dist/modules/documents/events.js.map +7 -0
  265. package/dist/modules/documents/index.js +19 -0
  266. package/dist/modules/documents/index.js.map +7 -0
  267. package/dist/modules/documents/lib/attachmentServicePort.js +30 -0
  268. package/dist/modules/documents/lib/attachmentServicePort.js.map +7 -0
  269. package/dist/modules/documents/lib/capabilities.js +32 -0
  270. package/dist/modules/documents/lib/capabilities.js.map +7 -0
  271. package/dist/modules/documents/lib/collabCloseEvents.js +17 -0
  272. package/dist/modules/documents/lib/collabCloseEvents.js.map +7 -0
  273. package/dist/modules/documents/lib/collabEndpoint.js +22 -0
  274. package/dist/modules/documents/lib/collabEndpoint.js.map +7 -0
  275. package/dist/modules/documents/lib/collabMaterializer.js +107 -0
  276. package/dist/modules/documents/lib/collabMaterializer.js.map +7 -0
  277. package/dist/modules/documents/lib/collabToken.js +120 -0
  278. package/dist/modules/documents/lib/collabToken.js.map +7 -0
  279. package/dist/modules/documents/lib/collaborationAwareness.js +68 -0
  280. package/dist/modules/documents/lib/collaborationAwareness.js.map +7 -0
  281. package/dist/modules/documents/lib/commentAnchors.js +15 -0
  282. package/dist/modules/documents/lib/commentAnchors.js.map +7 -0
  283. package/dist/modules/documents/lib/constants.js +25 -0
  284. package/dist/modules/documents/lib/constants.js.map +7 -0
  285. package/dist/modules/documents/lib/contentService.js +248 -0
  286. package/dist/modules/documents/lib/contentService.js.map +7 -0
  287. package/dist/modules/documents/lib/displayLabels.js +23 -0
  288. package/dist/modules/documents/lib/displayLabels.js.map +7 -0
  289. package/dist/modules/documents/lib/docxRenderer.js +601 -0
  290. package/dist/modules/documents/lib/docxRenderer.js.map +7 -0
  291. package/dist/modules/documents/lib/duplicateContent.js +22 -0
  292. package/dist/modules/documents/lib/duplicateContent.js.map +7 -0
  293. package/dist/modules/documents/lib/editorConfig.js +287 -0
  294. package/dist/modules/documents/lib/editorConfig.js.map +7 -0
  295. package/dist/modules/documents/lib/entityLinks.js +148 -0
  296. package/dist/modules/documents/lib/entityLinks.js.map +7 -0
  297. package/dist/modules/documents/lib/entityRegistry.js +258 -0
  298. package/dist/modules/documents/lib/entityRegistry.js.map +7 -0
  299. package/dist/modules/documents/lib/entityRegistry.server.js +189 -0
  300. package/dist/modules/documents/lib/entityRegistry.server.js.map +7 -0
  301. package/dist/modules/documents/lib/entityRegistryAvailability.server.js +9 -0
  302. package/dist/modules/documents/lib/entityRegistryAvailability.server.js.map +7 -0
  303. package/dist/modules/documents/lib/entitySuggestion.js +50 -0
  304. package/dist/modules/documents/lib/entitySuggestion.js.map +7 -0
  305. package/dist/modules/documents/lib/favorites.js +22 -0
  306. package/dist/modules/documents/lib/favorites.js.map +7 -0
  307. package/dist/modules/documents/lib/folderHierarchySerialization.js +19 -0
  308. package/dist/modules/documents/lib/folderHierarchySerialization.js.map +7 -0
  309. package/dist/modules/documents/lib/historyLimits.js +100 -0
  310. package/dist/modules/documents/lib/historyLimits.js.map +7 -0
  311. package/dist/modules/documents/lib/historyPolicy.js +14 -0
  312. package/dist/modules/documents/lib/historyPolicy.js.map +7 -0
  313. package/dist/modules/documents/lib/organizationAccess.js +17 -0
  314. package/dist/modules/documents/lib/organizationAccess.js.map +7 -0
  315. package/dist/modules/documents/lib/pdfHtml.js +62 -0
  316. package/dist/modules/documents/lib/pdfHtml.js.map +7 -0
  317. package/dist/modules/documents/lib/pdfRenderer.js +284 -0
  318. package/dist/modules/documents/lib/pdfRenderer.js.map +7 -0
  319. package/dist/modules/documents/lib/permissions.js +222 -0
  320. package/dist/modules/documents/lib/permissions.js.map +7 -0
  321. package/dist/modules/documents/lib/platformServices.js +35 -0
  322. package/dist/modules/documents/lib/platformServices.js.map +7 -0
  323. package/dist/modules/documents/lib/recordFieldInsertion.js +58 -0
  324. package/dist/modules/documents/lib/recordFieldInsertion.js.map +7 -0
  325. package/dist/modules/documents/lib/requestBody.js +80 -0
  326. package/dist/modules/documents/lib/requestBody.js.map +7 -0
  327. package/dist/modules/documents/lib/resourceLimits.js +62 -0
  328. package/dist/modules/documents/lib/resourceLimits.js.map +7 -0
  329. package/dist/modules/documents/lib/shareCounts.js +25 -0
  330. package/dist/modules/documents/lib/shareCounts.js.map +7 -0
  331. package/dist/modules/documents/lib/templateFill.js +84 -0
  332. package/dist/modules/documents/lib/templateFill.js.map +7 -0
  333. package/dist/modules/documents/lib/templateInstantiation.js +147 -0
  334. package/dist/modules/documents/lib/templateInstantiation.js.map +7 -0
  335. package/dist/modules/documents/lib/templatePreviewDigest.js +82 -0
  336. package/dist/modules/documents/lib/templatePreviewDigest.js.map +7 -0
  337. package/dist/modules/documents/lib/templateSeeds.js +201 -0
  338. package/dist/modules/documents/lib/templateSeeds.js.map +7 -0
  339. package/dist/modules/documents/lib/userLabels.js +37 -0
  340. package/dist/modules/documents/lib/userLabels.js.map +7 -0
  341. package/dist/modules/documents/lib/versionContent.js +131 -0
  342. package/dist/modules/documents/lib/versionContent.js.map +7 -0
  343. package/dist/modules/documents/lib/versionLabels.js +12 -0
  344. package/dist/modules/documents/lib/versionLabels.js.map +7 -0
  345. package/dist/modules/documents/lib/versioning.js +14 -0
  346. package/dist/modules/documents/lib/versioning.js.map +7 -0
  347. package/dist/modules/documents/lib/visibility.js +422 -0
  348. package/dist/modules/documents/lib/visibility.js.map +7 -0
  349. package/dist/modules/documents/lib/watchers.js +106 -0
  350. package/dist/modules/documents/lib/watchers.js.map +7 -0
  351. package/dist/modules/documents/migrations/Migration20260708163836_documents.js +44 -0
  352. package/dist/modules/documents/migrations/Migration20260708163836_documents.js.map +7 -0
  353. package/dist/modules/documents/migrations/Migration20260709164720_documents.js +17 -0
  354. package/dist/modules/documents/migrations/Migration20260709164720_documents.js.map +7 -0
  355. package/dist/modules/documents/migrations/Migration20260710002318_documents.js +32 -0
  356. package/dist/modules/documents/migrations/Migration20260710002318_documents.js.map +7 -0
  357. package/dist/modules/documents/migrations/Migration20260710071003_documents.js +13 -0
  358. package/dist/modules/documents/migrations/Migration20260710071003_documents.js.map +7 -0
  359. package/dist/modules/documents/migrations/Migration20260712120000_documents.js +13 -0
  360. package/dist/modules/documents/migrations/Migration20260712120000_documents.js.map +7 -0
  361. package/dist/modules/documents/migrations/Migration20260712183000_documents.js +14 -0
  362. package/dist/modules/documents/migrations/Migration20260712183000_documents.js.map +7 -0
  363. package/dist/modules/documents/migrations/Migration20260713092156_documents.js +29 -0
  364. package/dist/modules/documents/migrations/Migration20260713092156_documents.js.map +7 -0
  365. package/dist/modules/documents/migrations/Migration20260717000000_documents.js +37 -0
  366. package/dist/modules/documents/migrations/Migration20260717000000_documents.js.map +7 -0
  367. package/dist/modules/documents/notifications.js +47 -0
  368. package/dist/modules/documents/notifications.js.map +7 -0
  369. package/dist/modules/documents/search.js +101 -0
  370. package/dist/modules/documents/search.js.map +7 -0
  371. package/dist/modules/documents/setup.js +19 -0
  372. package/dist/modules/documents/setup.js.map +7 -0
  373. package/dist/modules/documents/widgets/injection/related-documents/LinkDocumentDialog.js +214 -0
  374. package/dist/modules/documents/widgets/injection/related-documents/LinkDocumentDialog.js.map +7 -0
  375. package/dist/modules/documents/widgets/injection/related-documents/context.js +84 -0
  376. package/dist/modules/documents/widgets/injection/related-documents/context.js.map +7 -0
  377. package/dist/modules/documents/widgets/injection/related-documents/useRelatedDocuments.js +98 -0
  378. package/dist/modules/documents/widgets/injection/related-documents/useRelatedDocuments.js.map +7 -0
  379. package/dist/modules/documents/widgets/injection/related-documents/widget.client.js +83 -0
  380. package/dist/modules/documents/widgets/injection/related-documents/widget.client.js.map +7 -0
  381. package/dist/modules/documents/widgets/injection/related-documents/widget.js +19 -0
  382. package/dist/modules/documents/widgets/injection/related-documents/widget.js.map +7 -0
  383. package/dist/modules/documents/widgets/injection-table.js +21 -0
  384. package/dist/modules/documents/widgets/injection-table.js.map +7 -0
  385. package/dist/server/documents-collab-server.js +2260 -0
  386. package/dist/server/documents-collab-server.js.map +7 -0
  387. package/generated/entities/document/index.ts +12 -0
  388. package/generated/entities/document_attachment/index.ts +9 -0
  389. package/generated/entities/document_comment/index.ts +14 -0
  390. package/generated/entities/document_content/index.ts +11 -0
  391. package/generated/entities/document_entity_link/index.ts +19 -0
  392. package/generated/entities/document_favorite/index.ts +7 -0
  393. package/generated/entities/document_folder/index.ts +9 -0
  394. package/generated/entities/document_share/index.ts +11 -0
  395. package/generated/entities/document_template/index.ts +13 -0
  396. package/generated/entities/document_version/index.ts +9 -0
  397. package/generated/entities/document_watcher/index.ts +7 -0
  398. package/generated/entities.ids.generated.ts +21 -0
  399. package/generated/entity-fields-registry.ts +151 -0
  400. package/jest.config.cjs +34 -0
  401. package/package.json +149 -0
  402. package/server/documents-collab-server.ts +3416 -0
  403. package/src/index.ts +1 -0
  404. package/src/modules/documents/acl.ts +11 -0
  405. package/src/modules/documents/api/[id]/archive/route.ts +132 -0
  406. package/src/modules/documents/api/[id]/attachments/[attachmentId]/route.ts +166 -0
  407. package/src/modules/documents/api/[id]/attachments/route.ts +173 -0
  408. package/src/modules/documents/api/[id]/collab-token/route.ts +203 -0
  409. package/src/modules/documents/api/[id]/comments/access-check/route.ts +96 -0
  410. package/src/modules/documents/api/[id]/comments/route.ts +442 -0
  411. package/src/modules/documents/api/[id]/content/route.ts +156 -0
  412. package/src/modules/documents/api/[id]/duplicate/route.ts +231 -0
  413. package/src/modules/documents/api/[id]/export/route.ts +535 -0
  414. package/src/modules/documents/api/[id]/favorite/route.ts +110 -0
  415. package/src/modules/documents/api/[id]/links/[linkId]/route.ts +92 -0
  416. package/src/modules/documents/api/[id]/links/route.ts +301 -0
  417. package/src/modules/documents/api/[id]/principals/route.ts +375 -0
  418. package/src/modules/documents/api/[id]/route.ts +267 -0
  419. package/src/modules/documents/api/[id]/shares/route.ts +372 -0
  420. package/src/modules/documents/api/[id]/unarchive/route.ts +42 -0
  421. package/src/modules/documents/api/[id]/versions/[versionId]/restore/route.ts +152 -0
  422. package/src/modules/documents/api/[id]/versions/[versionId]/route.ts +100 -0
  423. package/src/modules/documents/api/[id]/versions/route.ts +207 -0
  424. package/src/modules/documents/api/[id]/watch/route.ts +111 -0
  425. package/src/modules/documents/api/_commands.ts +53 -0
  426. package/src/modules/documents/api/_shared.ts +676 -0
  427. package/src/modules/documents/api/folders/route.ts +287 -0
  428. package/src/modules/documents/api/instantiate/route.ts +114 -0
  429. package/src/modules/documents/api/route.ts +362 -0
  430. package/src/modules/documents/api/templates/[templateId]/preview/route.ts +97 -0
  431. package/src/modules/documents/api/templates/[templateId]/route.ts +92 -0
  432. package/src/modules/documents/api/templates/route.ts +370 -0
  433. package/src/modules/documents/backend/documents/DocumentsPageClient.tsx +150 -0
  434. package/src/modules/documents/backend/documents/DocumentsTable.tsx +168 -0
  435. package/src/modules/documents/backend/documents/FolderDialog.tsx +62 -0
  436. package/src/modules/documents/backend/documents/FolderTree.tsx +72 -0
  437. package/src/modules/documents/backend/documents/MoveDocumentDialog.tsx +70 -0
  438. package/src/modules/documents/backend/documents/[id]/CommentAnchorNavigation.ts +88 -0
  439. package/src/modules/documents/backend/documents/[id]/CommentComposer.tsx +136 -0
  440. package/src/modules/documents/backend/documents/[id]/CommentThreadList.tsx +131 -0
  441. package/src/modules/documents/backend/documents/[id]/CommentsRail.tsx +138 -0
  442. package/src/modules/documents/backend/documents/[id]/DocumentCanvas.tsx +111 -0
  443. package/src/modules/documents/backend/documents/[id]/DocumentEditorErrorBoundary.tsx +39 -0
  444. package/src/modules/documents/backend/documents/[id]/DocumentEditorIsland.tsx +50 -0
  445. package/src/modules/documents/backend/documents/[id]/DocumentEditorSurface.tsx +162 -0
  446. package/src/modules/documents/backend/documents/[id]/DocumentNavigator.tsx +129 -0
  447. package/src/modules/documents/backend/documents/[id]/DocumentPageClient.tsx +463 -0
  448. package/src/modules/documents/backend/documents/[id]/EditorColorTools.tsx +71 -0
  449. package/src/modules/documents/backend/documents/[id]/EditorStatusPresence.tsx +78 -0
  450. package/src/modules/documents/backend/documents/[id]/EditorToolbar.tsx +108 -0
  451. package/src/modules/documents/backend/documents/[id]/ExportMenu.tsx +144 -0
  452. package/src/modules/documents/backend/documents/[id]/MentionPicker.tsx +137 -0
  453. package/src/modules/documents/backend/documents/[id]/OutlinePane.tsx +98 -0
  454. package/src/modules/documents/backend/documents/[id]/RecordFieldsDialog.tsx +185 -0
  455. package/src/modules/documents/backend/documents/[id]/RelatedRecordsPanel.tsx +320 -0
  456. package/src/modules/documents/backend/documents/[id]/VersionHistoryPanel.tsx +244 -0
  457. package/src/modules/documents/backend/documents/[id]/VersionPreviewDialog.tsx +197 -0
  458. package/src/modules/documents/backend/documents/[id]/commentTypes.ts +192 -0
  459. package/src/modules/documents/backend/documents/[id]/componentCapabilities.ts +17 -0
  460. package/src/modules/documents/backend/documents/[id]/documentPagination.ts +352 -0
  461. package/src/modules/documents/backend/documents/[id]/editorPresenceStyles.ts +77 -0
  462. package/src/modules/documents/backend/documents/[id]/editorTypes.ts +84 -0
  463. package/src/modules/documents/backend/documents/[id]/page.meta.ts +12 -0
  464. package/src/modules/documents/backend/documents/[id]/page.tsx +6 -0
  465. package/src/modules/documents/backend/documents/[id]/relatedRecordModel.ts +49 -0
  466. package/src/modules/documents/backend/documents/[id]/restoreVersion.ts +49 -0
  467. package/src/modules/documents/backend/documents/[id]/useDocumentCollaboration.ts +458 -0
  468. package/src/modules/documents/backend/documents/[id]/useDocumentComments.ts +286 -0
  469. package/src/modules/documents/backend/documents/[id]/useDocumentEditor.ts +157 -0
  470. package/src/modules/documents/backend/documents/[id]/useDocumentTitle.ts +77 -0
  471. package/src/modules/documents/backend/documents/[id]/useEditorInsertions.ts +147 -0
  472. package/src/modules/documents/backend/documents/[id]/useFallbackContentPersistence.ts +277 -0
  473. package/src/modules/documents/backend/documents/[id]/useMentionPicker.ts +259 -0
  474. package/src/modules/documents/backend/documents/[id]/versionHistoryModel.ts +29 -0
  475. package/src/modules/documents/backend/documents/components/EntityPicker.tsx +171 -0
  476. package/src/modules/documents/backend/documents/components/EntityPickerResults.tsx +64 -0
  477. package/src/modules/documents/backend/documents/components/NewFromTemplateDialog.tsx +136 -0
  478. package/src/modules/documents/backend/documents/components/PrincipalPicker.tsx +90 -0
  479. package/src/modules/documents/backend/documents/components/ShareDialog.tsx +88 -0
  480. package/src/modules/documents/backend/documents/components/ShareDialogAddForm.tsx +113 -0
  481. package/src/modules/documents/backend/documents/components/ShareDialogList.tsx +117 -0
  482. package/src/modules/documents/backend/documents/components/TemplateBodyEditor.tsx +96 -0
  483. package/src/modules/documents/backend/documents/components/TemplateEditorDialog.tsx +171 -0
  484. package/src/modules/documents/backend/documents/components/TemplatePreview.tsx +43 -0
  485. package/src/modules/documents/backend/documents/components/TemplateSlotFields.tsx +72 -0
  486. package/src/modules/documents/backend/documents/components/TemplateSlotsEditor.tsx +72 -0
  487. package/src/modules/documents/backend/documents/components/principalPickerModel.ts +86 -0
  488. package/src/modules/documents/backend/documents/components/shareDialogModel.ts +94 -0
  489. package/src/modules/documents/backend/documents/components/templateUi.ts +128 -0
  490. package/src/modules/documents/backend/documents/components/useAvailableEntityRegistry.ts +43 -0
  491. package/src/modules/documents/backend/documents/components/useEntitySearch.ts +197 -0
  492. package/src/modules/documents/backend/documents/components/usePrincipalPicker.ts +165 -0
  493. package/src/modules/documents/backend/documents/components/useShareDialog.ts +267 -0
  494. package/src/modules/documents/backend/documents/components/useTemplateDetail.ts +54 -0
  495. package/src/modules/documents/backend/documents/components/useTemplateInstantiation.ts +252 -0
  496. package/src/modules/documents/backend/documents/documentUi.ts +134 -0
  497. package/src/modules/documents/backend/documents/documentsListTypes.ts +163 -0
  498. package/src/modules/documents/backend/documents/page.meta.ts +24 -0
  499. package/src/modules/documents/backend/documents/page.tsx +5 -0
  500. package/src/modules/documents/backend/documents/templates/TemplatesPageClient.tsx +93 -0
  501. package/src/modules/documents/backend/documents/templates/TemplatesTable.tsx +71 -0
  502. package/src/modules/documents/backend/documents/templates/page.meta.ts +32 -0
  503. package/src/modules/documents/backend/documents/templates/page.tsx +5 -0
  504. package/src/modules/documents/backend/documents/templates/useTemplatesPage.tsx +148 -0
  505. package/src/modules/documents/backend/documents/useDocumentsList.tsx +305 -0
  506. package/src/modules/documents/ce.ts +6 -0
  507. package/src/modules/documents/commands/aggregate.ts +128 -0
  508. package/src/modules/documents/commands/attachments.ts +309 -0
  509. package/src/modules/documents/commands/comments.ts +966 -0
  510. package/src/modules/documents/commands/content.ts +294 -0
  511. package/src/modules/documents/commands/document-crud.ts +609 -0
  512. package/src/modules/documents/commands/documents.ts +581 -0
  513. package/src/modules/documents/commands/duplicate.ts +772 -0
  514. package/src/modules/documents/commands/favorites.ts +171 -0
  515. package/src/modules/documents/commands/folders.ts +506 -0
  516. package/src/modules/documents/commands/index.ts +33 -0
  517. package/src/modules/documents/commands/interceptors.ts +402 -0
  518. package/src/modules/documents/commands/lifecycle.ts +333 -0
  519. package/src/modules/documents/commands/links.ts +486 -0
  520. package/src/modules/documents/commands/mutation-helpers.ts +48 -0
  521. package/src/modules/documents/commands/projection-types.ts +77 -0
  522. package/src/modules/documents/commands/shared.ts +219 -0
  523. package/src/modules/documents/commands/shares.ts +612 -0
  524. package/src/modules/documents/commands/side-effects.ts +130 -0
  525. package/src/modules/documents/commands/templates.ts +522 -0
  526. package/src/modules/documents/commands/versions.ts +537 -0
  527. package/src/modules/documents/commands/watchers.ts +181 -0
  528. package/src/modules/documents/data/entities.ts +631 -0
  529. package/src/modules/documents/data/extensions.ts +42 -0
  530. package/src/modules/documents/data/validators.ts +248 -0
  531. package/src/modules/documents/di.ts +33 -0
  532. package/src/modules/documents/encryption.ts +14 -0
  533. package/src/modules/documents/events.ts +23 -0
  534. package/src/modules/documents/i18n/.hardcoded-allowlist.json +9 -0
  535. package/src/modules/documents/i18n/de.json +522 -0
  536. package/src/modules/documents/i18n/en.json +522 -0
  537. package/src/modules/documents/i18n/es.json +522 -0
  538. package/src/modules/documents/i18n/ko.json +522 -0
  539. package/src/modules/documents/i18n/pl.json +522 -0
  540. package/src/modules/documents/index.ts +17 -0
  541. package/src/modules/documents/lib/attachmentServicePort.ts +94 -0
  542. package/src/modules/documents/lib/capabilities.ts +57 -0
  543. package/src/modules/documents/lib/collabCloseEvents.ts +26 -0
  544. package/src/modules/documents/lib/collabEndpoint.ts +43 -0
  545. package/src/modules/documents/lib/collabMaterializer.ts +140 -0
  546. package/src/modules/documents/lib/collabToken.ts +170 -0
  547. package/src/modules/documents/lib/collaborationAwareness.ts +102 -0
  548. package/src/modules/documents/lib/commentAnchors.ts +19 -0
  549. package/src/modules/documents/lib/constants.ts +39 -0
  550. package/src/modules/documents/lib/contentService.ts +340 -0
  551. package/src/modules/documents/lib/displayLabels.ts +31 -0
  552. package/src/modules/documents/lib/docxRenderer.ts +685 -0
  553. package/src/modules/documents/lib/duplicateContent.ts +32 -0
  554. package/src/modules/documents/lib/editorConfig.ts +371 -0
  555. package/src/modules/documents/lib/entityLinks.ts +250 -0
  556. package/src/modules/documents/lib/entityRegistry.server.ts +267 -0
  557. package/src/modules/documents/lib/entityRegistry.ts +345 -0
  558. package/src/modules/documents/lib/entityRegistryAvailability.server.ts +19 -0
  559. package/src/modules/documents/lib/entitySuggestion.ts +61 -0
  560. package/src/modules/documents/lib/favorites.ts +35 -0
  561. package/src/modules/documents/lib/folderHierarchySerialization.ts +34 -0
  562. package/src/modules/documents/lib/historyLimits.ts +154 -0
  563. package/src/modules/documents/lib/historyPolicy.ts +7 -0
  564. package/src/modules/documents/lib/organizationAccess.ts +44 -0
  565. package/src/modules/documents/lib/pdfHtml.ts +65 -0
  566. package/src/modules/documents/lib/pdfRenderer.ts +367 -0
  567. package/src/modules/documents/lib/permissions.ts +299 -0
  568. package/src/modules/documents/lib/platformServices.ts +70 -0
  569. package/src/modules/documents/lib/recordFieldInsertion.ts +72 -0
  570. package/src/modules/documents/lib/requestBody.ts +97 -0
  571. package/src/modules/documents/lib/resourceLimits.ts +94 -0
  572. package/src/modules/documents/lib/shareCounts.ts +44 -0
  573. package/src/modules/documents/lib/templateFill.ts +138 -0
  574. package/src/modules/documents/lib/templateInstantiation.ts +206 -0
  575. package/src/modules/documents/lib/templatePreviewDigest.ts +122 -0
  576. package/src/modules/documents/lib/templateSeeds.ts +248 -0
  577. package/src/modules/documents/lib/userLabels.ts +58 -0
  578. package/src/modules/documents/lib/versionContent.ts +163 -0
  579. package/src/modules/documents/lib/versionLabels.ts +9 -0
  580. package/src/modules/documents/lib/versioning.ts +29 -0
  581. package/src/modules/documents/lib/visibility.ts +610 -0
  582. package/src/modules/documents/lib/watchers.ts +129 -0
  583. package/src/modules/documents/migrations/.snapshot-open-mercato.json +2892 -0
  584. package/src/modules/documents/migrations/Migration20260708163836_documents.ts +50 -0
  585. package/src/modules/documents/migrations/Migration20260709164720_documents.ts +19 -0
  586. package/src/modules/documents/migrations/Migration20260710002318_documents.ts +35 -0
  587. package/src/modules/documents/migrations/Migration20260710071003_documents.ts +13 -0
  588. package/src/modules/documents/migrations/Migration20260712120000_documents.ts +13 -0
  589. package/src/modules/documents/migrations/Migration20260712183000_documents.ts +12 -0
  590. package/src/modules/documents/migrations/Migration20260713092156_documents.ts +43 -0
  591. package/src/modules/documents/migrations/Migration20260717000000_documents.ts +40 -0
  592. package/src/modules/documents/notifications.ts +45 -0
  593. package/src/modules/documents/search.ts +118 -0
  594. package/src/modules/documents/setup.ts +19 -0
  595. package/src/modules/documents/widgets/injection/related-documents/LinkDocumentDialog.tsx +224 -0
  596. package/src/modules/documents/widgets/injection/related-documents/context.ts +101 -0
  597. package/src/modules/documents/widgets/injection/related-documents/useRelatedDocuments.ts +112 -0
  598. package/src/modules/documents/widgets/injection/related-documents/widget.client.tsx +76 -0
  599. package/src/modules/documents/widgets/injection/related-documents/widget.ts +19 -0
  600. package/src/modules/documents/widgets/injection-table.ts +20 -0
  601. package/tsconfig.json +9 -0
  602. package/watch.mjs +6 -0
@@ -0,0 +1,966 @@
1
+ import { LockMode } from '@mikro-orm/core'
2
+ import type { EntityManager } from '@mikro-orm/postgresql'
3
+ import { z } from 'zod'
4
+ import { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'
5
+ import { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'
6
+ import { extractUndoPayload } from '@open-mercato/shared/lib/commands/undo'
7
+ import { CrudHttpError, isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'
8
+ import {
9
+ assertOptimisticLock,
10
+ buildOptimisticLockConflictBody,
11
+ } from '@open-mercato/shared/lib/crud/optimistic-lock-command'
12
+ import {
13
+ findOneWithDecryption,
14
+ findWithDecryption,
15
+ } from '@open-mercato/shared/lib/encryption/find'
16
+ import { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'
17
+ import { Document, DocumentComment, DocumentShare } from '../data/entities'
18
+ import {
19
+ documentCommentCreateSchema,
20
+ documentSharePermissionSchema,
21
+ } from '../data/validators'
22
+ import { DOCUMENTS_ENTITY_IDS } from '../lib/constants'
23
+ import { assertDocumentCommentCapacity } from '../lib/historyLimits'
24
+ import { resolveUserAccess } from '../lib/permissions'
25
+ import { resolveWatcherRecipients } from '../lib/watchers'
26
+ import type { DocumentsProjectionDescriptor } from './projection-types'
27
+ import {
28
+ assertDocumentCommandCapability,
29
+ resolveDocumentsCommandActor,
30
+ resolveDocumentsCommandScope,
31
+ } from './shared'
32
+ import { nextDocumentVersion } from './mutation-helpers'
33
+ import { resolveAuthPrincipalService, type DocumentsServiceContainer } from '../lib/platformServices'
34
+
35
+ const scopeSchema = z.object({
36
+ tenantId: z.string().uuid(),
37
+ organizationId: z.string().uuid(),
38
+ })
39
+
40
+ const commentStateSchema = z.object({
41
+ id: z.string().uuid(),
42
+ existed: z.boolean(),
43
+ parentCommentId: z.string().uuid().nullable(),
44
+ authorUserId: z.string().uuid(),
45
+ body: z.string(),
46
+ // Snapshots must remain permissive for legacy anchors. New command input is
47
+ // still validated by documentCommentCreateSchema's strict write union.
48
+ anchor: z.record(z.string(), z.unknown()).nullable(),
49
+ mentions: z.array(z.object({ userId: z.string().uuid() })).nullable(),
50
+ resolvedAt: z.string().datetime().nullable(),
51
+ resolvedByUserId: z.string().uuid().nullable(),
52
+ deletedAt: z.string().datetime().nullable(),
53
+ updatedAt: z.string().datetime().nullable(),
54
+ })
55
+
56
+ const mentionShareStateSchema = z.object({
57
+ id: z.string().uuid(),
58
+ userId: z.string().uuid(),
59
+ existed: z.boolean(),
60
+ permission: documentSharePermissionSchema,
61
+ createdByUserId: z.string().uuid(),
62
+ deletedAt: z.string().datetime().nullable(),
63
+ updatedAt: z.string().datetime().nullable(),
64
+ })
65
+
66
+ const commentRedoExpectationSchema = z.discriminatedUnion('kind', [
67
+ z.object({ kind: z.literal('snapshot'), state: commentStateSchema }),
68
+ z.object({ kind: z.literal('soft-deleted-created'), state: commentStateSchema }),
69
+ ])
70
+
71
+ const commentCommandBaseSchema = scopeSchema.extend({
72
+ documentId: z.string().uuid(),
73
+ actorUserId: z.string().uuid(),
74
+ })
75
+
76
+ export const commentCreateCommandSchema = commentCommandBaseSchema.extend({
77
+ commentId: z.string().uuid(),
78
+ comment: documentCommentCreateSchema,
79
+ grantShares: z.array(z.object({ userId: z.string().uuid(), shareId: z.string().uuid() })).max(50),
80
+ redoExpectation: commentRedoExpectationSchema.optional(),
81
+ }).superRefine((input, context) => {
82
+ const grantIds = Array.from(new Set((input.comment.grantAccessTo ?? []).map((id) => id.toLowerCase())))
83
+ const identities = Array.from(new Set(input.grantShares.map((grant) => grant.userId.toLowerCase())))
84
+ if (grantIds.length !== identities.length || grantIds.some((id) => !identities.includes(id))) {
85
+ context.addIssue({ code: 'custom', message: 'documents.comments.grantIdentityMismatch' })
86
+ }
87
+ })
88
+
89
+ export const commentResolveCommandSchema = commentCommandBaseSchema.extend({
90
+ comment: z.object({ id: z.string().uuid(), resolved: z.boolean() }),
91
+ expectedUpdatedAt: z.string().datetime().nullable().optional(),
92
+ redoExpectation: commentRedoExpectationSchema.optional(),
93
+ })
94
+
95
+ export type CommentCreateCommandInput = z.infer<typeof commentCreateCommandSchema>
96
+ export type CommentResolveCommandInput = z.infer<typeof commentResolveCommandSchema>
97
+ export type CommentState = z.infer<typeof commentStateSchema>
98
+ export type MentionShareState = z.infer<typeof mentionShareStateSchema>
99
+
100
+ export type CommentCreateCommandResult = {
101
+ id: string
102
+ updatedAt: string
103
+ projections?: DocumentsProjectionDescriptor[]
104
+ undo: CommentCreateUndoPayload
105
+ }
106
+
107
+ export type CommentResolveCommandResult = {
108
+ id: string
109
+ resolvedAt: string | null
110
+ resolvedByUserId: string | null
111
+ updatedAt: string
112
+ before: CommentState
113
+ after: CommentState
114
+ projections?: DocumentsProjectionDescriptor[]
115
+ }
116
+
117
+ type MentionShareMutation = {
118
+ before: MentionShareState
119
+ after: MentionShareState
120
+ }
121
+
122
+ type PendingMentionShareMutation = {
123
+ before: MentionShareState
124
+ share: DocumentShare
125
+ fallback: { shareId: string; userId: string; actorUserId: string }
126
+ }
127
+
128
+ type CommentCreateUndoPayload = {
129
+ before?: CommentState | null
130
+ after?: CommentState | null
131
+ shareMutations: MentionShareMutation[]
132
+ projectionsAfterUndo?: DocumentsProjectionDescriptor[]
133
+ }
134
+
135
+ type CommentResolveUndoPayload = {
136
+ before?: CommentState | null
137
+ after?: CommentState | null
138
+ projectionsAfterUndo?: DocumentsProjectionDescriptor[]
139
+ }
140
+
141
+ function extractMentionedUserIds(body: string): string[] {
142
+ const pattern = /@\[([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\]/gi
143
+ return Array.from(new Set(
144
+ Array.from(body.matchAll(pattern))
145
+ .map((match) => match[1])
146
+ .filter((value): value is string => typeof value === 'string')
147
+ .map((value) => value.toLowerCase()),
148
+ ))
149
+ }
150
+
151
+ function dedupeUserIds(values: string[]): string[] {
152
+ return Array.from(new Set(values.map((value) => value.toLowerCase())))
153
+ }
154
+
155
+ function commentFallback(input: CommentCreateCommandInput): Omit<CommentState, 'existed' | 'deletedAt' | 'updatedAt'> {
156
+ const mentionUserIds = dedupeUserIds([
157
+ ...(input.comment.mentions ?? []).map((mention) => mention.userId),
158
+ ...extractMentionedUserIds(input.comment.body),
159
+ ])
160
+ return {
161
+ id: input.commentId,
162
+ parentCommentId: input.comment.parentCommentId ?? null,
163
+ authorUserId: input.actorUserId,
164
+ body: input.comment.body,
165
+ anchor: input.comment.anchor ?? null,
166
+ mentions: mentionUserIds.length ? mentionUserIds.map((userId) => ({ userId })) : null,
167
+ resolvedAt: null,
168
+ resolvedByUserId: null,
169
+ }
170
+ }
171
+
172
+ function captureCommentState(
173
+ comment: DocumentComment | null,
174
+ fallback: Omit<CommentState, 'existed' | 'deletedAt' | 'updatedAt'>,
175
+ ): CommentState {
176
+ return {
177
+ id: comment?.id ?? fallback.id,
178
+ existed: comment !== null,
179
+ parentCommentId: comment?.parentCommentId ?? fallback.parentCommentId,
180
+ authorUserId: comment?.authorUserId ?? fallback.authorUserId,
181
+ body: comment?.body ?? fallback.body,
182
+ anchor: (comment?.anchor ?? fallback.anchor) as CommentState['anchor'],
183
+ mentions: comment?.mentions ?? fallback.mentions,
184
+ resolvedAt: comment?.resolvedAt?.toISOString() ?? fallback.resolvedAt,
185
+ resolvedByUserId: comment?.resolvedByUserId ?? fallback.resolvedByUserId,
186
+ deletedAt: comment?.deletedAt?.toISOString() ?? null,
187
+ updatedAt: comment?.updatedAt?.toISOString() ?? null,
188
+ }
189
+ }
190
+
191
+ function captureMentionShareState(
192
+ share: DocumentShare | null,
193
+ fallback: { shareId: string; userId: string; actorUserId: string },
194
+ ): MentionShareState {
195
+ return {
196
+ id: share?.id ?? fallback.shareId,
197
+ userId: share?.principalId ?? fallback.userId,
198
+ existed: share !== null,
199
+ permission: share?.permission ?? 'commenter',
200
+ createdByUserId: share?.createdByUserId ?? fallback.actorUserId,
201
+ deletedAt: share?.deletedAt?.toISOString() ?? null,
202
+ updatedAt: share?.updatedAt?.toISOString() ?? null,
203
+ }
204
+ }
205
+
206
+ function assertActor(input: { actorUserId: string }, ctx: Parameters<CommandHandler['execute']>[1]): void {
207
+ if (resolveDocumentsCommandActor(ctx) !== input.actorUserId) {
208
+ throw new CrudHttpError(403, { error: 'Forbidden' })
209
+ }
210
+ }
211
+
212
+ async function loadLockedDocument(
213
+ em: EntityManager,
214
+ input: { tenantId: string; organizationId: string; documentId: string },
215
+ ): Promise<Document> {
216
+ const document = await findOneWithDecryption(
217
+ em,
218
+ Document,
219
+ {
220
+ id: input.documentId,
221
+ tenantId: input.tenantId,
222
+ organizationId: input.organizationId,
223
+ deletedAt: null,
224
+ },
225
+ { lockMode: LockMode.PESSIMISTIC_WRITE },
226
+ { tenantId: input.tenantId, organizationId: input.organizationId },
227
+ )
228
+ if (!document) throw new CrudHttpError(404, { error: 'Document not found' })
229
+ return document
230
+ }
231
+
232
+ async function loadComment(
233
+ em: EntityManager,
234
+ input: { tenantId: string; organizationId: string; documentId: string },
235
+ commentId: string,
236
+ lock = false,
237
+ ): Promise<DocumentComment | null> {
238
+ return findOneWithDecryption(
239
+ em,
240
+ DocumentComment,
241
+ {
242
+ id: commentId,
243
+ documentId: input.documentId,
244
+ tenantId: input.tenantId,
245
+ organizationId: input.organizationId,
246
+ },
247
+ {
248
+ filters: false,
249
+ ...(lock ? { lockMode: LockMode.PESSIMISTIC_WRITE } : {}),
250
+ },
251
+ { tenantId: input.tenantId, organizationId: input.organizationId },
252
+ )
253
+ }
254
+
255
+ async function assertNoActiveReplies(
256
+ em: EntityManager,
257
+ input: { tenantId: string; organizationId: string; documentId: string },
258
+ commentId: string,
259
+ ): Promise<void> {
260
+ const replies = await findWithDecryption(
261
+ em,
262
+ DocumentComment,
263
+ {
264
+ documentId: input.documentId,
265
+ parentCommentId: commentId,
266
+ tenantId: input.tenantId,
267
+ organizationId: input.organizationId,
268
+ deletedAt: null,
269
+ },
270
+ { limit: 1 },
271
+ { tenantId: input.tenantId, organizationId: input.organizationId },
272
+ )
273
+ if (replies.length > 0) {
274
+ throw new CrudHttpError(409, { error: 'Record changed by another user' })
275
+ }
276
+ }
277
+
278
+ async function loadMentionShare(
279
+ em: EntityManager,
280
+ input: { tenantId: string; organizationId: string; documentId: string },
281
+ userId: string,
282
+ lock = false,
283
+ ): Promise<DocumentShare | null> {
284
+ return findOneWithDecryption(
285
+ em,
286
+ DocumentShare,
287
+ {
288
+ documentId: input.documentId,
289
+ tenantId: input.tenantId,
290
+ organizationId: input.organizationId,
291
+ principalType: 'user',
292
+ principalId: userId,
293
+ },
294
+ {
295
+ filters: false,
296
+ orderBy: { updatedAt: 'DESC' },
297
+ ...(lock ? { lockMode: LockMode.PESSIMISTIC_WRITE } : {}),
298
+ },
299
+ { tenantId: input.tenantId, organizationId: input.organizationId },
300
+ )
301
+ }
302
+
303
+ async function assertUserPrincipal(
304
+ container: DocumentsServiceContainer,
305
+ input: { tenantId: string; organizationId: string },
306
+ userId: string,
307
+ ): Promise<void> {
308
+ const exists = await resolveAuthPrincipalService(container)?.principalExists({
309
+ type: 'user',
310
+ id: userId,
311
+ scope: { tenantId: input.tenantId, organizationId: input.organizationId },
312
+ }) ?? false
313
+ if (!exists) throw new CrudHttpError(400, { error: 'Share principal not found in this organization' })
314
+ }
315
+
316
+ async function hasDocumentCapability(
317
+ ctx: Parameters<CommandHandler['execute']>[1],
318
+ em: EntityManager,
319
+ input: { tenantId: string; organizationId: string; documentId: string },
320
+ capability: 'canComment' | 'canShare',
321
+ ): Promise<boolean> {
322
+ try {
323
+ await assertDocumentCommandCapability(
324
+ ctx,
325
+ em,
326
+ input.documentId,
327
+ resolveDocumentsCommandScope(ctx, input),
328
+ capability,
329
+ )
330
+ return true
331
+ } catch (error) {
332
+ if (isCrudHttpError(error) && error.status === 403) return false
333
+ throw error
334
+ }
335
+ }
336
+
337
+ async function assertCanCreateComment(
338
+ ctx: Parameters<CommandHandler['execute']>[1],
339
+ em: EntityManager,
340
+ input: CommentCreateCommandInput,
341
+ ): Promise<void> {
342
+ resolveDocumentsCommandScope(ctx, input)
343
+ if (!(await hasDocumentCapability(ctx, em, input, 'canComment'))) {
344
+ throw new CrudHttpError(403, { error: 'Forbidden' })
345
+ }
346
+ }
347
+
348
+ async function assertCanMutateMentionShares(
349
+ ctx: Parameters<CommandHandler['execute']>[1],
350
+ em: EntityManager,
351
+ input: CommentCreateCommandInput,
352
+ ): Promise<void> {
353
+ const scope = resolveDocumentsCommandScope(ctx, input)
354
+ await assertDocumentCommandCapability(ctx, em, input.documentId, scope, 'canShare')
355
+ }
356
+
357
+ async function assertCanResolveComment(
358
+ ctx: Parameters<CommandHandler['execute']>[1],
359
+ em: EntityManager,
360
+ input: CommentResolveCommandInput,
361
+ comment: DocumentComment,
362
+ ): Promise<void> {
363
+ const scope = resolveDocumentsCommandScope(ctx, input)
364
+ await assertDocumentCommandCapability(ctx, em, input.documentId, scope, 'canView')
365
+ if (comment.authorUserId === resolveDocumentsCommandActor(ctx)) return
366
+ if (!(await hasDocumentCapability(ctx, em, input, 'canComment'))) {
367
+ throw new CrudHttpError(403, { error: 'Forbidden' })
368
+ }
369
+ }
370
+
371
+ function assertCommentMatchesState(
372
+ comment: DocumentComment | null,
373
+ expected: CommentState,
374
+ softDeletedCreated = false,
375
+ ): void {
376
+ if (!comment || !expected.existed) throw new CrudHttpError(409, { error: 'Record changed by another user' })
377
+ const scalarsMatch = comment.id === expected.id
378
+ && (comment.parentCommentId ?? null) === expected.parentCommentId
379
+ && comment.authorUserId === expected.authorUserId
380
+ && comment.body === expected.body
381
+ && JSON.stringify(comment.anchor ?? null) === JSON.stringify(expected.anchor)
382
+ && JSON.stringify(comment.mentions ?? null) === JSON.stringify(expected.mentions)
383
+ && (comment.resolvedAt?.toISOString() ?? null) === expected.resolvedAt
384
+ && (comment.resolvedByUserId ?? null) === expected.resolvedByUserId
385
+ const deletedAt = comment.deletedAt?.toISOString() ?? null
386
+ const deletionMatches = softDeletedCreated ? deletedAt !== null : deletedAt === expected.deletedAt
387
+ if (!scalarsMatch || !deletionMatches) {
388
+ throw new CrudHttpError(409, buildOptimisticLockConflictBody(
389
+ comment.updatedAt.toISOString(),
390
+ expected.updatedAt ?? comment.updatedAt.toISOString(),
391
+ ))
392
+ }
393
+ }
394
+
395
+ function assertCommentUnchanged(
396
+ comment: DocumentComment | null,
397
+ expected: CommentState,
398
+ ): asserts comment is DocumentComment {
399
+ if (!comment || !expected.existed || !expected.updatedAt) {
400
+ throw new CrudHttpError(409, { error: 'Record changed by another user' })
401
+ }
402
+ assertOptimisticLock({
403
+ resourceKind: DOCUMENTS_ENTITY_IDS.documentComment,
404
+ resourceId: expected.id,
405
+ current: comment.updatedAt,
406
+ expected: expected.updatedAt,
407
+ envValue: 'all',
408
+ })
409
+ assertCommentMatchesState(comment, expected)
410
+ }
411
+
412
+ function assertMentionShareUnchanged(share: DocumentShare | null, expected: MentionShareState): asserts share is DocumentShare {
413
+ if (!share || !expected.existed || !expected.updatedAt) {
414
+ throw new CrudHttpError(409, { error: 'Record changed by another user' })
415
+ }
416
+ assertOptimisticLock({
417
+ resourceKind: DOCUMENTS_ENTITY_IDS.documentShare,
418
+ resourceId: expected.id,
419
+ current: share.updatedAt,
420
+ expected: expected.updatedAt,
421
+ envValue: 'all',
422
+ })
423
+ if (
424
+ share.id !== expected.id
425
+ || share.principalType !== 'user'
426
+ || share.principalId !== expected.userId
427
+ || share.permission !== expected.permission
428
+ || share.createdByUserId !== expected.createdByUserId
429
+ || (share.deletedAt?.toISOString() ?? null) !== expected.deletedAt
430
+ ) {
431
+ throw new CrudHttpError(409, { error: 'Record changed by another user' })
432
+ }
433
+ }
434
+
435
+ function restoreCommentState(comment: DocumentComment, before: CommentState): void {
436
+ const now = nextDocumentVersion(comment.updatedAt)
437
+ if (!before.existed) {
438
+ comment.deletedAt = now
439
+ comment.updatedAt = now
440
+ return
441
+ }
442
+ comment.parentCommentId = before.parentCommentId
443
+ comment.authorUserId = before.authorUserId
444
+ comment.body = before.body
445
+ comment.anchor = before.anchor
446
+ comment.mentions = before.mentions
447
+ comment.resolvedAt = before.resolvedAt ? new Date(before.resolvedAt) : null
448
+ comment.resolvedByUserId = before.resolvedByUserId
449
+ comment.deletedAt = before.deletedAt ? new Date(before.deletedAt) : null
450
+ comment.updatedAt = now
451
+ }
452
+
453
+ function restoreMentionShareState(share: DocumentShare, before: MentionShareState): void {
454
+ const now = nextDocumentVersion(share.updatedAt)
455
+ if (!before.existed) {
456
+ share.deletedAt = now
457
+ share.updatedAt = now
458
+ return
459
+ }
460
+ share.permission = before.permission
461
+ share.createdByUserId = before.createdByUserId
462
+ share.deletedAt = before.deletedAt ? new Date(before.deletedAt) : null
463
+ share.updatedAt = now
464
+ }
465
+
466
+ function shareProjection(
467
+ eventId: 'documents.document.shared' | 'documents.document.unshared',
468
+ input: CommentCreateCommandInput,
469
+ state: MentionShareState,
470
+ includeActor = true,
471
+ ): DocumentsProjectionDescriptor {
472
+ return {
473
+ kind: 'event',
474
+ eventId,
475
+ tenantId: input.tenantId,
476
+ organizationId: input.organizationId,
477
+ payload: {
478
+ id: input.documentId,
479
+ shareId: state.id,
480
+ principalType: 'user',
481
+ principalId: state.userId,
482
+ tenantId: input.tenantId,
483
+ organizationId: input.organizationId,
484
+ ...(includeActor ? { userId: input.actorUserId } : {}),
485
+ },
486
+ }
487
+ }
488
+
489
+ function resolutionProjection(
490
+ input: CommentResolveCommandInput,
491
+ state: CommentState,
492
+ includeActor = true,
493
+ ): DocumentsProjectionDescriptor {
494
+ return {
495
+ kind: 'event',
496
+ eventId: 'documents.comment.resolved',
497
+ tenantId: input.tenantId,
498
+ organizationId: input.organizationId,
499
+ payload: {
500
+ id: state.id,
501
+ documentId: input.documentId,
502
+ resolved: state.resolvedAt !== null,
503
+ tenantId: input.tenantId,
504
+ organizationId: input.organizationId,
505
+ ...(includeActor ? { userId: input.actorUserId } : {}),
506
+ },
507
+ }
508
+ }
509
+
510
+ function mentionProjections(
511
+ input: CommentCreateCommandInput,
512
+ document: Document,
513
+ mentionedUserIds: string[],
514
+ ): DocumentsProjectionDescriptor[] {
515
+ return mentionedUserIds.flatMap((recipientUserId): DocumentsProjectionDescriptor[] => [
516
+ {
517
+ kind: 'event',
518
+ eventId: 'documents.comment.mentioned',
519
+ tenantId: input.tenantId,
520
+ organizationId: input.organizationId,
521
+ payload: {
522
+ id: input.commentId,
523
+ documentId: input.documentId,
524
+ mentionedUserId: recipientUserId,
525
+ tenantId: input.tenantId,
526
+ organizationId: input.organizationId,
527
+ userId: input.actorUserId,
528
+ },
529
+ },
530
+ {
531
+ kind: 'mention-notification',
532
+ recipientUserId,
533
+ tenantId: input.tenantId,
534
+ organizationId: input.organizationId,
535
+ documentId: input.documentId,
536
+ documentTitle: document.title,
537
+ commentId: input.commentId,
538
+ authorUserId: input.actorUserId,
539
+ },
540
+ ])
541
+ }
542
+
543
+ function watchCommentProjections(
544
+ input: { tenantId: string; organizationId: string; documentId: string; commentId: string },
545
+ documentTitle: string,
546
+ recipientUserIds: readonly string[],
547
+ bodyKey: string,
548
+ ): DocumentsProjectionDescriptor[] {
549
+ return recipientUserIds.map((recipientUserId) => ({
550
+ kind: 'watch-notification',
551
+ recipientUserId,
552
+ tenantId: input.tenantId,
553
+ organizationId: input.organizationId,
554
+ documentId: input.documentId,
555
+ documentTitle,
556
+ notificationType: 'documents.watch.commented',
557
+ bodyKey,
558
+ sourceEntityType: DOCUMENTS_ENTITY_IDS.documentComment,
559
+ sourceEntityId: input.commentId,
560
+ linkHref: `/backend/documents/${encodeURIComponent(input.documentId)}?commentId=${encodeURIComponent(input.commentId)}`,
561
+ }))
562
+ }
563
+
564
+ function commentCreatedProjection(
565
+ input: CommentCreateCommandInput,
566
+ ): DocumentsProjectionDescriptor {
567
+ return {
568
+ kind: 'event',
569
+ eventId: 'documents.comment.created',
570
+ tenantId: input.tenantId,
571
+ organizationId: input.organizationId,
572
+ payload: {
573
+ id: input.commentId,
574
+ documentId: input.documentId,
575
+ tenantId: input.tenantId,
576
+ organizationId: input.organizationId,
577
+ userId: input.actorUserId,
578
+ },
579
+ }
580
+ }
581
+
582
+ function readRedoInput<T>(logEntry: { commandPayload?: unknown }, schema: z.ZodType<T>): T {
583
+ const raw = logEntry.commandPayload && typeof logEntry.commandPayload === 'object'
584
+ ? (logEntry.commandPayload as { __redoInput?: unknown }).__redoInput
585
+ : null
586
+ return schema.parse(raw)
587
+ }
588
+
589
+ export const createCommentCommand: CommandHandler<CommentCreateCommandInput, CommentCreateCommandResult> = {
590
+ id: 'documents.comment.create',
591
+ async prepare(rawInput, ctx) {
592
+ const input = commentCreateCommandSchema.parse(rawInput)
593
+ assertActor(input, ctx)
594
+ const em = ctx.container.resolve('em') as EntityManager
595
+ await assertCanCreateComment(ctx, em, input)
596
+ return null
597
+ },
598
+ async execute(rawInput, ctx) {
599
+ const input = commentCreateCommandSchema.parse(rawInput)
600
+ const em = ctx.transactionalEm ?? (ctx.container.resolve('em') as EntityManager).fork()
601
+ const fallback = commentFallback(input)
602
+ const mentionUserIds = fallback.mentions?.map((mention) => mention.userId) ?? []
603
+ const grantIdentityByUserId = new Map(input.grantShares.map((grant) => [grant.userId.toLowerCase(), grant]))
604
+ const shareMutations: MentionShareMutation[] = []
605
+ const pendingShareMutations: PendingMentionShareMutation[] = []
606
+ const notifyMentionedIds: string[] = []
607
+ let document: Document | null = null
608
+ let comment: DocumentComment | null = null
609
+ let before: CommentState | null = null
610
+ let after: CommentState | null = null
611
+ await withAtomicFlush(em, [async () => {
612
+ document = await loadLockedDocument(em, input)
613
+ assertActor(input, ctx)
614
+ await assertCanCreateComment(ctx, em, input)
615
+ comment = await loadComment(em, input, input.commentId, true)
616
+ if (input.redoExpectation) {
617
+ assertCommentMatchesState(
618
+ comment,
619
+ input.redoExpectation.state,
620
+ input.redoExpectation.kind === 'soft-deleted-created',
621
+ )
622
+ } else if (comment) {
623
+ throw new CrudHttpError(409, { error: 'Record changed by another user' })
624
+ }
625
+ before = captureCommentState(comment, fallback)
626
+ if (fallback.parentCommentId) {
627
+ const parent = await loadComment(em, input, fallback.parentCommentId, true)
628
+ if (!parent || parent.deletedAt) throw new CrudHttpError(404, { error: 'Comment not found' })
629
+ }
630
+ await assertDocumentCommentCapacity(em, input)
631
+
632
+ const canShare = await hasDocumentCapability(ctx, em, input, 'canShare')
633
+ // An explicit grant request from an actor without the share capability
634
+ // must fail loudly instead of returning 201 with the grants silently
635
+ // dropped. Redo replays are exempt: their grants either already exist
636
+ // (mentioned users resolve a tier below) or were legitimately created
637
+ // by the original execution.
638
+ if (input.grantShares.length > 0 && !canShare && !input.redoExpectation) {
639
+ throw new CrudHttpError(403, { error: 'Forbidden' })
640
+ }
641
+ for (const mentionedUserId of mentionUserIds) {
642
+ const grantIdentity = grantIdentityByUserId.get(mentionedUserId)
643
+ if (!grantIdentity || !canShare) continue
644
+ const tier = await resolveUserAccess(em, input.documentId, input, mentionedUserId, ctx.container)
645
+ if (tier) continue
646
+ await assertUserPrincipal(ctx.container, input, mentionedUserId)
647
+ let share = await loadMentionShare(em, input, mentionedUserId, true)
648
+ const shareFallback = {
649
+ shareId: share?.id ?? grantIdentity.shareId,
650
+ userId: mentionedUserId,
651
+ actorUserId: input.actorUserId,
652
+ }
653
+ const shareBefore = captureMentionShareState(share, shareFallback)
654
+ const created = !share
655
+ share = share ?? em.create(DocumentShare, {
656
+ id: grantIdentity.shareId,
657
+ tenantId: input.tenantId,
658
+ organizationId: input.organizationId,
659
+ documentId: input.documentId,
660
+ principalType: 'user',
661
+ principalId: mentionedUserId,
662
+ permission: 'commenter',
663
+ createdByUserId: input.actorUserId,
664
+ })
665
+ share.permission = 'commenter'
666
+ share.deletedAt = null
667
+ share.updatedAt = nextDocumentVersion(share.updatedAt)
668
+ if (created) em.persist(share)
669
+ pendingShareMutations.push({ before: shareBefore, share, fallback: shareFallback })
670
+ }
671
+
672
+ const created = !comment
673
+ comment = comment ?? em.create(DocumentComment, {
674
+ id: input.commentId,
675
+ tenantId: input.tenantId,
676
+ organizationId: input.organizationId,
677
+ documentId: input.documentId,
678
+ parentCommentId: fallback.parentCommentId,
679
+ authorUserId: fallback.authorUserId,
680
+ body: fallback.body,
681
+ anchor: fallback.anchor,
682
+ mentions: fallback.mentions,
683
+ })
684
+ comment.parentCommentId = fallback.parentCommentId
685
+ comment.authorUserId = fallback.authorUserId
686
+ comment.body = fallback.body
687
+ comment.anchor = fallback.anchor
688
+ comment.mentions = fallback.mentions
689
+ comment.resolvedAt = null
690
+ comment.resolvedByUserId = null
691
+ comment.deletedAt = null
692
+ comment.updatedAt = nextDocumentVersion(comment.updatedAt)
693
+ if (created) em.persist(comment)
694
+ }, async () => {
695
+ after = captureCommentState(comment, fallback)
696
+ for (const mutation of pendingShareMutations) {
697
+ shareMutations.push({
698
+ before: mutation.before,
699
+ after: captureMentionShareState(mutation.share, mutation.fallback),
700
+ })
701
+ }
702
+ for (const mentionedUserId of mentionUserIds) {
703
+ const tier = await resolveUserAccess(em, input.documentId, input, mentionedUserId, ctx.container)
704
+ if (tier) notifyMentionedIds.push(mentionedUserId)
705
+ }
706
+ }], { transaction: true, label: 'documents.comment.create' })
707
+
708
+ const finalDocument = document as Document | null
709
+ const finalComment = comment as DocumentComment | null
710
+ const beforeState = before as CommentState | null
711
+ if (!finalDocument || !finalComment || !beforeState) {
712
+ throw new Error('[internal] comment create produced no row')
713
+ }
714
+ const afterState = after as CommentState | null
715
+ if (!afterState) throw new Error('[internal] comment create produced no after snapshot')
716
+ const watcherRecipientIds = await resolveWatcherRecipients({
717
+ em,
718
+ container: ctx.container,
719
+ scope: { tenantId: input.tenantId, organizationId: input.organizationId },
720
+ documentId: input.documentId,
721
+ actorUserId: input.actorUserId,
722
+ excludeUserIds: notifyMentionedIds,
723
+ })
724
+ const projections = [
725
+ commentCreatedProjection(input),
726
+ ...shareMutations.map((mutation) => shareProjection('documents.document.shared', input, mutation.after)),
727
+ ...mentionProjections(input, finalDocument, notifyMentionedIds),
728
+ ...watchCommentProjections(
729
+ { ...input, commentId: input.commentId },
730
+ finalDocument.title,
731
+ watcherRecipientIds,
732
+ 'documents.notifications.watch.commented.body',
733
+ ),
734
+ ]
735
+ const projectionsAfterUndo: DocumentsProjectionDescriptor[] = [
736
+ ...shareMutations.map((mutation) => shareProjection(
737
+ mutation.before.existed && mutation.before.deletedAt === null
738
+ ? 'documents.document.shared'
739
+ : 'documents.document.unshared',
740
+ input,
741
+ mutation.before,
742
+ false,
743
+ )),
744
+ {
745
+ kind: 'mention-notification-delete',
746
+ commentId: input.commentId,
747
+ tenantId: input.tenantId,
748
+ organizationId: input.organizationId,
749
+ },
750
+ ]
751
+ return {
752
+ id: finalComment.id,
753
+ updatedAt: finalComment.updatedAt.toISOString(),
754
+ projections,
755
+ undo: { before: beforeState, after: afterState, shareMutations, projectionsAfterUndo },
756
+ }
757
+ },
758
+ async buildLog({ input, result }) {
759
+ const before = commentStateSchema.parse(result.undo.before)
760
+ const after = commentStateSchema.parse(result.undo.after)
761
+ const { translate } = await resolveTranslations()
762
+ return {
763
+ actionLabel: translate('documents.audit.commentCreated', 'Add comment'),
764
+ resourceKind: DOCUMENTS_ENTITY_IDS.documentComment,
765
+ resourceId: result.id,
766
+ parentResourceKind: DOCUMENTS_ENTITY_IDS.document,
767
+ parentResourceId: input.documentId,
768
+ tenantId: input.tenantId,
769
+ organizationId: input.organizationId,
770
+ snapshotBefore: before,
771
+ snapshotAfter: after,
772
+ payload: {
773
+ undo: result.undo,
774
+ __redoInput: {
775
+ ...input,
776
+ redoExpectation: before.existed
777
+ ? { kind: 'snapshot', state: before }
778
+ : { kind: 'soft-deleted-created', state: after },
779
+ } satisfies CommentCreateCommandInput,
780
+ },
781
+ }
782
+ },
783
+ async undo({ logEntry, ctx }) {
784
+ const undo = extractUndoPayload<CommentCreateUndoPayload>(logEntry)
785
+ if (!undo?.before || !undo.after) return
786
+ const input = readRedoInput(logEntry, commentCreateCommandSchema)
787
+ const em = ctx.transactionalEm ?? (ctx.container.resolve('em') as EntityManager).fork()
788
+ await withAtomicFlush(em, [async () => {
789
+ await loadLockedDocument(em, input)
790
+ if (undo.shareMutations.length > 0) {
791
+ await assertCanMutateMentionShares(ctx, em, input)
792
+ }
793
+ const comment = await loadComment(em, input, undo.after!.id, true)
794
+ if (!comment) throw new CrudHttpError(409, { error: 'Record changed by another user' })
795
+ await assertCanResolveComment(ctx, em, {
796
+ ...input,
797
+ comment: { id: comment.id, resolved: false },
798
+ expectedUpdatedAt: null,
799
+ }, comment)
800
+ assertCommentUnchanged(comment, undo.after!)
801
+ await assertNoActiveReplies(em, input, comment.id)
802
+ for (const mutation of undo.shareMutations) {
803
+ const share = await loadMentionShare(em, input, mutation.after.userId, true)
804
+ assertMentionShareUnchanged(share, mutation.after)
805
+ restoreMentionShareState(share, mutation.before)
806
+ }
807
+ restoreCommentState(comment, undo.before!)
808
+ }], { transaction: true, label: 'documents.comment.create.undo' })
809
+ },
810
+ }
811
+
812
+ export const resolveCommentCommand: CommandHandler<CommentResolveCommandInput, CommentResolveCommandResult> = {
813
+ id: 'documents.comment.resolve',
814
+ async prepare(rawInput, ctx) {
815
+ const input = commentResolveCommandSchema.parse(rawInput)
816
+ assertActor(input, ctx)
817
+ const em = ctx.container.resolve('em') as EntityManager
818
+ const comment = await loadComment(em, input, input.comment.id)
819
+ if (!comment || comment.deletedAt) throw new CrudHttpError(404, { error: 'Comment not found' })
820
+ await assertCanResolveComment(ctx, em, input, comment)
821
+ return null
822
+ },
823
+ async execute(rawInput, ctx) {
824
+ const input = commentResolveCommandSchema.parse(rawInput)
825
+ const em = ctx.transactionalEm ?? (ctx.container.resolve('em') as EntityManager).fork()
826
+ let comment: DocumentComment | null = null
827
+ let before: CommentState | null = null
828
+ let after: CommentState | null = null
829
+ await withAtomicFlush(em, [async () => {
830
+ await loadLockedDocument(em, input)
831
+ assertActor(input, ctx)
832
+ comment = await loadComment(em, input, input.comment.id, true)
833
+ if (!comment || comment.deletedAt) throw new CrudHttpError(404, { error: 'Comment not found' })
834
+ await assertCanResolveComment(ctx, em, input, comment)
835
+ if (input.redoExpectation) {
836
+ assertCommentMatchesState(comment, input.redoExpectation.state)
837
+ } else {
838
+ assertOptimisticLock({
839
+ resourceKind: DOCUMENTS_ENTITY_IDS.documentComment,
840
+ resourceId: comment.id,
841
+ current: comment.updatedAt,
842
+ expected: input.expectedUpdatedAt,
843
+ })
844
+ }
845
+ before = captureCommentState(comment, {
846
+ id: comment.id,
847
+ parentCommentId: comment.parentCommentId ?? null,
848
+ authorUserId: comment.authorUserId,
849
+ body: comment.body,
850
+ anchor: comment.anchor as CommentState['anchor'],
851
+ mentions: comment.mentions ?? null,
852
+ resolvedAt: comment.resolvedAt?.toISOString() ?? null,
853
+ resolvedByUserId: comment.resolvedByUserId ?? null,
854
+ })
855
+ const now = new Date()
856
+ ;(comment as DocumentComment).resolvedAt = input.comment.resolved ? now : null
857
+ ;(comment as DocumentComment).resolvedByUserId = input.comment.resolved ? input.actorUserId : null
858
+ ;(comment as DocumentComment).updatedAt = nextDocumentVersion(
859
+ (comment as DocumentComment).updatedAt,
860
+ now,
861
+ )
862
+ }, async () => {
863
+ const finalComment = comment as DocumentComment | null
864
+ if (!finalComment) throw new Error('[internal] comment resolve produced no row')
865
+ after = captureCommentState(finalComment, {
866
+ id: finalComment.id,
867
+ parentCommentId: finalComment.parentCommentId ?? null,
868
+ authorUserId: finalComment.authorUserId,
869
+ body: finalComment.body,
870
+ anchor: finalComment.anchor as CommentState['anchor'],
871
+ mentions: finalComment.mentions ?? null,
872
+ resolvedAt: finalComment.resolvedAt?.toISOString() ?? null,
873
+ resolvedByUserId: finalComment.resolvedByUserId ?? null,
874
+ })
875
+ }], { transaction: true, label: 'documents.comment.resolve' })
876
+ const finalComment = comment as DocumentComment | null
877
+ const beforeState = before as CommentState | null
878
+ const afterState = after as CommentState | null
879
+ if (!finalComment || !beforeState || !afterState) throw new Error('[internal] comment resolve produced no row')
880
+ const resolvedDocument = await findOneWithDecryption(
881
+ em,
882
+ Document,
883
+ {
884
+ id: input.documentId,
885
+ tenantId: input.tenantId,
886
+ organizationId: input.organizationId,
887
+ deletedAt: null,
888
+ },
889
+ { fields: ['id', 'title'] },
890
+ { tenantId: input.tenantId, organizationId: input.organizationId },
891
+ )
892
+ const watcherRecipientIds = resolvedDocument
893
+ ? await resolveWatcherRecipients({
894
+ em,
895
+ container: ctx.container,
896
+ scope: { tenantId: input.tenantId, organizationId: input.organizationId },
897
+ documentId: input.documentId,
898
+ actorUserId: input.actorUserId,
899
+ })
900
+ : []
901
+ return {
902
+ id: finalComment.id,
903
+ resolvedAt: afterState.resolvedAt,
904
+ resolvedByUserId: afterState.resolvedByUserId,
905
+ updatedAt: finalComment.updatedAt.toISOString(),
906
+ before: beforeState,
907
+ after: afterState,
908
+ projections: [
909
+ resolutionProjection(input, afterState),
910
+ ...(resolvedDocument
911
+ ? watchCommentProjections(
912
+ { ...input, commentId: finalComment.id },
913
+ resolvedDocument.title,
914
+ watcherRecipientIds,
915
+ 'documents.notifications.watch.commented.resolvedBody',
916
+ )
917
+ : []),
918
+ ],
919
+ }
920
+ },
921
+ async buildLog({ input, result }) {
922
+ const before = commentStateSchema.parse(result.before)
923
+ const after = commentStateSchema.parse(result.after)
924
+ const { translate } = await resolveTranslations()
925
+ return {
926
+ actionLabel: translate('documents.audit.commentResolved', 'Resolve comment'),
927
+ resourceKind: DOCUMENTS_ENTITY_IDS.documentComment,
928
+ resourceId: result.id,
929
+ parentResourceKind: DOCUMENTS_ENTITY_IDS.document,
930
+ parentResourceId: input.documentId,
931
+ tenantId: input.tenantId,
932
+ organizationId: input.organizationId,
933
+ snapshotBefore: before,
934
+ snapshotAfter: after,
935
+ payload: {
936
+ undo: {
937
+ before,
938
+ after,
939
+ projectionsAfterUndo: [resolutionProjection(input, before, false)],
940
+ } satisfies CommentResolveUndoPayload,
941
+ __redoInput: {
942
+ ...input,
943
+ expectedUpdatedAt: null,
944
+ redoExpectation: { kind: 'snapshot', state: before },
945
+ } satisfies CommentResolveCommandInput,
946
+ },
947
+ }
948
+ },
949
+ async undo({ logEntry, ctx }) {
950
+ const undo = extractUndoPayload<CommentResolveUndoPayload>(logEntry)
951
+ if (!undo?.before || !undo.after) return
952
+ const input = readRedoInput(logEntry, commentResolveCommandSchema)
953
+ const em = ctx.transactionalEm ?? (ctx.container.resolve('em') as EntityManager).fork()
954
+ await withAtomicFlush(em, [async () => {
955
+ await loadLockedDocument(em, input)
956
+ const comment = await loadComment(em, input, undo.after!.id, true)
957
+ if (!comment) throw new CrudHttpError(409, { error: 'Record changed by another user' })
958
+ await assertCanResolveComment(ctx, em, input, comment)
959
+ assertCommentUnchanged(comment, undo.after!)
960
+ restoreCommentState(comment, undo.before!)
961
+ }], { transaction: true, label: 'documents.comment.resolve.undo' })
962
+ },
963
+ }
964
+
965
+ registerCommand(createCommentCommand)
966
+ registerCommand(resolveCommentCommand)