openloops 0.5.0 → 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 (224) hide show
  1. package/CHANGELOG.md +27 -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/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
  26. package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
  27. package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
  28. package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
  29. package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
  30. package/dist/services/authentication/use-cases/login.use-case.js +2 -1
  31. package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
  32. package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
  33. package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
  34. package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
  35. package/dist/services/automations/repositories/automation-run.repository.js +21 -18
  36. package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
  37. package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
  38. package/dist/services/automations/repositories/automation.repository.js +30 -18
  39. package/dist/services/automations/repositories/automation.repository.mjs +30 -18
  40. package/dist/services/automations/runner/automation-runner.d.ts +3 -0
  41. package/dist/services/automations/runner/automation-runner.js +1 -1
  42. package/dist/services/automations/runner/automation-runner.mjs +1 -1
  43. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
  44. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
  45. package/dist/services/automations/use-cases/claim-due-automations.use-case.js +7 -6
  46. package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +7 -6
  47. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +9 -8
  48. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +9 -8
  49. package/dist/services/automations/use-cases/create-automation.use-case.js +4 -5
  50. package/dist/services/automations/use-cases/create-automation.use-case.mjs +2 -3
  51. package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
  52. package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
  53. package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
  54. package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
  55. package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
  56. package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
  57. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +5 -5
  58. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +3 -3
  59. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
  60. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
  61. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
  62. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
  63. package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
  64. package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
  65. package/dist/services/automations/use-cases/receive-automation-event.use-case.js +7 -6
  66. package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +7 -6
  67. package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
  68. package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
  69. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
  70. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
  71. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +8 -8
  72. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +6 -6
  73. package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
  74. package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
  75. package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
  76. package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
  77. package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
  78. package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
  79. package/dist/services/automations/use-cases/update-automation.use-case.js +8 -7
  80. package/dist/services/automations/use-cases/update-automation.use-case.mjs +6 -5
  81. package/dist/services/automations/utils/open-run.d.ts +2 -0
  82. package/dist/services/automations/utils/open-run.js +2 -2
  83. package/dist/services/automations/utils/open-run.mjs +2 -2
  84. package/dist/services/automations/utils/previous-run.js +3 -2
  85. package/dist/services/automations/utils/previous-run.mjs +3 -2
  86. package/dist/services/automations/utils/runner-scope.d.ts +3 -0
  87. package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
  88. package/dist/services/automations/utils/runner-scope.mjs +8 -0
  89. package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
  90. package/dist/services/chats/repositories/chat.repository.js +16 -11
  91. package/dist/services/chats/repositories/chat.repository.mjs +16 -11
  92. package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
  93. package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
  94. package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
  95. package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
  96. package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
  97. package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
  98. package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
  99. package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
  100. package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
  101. package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
  102. package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
  103. package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
  104. package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
  105. package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
  106. package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
  107. package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
  108. package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
  109. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
  110. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
  111. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
  112. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
  113. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
  114. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
  115. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
  116. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
  117. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
  118. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
  119. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
  120. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
  121. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
  122. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
  123. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
  124. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
  125. package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
  126. package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
  127. package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
  128. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
  129. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
  130. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
  131. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
  132. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
  133. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
  134. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
  135. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
  136. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
  137. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
  138. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
  139. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
  140. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
  141. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
  142. package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
  143. package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
  144. package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
  145. package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
  146. package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
  147. package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
  148. package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
  149. package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
  150. package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
  151. package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
  152. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
  153. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
  154. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
  155. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
  156. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
  157. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
  158. package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
  159. package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
  160. package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
  161. package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
  162. package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
  163. package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
  164. package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
  165. package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
  166. package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
  167. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
  168. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
  169. package/dist/services/users/entities/user-acquisition.js +3 -16
  170. package/dist/services/users/entities/user-acquisition.mjs +4 -11
  171. package/dist/services/users/entities/user-marketing.js +3 -16
  172. package/dist/services/users/entities/user-marketing.mjs +4 -11
  173. package/dist/services/users/entities/user.entity.js +0 -3
  174. package/dist/services/users/entities/user.entity.mjs +1 -4
  175. package/dist/services/users/repositories/user.repository.d.ts +8 -7
  176. package/dist/services/users/repositories/user.repository.js +26 -12
  177. package/dist/services/users/repositories/user.repository.mjs +26 -12
  178. package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
  179. package/dist/services/users/use-cases/create-user.use-case.js +5 -5
  180. package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
  181. package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
  182. package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
  183. package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
  184. package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
  185. package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
  186. package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
  187. package/dist/services/users/use-cases/update-user.use-case.js +5 -7
  188. package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
  189. package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
  190. package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
  191. package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
  192. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
  193. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
  194. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
  195. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
  196. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
  197. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
  198. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
  199. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
  200. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
  201. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
  202. package/docs/README.md +2 -1
  203. package/docs/authorization.md +77 -0
  204. package/docs/automations.md +4 -4
  205. package/docs/integration-guide.md +8 -10
  206. package/docs/llms.txt +1 -0
  207. package/docs/users.md +6 -4
  208. package/examples/automations/list-automation-runs.ts +2 -0
  209. package/examples/automations/run-automation-now.ts +3 -1
  210. package/examples/chats/delete-chat.ts +1 -1
  211. package/examples/users/create-user.ts +2 -1
  212. package/examples/users/list-users.ts +1 -0
  213. package/package.json +1 -1
  214. package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
  215. package/dist/common/decorators/admin-only/admin-only.js +0 -52
  216. package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
  217. package/dist/common/decorators/admin-only/index.d.ts +0 -1
  218. package/dist/common/decorators/admin-only/index.mjs +0 -1
  219. package/dist/common/decorators/owner-only/index.d.ts +0 -1
  220. package/dist/common/decorators/owner-only/index.js +0 -22
  221. package/dist/common/decorators/owner-only/index.mjs +0 -1
  222. package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
  223. package/dist/common/decorators/owner-only/owner-only.js +0 -52
  224. package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
@@ -8,39 +8,46 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
8
8
  let AutomationRepository = class {
9
9
  _ = new MongoRepository({
10
10
  database: getDatabaseName(),
11
- collection: "automations"
11
+ collection: "automations",
12
+ scoped: true
12
13
  });
13
- async create(automation) {
14
- const id = await this._.createOne({ payload: automation.toDocument() });
15
- id && automation.setId(id);
14
+ async create(automation, scope) {
15
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: automation.toDocument(), scope });
16
+ Object.assign(automation, { _id, ownerId, createdAt, createdBy });
16
17
  return automation;
17
18
  }
18
- async update(automation, options) {
19
+ async update(automation, scope, options) {
19
20
  if (options && typeof options.expectedVersion !== "number") throw new Error("[AutomationRepository] The expected version is required to check for conflicts");
20
21
  return await this._.updateOne({
22
+ scope,
21
23
  where: { _id: automation._id },
22
24
  payload: automation.toFlatDocument(),
23
25
  expectedVersion: options?.expectedVersion
24
26
  });
25
27
  }
26
- async findById(id) {
27
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
28
+ async findById(id, scope) {
29
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
28
30
  }
29
- async findByOwner(ownerId, options) {
30
- return await this._.findMany({ where: { ownerId: new ObjectId(ownerId) }, options });
31
+ async findByOwner(ownerId, scope, options) {
32
+ return await this._.findMany({ scope, where: { ownerId: new ObjectId(ownerId) }, options });
31
33
  }
32
- async deleteById(id) {
33
- return await this._.deleteOne({ where: { _id: this._.parseId(id) } });
34
+ async softDeleteById(id, scope) {
35
+ return await this._.softDelete({ scope, where: { _id: this._.parseId(id) }, payload: { toUnset: { "triggers.schedule.nextRunAt": "" } } });
34
36
  }
35
- async findDue(now, limit) {
37
+ async deleteById(id, scope) {
38
+ return await this._.deleteOne({ scope, where: { _id: this._.parseId(id) } });
39
+ }
40
+ async findDue(now, limit, scope) {
36
41
  return await this._.findMany({
42
+ scope,
37
43
  where: { "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
38
44
  options: { page: 1, limit, sortBy: { "triggers.schedule.nextRunAt": 1 } }
39
45
  });
40
46
  }
41
- async claim(params) {
47
+ async claim(params, scope) {
42
48
  const { id, workerId, now, leaseMs, nextRunAt } = params;
43
49
  return await this._.updateOne({
50
+ scope,
44
51
  where: { _id: this._.parseId(id), "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
45
52
  payload: {
46
53
  lockedBy: workerId,
@@ -49,34 +56,39 @@ let AutomationRepository = class {
49
56
  }
50
57
  });
51
58
  }
52
- async claimManual(params) {
59
+ async claimManual(params, scope) {
53
60
  const { id, workerId, now, leaseMs } = params;
54
61
  return await this._.updateOne({
62
+ scope,
55
63
  where: { _id: this._.parseId(id), ...this.unlocked(now) },
56
64
  payload: { lockedBy: workerId, lockedUntil: new Date(now.getTime() + leaseMs) }
57
65
  });
58
66
  }
59
- async setActiveRun(id, workerId, runId) {
67
+ async setActiveRun(id, workerId, runId, scope) {
60
68
  return await this._.updateOne({
69
+ scope,
61
70
  where: { _id: this._.parseId(id), lockedBy: workerId },
62
71
  payload: { activeRunId: this._.parseId(runId) }
63
72
  });
64
73
  }
65
- async settleRun(id, runId, status, at) {
74
+ async settleRun(id, runId, status, at, scope) {
66
75
  return await this._.updateOne({
76
+ scope,
67
77
  where: { _id: this._.parseId(id), activeRunId: this._.parseId(runId) },
68
78
  payload: { lastRunStatus: status, lastRunAt: at, toUnset: { activeRunId: "" } }
69
79
  });
70
80
  }
71
- async renewLease(id, workerId, until) {
81
+ async renewLease(id, workerId, until, scope) {
72
82
  return await this._.updateOne({
83
+ scope,
73
84
  where: { _id: this._.parseId(id), lockedBy: workerId },
74
85
  payload: { lockedUntil: until }
75
86
  });
76
87
  }
77
- async release(id, workerId, result) {
88
+ async release(id, workerId, result, scope) {
78
89
  const { activeRunId, lastRunAt, lastRunStatus } = result;
79
90
  return await this._.updateOne({
91
+ scope,
80
92
  where: { _id: this._.parseId(id), lockedBy: workerId },
81
93
  payload: {
82
94
  ...lastRunAt ? { lastRunAt } : {},
@@ -2,6 +2,7 @@ import { CurrentUser } from '../../../common/base/index.js';
2
2
  import { AgentOptions } from '../../../harness/agent.js';
3
3
  import { Chat } from '../../chats/entities/chat.entity.js';
4
4
  import { AutomationProps, AutomationRunProps } from '../entities';
5
+ import { Scope } from '../../../common/repositories/index.js';
5
6
  export interface AutomationContext {
6
7
  automation: AutomationProps;
7
8
  currentUser: CurrentUser;
@@ -37,6 +38,8 @@ export declare class AutomationRunner {
37
38
  tick(): Promise<void>;
38
39
  runNow(params: {
39
40
  automationId: string;
41
+ currentUser?: CurrentUser;
42
+ scope?: Scope;
40
43
  }): Promise<{
41
44
  run: AutomationRunProps;
42
45
  }>;
@@ -97,7 +97,7 @@ class AutomationRunner {
97
97
  for (const { automation, run } of queued.claimed) this.track(this.execute(automation, run));
98
98
  }
99
99
  async runNow(params) {
100
- const { data } = await import_tsyringe.container.resolve(import_start_automation_run.StartAutomationRunUseCase).execute({ automationId: params.automationId, workerId: this.workerId, leaseMs: this.options.leaseMs ?? (0, import_settings.leaseMs)() });
100
+ const { data } = await import_tsyringe.container.resolve(import_start_automation_run.StartAutomationRunUseCase).execute({ automationId: params.automationId, currentUser: params.currentUser, scope: params.scope, workerId: this.workerId, leaseMs: this.options.leaseMs ?? (0, import_settings.leaseMs)() });
101
101
  this.track(this.execute(data.automation, data.run));
102
102
  return { run: data.run };
103
103
  }
@@ -74,7 +74,7 @@ class AutomationRunner {
74
74
  for (const { automation, run } of queued.claimed) this.track(this.execute(automation, run));
75
75
  }
76
76
  async runNow(params) {
77
- const { data } = await container.resolve(StartAutomationRunUseCase).execute({ automationId: params.automationId, workerId: this.workerId, leaseMs: this.options.leaseMs ?? defaultLeaseMs() });
77
+ const { data } = await container.resolve(StartAutomationRunUseCase).execute({ automationId: params.automationId, currentUser: params.currentUser, scope: params.scope, workerId: this.workerId, leaseMs: this.options.leaseMs ?? defaultLeaseMs() });
78
78
  this.track(this.execute(data.automation, data.run));
79
79
  return { run: data.run };
80
80
  }
@@ -34,6 +34,7 @@ var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
35
  var import_entities = require("../entities");
36
36
  var import_repositories = require("../repositories");
37
+ var import_runner_scope = require("../utils/runner-scope");
37
38
  let AttachAutomationRunChatUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRunRepository) {
39
40
  super();
@@ -42,9 +43,9 @@ let AttachAutomationRunChatUseCase = class extends import_base.BaseUseCase {
42
43
  automationRunRepository;
43
44
  async execute(params) {
44
45
  const { runId, chatId } = params;
45
- const run = await this.automationRunRepository.findById(runId);
46
+ const run = await this.automationRunRepository.findById(runId, import_runner_scope.runnerScope);
46
47
  if (!run) return { data: null };
47
- const saved = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, chatId }));
48
+ const saved = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, chatId }), import_runner_scope.runnerScope);
48
49
  return { data: saved };
49
50
  }
50
51
  };
@@ -6,6 +6,7 @@ import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRun } from "../entities";
8
8
  import { AutomationRunRepository } from "../repositories";
9
+ import { runnerScope } from "../utils/runner-scope";
9
10
  let AttachAutomationRunChatUseCase = class extends BaseUseCase {
10
11
  constructor(automationRunRepository) {
11
12
  super();
@@ -14,9 +15,9 @@ let AttachAutomationRunChatUseCase = class extends BaseUseCase {
14
15
  automationRunRepository;
15
16
  async execute(params) {
16
17
  const { runId, chatId } = params;
17
- const run = await this.automationRunRepository.findById(runId);
18
+ const run = await this.automationRunRepository.findById(runId, runnerScope);
18
19
  if (!run) return { data: null };
19
- const saved = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, chatId }));
20
+ const saved = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, chatId }), runnerScope);
20
21
  return { data: saved };
21
22
  }
22
23
  };
@@ -39,6 +39,7 @@ var import_open_run = require("../utils/open-run");
39
39
  var import_settings = require("../utils/settings");
40
40
  var import_previous_run = require("../utils/previous-run");
41
41
  var import_sync_automation_run = require("./sync-automation-run.use-case");
42
+ var import_runner_scope = require("../utils/runner-scope");
42
43
  const DEFAULT_LIMIT = 10;
43
44
  let ClaimDueAutomationsUseCase = class extends import_base.BaseUseCase {
44
45
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -56,7 +57,7 @@ let ClaimDueAutomationsUseCase = class extends import_base.BaseUseCase {
56
57
  if (!workerId) throw new Error("The id of the worker is required");
57
58
  const now = params.now ?? /* @__PURE__ */ new Date();
58
59
  const leaseMs = params.leaseMs ?? (0, import_settings.leaseMs)();
59
- const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT);
60
+ const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT, import_runner_scope.runnerScope);
60
61
  const claimed = [];
61
62
  const skipped = [];
62
63
  for (const candidate of due) {
@@ -71,17 +72,17 @@ let ClaimDueAutomationsUseCase = class extends import_base.BaseUseCase {
71
72
  this.log.error({ error: error?.message ?? error, automationId: id }, "[AUTOMATIONS] The recurrence of an automation cannot be calculated, skipping it");
72
73
  continue;
73
74
  }
74
- const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 });
75
+ const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 }, import_runner_scope.runnerScope);
75
76
  if (!automation) continue;
76
77
  const pending = await (0, import_previous_run.findPausedRun)(this.automationRunRepository, this.syncRun, automation, now);
77
78
  if (pending) {
78
- const run2 = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
79
- await this.automationRepository.release(id, workerId, { activeRunId: pending._id });
79
+ const run2 = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.runnerScope, automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
80
+ await this.automationRepository.release(id, workerId, { activeRunId: pending._id }, import_runner_scope.runnerScope);
80
81
  skipped.push({ automation, run: run2.toJSON() });
81
82
  continue;
82
83
  }
83
- const run = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
84
- await this.automationRepository.setActiveRun(id, workerId, String(run._id));
84
+ const run = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.runnerScope, automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
85
+ await this.automationRepository.setActiveRun(id, workerId, String(run._id), import_runner_scope.runnerScope);
85
86
  claimed.push({ automation, run: run.toJSON() });
86
87
  }
87
88
  return { data: { claimed, skipped } };
@@ -11,6 +11,7 @@ import { openRun } from "../utils/open-run";
11
11
  import { leaseMs as defaultLeaseMs } from "../utils/settings";
12
12
  import { findPausedRun } from "../utils/previous-run";
13
13
  import { SyncAutomationRunUseCase } from "./sync-automation-run.use-case";
14
+ import { runnerScope } from "../utils/runner-scope";
14
15
  const DEFAULT_LIMIT = 10;
15
16
  let ClaimDueAutomationsUseCase = class extends BaseUseCase {
16
17
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -28,7 +29,7 @@ let ClaimDueAutomationsUseCase = class extends BaseUseCase {
28
29
  if (!workerId) throw new Error("The id of the worker is required");
29
30
  const now = params.now ?? /* @__PURE__ */ new Date();
30
31
  const leaseMs = params.leaseMs ?? defaultLeaseMs();
31
- const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT);
32
+ const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT, runnerScope);
32
33
  const claimed = [];
33
34
  const skipped = [];
34
35
  for (const candidate of due) {
@@ -43,17 +44,17 @@ let ClaimDueAutomationsUseCase = class extends BaseUseCase {
43
44
  this.log.error({ error: error?.message ?? error, automationId: id }, "[AUTOMATIONS] The recurrence of an automation cannot be calculated, skipping it");
44
45
  continue;
45
46
  }
46
- const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 });
47
+ const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 }, runnerScope);
47
48
  if (!automation) continue;
48
49
  const pending = await findPausedRun(this.automationRunRepository, this.syncRun, automation, now);
49
50
  if (pending) {
50
- const run2 = await openRun(this.automationRunRepository, { automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
51
- await this.automationRepository.release(id, workerId, { activeRunId: pending._id });
51
+ const run2 = await openRun(this.automationRunRepository, { scope: runnerScope, automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
52
+ await this.automationRepository.release(id, workerId, { activeRunId: pending._id }, runnerScope);
52
53
  skipped.push({ automation, run: run2.toJSON() });
53
54
  continue;
54
55
  }
55
- const run = await openRun(this.automationRunRepository, { automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
56
- await this.automationRepository.setActiveRun(id, workerId, String(run._id));
56
+ const run = await openRun(this.automationRunRepository, { scope: runnerScope, automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
57
+ await this.automationRepository.setActiveRun(id, workerId, String(run._id), runnerScope);
57
58
  claimed.push({ automation, run: run.toJSON() });
58
59
  }
59
60
  return { data: { claimed, skipped } };
@@ -37,6 +37,7 @@ var import_repositories = require("../repositories");
37
37
  var import_previous_run = require("../utils/previous-run");
38
38
  var import_settings = require("../utils/settings");
39
39
  var import_sync_automation_run = require("./sync-automation-run.use-case");
40
+ var import_runner_scope = require("../utils/runner-scope");
40
41
  const SCAN_LIMIT = 200;
41
42
  let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
42
43
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -54,7 +55,7 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
54
55
  const now = params.now ?? /* @__PURE__ */ new Date();
55
56
  const leaseMs = params.leaseMs ?? (0, import_settings.leaseMs)();
56
57
  const limit = params.limit ?? SCAN_LIMIT;
57
- const queued = await this.automationRunRepository.findByStatus("queued", SCAN_LIMIT);
58
+ const queued = await this.automationRunRepository.findByStatus("queued", SCAN_LIMIT, import_runner_scope.runnerScope);
58
59
  const claimed = [];
59
60
  const skipped = [];
60
61
  const seen = /* @__PURE__ */ new Set();
@@ -63,18 +64,18 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
63
64
  const automationId = String(run.automationId);
64
65
  if (seen.has(automationId)) continue;
65
66
  seen.add(automationId);
66
- const existing = await this.automationRepository.findById(automationId);
67
+ const existing = await this.automationRepository.findById(automationId, import_runner_scope.runnerScope);
67
68
  const reason = !existing ? "The automation no longer exists" : !existing.triggers?.event?.enabled ? "The event trigger of the automation is turned off" : void 0;
68
69
  if (reason) {
69
- const settled = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }));
70
+ const settled = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }), import_runner_scope.runnerScope);
70
71
  if (existing && settled) skipped.push({ automation: existing, run: settled });
71
72
  continue;
72
73
  }
73
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs });
74
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs }, import_runner_scope.runnerScope);
74
75
  if (!automation) continue;
75
76
  const paused = await (0, import_previous_run.findPausedRun)(this.automationRunRepository, this.syncRun, automation, now);
76
77
  if (paused) {
77
- await this.automationRepository.release(automationId, workerId, { activeRunId: paused._id });
78
+ await this.automationRepository.release(automationId, workerId, { activeRunId: paused._id }, import_runner_scope.runnerScope);
78
79
  continue;
79
80
  }
80
81
  const started = await this.automationRunRepository.claimQueued(String(run._id), {
@@ -83,12 +84,12 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
83
84
  instructions: automation.instructions,
84
85
  modelProvider: automation.modelProvider,
85
86
  modelName: automation.modelName
86
- });
87
+ }, import_runner_scope.runnerScope);
87
88
  if (!started) {
88
- await this.automationRepository.release(automationId, workerId, {});
89
+ await this.automationRepository.release(automationId, workerId, {}, import_runner_scope.runnerScope);
89
90
  continue;
90
91
  }
91
- await this.automationRepository.setActiveRun(automationId, workerId, String(started._id));
92
+ await this.automationRepository.setActiveRun(automationId, workerId, String(started._id), import_runner_scope.runnerScope);
92
93
  claimed.push({ automation, run: started });
93
94
  }
94
95
  return { data: { claimed, skipped } };
@@ -9,6 +9,7 @@ import { AutomationRepository, AutomationRunRepository } from "../repositories";
9
9
  import { findPausedRun } from "../utils/previous-run";
10
10
  import { leaseMs as defaultLeaseMs } from "../utils/settings";
11
11
  import { SyncAutomationRunUseCase } from "./sync-automation-run.use-case";
12
+ import { runnerScope } from "../utils/runner-scope";
12
13
  const SCAN_LIMIT = 200;
13
14
  let ClaimQueuedAutomationRunsUseCase = class extends BaseUseCase {
14
15
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -26,7 +27,7 @@ let ClaimQueuedAutomationRunsUseCase = class extends BaseUseCase {
26
27
  const now = params.now ?? /* @__PURE__ */ new Date();
27
28
  const leaseMs = params.leaseMs ?? defaultLeaseMs();
28
29
  const limit = params.limit ?? SCAN_LIMIT;
29
- const queued = await this.automationRunRepository.findByStatus("queued", SCAN_LIMIT);
30
+ const queued = await this.automationRunRepository.findByStatus("queued", SCAN_LIMIT, runnerScope);
30
31
  const claimed = [];
31
32
  const skipped = [];
32
33
  const seen = /* @__PURE__ */ new Set();
@@ -35,18 +36,18 @@ let ClaimQueuedAutomationRunsUseCase = class extends BaseUseCase {
35
36
  const automationId = String(run.automationId);
36
37
  if (seen.has(automationId)) continue;
37
38
  seen.add(automationId);
38
- const existing = await this.automationRepository.findById(automationId);
39
+ const existing = await this.automationRepository.findById(automationId, runnerScope);
39
40
  const reason = !existing ? "The automation no longer exists" : !existing.triggers?.event?.enabled ? "The event trigger of the automation is turned off" : void 0;
40
41
  if (reason) {
41
- const settled = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }));
42
+ const settled = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }), runnerScope);
42
43
  if (existing && settled) skipped.push({ automation: existing, run: settled });
43
44
  continue;
44
45
  }
45
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs });
46
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs }, runnerScope);
46
47
  if (!automation) continue;
47
48
  const paused = await findPausedRun(this.automationRunRepository, this.syncRun, automation, now);
48
49
  if (paused) {
49
- await this.automationRepository.release(automationId, workerId, { activeRunId: paused._id });
50
+ await this.automationRepository.release(automationId, workerId, { activeRunId: paused._id }, runnerScope);
50
51
  continue;
51
52
  }
52
53
  const started = await this.automationRunRepository.claimQueued(String(run._id), {
@@ -55,12 +56,12 @@ let ClaimQueuedAutomationRunsUseCase = class extends BaseUseCase {
55
56
  instructions: automation.instructions,
56
57
  modelProvider: automation.modelProvider,
57
58
  modelName: automation.modelName
58
- });
59
+ }, runnerScope);
59
60
  if (!started) {
60
- await this.automationRepository.release(automationId, workerId, {});
61
+ await this.automationRepository.release(automationId, workerId, {}, runnerScope);
61
62
  continue;
62
63
  }
63
- await this.automationRepository.setActiveRun(automationId, workerId, String(started._id));
64
+ await this.automationRepository.setActiveRun(automationId, workerId, String(started._id), runnerScope);
64
65
  claimed.push({ automation, run: started });
65
66
  }
66
67
  return { data: { claimed, skipped } };
@@ -32,8 +32,9 @@ __export(create_automation_use_case_exports, {
32
32
  module.exports = __toCommonJS(create_automation_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_entities = require("../entities");
36
- var import_repositories = require("../repositories");
37
+ var import_repositories2 = require("../repositories");
37
38
  var import_automation_fields = require("../utils/automation-fields");
38
39
  let CreateAutomationUseCase = class extends import_base.BaseUseCase {
39
40
  constructor(automationRepository) {
@@ -54,18 +55,16 @@ let CreateAutomationUseCase = class extends import_base.BaseUseCase {
54
55
  modelProvider: fields.modelProvider ?? void 0,
55
56
  modelName: fields.modelName ?? void 0
56
57
  });
57
- automation.setCreatedBy(currentUser);
58
- automation.setOwner(currentUser);
59
58
  automation.scheduleNextRun();
60
59
  const schedule = automation.triggers?.schedule;
61
60
  if (schedule?.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
62
- await this.automationRepository?.create(automation);
61
+ await this.automationRepository?.create(automation, params.scope ?? import_repositories.Scope.canEdit(currentUser));
63
62
  return { data: automation.toJSON() };
64
63
  }
65
64
  };
66
65
  CreateAutomationUseCase = __decorateClass([
67
66
  (0, import_tsyringe.autoInjectable)(),
68
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
67
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
69
68
  ], CreateAutomationUseCase);
70
69
  // Annotate the CommonJS export names for ESM import in node:
71
70
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { Automation } from "../entities";
8
9
  import { AutomationRepository } from "../repositories";
9
10
  import { readAutomationFields, assertModel } from "../utils/automation-fields";
@@ -26,12 +27,10 @@ let CreateAutomationUseCase = class extends BaseUseCase {
26
27
  modelProvider: fields.modelProvider ?? void 0,
27
28
  modelName: fields.modelName ?? void 0
28
29
  });
29
- automation.setCreatedBy(currentUser);
30
- automation.setOwner(currentUser);
31
30
  automation.scheduleNextRun();
32
31
  const schedule = automation.triggers?.schedule;
33
32
  if (schedule?.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
34
- await this.automationRepository?.create(automation);
33
+ await this.automationRepository?.create(automation, params.scope ?? Scope.canEdit(currentUser));
35
34
  return { data: automation.toJSON() };
36
35
  }
37
36
  };
@@ -32,8 +32,8 @@ __export(delete_automation_use_case_exports, {
32
32
  module.exports = __toCommonJS(delete_automation_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
- var import_entities = require("../entities");
36
- var import_repositories = require("../repositories");
35
+ var import_repositories = require("../../../common/repositories/index.js");
36
+ var import_repositories2 = require("../repositories");
37
37
  let DeleteAutomationUseCase = class extends import_base.BaseUseCase {
38
38
  constructor(automationRepository) {
39
39
  super();
@@ -44,20 +44,17 @@ let DeleteAutomationUseCase = class extends import_base.BaseUseCase {
44
44
  const { id, hardDelete, currentUser } = params;
45
45
  if (!id) throw new Error("Missing id");
46
46
  if (hardDelete) {
47
- if (!currentUser?.isAdmin()) this.throwError403();
48
- const result2 = await this.automationRepository?.deleteById(id);
47
+ const result2 = await this.automationRepository?.deleteById(id, params.scope ?? import_repositories.Scope.asAdmin(currentUser));
49
48
  return { data: { deletedCount: result2 } };
50
49
  }
51
- const automation = import_entities.Automation.fromJSON({ _id: id });
52
- automation.setDeletedBy(currentUser);
53
- automation.unset(["triggers.schedule.nextRunAt"]);
54
- const result = await this.automationRepository?.update(automation);
50
+ const result = await this.automationRepository?.softDeleteById(id, params.scope ?? import_repositories.Scope.canManage(currentUser));
51
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
55
52
  return { data: result };
56
53
  }
57
54
  };
58
55
  DeleteAutomationUseCase = __decorateClass([
59
56
  (0, import_tsyringe.autoInjectable)(),
60
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
57
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
61
58
  ], DeleteAutomationUseCase);
62
59
  // Annotate the CommonJS export names for ESM import in node:
63
60
  0 && (module.exports = {
@@ -4,7 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
- import { Automation } from "../entities";
7
+ import { Scope } from "../../../common/repositories/index.js";
8
8
  import { AutomationRepository } from "../repositories";
9
9
  let DeleteAutomationUseCase = class extends BaseUseCase {
10
10
  constructor(automationRepository) {
@@ -16,14 +16,11 @@ let DeleteAutomationUseCase = class extends BaseUseCase {
16
16
  const { id, hardDelete, currentUser } = params;
17
17
  if (!id) throw new Error("Missing id");
18
18
  if (hardDelete) {
19
- if (!currentUser?.isAdmin()) this.throwError403();
20
- const result2 = await this.automationRepository?.deleteById(id);
19
+ const result2 = await this.automationRepository?.deleteById(id, params.scope ?? Scope.asAdmin(currentUser));
21
20
  return { data: { deletedCount: result2 } };
22
21
  }
23
- const automation = Automation.fromJSON({ _id: id });
24
- automation.setDeletedBy(currentUser);
25
- automation.unset(["triggers.schedule.nextRunAt"]);
26
- const result = await this.automationRepository?.update(automation);
22
+ const result = await this.automationRepository?.softDeleteById(id, params.scope ?? Scope.canManage(currentUser));
23
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
27
24
  return { data: result };
28
25
  }
29
26
  };
@@ -34,6 +34,7 @@ var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
35
  var import_entities = require("../entities");
36
36
  var import_repositories = require("../repositories");
37
+ var import_runner_scope = require("../utils/runner-scope");
37
38
  let FinishAutomationRunUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRepository, automationRunRepository) {
39
40
  super();
@@ -45,7 +46,7 @@ let FinishAutomationRunUseCase = class extends import_base.BaseUseCase {
45
46
  async execute(params) {
46
47
  const { runId, workerId, status, error } = params;
47
48
  const now = params.now ?? /* @__PURE__ */ new Date();
48
- const run = await this.automationRunRepository.findById(runId);
49
+ const run = await this.automationRunRepository.findById(runId, import_runner_scope.runnerScope);
49
50
  if (!run) return { data: null };
50
51
  const finished = import_entities.AutomationRun.fromJSON({
51
52
  ...run,
@@ -53,13 +54,13 @@ let FinishAutomationRunUseCase = class extends import_base.BaseUseCase {
53
54
  ...status === "paused" ? {} : { finishedAt: now },
54
55
  ...error ? { error } : {}
55
56
  });
56
- const saved = await this.automationRunRepository.update(finished);
57
+ const saved = await this.automationRunRepository.update(finished, import_runner_scope.runnerScope);
57
58
  if (run.automationId) {
58
59
  await this.automationRepository.release(String(run.automationId), workerId, {
59
60
  lastRunAt: now,
60
61
  lastRunStatus: status,
61
62
  ...status === "paused" ? { activeRunId: run._id } : {}
62
- });
63
+ }, import_runner_scope.runnerScope);
63
64
  }
64
65
  return { data: saved ?? finished.toJSON() };
65
66
  }
@@ -6,6 +6,7 @@ import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRun } from "../entities";
8
8
  import { AutomationRepository, AutomationRunRepository } from "../repositories";
9
+ import { runnerScope } from "../utils/runner-scope";
9
10
  let FinishAutomationRunUseCase = class extends BaseUseCase {
10
11
  constructor(automationRepository, automationRunRepository) {
11
12
  super();
@@ -17,7 +18,7 @@ let FinishAutomationRunUseCase = class extends BaseUseCase {
17
18
  async execute(params) {
18
19
  const { runId, workerId, status, error } = params;
19
20
  const now = params.now ?? /* @__PURE__ */ new Date();
20
- const run = await this.automationRunRepository.findById(runId);
21
+ const run = await this.automationRunRepository.findById(runId, runnerScope);
21
22
  if (!run) return { data: null };
22
23
  const finished = AutomationRun.fromJSON({
23
24
  ...run,
@@ -25,13 +26,13 @@ let FinishAutomationRunUseCase = class extends BaseUseCase {
25
26
  ...status === "paused" ? {} : { finishedAt: now },
26
27
  ...error ? { error } : {}
27
28
  });
28
- const saved = await this.automationRunRepository.update(finished);
29
+ const saved = await this.automationRunRepository.update(finished, runnerScope);
29
30
  if (run.automationId) {
30
31
  await this.automationRepository.release(String(run.automationId), workerId, {
31
32
  lastRunAt: now,
32
33
  lastRunStatus: status,
33
34
  ...status === "paused" ? { activeRunId: run._id } : {}
34
- });
35
+ }, runnerScope);
35
36
  }
36
37
  return { data: saved ?? finished.toJSON() };
37
38
  }
@@ -32,7 +32,8 @@ __export(get_automation_by_id_use_case_exports, {
32
32
  module.exports = __toCommonJS(get_automation_by_id_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
- var import_repositories = require("../repositories");
35
+ var import_repositories = require("../../../common/repositories/index.js");
36
+ var import_repositories2 = require("../repositories");
36
37
  let GetAutomationByIdUseCase = class extends import_base.BaseUseCase {
37
38
  constructor(automationRepository) {
38
39
  super();
@@ -40,14 +41,14 @@ let GetAutomationByIdUseCase = class extends import_base.BaseUseCase {
40
41
  }
41
42
  automationRepository;
42
43
  async execute(params) {
43
- const { id } = params;
44
- const result = await this.automationRepository?.findById(id);
44
+ const { id, currentUser } = params;
45
+ const result = await this.automationRepository?.findById(id, params.scope ?? import_repositories.Scope.canView(currentUser));
45
46
  return { data: result ?? null };
46
47
  }
47
48
  };
48
49
  GetAutomationByIdUseCase = __decorateClass([
49
50
  (0, import_tsyringe.autoInjectable)(),
50
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
51
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
51
52
  ], GetAutomationByIdUseCase);
52
53
  // Annotate the CommonJS export names for ESM import in node:
53
54
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { AutomationRepository } from "../repositories";
8
9
  let GetAutomationByIdUseCase = class extends BaseUseCase {
9
10
  constructor(automationRepository) {
@@ -12,8 +13,8 @@ let GetAutomationByIdUseCase = class extends BaseUseCase {
12
13
  }
13
14
  automationRepository;
14
15
  async execute(params) {
15
- const { id } = params;
16
- const result = await this.automationRepository?.findById(id);
16
+ const { id, currentUser } = params;
17
+ const result = await this.automationRepository?.findById(id, params.scope ?? Scope.canView(currentUser));
17
18
  return { data: result ?? null };
18
19
  }
19
20
  };