openloops 0.4.5 → 0.6.0

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 (250) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +2 -1
  3. package/dist/common/base/base.entity.d.ts +3 -9
  4. package/dist/common/base/base.entity.js +17 -96
  5. package/dist/common/base/base.entity.mjs +19 -92
  6. package/dist/common/base/base.use-case.d.ts +2 -0
  7. package/dist/common/base/index.d.ts +1 -0
  8. package/dist/common/base/index.js +3 -1
  9. package/dist/common/base/index.mjs +1 -0
  10. package/dist/common/base/share.d.ts +12 -0
  11. package/dist/common/base/share.js +45 -0
  12. package/dist/common/base/share.mjs +21 -0
  13. package/dist/common/decorators/index.d.ts +0 -2
  14. package/dist/common/decorators/index.js +1 -5
  15. package/dist/common/decorators/index.mjs +0 -2
  16. package/dist/common/repositories/index.d.ts +1 -0
  17. package/dist/common/repositories/index.js +3 -1
  18. package/dist/common/repositories/index.mjs +1 -0
  19. package/dist/common/repositories/mongo-db.repository.d.ts +31 -11
  20. package/dist/common/repositories/mongo-db.repository.js +121 -40
  21. package/dist/common/repositories/mongo-db.repository.mjs +121 -40
  22. package/dist/common/repositories/scope.d.ts +19 -0
  23. package/dist/common/repositories/scope.js +63 -0
  24. package/dist/common/repositories/scope.mjs +40 -0
  25. package/dist/db/indexes/automations.js +1 -1
  26. package/dist/db/indexes/automations.mjs +1 -1
  27. package/dist/db/migrations/automations.d.ts +1 -0
  28. package/dist/db/migrations/automations.js +32 -5
  29. package/dist/db/migrations/automations.mjs +32 -5
  30. package/dist/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
  31. package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
  32. package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
  33. package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
  34. package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
  35. package/dist/services/authentication/use-cases/login.use-case.js +2 -1
  36. package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
  37. package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
  38. package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
  39. package/dist/services/automations/entities/automation.entity.d.ts +9 -9
  40. package/dist/services/automations/entities/automation.entity.js +21 -20
  41. package/dist/services/automations/entities/automation.entity.mjs +21 -20
  42. package/dist/services/automations/entities/trigger.d.ts +16 -8
  43. package/dist/services/automations/entities/trigger.js +28 -17
  44. package/dist/services/automations/entities/trigger.mjs +27 -16
  45. package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
  46. package/dist/services/automations/repositories/automation-run.repository.js +21 -18
  47. package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
  48. package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
  49. package/dist/services/automations/repositories/automation.repository.js +34 -22
  50. package/dist/services/automations/repositories/automation.repository.mjs +34 -22
  51. package/dist/services/automations/runner/automation-runner.d.ts +3 -0
  52. package/dist/services/automations/runner/automation-runner.js +2 -2
  53. package/dist/services/automations/runner/automation-runner.mjs +2 -2
  54. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
  55. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
  56. package/dist/services/automations/use-cases/claim-due-automations.use-case.js +11 -10
  57. package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +11 -10
  58. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +10 -9
  59. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +10 -9
  60. package/dist/services/automations/use-cases/create-automation.use-case.d.ts +2 -2
  61. package/dist/services/automations/use-cases/create-automation.use-case.js +8 -8
  62. package/dist/services/automations/use-cases/create-automation.use-case.mjs +6 -6
  63. package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
  64. package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
  65. package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
  66. package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
  67. package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
  68. package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
  69. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +7 -7
  70. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +5 -5
  71. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
  72. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
  73. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
  74. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
  75. package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
  76. package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
  77. package/dist/services/automations/use-cases/receive-automation-event.use-case.js +10 -8
  78. package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +10 -8
  79. package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
  80. package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
  81. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
  82. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
  83. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +10 -10
  84. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +8 -8
  85. package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
  86. package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
  87. package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
  88. package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
  89. package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
  90. package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
  91. package/dist/services/automations/use-cases/update-automation.use-case.d.ts +2 -2
  92. package/dist/services/automations/use-cases/update-automation.use-case.js +16 -12
  93. package/dist/services/automations/use-cases/update-automation.use-case.mjs +14 -10
  94. package/dist/services/automations/utils/automation-fields.d.ts +3 -4
  95. package/dist/services/automations/utils/automation-fields.js +3 -5
  96. package/dist/services/automations/utils/automation-fields.mjs +4 -6
  97. package/dist/services/automations/utils/open-run.d.ts +2 -0
  98. package/dist/services/automations/utils/open-run.js +2 -2
  99. package/dist/services/automations/utils/open-run.mjs +2 -2
  100. package/dist/services/automations/utils/previous-run.js +3 -2
  101. package/dist/services/automations/utils/previous-run.mjs +3 -2
  102. package/dist/services/automations/utils/runner-scope.d.ts +3 -0
  103. package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
  104. package/dist/services/automations/utils/runner-scope.mjs +8 -0
  105. package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
  106. package/dist/services/chats/repositories/chat.repository.js +16 -11
  107. package/dist/services/chats/repositories/chat.repository.mjs +16 -11
  108. package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
  109. package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
  110. package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
  111. package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
  112. package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
  113. package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
  114. package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
  115. package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
  116. package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
  117. package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
  118. package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
  119. package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
  120. package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
  121. package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
  122. package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
  123. package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
  124. package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
  125. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
  126. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
  127. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
  128. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
  129. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
  130. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
  131. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
  132. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
  133. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
  134. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
  135. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
  136. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
  137. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
  138. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
  139. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
  140. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
  141. package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
  142. package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
  143. package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
  144. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
  145. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
  146. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
  147. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
  148. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
  149. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
  150. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
  151. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
  152. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
  153. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
  154. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
  155. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
  156. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
  157. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
  158. package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
  159. package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
  160. package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
  161. package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
  162. package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
  163. package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
  164. package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
  165. package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
  166. package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
  167. package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
  168. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
  169. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
  170. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
  171. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
  172. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
  173. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
  174. package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
  175. package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
  176. package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
  177. package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
  178. package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
  179. package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
  180. package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
  181. package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
  182. package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
  183. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
  184. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
  185. package/dist/services/users/entities/user-acquisition.js +3 -16
  186. package/dist/services/users/entities/user-acquisition.mjs +4 -11
  187. package/dist/services/users/entities/user-marketing.js +3 -16
  188. package/dist/services/users/entities/user-marketing.mjs +4 -11
  189. package/dist/services/users/entities/user.entity.js +0 -3
  190. package/dist/services/users/entities/user.entity.mjs +1 -4
  191. package/dist/services/users/repositories/user.repository.d.ts +8 -7
  192. package/dist/services/users/repositories/user.repository.js +26 -12
  193. package/dist/services/users/repositories/user.repository.mjs +26 -12
  194. package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
  195. package/dist/services/users/use-cases/create-user.use-case.js +5 -5
  196. package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
  197. package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
  198. package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
  199. package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
  200. package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
  201. package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
  202. package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
  203. package/dist/services/users/use-cases/update-user.use-case.js +5 -7
  204. package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
  205. package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
  206. package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
  207. package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
  208. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
  209. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
  210. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
  211. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
  212. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
  213. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
  214. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
  215. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
  216. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
  217. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
  218. package/docs/README.md +3 -2
  219. package/docs/agent.md +2 -2
  220. package/docs/authorization.md +77 -0
  221. package/docs/automations.md +143 -41
  222. package/docs/integration-guide.md +8 -10
  223. package/docs/llms.txt +1 -0
  224. package/docs/mcp-servers.md +4 -4
  225. package/docs/users.md +6 -4
  226. package/examples/automations/create-automation.ts +6 -5
  227. package/examples/automations/create-event-automation.ts +1 -1
  228. package/examples/automations/list-automation-runs.ts +2 -0
  229. package/examples/automations/list-automations.ts +3 -2
  230. package/examples/automations/preview-automation-runs.ts +1 -1
  231. package/examples/automations/revoke-automation-event-token.ts +1 -1
  232. package/examples/automations/run-automation-now.ts +3 -1
  233. package/examples/automations/update-automation.ts +4 -5
  234. package/examples/chats/delete-chat.ts +1 -1
  235. package/examples/mcps/internal-mcp.ts +3 -3
  236. package/examples/run-agent/run-agent-with-identity.ts +1 -1
  237. package/examples/users/create-user.ts +2 -1
  238. package/examples/users/list-users.ts +1 -0
  239. package/package.json +1 -1
  240. package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
  241. package/dist/common/decorators/admin-only/admin-only.js +0 -52
  242. package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
  243. package/dist/common/decorators/admin-only/index.d.ts +0 -1
  244. package/dist/common/decorators/admin-only/index.mjs +0 -1
  245. package/dist/common/decorators/owner-only/index.d.ts +0 -1
  246. package/dist/common/decorators/owner-only/index.js +0 -22
  247. package/dist/common/decorators/owner-only/index.mjs +0 -1
  248. package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
  249. package/dist/common/decorators/owner-only/owner-only.js +0 -52
  250. package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
@@ -3,6 +3,7 @@ import { ObjectId } from "mongodb";
3
3
  import { reportError } from "../error/index.js";
4
4
  import { getMongoDBClient } from "./mongo-db.client";
5
5
  const DEFAULT_LIMIT = 10;
6
+ const PROTECTED_FIELDS = ["__v", "ownerId", "sharedWith", "createdAt", "createdBy", "updatedAt", "updatedBy", "deletedAt", "deletedBy", "archivedAt", "archivedBy"];
6
7
  class VersionConflictError extends Error {
7
8
  code = "VERSION_CONFLICT";
8
9
  statusCode = 409;
@@ -16,15 +17,65 @@ class MongoRepository {
16
17
  collection;
17
18
  dbClient;
18
19
  _where = { deletedAt: null, archivedAt: null };
20
+ scoped;
21
+ ownerField;
22
+ protectedFields;
19
23
  report = reportError;
20
24
  constructor(props) {
21
- const { database, collection, envvar } = props || {};
25
+ const { database, collection, envvar, scoped = false, ownerField = "ownerId", protectedFields = [] } = props || {};
22
26
  if (!database) throw new Error("[Mongo Repository] Database name is required");
23
27
  if (!collection) throw new Error("[Mongo Repository] Collection name is required");
24
28
  this.dbClient = getMongoDBClient(envvar);
25
29
  if (!this.dbClient) throw new Error("[Mongo Repository] Database client not found");
26
30
  this.database = database;
27
31
  this.collection = collection;
32
+ this.scoped = scoped;
33
+ this.ownerField = ownerField;
34
+ this.protectedFields = [...PROTECTED_FIELDS, ...protectedFields];
35
+ }
36
+ assertScope(scope) {
37
+ if (!scope && this.scoped) throw new Error(`[Mongo Repository] Scope is required in ${this.collection}`);
38
+ }
39
+ actorId(scope) {
40
+ if (!scope || scope.kind === "system") return void 0;
41
+ const userId = this.parseId(scope.userId);
42
+ if (!userId) throw new Error("[Mongo Repository] Invalid user id in scope");
43
+ return userId;
44
+ }
45
+ withScope(filter, scope) {
46
+ this.assertScope(scope);
47
+ if (!scope || scope.trusted) return filter;
48
+ return { $and: [filter, { [this.ownerField]: this.actorId(scope) }] };
49
+ }
50
+ stampCreate(payload, scope) {
51
+ this.assertScope(scope);
52
+ if (!scope) return { ...payload };
53
+ const actor = this.actorId(scope);
54
+ return {
55
+ ...this.withoutProtected(payload, scope),
56
+ ...scope.trusted || this.ownerField === "_id" ? {} : { [this.ownerField]: actor },
57
+ createdAt: /* @__PURE__ */ new Date(),
58
+ ...actor ? { createdBy: actor } : {}
59
+ };
60
+ }
61
+ withoutProtected(payload, scope) {
62
+ if (!scope || scope.trusted) return payload;
63
+ const kept = Object.fromEntries(Object.entries(payload).filter(([key]) => !this.protectedFields.includes(key)));
64
+ return this.protectedFields.filter((path) => path.includes(".")).reduce((result, path) => this.withoutPath(result, path.split(".")), kept);
65
+ }
66
+ withoutPath(node, path) {
67
+ const [head, ...rest] = path;
68
+ if (!(head in node)) return node;
69
+ if (!rest.length) {
70
+ const { [head]: removed, ...others2 } = node;
71
+ return others2;
72
+ }
73
+ const child = node[head];
74
+ if (!child || typeof child !== "object" || Array.isArray(child) || child instanceof Date || child instanceof ObjectId) return node;
75
+ const cleaned = this.withoutPath(child, rest);
76
+ if (Object.keys(cleaned).length) return { ...node, [head]: cleaned };
77
+ const { [head]: dropped, ...others } = node;
78
+ return others;
28
79
  }
29
80
  parseId(id) {
30
81
  return typeof id === "string" && ObjectId.isValid(id) ? new ObjectId(id) : void 0;
@@ -36,12 +87,13 @@ class MongoRepository {
36
87
  const n = Number(limit);
37
88
  return !Number.isFinite(n) || n <= 0 ? DEFAULT_LIMIT : n;
38
89
  }
39
- async findOne({ where, select, options }) {
90
+ async findOne({ where, select, options, scope }) {
91
+ const filter = this.withScope({ ...where, ...this._where }, scope);
40
92
  try {
41
93
  const db = await this.dbClient.db(this.database);
42
94
  const collection = db.collection(this.collection);
43
95
  const result = await collection.findOne(
44
- { ...where, ...this._where },
96
+ filter,
45
97
  {
46
98
  ...select ? { projection: select } : {},
47
99
  ...options
@@ -62,24 +114,26 @@ class MongoRepository {
62
114
  const results = await this.query(params, _limit, _limit + 1);
63
115
  return { items: results.slice(0, _limit), hasMore: results.length > _limit };
64
116
  }
65
- async query({ where, select, options }, pageSize, fetchSize) {
117
+ async query({ where, select, options, scope }, pageSize, fetchSize) {
118
+ const filter = this.withScope({ ...where, ...this._where }, scope);
66
119
  try {
67
120
  const db = await this.dbClient.db(this.database);
68
121
  const collection = db.collection(this.collection);
69
122
  const { page = 1 } = options || {};
70
123
  const _skip = +page > 0 ? (+page - 1) * pageSize : 0;
71
- const result = await collection.find({ ...where, ...this._where }, { projection: select }).sort(options?.sortBy || { _id: -1 }).skip(_skip).limit(fetchSize).toArray();
124
+ const result = await collection.find(filter, { projection: select }).sort(options?.sortBy || { _id: -1 }).skip(_skip).limit(fetchSize).toArray();
72
125
  return result;
73
126
  } catch (error) {
74
127
  await this.report(error);
75
128
  throw error;
76
129
  }
77
130
  }
78
- async findAll({ where, select, options }) {
131
+ async findAll({ where, select, options, scope }) {
132
+ const filter = this.withScope({ ...where, ...this._where }, scope);
79
133
  try {
80
134
  const db = await this.dbClient.db(this.database);
81
135
  const collection = db.collection(this.collection);
82
- const cursor = collection.find({ ...where, ...this._where }, { projection: select });
136
+ const cursor = collection.find(filter, { projection: select });
83
137
  const result = await (options?.sortBy ? cursor.sort(options.sortBy) : cursor).toArray();
84
138
  return result;
85
139
  } catch (error) {
@@ -87,53 +141,68 @@ class MongoRepository {
87
141
  throw error;
88
142
  }
89
143
  }
90
- async createOne({ payload }) {
144
+ async createOne({ payload, scope }) {
91
145
  if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for createOne operation");
146
+ const document = this.stampCreate(payload, scope);
92
147
  try {
93
148
  const db = await this.dbClient.db(this.database);
94
149
  const collection = db.collection(this.collection);
95
- const { insertedId } = await collection.insertOne({ ...payload, __v: 1 });
96
- return insertedId;
150
+ const { insertedId } = await collection.insertOne({ ...document, __v: 1 });
151
+ return { ...document, _id: insertedId, __v: 1 };
97
152
  } catch (error) {
98
153
  await this.report(error);
99
154
  throw error;
100
155
  }
101
156
  }
102
- async createMany({ payload }) {
157
+ async createMany({ payload, scope }) {
158
+ const documents = payload.map((item) => this.stampCreate(item, scope));
103
159
  try {
104
160
  const db = await this.dbClient.db(this.database);
105
161
  const collection = db.collection(this.collection);
106
- const { insertedIds } = await collection.insertMany(payload);
162
+ const { insertedIds } = await collection.insertMany(documents);
107
163
  return insertedIds;
108
164
  } catch (error) {
109
165
  await this.report(error);
110
166
  throw error;
111
167
  }
112
168
  }
113
- async updateOne({ where, payload, options, expectedVersion }) {
169
+ async updateOne({ where, payload, expectedVersion, scope }) {
114
170
  if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for updateOne operation");
115
171
  if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for updateOne operation");
172
+ const actor = this.actorId(scope);
173
+ const { _id, __v, createdAt, createdBy, toUnset, ...rest } = payload;
174
+ const toSet = {
175
+ ...this.withoutProtected(rest, scope),
176
+ ...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
177
+ };
178
+ return this.modifyOne(where, {
179
+ ...toSet ? { $set: toSet } : {},
180
+ ...toUnset ? { $unset: toUnset } : {}
181
+ }, scope, expectedVersion);
182
+ }
183
+ async softDelete({ where, payload, scope }) {
184
+ if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for softDelete operation");
185
+ const actor = this.actorId(scope);
186
+ const { toUnset, ...rest } = payload ?? {};
187
+ const extra = this.withoutProtected(rest, scope);
188
+ return this.modifyOne(where, {
189
+ $set: { ...extra, deletedAt: /* @__PURE__ */ new Date(), ...actor ? { deletedBy: actor } : {} },
190
+ ...toUnset ? { $unset: toUnset } : {}
191
+ }, scope);
192
+ }
193
+ async modifyOne(where, update, scope, expectedVersion) {
116
194
  const checkVersion = typeof expectedVersion === "number";
195
+ const filter = this.withScope({ ...where, ...checkVersion ? { __v: expectedVersion } : {}, ...this._where }, scope);
117
196
  try {
118
197
  const db = await this.dbClient.db(this.database);
119
198
  const collection = db.collection(this.collection);
120
- const { _id, __v, createdAt, createdBy, toUnset, ...toSet } = payload;
121
- const __payload = {
122
- ...toSet ? { $set: toSet } : {},
123
- ...toUnset ? { $unset: toUnset } : {}
124
- };
125
199
  const result = await collection.findOneAndUpdate(
126
- { ...where, ...checkVersion ? { __v: expectedVersion } : {}, ...this._where },
127
- {
128
- $inc: { __v: 1 },
129
- ...__payload
130
- },
131
- {
132
- returnDocument: "after"
133
- }
200
+ filter,
201
+ { $inc: { __v: 1 }, ...update },
202
+ { returnDocument: "after" }
134
203
  );
135
204
  if (!result && checkVersion) {
136
- const exists = await collection.findOne({ ...where, ...this._where }, { projection: { _id: 1 } });
205
+ const exists = await collection.findOne(this.withScope({ ...where, ...this._where }, scope), { projection: { _id: 1 } });
137
206
  if (exists) throw new VersionConflictError();
138
207
  }
139
208
  return result;
@@ -142,13 +211,19 @@ class MongoRepository {
142
211
  throw error;
143
212
  }
144
213
  }
145
- async updateMany({ where, payload }) {
214
+ async updateMany({ where, payload, scope }) {
215
+ const filter = this.withScope({ ...where, ...this._where }, scope);
216
+ const actor = this.actorId(scope);
217
+ const toSet = {
218
+ ...this.withoutProtected(payload ?? {}, scope),
219
+ ...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
220
+ };
146
221
  try {
147
222
  const db = await this.dbClient.db(this.database);
148
223
  const collection = db.collection(this.collection);
149
224
  const result = await collection.updateMany(
150
- { ...where, ...this._where },
151
- { $set: payload }
225
+ filter,
226
+ { $set: toSet }
152
227
  );
153
228
  const countUpdated = result.modifiedCount;
154
229
  return countUpdated;
@@ -157,55 +232,61 @@ class MongoRepository {
157
232
  throw error;
158
233
  }
159
234
  }
160
- async deleteOne({ where }) {
235
+ async deleteOne({ where, scope }) {
236
+ const filter = this.withScope({ ...where }, scope);
161
237
  try {
162
238
  const db = await this.dbClient.db(this.database);
163
239
  const collection = db.collection(this.collection);
164
- const { deletedCount } = await collection.deleteOne({ ...where });
240
+ const { deletedCount } = await collection.deleteOne(filter);
165
241
  return deletedCount;
166
242
  } catch (error) {
167
243
  await this.report(error);
168
244
  throw error;
169
245
  }
170
246
  }
171
- async deleteMany({ where }) {
247
+ async deleteMany({ where, scope }) {
248
+ const filter = this.withScope({ ...where }, scope);
172
249
  try {
173
250
  const db = await this.dbClient.db(this.database);
174
251
  const collection = db.collection(this.collection);
175
- const { deletedCount } = await collection.deleteMany({ ...where });
252
+ const { deletedCount } = await collection.deleteMany(filter);
176
253
  return deletedCount;
177
254
  } catch (error) {
178
255
  await this.report(error);
179
256
  throw error;
180
257
  }
181
258
  }
182
- async count({ where, options }) {
259
+ async count({ where, options, scope }) {
260
+ const filter = this.withScope({ ...where, ...options?.includeDeleted ? {} : this._where }, scope);
183
261
  try {
184
262
  const db = await this.dbClient.db(this.database);
185
263
  const collection = db.collection(this.collection);
186
- const result = await collection.countDocuments({ ...where, ...options?.includeDeleted ? {} : this._where });
264
+ const result = await collection.countDocuments(filter);
187
265
  return result;
188
266
  } catch (error) {
189
267
  await this.report(error);
190
268
  throw error;
191
269
  }
192
270
  }
193
- async aggregate(pipeline) {
271
+ async aggregate(pipeline, scope) {
272
+ const scopeFilter = this.withScope({}, scope);
273
+ const scopedPipeline = Object.keys(scopeFilter).length ? [{ $match: scopeFilter }, ...pipeline] : pipeline;
194
274
  try {
195
275
  const db = await this.dbClient.db(this.database);
196
276
  const collection = db.collection(this.collection);
197
- const result = await collection.aggregate(pipeline).toArray();
277
+ const result = await collection.aggregate(scopedPipeline).toArray();
198
278
  return result;
199
279
  } catch (error) {
200
280
  await this.report(error);
201
281
  throw error;
202
282
  }
203
283
  }
204
- async distinct(field, where) {
284
+ async distinct(field, where, scope) {
285
+ const filter = this.withScope({ ...where, ...this._where }, scope);
205
286
  try {
206
287
  const db = await this.dbClient.db(this.database);
207
288
  const collection = db.collection(this.collection);
208
- const result = await collection.distinct(field, { ...where, ...this._where });
289
+ const result = await collection.distinct(field, filter);
209
290
  return result;
210
291
  } catch (error) {
211
292
  await this.report(error);
@@ -0,0 +1,19 @@
1
+ export interface ScopeUser {
2
+ userId?: string;
3
+ isAdmin(): boolean;
4
+ }
5
+ type ScopeKind = 'view' | 'edit' | 'manage' | 'admin' | 'system';
6
+ export declare class Scope {
7
+ readonly kind: ScopeKind;
8
+ readonly userId?: string | undefined;
9
+ readonly reason?: string | undefined;
10
+ private constructor();
11
+ get trusted(): boolean;
12
+ static canView(user?: ScopeUser): Scope;
13
+ static canEdit(user?: ScopeUser): Scope;
14
+ static canManage(user?: ScopeUser): Scope;
15
+ static asAdmin(user?: ScopeUser): Scope;
16
+ static system(reason: string): Scope;
17
+ private static requireUserId;
18
+ }
19
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var scope_exports = {};
20
+ __export(scope_exports, {
21
+ Scope: () => Scope
22
+ });
23
+ module.exports = __toCommonJS(scope_exports);
24
+ var import_error = require("../error/index.js");
25
+ class Scope {
26
+ constructor(kind, userId, reason) {
27
+ this.kind = kind;
28
+ this.userId = userId;
29
+ this.reason = reason;
30
+ }
31
+ kind;
32
+ userId;
33
+ reason;
34
+ get trusted() {
35
+ return this.kind === "admin" || this.kind === "system";
36
+ }
37
+ static canView(user) {
38
+ return new Scope("view", Scope.requireUserId(user));
39
+ }
40
+ static canEdit(user) {
41
+ return new Scope("edit", Scope.requireUserId(user));
42
+ }
43
+ static canManage(user) {
44
+ return new Scope("manage", Scope.requireUserId(user));
45
+ }
46
+ static asAdmin(user) {
47
+ const userId = Scope.requireUserId(user);
48
+ if (!user?.isAdmin()) throw new import_error.HttpError({ statusCode: 403, errorMessage: "{{forbidden_error}}: {{forbidden_error_details}}" });
49
+ return new Scope("admin", userId);
50
+ }
51
+ static system(reason) {
52
+ if (!reason?.trim()) throw new Error("[Scope] A reason is required for a system scope");
53
+ return new Scope("system", void 0, reason);
54
+ }
55
+ static requireUserId(user) {
56
+ if (!user?.userId) throw new import_error.HttpError({ statusCode: 401, errorMessage: "{{not_authorized}}" });
57
+ return user.userId;
58
+ }
59
+ }
60
+ // Annotate the CommonJS export names for ESM import in node:
61
+ 0 && (module.exports = {
62
+ Scope
63
+ });
@@ -0,0 +1,40 @@
1
+ import "../../chunk-CF3WOMB3.mjs";
2
+ import { HttpError } from "../error/index.js";
3
+ class Scope {
4
+ constructor(kind, userId, reason) {
5
+ this.kind = kind;
6
+ this.userId = userId;
7
+ this.reason = reason;
8
+ }
9
+ kind;
10
+ userId;
11
+ reason;
12
+ get trusted() {
13
+ return this.kind === "admin" || this.kind === "system";
14
+ }
15
+ static canView(user) {
16
+ return new Scope("view", Scope.requireUserId(user));
17
+ }
18
+ static canEdit(user) {
19
+ return new Scope("edit", Scope.requireUserId(user));
20
+ }
21
+ static canManage(user) {
22
+ return new Scope("manage", Scope.requireUserId(user));
23
+ }
24
+ static asAdmin(user) {
25
+ const userId = Scope.requireUserId(user);
26
+ if (!user?.isAdmin()) throw new HttpError({ statusCode: 403, errorMessage: "{{forbidden_error}}: {{forbidden_error_details}}" });
27
+ return new Scope("admin", userId);
28
+ }
29
+ static system(reason) {
30
+ if (!reason?.trim()) throw new Error("[Scope] A reason is required for a system scope");
31
+ return new Scope("system", void 0, reason);
32
+ }
33
+ static requireUserId(user) {
34
+ if (!user?.userId) throw new HttpError({ statusCode: 401, errorMessage: "{{not_authorized}}" });
35
+ return user.userId;
36
+ }
37
+ }
38
+ export {
39
+ Scope
40
+ };
@@ -22,7 +22,7 @@ __export(automations_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(automations_exports);
24
24
  const automations = [
25
- { key: { enabled: 1, nextRunAt: 1 } },
25
+ { key: { "triggers.schedule.enabled": 1, "triggers.schedule.nextRunAt": 1 } },
26
26
  { key: { ownerId: 1, createdAt: -1 } }
27
27
  ];
28
28
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,6 +1,6 @@
1
1
  import "../../chunk-CF3WOMB3.mjs";
2
2
  const automations = [
3
- { key: { enabled: 1, nextRunAt: 1 } },
3
+ { key: { "triggers.schedule.enabled": 1, "triggers.schedule.nextRunAt": 1 } },
4
4
  { key: { ownerId: 1, createdAt: -1 } }
5
5
  ];
6
6
  export {
@@ -6,6 +6,7 @@ export interface AutomationsMigrationReport {
6
6
  dryRun: boolean;
7
7
  renamedCollections: string[];
8
8
  triggersCreated: number;
9
+ triggersMoved: number;
9
10
  runsUpdated: number;
10
11
  droppedIndexes: string[];
11
12
  }
@@ -27,10 +27,11 @@ const RENAMES = [
27
27
  ["agentic_job_runs", "automation_runs"]
28
28
  ];
29
29
  const OLD_RUN_INDEX = "jobId_1_createdAt_-1";
30
+ const OLD_AUTOMATION_INDEX = "enabled_1_nextRunAt_1";
30
31
  const exists = async (db, name) => (await db.listCollections({ name }).toArray()).length > 0;
31
32
  async function migrateAutomations(db, options = {}) {
32
33
  const dryRun = Boolean(options.dryRun);
33
- const report = { dryRun, renamedCollections: [], triggersCreated: 0, runsUpdated: 0, droppedIndexes: [] };
34
+ const report = { dryRun, renamedCollections: [], triggersCreated: 0, triggersMoved: 0, runsUpdated: 0, droppedIndexes: [] };
34
35
  for (const [from, to] of RENAMES) {
35
36
  if (!await exists(db, from)) continue;
36
37
  if (await exists(db, to)) throw new Error(`Cannot rename ${from} to ${to} because ${to} already exists`);
@@ -40,26 +41,52 @@ async function migrateAutomations(db, options = {}) {
40
41
  const automations = db.collection(dryRun && report.renamedCollections.length ? "agentic_jobs" : "automations");
41
42
  const runs = db.collection(dryRun && report.renamedCollections.length ? "agentic_job_runs" : "automation_runs");
42
43
  const withoutTrigger = { trigger: { $exists: false }, recurrence: { $exists: true } };
44
+ const withTrigger = { trigger: { $exists: true } };
43
45
  const withJobId = { jobId: { $exists: true } };
44
46
  if (dryRun) {
45
47
  report.triggersCreated = await automations.countDocuments(withoutTrigger);
48
+ report.triggersMoved = await automations.countDocuments(withTrigger) + report.triggersCreated;
46
49
  report.runsUpdated = await runs.countDocuments(withJobId);
47
50
  } else {
48
51
  const triggers = await automations.updateMany(withoutTrigger, [
49
52
  { $set: { trigger: { type: "schedule", recurrence: "$recurrence", timezone: "$timezone" } } },
50
53
  { $unset: ["recurrence", "timezone"] }
51
54
  ]);
55
+ const split = await automations.updateMany(withTrigger, [
56
+ {
57
+ $set: {
58
+ triggers: {
59
+ $cond: [
60
+ { $eq: ["$trigger.type", "schedule"] },
61
+ {
62
+ schedule: {
63
+ $mergeObjects: [
64
+ { enabled: { $ifNull: ["$enabled", true] }, recurrence: "$trigger.recurrence", timezone: "$trigger.timezone" },
65
+ { $cond: [{ $ifNull: ["$nextRunAt", false] }, { nextRunAt: "$nextRunAt" }, {}] }
66
+ ]
67
+ }
68
+ },
69
+ { event: { enabled: { $ifNull: ["$enabled", true] } } }
70
+ ]
71
+ },
72
+ eventTokenVersion: { $ifNull: ["$trigger.tokenVersion", 1] }
73
+ }
74
+ },
75
+ { $unset: ["trigger", "enabled", "nextRunAt"] }
76
+ ]);
52
77
  const moved = await runs.updateMany(withJobId, [
53
78
  { $set: { automationId: "$jobId" } },
54
79
  { $unset: "jobId" }
55
80
  ]);
56
81
  report.triggersCreated = triggers.modifiedCount;
82
+ report.triggersMoved = split.modifiedCount;
57
83
  report.runsUpdated = moved.modifiedCount;
58
84
  }
59
- const indexes = await runs.indexes().catch(() => []);
60
- if (indexes.some((index) => index.name === OLD_RUN_INDEX)) {
61
- if (!dryRun) await runs.dropIndex(OLD_RUN_INDEX);
62
- report.droppedIndexes.push(OLD_RUN_INDEX);
85
+ for (const [collection, name] of [[runs, OLD_RUN_INDEX], [automations, OLD_AUTOMATION_INDEX]]) {
86
+ const indexes = await collection.indexes().catch(() => []);
87
+ if (!indexes.some((index) => index.name === name)) continue;
88
+ if (!dryRun) await collection.dropIndex(name);
89
+ report.droppedIndexes.push(name);
63
90
  }
64
91
  if (!dryRun) await (0, import_indexes.ensureIndexes)(db);
65
92
  return report;
@@ -5,10 +5,11 @@ const RENAMES = [
5
5
  ["agentic_job_runs", "automation_runs"]
6
6
  ];
7
7
  const OLD_RUN_INDEX = "jobId_1_createdAt_-1";
8
+ const OLD_AUTOMATION_INDEX = "enabled_1_nextRunAt_1";
8
9
  const exists = async (db, name) => (await db.listCollections({ name }).toArray()).length > 0;
9
10
  async function migrateAutomations(db, options = {}) {
10
11
  const dryRun = Boolean(options.dryRun);
11
- const report = { dryRun, renamedCollections: [], triggersCreated: 0, runsUpdated: 0, droppedIndexes: [] };
12
+ const report = { dryRun, renamedCollections: [], triggersCreated: 0, triggersMoved: 0, runsUpdated: 0, droppedIndexes: [] };
12
13
  for (const [from, to] of RENAMES) {
13
14
  if (!await exists(db, from)) continue;
14
15
  if (await exists(db, to)) throw new Error(`Cannot rename ${from} to ${to} because ${to} already exists`);
@@ -18,26 +19,52 @@ async function migrateAutomations(db, options = {}) {
18
19
  const automations = db.collection(dryRun && report.renamedCollections.length ? "agentic_jobs" : "automations");
19
20
  const runs = db.collection(dryRun && report.renamedCollections.length ? "agentic_job_runs" : "automation_runs");
20
21
  const withoutTrigger = { trigger: { $exists: false }, recurrence: { $exists: true } };
22
+ const withTrigger = { trigger: { $exists: true } };
21
23
  const withJobId = { jobId: { $exists: true } };
22
24
  if (dryRun) {
23
25
  report.triggersCreated = await automations.countDocuments(withoutTrigger);
26
+ report.triggersMoved = await automations.countDocuments(withTrigger) + report.triggersCreated;
24
27
  report.runsUpdated = await runs.countDocuments(withJobId);
25
28
  } else {
26
29
  const triggers = await automations.updateMany(withoutTrigger, [
27
30
  { $set: { trigger: { type: "schedule", recurrence: "$recurrence", timezone: "$timezone" } } },
28
31
  { $unset: ["recurrence", "timezone"] }
29
32
  ]);
33
+ const split = await automations.updateMany(withTrigger, [
34
+ {
35
+ $set: {
36
+ triggers: {
37
+ $cond: [
38
+ { $eq: ["$trigger.type", "schedule"] },
39
+ {
40
+ schedule: {
41
+ $mergeObjects: [
42
+ { enabled: { $ifNull: ["$enabled", true] }, recurrence: "$trigger.recurrence", timezone: "$trigger.timezone" },
43
+ { $cond: [{ $ifNull: ["$nextRunAt", false] }, { nextRunAt: "$nextRunAt" }, {}] }
44
+ ]
45
+ }
46
+ },
47
+ { event: { enabled: { $ifNull: ["$enabled", true] } } }
48
+ ]
49
+ },
50
+ eventTokenVersion: { $ifNull: ["$trigger.tokenVersion", 1] }
51
+ }
52
+ },
53
+ { $unset: ["trigger", "enabled", "nextRunAt"] }
54
+ ]);
30
55
  const moved = await runs.updateMany(withJobId, [
31
56
  { $set: { automationId: "$jobId" } },
32
57
  { $unset: "jobId" }
33
58
  ]);
34
59
  report.triggersCreated = triggers.modifiedCount;
60
+ report.triggersMoved = split.modifiedCount;
35
61
  report.runsUpdated = moved.modifiedCount;
36
62
  }
37
- const indexes = await runs.indexes().catch(() => []);
38
- if (indexes.some((index) => index.name === OLD_RUN_INDEX)) {
39
- if (!dryRun) await runs.dropIndex(OLD_RUN_INDEX);
40
- report.droppedIndexes.push(OLD_RUN_INDEX);
63
+ for (const [collection, name] of [[runs, OLD_RUN_INDEX], [automations, OLD_AUTOMATION_INDEX]]) {
64
+ const indexes = await collection.indexes().catch(() => []);
65
+ if (!indexes.some((index) => index.name === name)) continue;
66
+ if (!dryRun) await collection.dropIndex(name);
67
+ report.droppedIndexes.push(name);
41
68
  }
42
69
  if (!dryRun) await ensureIndexes(db);
43
70
  return report;
@@ -1,7 +1,7 @@
1
- import { MongoRepository } from '../../../common/repositories/index.js';
1
+ import { MongoRepository, Scope } from '../../../common/repositories/index.js';
2
2
  import { RevokedToken, RevokedTokenProps } from '../entities';
3
3
  export declare class RevokedTokenRepository {
4
4
  readonly _: MongoRepository<RevokedTokenProps>;
5
- create(token: RevokedToken): Promise<RevokedToken>;
6
- findByJti(jti: string): Promise<RevokedTokenProps | null>;
5
+ create(token: RevokedToken, scope: Scope): Promise<RevokedToken>;
6
+ findByJti(jti: string, scope: Scope): Promise<RevokedTokenProps | null>;
7
7
  }
@@ -35,15 +35,16 @@ var import_database_name = require("../../../common/utils/database-name.js");
35
35
  let RevokedTokenRepository = class {
36
36
  _ = new import_repositories.MongoRepository({
37
37
  database: (0, import_database_name.getDatabaseName)(),
38
- collection: "revoked_tokens"
38
+ collection: "revoked_tokens",
39
+ scoped: true
39
40
  });
40
- async create(token) {
41
- const id = await this._.createOne({ payload: token.toDocument() });
42
- id && token.setId(id);
41
+ async create(token, scope) {
42
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: token.toDocument(), scope });
43
+ Object.assign(token, { _id, ownerId, createdAt, createdBy });
43
44
  return token;
44
45
  }
45
- async findByJti(jti) {
46
- return await this._.findOne({ where: { jti } });
46
+ async findByJti(jti, scope) {
47
+ return await this._.findOne({ scope, where: { jti } });
47
48
  }
48
49
  };
49
50
  RevokedTokenRepository = __decorateClass([
@@ -7,15 +7,16 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
7
7
  let RevokedTokenRepository = class {
8
8
  _ = new MongoRepository({
9
9
  database: getDatabaseName(),
10
- collection: "revoked_tokens"
10
+ collection: "revoked_tokens",
11
+ scoped: true
11
12
  });
12
- async create(token) {
13
- const id = await this._.createOne({ payload: token.toDocument() });
14
- id && token.setId(id);
13
+ async create(token, scope) {
14
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: token.toDocument(), scope });
15
+ Object.assign(token, { _id, ownerId, createdAt, createdBy });
15
16
  return token;
16
17
  }
17
- async findByJti(jti) {
18
- return await this._.findOne({ where: { jti } });
18
+ async findByJti(jti, scope) {
19
+ return await this._.findOne({ scope, where: { jti } });
19
20
  }
20
21
  };
21
22
  RevokedTokenRepository = __decorateClass([