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
@@ -32,7 +32,8 @@ __export(issue_automation_event_token_use_case_exports, {
32
32
  module.exports = __toCommonJS(issue_automation_event_token_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
  var import_event_token = require("../utils/event-token");
37
38
  let IssueAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRepository) {
@@ -42,16 +43,15 @@ let IssueAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
42
43
  automationRepository;
43
44
  async execute(params) {
44
45
  const { id, expiresIn, currentUser } = params;
45
- const automation = await this.automationRepository?.findById(id);
46
- if (!automation) throw new Error("Automation not found");
47
- if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
46
+ const automation = await this.automationRepository?.findById(id, params.scope ?? import_repositories.Scope.canManage(currentUser));
47
+ if (!automation) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
48
48
  if (!automation.triggers?.event) throw new Error("Only an automation with an event trigger has a token");
49
49
  return { data: (0, import_event_token.signEventToken)({ automationId: id, tokenVersion: automation.eventTokenVersion, expiresIn }) };
50
50
  }
51
51
  };
52
52
  IssueAutomationEventTokenUseCase = __decorateClass([
53
53
  (0, import_tsyringe.autoInjectable)(),
54
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
54
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
55
55
  ], IssueAutomationEventTokenUseCase);
56
56
  // Annotate the CommonJS export names for ESM import in node:
57
57
  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
  import { signEventToken } from "../utils/event-token";
9
10
  let IssueAutomationEventTokenUseCase = class extends BaseUseCase {
@@ -14,9 +15,8 @@ let IssueAutomationEventTokenUseCase = class extends BaseUseCase {
14
15
  automationRepository;
15
16
  async execute(params) {
16
17
  const { id, expiresIn, currentUser } = params;
17
- const automation = await this.automationRepository?.findById(id);
18
- if (!automation) throw new Error("Automation not found");
19
- if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
18
+ const automation = await this.automationRepository?.findById(id, params.scope ?? Scope.canManage(currentUser));
19
+ if (!automation) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
20
20
  if (!automation.triggers?.event) throw new Error("Only an automation with an event trigger has a token");
21
21
  return { data: signEventToken({ automationId: id, tokenVersion: automation.eventTokenVersion, expiresIn }) };
22
22
  }
@@ -32,6 +32,7 @@ __export(list_automation_runs_by_automation_use_case_exports, {
32
32
  module.exports = __toCommonJS(list_automation_runs_by_automation_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_mongodb = require("mongodb");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_base = require("../../../common/base/base.use-case.js");
36
37
  var import_repositories2 = require("../repositories");
37
38
  let ListAutomationRunsByAutomationUseCase = class extends import_base.BaseUseCase {
@@ -41,10 +42,11 @@ let ListAutomationRunsByAutomationUseCase = class extends import_base.BaseUseCas
41
42
  }
42
43
  automationRunRepository;
43
44
  async execute(params) {
44
- const { automationId, options } = params;
45
+ const { automationId, options, currentUser } = params;
45
46
  if (!automationId) throw new Error("Missing automationId");
46
47
  const { page = 1, limit = 10, sortBy = { createdAt: -1 } } = options || {};
47
48
  const result = await this.automationRunRepository?._.findManyPaginated({
49
+ scope: params.scope ?? import_repositories.Scope.canView(currentUser),
48
50
  where: { automationId: new import_mongodb.ObjectId(automationId) },
49
51
  options: { page, limit, sortBy }
50
52
  });
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { ObjectId } from "mongodb";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
8
9
  import { AutomationRunRepository } from "../repositories";
9
10
  let ListAutomationRunsByAutomationUseCase = class extends BaseUseCase {
@@ -13,10 +14,11 @@ let ListAutomationRunsByAutomationUseCase = class extends BaseUseCase {
13
14
  }
14
15
  automationRunRepository;
15
16
  async execute(params) {
16
- const { automationId, options } = params;
17
+ const { automationId, options, currentUser } = params;
17
18
  if (!automationId) throw new Error("Missing automationId");
18
19
  const { page = 1, limit = 10, sortBy = { createdAt: -1 } } = options || {};
19
20
  const result = await this.automationRunRepository?._.findManyPaginated({
21
+ scope: params.scope ?? Scope.canView(currentUser),
20
22
  where: { automationId: new ObjectId(automationId) },
21
23
  options: { page, limit, sortBy }
22
24
  });
@@ -32,6 +32,7 @@ __export(list_automation_runs_by_user_use_case_exports, {
32
32
  module.exports = __toCommonJS(list_automation_runs_by_user_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_mongodb = require("mongodb");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_base = require("../../../common/base/base.use-case.js");
36
37
  var import_repositories2 = require("../repositories");
37
38
  let ListAutomationRunsByUserUseCase = class extends import_base.BaseUseCase {
@@ -45,6 +46,7 @@ let ListAutomationRunsByUserUseCase = class extends import_base.BaseUseCase {
45
46
  if (!currentUser?.userId) throw new Error("CurrentUser is required to list the runs of the automations");
46
47
  const { page = 1, limit = 10, sortBy = { createdAt: -1 } } = options || {};
47
48
  const result = await this.automationRunRepository?._.findManyPaginated({
49
+ scope: params.scope ?? import_repositories.Scope.canView(currentUser),
48
50
  where: { ownerId: new import_mongodb.ObjectId(currentUser.userId), ...status ? { status } : {} },
49
51
  options: { page, limit, sortBy }
50
52
  });
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { ObjectId } from "mongodb";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
8
9
  import { AutomationRunRepository } from "../repositories";
9
10
  let ListAutomationRunsByUserUseCase = class extends BaseUseCase {
@@ -17,6 +18,7 @@ let ListAutomationRunsByUserUseCase = class extends BaseUseCase {
17
18
  if (!currentUser?.userId) throw new Error("CurrentUser is required to list the runs of the automations");
18
19
  const { page = 1, limit = 10, sortBy = { createdAt: -1 } } = options || {};
19
20
  const result = await this.automationRunRepository?._.findManyPaginated({
21
+ scope: params.scope ?? Scope.canView(currentUser),
20
22
  where: { ownerId: new ObjectId(currentUser.userId), ...status ? { status } : {} },
21
23
  options: { page, limit, sortBy }
22
24
  });
@@ -32,6 +32,7 @@ __export(list_automations_by_user_use_case_exports, {
32
32
  module.exports = __toCommonJS(list_automations_by_user_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_mongodb = require("mongodb");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_base = require("../../../common/base/base.use-case.js");
36
37
  var import_repositories2 = require("../repositories");
37
38
  let ListAutomationsByUserUseCase = class extends import_base.BaseUseCase {
@@ -45,6 +46,7 @@ let ListAutomationsByUserUseCase = class extends import_base.BaseUseCase {
45
46
  if (!currentUser?.userId) throw new Error("CurrentUser is required to list the automations");
46
47
  const { page = 1, limit = 10, sortBy = { createdAt: -1 } } = options || {};
47
48
  const result = await this.automationRepository?._.findManyPaginated({
49
+ scope: params.scope ?? import_repositories.Scope.canView(currentUser),
48
50
  where: { ownerId: new import_mongodb.ObjectId(currentUser.userId) },
49
51
  options: { page, limit, sortBy }
50
52
  });
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { ObjectId } from "mongodb";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
8
9
  import { AutomationRepository } from "../repositories";
9
10
  let ListAutomationsByUserUseCase = class extends BaseUseCase {
@@ -17,6 +18,7 @@ let ListAutomationsByUserUseCase = class extends BaseUseCase {
17
18
  if (!currentUser?.userId) throw new Error("CurrentUser is required to list the automations");
18
19
  const { page = 1, limit = 10, sortBy = { createdAt: -1 } } = options || {};
19
20
  const result = await this.automationRepository?._.findManyPaginated({
21
+ scope: params.scope ?? Scope.canView(currentUser),
20
22
  where: { ownerId: new ObjectId(currentUser.userId) },
21
23
  options: { page, limit, sortBy }
22
24
  });
@@ -38,6 +38,7 @@ var import_automation_event_error = require("../utils/automation-event-error");
38
38
  var import_event_token = require("../utils/event-token");
39
39
  var import_open_run = require("../utils/open-run");
40
40
  var import_settings = require("../utils/settings");
41
+ var import_runner_scope = require("../utils/runner-scope");
41
42
  const MAX_PAYLOAD_CHARS = 5e4;
42
43
  const MAX_KEY_CHARS = 200;
43
44
  const serialize = (payload) => {
@@ -57,7 +58,7 @@ let ReceiveAutomationEventUseCase = class extends import_base.BaseUseCase {
57
58
  const now = params.now ?? /* @__PURE__ */ new Date();
58
59
  const claims = (0, import_event_token.readEventToken)(token);
59
60
  if (claims.sub !== automationId) throw new import_automation_event_error.AutomationEventError("invalid");
60
- const automation = await this.automationRepository.findById(automationId);
61
+ const automation = await this.automationRepository.findById(automationId, import_runner_scope.eventScope);
61
62
  if (!automation) throw new import_automation_event_error.AutomationEventError("not_found");
62
63
  const event = automation.triggers?.event;
63
64
  if (!event || automation.eventTokenVersion !== claims.v) throw new import_automation_event_error.AutomationEventError("invalid");
@@ -69,19 +70,19 @@ let ReceiveAutomationEventUseCase = class extends import_base.BaseUseCase {
69
70
  const idempotencyKey = params.idempotencyKey?.trim() || void 0;
70
71
  if (idempotencyKey && idempotencyKey.length > MAX_KEY_CHARS) throw new Error(`The idempotency key cannot be longer than ${MAX_KEY_CHARS} characters`);
71
72
  if (idempotencyKey) {
72
- const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey);
73
+ const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey, import_runner_scope.eventScope);
73
74
  if (duplicate) return { data: { status: "duplicate", run: duplicate } };
74
75
  }
75
- if (await this.automationRunRepository.countQueued(automationId) >= (0, import_settings.maxQueuedRuns)()) {
76
- const skipped = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "event", status: "skipped", now, payload, skipReason: "The queue of this automation is full" });
76
+ if (await this.automationRunRepository.countQueued(automationId, import_runner_scope.eventScope) >= (0, import_settings.maxQueuedRuns)()) {
77
+ const skipped = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.eventScope, automation, trigger: "event", status: "skipped", now, payload, skipReason: "The queue of this automation is full" });
77
78
  return { data: { status: "skipped", run: skipped.toJSON() } };
78
79
  }
79
80
  try {
80
- const run = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "event", status: "queued", now, payload, idempotencyKey });
81
+ const run = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.eventScope, automation, trigger: "event", status: "queued", now, payload, idempotencyKey });
81
82
  return { data: { status: "queued", run: run.toJSON() } };
82
83
  } catch (error) {
83
84
  if (!idempotencyKey || error?.code !== 11e3) throw error;
84
- const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey);
85
+ const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey, import_runner_scope.eventScope);
85
86
  if (!duplicate) throw error;
86
87
  return { data: { status: "duplicate", run: duplicate } };
87
88
  }
@@ -10,6 +10,7 @@ import { AutomationEventError } from "../utils/automation-event-error";
10
10
  import { readEventToken } from "../utils/event-token";
11
11
  import { openRun } from "../utils/open-run";
12
12
  import { maxQueuedRuns } from "../utils/settings";
13
+ import { eventScope } from "../utils/runner-scope";
13
14
  const MAX_PAYLOAD_CHARS = 5e4;
14
15
  const MAX_KEY_CHARS = 200;
15
16
  const serialize = (payload) => {
@@ -29,7 +30,7 @@ let ReceiveAutomationEventUseCase = class extends BaseUseCase {
29
30
  const now = params.now ?? /* @__PURE__ */ new Date();
30
31
  const claims = readEventToken(token);
31
32
  if (claims.sub !== automationId) throw new AutomationEventError("invalid");
32
- const automation = await this.automationRepository.findById(automationId);
33
+ const automation = await this.automationRepository.findById(automationId, eventScope);
33
34
  if (!automation) throw new AutomationEventError("not_found");
34
35
  const event = automation.triggers?.event;
35
36
  if (!event || automation.eventTokenVersion !== claims.v) throw new AutomationEventError("invalid");
@@ -41,19 +42,19 @@ let ReceiveAutomationEventUseCase = class extends BaseUseCase {
41
42
  const idempotencyKey = params.idempotencyKey?.trim() || void 0;
42
43
  if (idempotencyKey && idempotencyKey.length > MAX_KEY_CHARS) throw new Error(`The idempotency key cannot be longer than ${MAX_KEY_CHARS} characters`);
43
44
  if (idempotencyKey) {
44
- const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey);
45
+ const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey, eventScope);
45
46
  if (duplicate) return { data: { status: "duplicate", run: duplicate } };
46
47
  }
47
- if (await this.automationRunRepository.countQueued(automationId) >= maxQueuedRuns()) {
48
- const skipped = await openRun(this.automationRunRepository, { automation, trigger: "event", status: "skipped", now, payload, skipReason: "The queue of this automation is full" });
48
+ if (await this.automationRunRepository.countQueued(automationId, eventScope) >= maxQueuedRuns()) {
49
+ const skipped = await openRun(this.automationRunRepository, { scope: eventScope, automation, trigger: "event", status: "skipped", now, payload, skipReason: "The queue of this automation is full" });
49
50
  return { data: { status: "skipped", run: skipped.toJSON() } };
50
51
  }
51
52
  try {
52
- const run = await openRun(this.automationRunRepository, { automation, trigger: "event", status: "queued", now, payload, idempotencyKey });
53
+ const run = await openRun(this.automationRunRepository, { scope: eventScope, automation, trigger: "event", status: "queued", now, payload, idempotencyKey });
53
54
  return { data: { status: "queued", run: run.toJSON() } };
54
55
  } catch (error) {
55
56
  if (!idempotencyKey || error?.code !== 11e3) throw error;
56
- const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey);
57
+ const duplicate = await this.automationRunRepository.findByIdempotencyKey(automationId, idempotencyKey, eventScope);
57
58
  if (!duplicate) throw error;
58
59
  return { data: { status: "duplicate", run: duplicate } };
59
60
  }
@@ -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_repositories = require("../repositories");
36
36
  var import_settings = require("../utils/settings");
37
+ var import_runner_scope = require("../utils/runner-scope");
37
38
  let RenewAutomationLeaseUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRepository) {
39
40
  super();
@@ -44,7 +45,7 @@ let RenewAutomationLeaseUseCase = class extends import_base.BaseUseCase {
44
45
  const { automationId, workerId } = params;
45
46
  const now = params.now ?? /* @__PURE__ */ new Date();
46
47
  const until = new Date(now.getTime() + (params.leaseMs ?? (0, import_settings.leaseMs)()));
47
- const automation = await this.automationRepository.renewLease(automationId, workerId, until);
48
+ const automation = await this.automationRepository.renewLease(automationId, workerId, until, import_runner_scope.runnerScope);
48
49
  return { data: { renewed: Boolean(automation) } };
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 { AutomationRepository } from "../repositories";
8
8
  import { leaseMs as defaultLeaseMs } from "../utils/settings";
9
+ import { runnerScope } from "../utils/runner-scope";
9
10
  let RenewAutomationLeaseUseCase = class extends BaseUseCase {
10
11
  constructor(automationRepository) {
11
12
  super();
@@ -16,7 +17,7 @@ let RenewAutomationLeaseUseCase = class extends BaseUseCase {
16
17
  const { automationId, workerId } = params;
17
18
  const now = params.now ?? /* @__PURE__ */ new Date();
18
19
  const until = new Date(now.getTime() + (params.leaseMs ?? defaultLeaseMs()));
19
- const automation = await this.automationRepository.renewLease(automationId, workerId, until);
20
+ const automation = await this.automationRepository.renewLease(automationId, workerId, until, runnerScope);
20
21
  return { data: { renewed: Boolean(automation) } };
21
22
  }
22
23
  };
@@ -32,8 +32,9 @@ __export(revoke_automation_event_token_use_case_exports, {
32
32
  module.exports = __toCommonJS(revoke_automation_event_token_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_authentication = require("../../authentication/index.js");
36
- var import_repositories = require("../repositories");
37
+ var import_repositories2 = require("../repositories");
37
38
  var import_event_token = require("../utils/event-token");
38
39
  let RevokeAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
39
40
  constructor(automationRepository) {
@@ -43,9 +44,8 @@ let RevokeAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
43
44
  automationRepository;
44
45
  async execute(params) {
45
46
  const { id, token, currentUser } = params;
46
- const automation = await this.automationRepository?.findById(id);
47
- if (!automation) throw new Error("Automation not found");
48
- if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
47
+ const automation = await this.automationRepository?.findById(id, params.scope ?? import_repositories.Scope.canManage(currentUser));
48
+ if (!automation) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
49
49
  const claims = (0, import_event_token.readEventToken)(token, { ignoreExpiration: true });
50
50
  if (claims.sub !== id) throw new Error("The token does not belong to this automation");
51
51
  await (0, import_authentication.revokeToken)({
@@ -57,7 +57,7 @@ let RevokeAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
57
57
  };
58
58
  RevokeAutomationEventTokenUseCase = __decorateClass([
59
59
  (0, import_tsyringe.autoInjectable)(),
60
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
60
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
61
61
  ], RevokeAutomationEventTokenUseCase);
62
62
  // Annotate the CommonJS export names for ESM import in node:
63
63
  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 { revokeToken } from "../../authentication/index.js";
8
9
  import { AutomationRepository } from "../repositories";
9
10
  import { EVENT_TOKEN_AUDIENCE, readEventToken } from "../utils/event-token";
@@ -15,9 +16,8 @@ let RevokeAutomationEventTokenUseCase = class extends BaseUseCase {
15
16
  automationRepository;
16
17
  async execute(params) {
17
18
  const { id, token, currentUser } = params;
18
- const automation = await this.automationRepository?.findById(id);
19
- if (!automation) throw new Error("Automation not found");
20
- if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
19
+ const automation = await this.automationRepository?.findById(id, params.scope ?? Scope.canManage(currentUser));
20
+ if (!automation) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
21
21
  const claims = readEventToken(token, { ignoreExpiration: true });
22
22
  if (claims.sub !== id) throw new Error("The token does not belong to this automation");
23
23
  await revokeToken({
@@ -32,8 +32,9 @@ __export(rotate_automation_event_tokens_use_case_exports, {
32
32
  module.exports = __toCommonJS(rotate_automation_event_tokens_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
  let RotateAutomationEventTokensUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRepository) {
39
40
  super();
@@ -42,20 +43,19 @@ let RotateAutomationEventTokensUseCase = class extends import_base.BaseUseCase {
42
43
  automationRepository;
43
44
  async execute(params) {
44
45
  const { id, currentUser } = params;
45
- const existing = await this.automationRepository?.findById(id);
46
- if (!existing) throw new Error("Automation not found");
47
- if (!currentUser?.isAdmin() && String(existing.ownerId) !== currentUser?.userId) this.throwError403();
46
+ const scope = params.scope ?? import_repositories.Scope.canManage(currentUser);
47
+ const existing = await this.automationRepository?.findById(id, scope);
48
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
48
49
  if (!existing.triggers?.event) throw new Error("Only an automation with an event trigger has tokens");
49
50
  const automation = import_entities.Automation.fromJSON({ ...existing, eventTokenVersion: existing.eventTokenVersion + 1 });
50
- automation.setUpdatedBy(currentUser);
51
- const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
52
- if (!result) throw new Error("Automation not found or update failed");
51
+ const result = await this.automationRepository?.update(automation, scope, { expectedVersion: existing.__v });
52
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
53
53
  return { data: result };
54
54
  }
55
55
  };
56
56
  RotateAutomationEventTokensUseCase = __decorateClass([
57
57
  (0, import_tsyringe.autoInjectable)(),
58
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
58
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
59
59
  ], RotateAutomationEventTokensUseCase);
60
60
  // Annotate the CommonJS export names for ESM import in node:
61
61
  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
  let RotateAutomationEventTokensUseCase = class extends BaseUseCase {
@@ -14,14 +15,13 @@ let RotateAutomationEventTokensUseCase = class extends BaseUseCase {
14
15
  automationRepository;
15
16
  async execute(params) {
16
17
  const { id, currentUser } = params;
17
- const existing = await this.automationRepository?.findById(id);
18
- if (!existing) throw new Error("Automation not found");
19
- if (!currentUser?.isAdmin() && String(existing.ownerId) !== currentUser?.userId) this.throwError403();
18
+ const scope = params.scope ?? Scope.canManage(currentUser);
19
+ const existing = await this.automationRepository?.findById(id, scope);
20
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
20
21
  if (!existing.triggers?.event) throw new Error("Only an automation with an event trigger has tokens");
21
22
  const automation = Automation.fromJSON({ ...existing, eventTokenVersion: existing.eventTokenVersion + 1 });
22
- automation.setUpdatedBy(currentUser);
23
- const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
24
- if (!result) throw new Error("Automation not found or update failed");
23
+ const result = await this.automationRepository?.update(automation, scope, { expectedVersion: existing.__v });
24
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
25
25
  return { data: result };
26
26
  }
27
27
  };
@@ -35,6 +35,8 @@ var import_base = require("../../../common/base/base.use-case.js");
35
35
  var import_repositories = require("../repositories");
36
36
  var import_open_run = require("../utils/open-run");
37
37
  var import_settings = require("../utils/settings");
38
+ var import_runner_scope = require("../utils/runner-scope");
39
+ var import_repositories2 = require("../../../common/repositories/index.js");
38
40
  let StartAutomationRunUseCase = class extends import_base.BaseUseCase {
39
41
  constructor(automationRepository, automationRunRepository) {
40
42
  super();
@@ -44,15 +46,15 @@ let StartAutomationRunUseCase = class extends import_base.BaseUseCase {
44
46
  automationRepository;
45
47
  automationRunRepository;
46
48
  async execute(params) {
47
- const { automationId, workerId } = params;
49
+ const { automationId, workerId, currentUser } = params;
48
50
  if (!automationId || !workerId) throw new Error("The automation and the worker are required");
49
51
  const now = params.now ?? /* @__PURE__ */ new Date();
50
- const existing = await this.automationRepository.findById(automationId);
51
- if (!existing) throw new Error("Automation not found");
52
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? (0, import_settings.leaseMs)() });
52
+ const existing = await this.automationRepository.findById(automationId, params.scope ?? import_repositories2.Scope.canEdit(currentUser));
53
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
54
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? (0, import_settings.leaseMs)() }, import_runner_scope.runnerScope);
53
55
  if (!automation) throw new Error("The automation is already running");
54
- const run = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "manual", status: "running", workerId, now });
55
- await this.automationRepository.setActiveRun(automationId, workerId, String(run._id));
56
+ const run = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.runnerScope, automation, trigger: "manual", status: "running", workerId, now });
57
+ await this.automationRepository.setActiveRun(automationId, workerId, String(run._id), import_runner_scope.runnerScope);
56
58
  return { data: { automation, run: run.toJSON() } };
57
59
  }
58
60
  };
@@ -7,6 +7,8 @@ import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRepository, AutomationRunRepository } from "../repositories";
8
8
  import { openRun } from "../utils/open-run";
9
9
  import { leaseMs as defaultLeaseMs } from "../utils/settings";
10
+ import { runnerScope } from "../utils/runner-scope";
11
+ import { Scope } from "../../../common/repositories/index.js";
10
12
  let StartAutomationRunUseCase = class extends BaseUseCase {
11
13
  constructor(automationRepository, automationRunRepository) {
12
14
  super();
@@ -16,15 +18,15 @@ let StartAutomationRunUseCase = class extends BaseUseCase {
16
18
  automationRepository;
17
19
  automationRunRepository;
18
20
  async execute(params) {
19
- const { automationId, workerId } = params;
21
+ const { automationId, workerId, currentUser } = params;
20
22
  if (!automationId || !workerId) throw new Error("The automation and the worker are required");
21
23
  const now = params.now ?? /* @__PURE__ */ new Date();
22
- const existing = await this.automationRepository.findById(automationId);
23
- if (!existing) throw new Error("Automation not found");
24
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? defaultLeaseMs() });
24
+ const existing = await this.automationRepository.findById(automationId, params.scope ?? Scope.canEdit(currentUser));
25
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
26
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? defaultLeaseMs() }, runnerScope);
25
27
  if (!automation) throw new Error("The automation is already running");
26
- const run = await openRun(this.automationRunRepository, { automation, trigger: "manual", status: "running", workerId, now });
27
- await this.automationRepository.setActiveRun(automationId, workerId, String(run._id));
28
+ const run = await openRun(this.automationRunRepository, { scope: runnerScope, automation, trigger: "manual", status: "running", workerId, now });
29
+ await this.automationRepository.setActiveRun(automationId, workerId, String(run._id), runnerScope);
28
30
  return { data: { automation, run: run.toJSON() } };
29
31
  }
30
32
  };
@@ -36,6 +36,7 @@ var import_base = require("../../../common/base/base.use-case.js");
36
36
  var import_chats = require("../../chats/index.js");
37
37
  var import_entities = require("../entities");
38
38
  var import_repositories = require("../repositories");
39
+ var import_runner_scope = require("../utils/runner-scope");
39
40
  const LOST_WORKER_MESSAGE = "The worker that was running this automation was lost before it finished";
40
41
  let SyncAutomationRunUseCase = class extends import_base.BaseUseCase {
41
42
  constructor(automationRepository, automationRunRepository) {
@@ -47,7 +48,7 @@ let SyncAutomationRunUseCase = class extends import_base.BaseUseCase {
47
48
  automationRunRepository;
48
49
  async execute(params) {
49
50
  const now = params.now ?? /* @__PURE__ */ new Date();
50
- const run = await this.automationRunRepository?.findById(params.runId);
51
+ const run = await this.automationRunRepository?.findById(params.runId, import_runner_scope.runnerScope);
51
52
  if (!run) return { data: null };
52
53
  if (run.status === "paused") return { data: await this.syncPaused(run, now) };
53
54
  if (run.status === "running") return { data: await this.syncRunning(run, now) };
@@ -55,22 +56,22 @@ let SyncAutomationRunUseCase = class extends import_base.BaseUseCase {
55
56
  }
56
57
  async syncPaused(run, now) {
57
58
  if (!run.chatId) return run;
58
- const { data: chat } = await (0, import_chats.getChatById)({ id: String(run.chatId) });
59
+ const { data: chat } = await (0, import_chats.getChatById)({ id: String(run.chatId), scope: import_runner_scope.runnerScope });
59
60
  const status = chat?.state?.status;
60
61
  if (status === "idle") return await this.settle(run, "completed", now);
61
62
  if (status === "failed") return await this.settle(run, "failed", now, { code: chat?.state?.lastError?.code, message: chat?.state?.lastError?.message ?? "The run failed" });
62
63
  return run;
63
64
  }
64
65
  async syncRunning(run, now) {
65
- const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId)) : null;
66
+ const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId), import_runner_scope.runnerScope) : null;
66
67
  const lockedUntil = automation?.lockedUntil ? new Date(automation.lockedUntil).getTime() : 0;
67
68
  if (automation && lockedUntil > now.getTime()) return run;
68
69
  return await this.settle(run, "failed", now, { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE });
69
70
  }
70
71
  async settle(run, status, now, error) {
71
72
  const updated = import_entities.AutomationRun.fromJSON({ ...run, status, finishedAt: now, ...error ? { error } : {} });
72
- const saved = await this.automationRunRepository?.update(updated);
73
- if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now);
73
+ const saved = await this.automationRunRepository?.update(updated, import_runner_scope.runnerScope);
74
+ if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now, import_runner_scope.runnerScope);
74
75
  return saved ?? updated.toJSON();
75
76
  }
76
77
  };
@@ -7,6 +7,7 @@ import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { getChatById } from "../../chats/index.js";
8
8
  import { AutomationRun } from "../entities";
9
9
  import { AutomationRepository, AutomationRunRepository } from "../repositories";
10
+ import { runnerScope } from "../utils/runner-scope";
10
11
  const LOST_WORKER_MESSAGE = "The worker that was running this automation was lost before it finished";
11
12
  let SyncAutomationRunUseCase = class extends BaseUseCase {
12
13
  constructor(automationRepository, automationRunRepository) {
@@ -18,7 +19,7 @@ let SyncAutomationRunUseCase = class extends BaseUseCase {
18
19
  automationRunRepository;
19
20
  async execute(params) {
20
21
  const now = params.now ?? /* @__PURE__ */ new Date();
21
- const run = await this.automationRunRepository?.findById(params.runId);
22
+ const run = await this.automationRunRepository?.findById(params.runId, runnerScope);
22
23
  if (!run) return { data: null };
23
24
  if (run.status === "paused") return { data: await this.syncPaused(run, now) };
24
25
  if (run.status === "running") return { data: await this.syncRunning(run, now) };
@@ -26,22 +27,22 @@ let SyncAutomationRunUseCase = class extends BaseUseCase {
26
27
  }
27
28
  async syncPaused(run, now) {
28
29
  if (!run.chatId) return run;
29
- const { data: chat } = await getChatById({ id: String(run.chatId) });
30
+ const { data: chat } = await getChatById({ id: String(run.chatId), scope: runnerScope });
30
31
  const status = chat?.state?.status;
31
32
  if (status === "idle") return await this.settle(run, "completed", now);
32
33
  if (status === "failed") return await this.settle(run, "failed", now, { code: chat?.state?.lastError?.code, message: chat?.state?.lastError?.message ?? "The run failed" });
33
34
  return run;
34
35
  }
35
36
  async syncRunning(run, now) {
36
- const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId)) : null;
37
+ const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId), runnerScope) : null;
37
38
  const lockedUntil = automation?.lockedUntil ? new Date(automation.lockedUntil).getTime() : 0;
38
39
  if (automation && lockedUntil > now.getTime()) return run;
39
40
  return await this.settle(run, "failed", now, { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE });
40
41
  }
41
42
  async settle(run, status, now, error) {
42
43
  const updated = AutomationRun.fromJSON({ ...run, status, finishedAt: now, ...error ? { error } : {} });
43
- const saved = await this.automationRunRepository?.update(updated);
44
- if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now);
44
+ const saved = await this.automationRunRepository?.update(updated, runnerScope);
45
+ if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now, runnerScope);
45
46
  return saved ?? updated.toJSON();
46
47
  }
47
48
  };
@@ -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_repositories = require("../repositories");
36
36
  var import_sync_automation_run = require("./sync-automation-run.use-case");
37
+ var import_runner_scope = require("../utils/runner-scope");
37
38
  const DEFAULT_LIMIT = 100;
38
39
  let SyncAutomationRunsUseCase = class extends import_base.BaseUseCase {
39
40
  constructor(automationRunRepository, syncRun) {
@@ -46,8 +47,8 @@ let SyncAutomationRunsUseCase = class extends import_base.BaseUseCase {
46
47
  async execute(params) {
47
48
  const limit = params.limit ?? DEFAULT_LIMIT;
48
49
  const open = [
49
- ...await this.automationRunRepository?.findByStatus("paused", limit) ?? [],
50
- ...await this.automationRunRepository?.findByStatus("running", limit) ?? []
50
+ ...await this.automationRunRepository?.findByStatus("paused", limit, import_runner_scope.runnerScope) ?? [],
51
+ ...await this.automationRunRepository?.findByStatus("running", limit, import_runner_scope.runnerScope) ?? []
51
52
  ];
52
53
  let settled = 0;
53
54
  for (const run of open) {
@@ -6,6 +6,7 @@ import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRunRepository } from "../repositories";
8
8
  import { SyncAutomationRunUseCase } from "./sync-automation-run.use-case";
9
+ import { runnerScope } from "../utils/runner-scope";
9
10
  const DEFAULT_LIMIT = 100;
10
11
  let SyncAutomationRunsUseCase = class extends BaseUseCase {
11
12
  constructor(automationRunRepository, syncRun) {
@@ -18,8 +19,8 @@ let SyncAutomationRunsUseCase = class extends BaseUseCase {
18
19
  async execute(params) {
19
20
  const limit = params.limit ?? DEFAULT_LIMIT;
20
21
  const open = [
21
- ...await this.automationRunRepository?.findByStatus("paused", limit) ?? [],
22
- ...await this.automationRunRepository?.findByStatus("running", limit) ?? []
22
+ ...await this.automationRunRepository?.findByStatus("paused", limit, runnerScope) ?? [],
23
+ ...await this.automationRunRepository?.findByStatus("running", limit, runnerScope) ?? []
23
24
  ];
24
25
  let settled = 0;
25
26
  for (const run of open) {