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
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var scope_exports = {};
20
+ __export(scope_exports, {
21
+ Scope: () => Scope
22
+ });
23
+ module.exports = __toCommonJS(scope_exports);
24
+ var import_error = require("../error/index.js");
25
+ class Scope {
26
+ constructor(kind, userId, reason) {
27
+ this.kind = kind;
28
+ this.userId = userId;
29
+ this.reason = reason;
30
+ }
31
+ kind;
32
+ userId;
33
+ reason;
34
+ get trusted() {
35
+ return this.kind === "admin" || this.kind === "system";
36
+ }
37
+ static canView(user) {
38
+ return new Scope("view", Scope.requireUserId(user));
39
+ }
40
+ static canEdit(user) {
41
+ return new Scope("edit", Scope.requireUserId(user));
42
+ }
43
+ static canManage(user) {
44
+ return new Scope("manage", Scope.requireUserId(user));
45
+ }
46
+ static asAdmin(user) {
47
+ const userId = Scope.requireUserId(user);
48
+ if (!user?.isAdmin()) throw new import_error.HttpError({ statusCode: 403, errorMessage: "{{forbidden_error}}: {{forbidden_error_details}}" });
49
+ return new Scope("admin", userId);
50
+ }
51
+ static system(reason) {
52
+ if (!reason?.trim()) throw new Error("[Scope] A reason is required for a system scope");
53
+ return new Scope("system", void 0, reason);
54
+ }
55
+ static requireUserId(user) {
56
+ if (!user?.userId) throw new import_error.HttpError({ statusCode: 401, errorMessage: "{{not_authorized}}" });
57
+ return user.userId;
58
+ }
59
+ }
60
+ // Annotate the CommonJS export names for ESM import in node:
61
+ 0 && (module.exports = {
62
+ Scope
63
+ });
@@ -0,0 +1,40 @@
1
+ import "../../chunk-CF3WOMB3.mjs";
2
+ import { HttpError } from "../error/index.js";
3
+ class Scope {
4
+ constructor(kind, userId, reason) {
5
+ this.kind = kind;
6
+ this.userId = userId;
7
+ this.reason = reason;
8
+ }
9
+ kind;
10
+ userId;
11
+ reason;
12
+ get trusted() {
13
+ return this.kind === "admin" || this.kind === "system";
14
+ }
15
+ static canView(user) {
16
+ return new Scope("view", Scope.requireUserId(user));
17
+ }
18
+ static canEdit(user) {
19
+ return new Scope("edit", Scope.requireUserId(user));
20
+ }
21
+ static canManage(user) {
22
+ return new Scope("manage", Scope.requireUserId(user));
23
+ }
24
+ static asAdmin(user) {
25
+ const userId = Scope.requireUserId(user);
26
+ if (!user?.isAdmin()) throw new HttpError({ statusCode: 403, errorMessage: "{{forbidden_error}}: {{forbidden_error_details}}" });
27
+ return new Scope("admin", userId);
28
+ }
29
+ static system(reason) {
30
+ if (!reason?.trim()) throw new Error("[Scope] A reason is required for a system scope");
31
+ return new Scope("system", void 0, reason);
32
+ }
33
+ static requireUserId(user) {
34
+ if (!user?.userId) throw new HttpError({ statusCode: 401, errorMessage: "{{not_authorized}}" });
35
+ return user.userId;
36
+ }
37
+ }
38
+ export {
39
+ Scope
40
+ };
@@ -1,7 +1,7 @@
1
- import { MongoRepository } from '../../../common/repositories/index.js';
1
+ import { MongoRepository, Scope } from '../../../common/repositories/index.js';
2
2
  import { RevokedToken, RevokedTokenProps } from '../entities';
3
3
  export declare class RevokedTokenRepository {
4
4
  readonly _: MongoRepository<RevokedTokenProps>;
5
- create(token: RevokedToken): Promise<RevokedToken>;
6
- findByJti(jti: string): Promise<RevokedTokenProps | null>;
5
+ create(token: RevokedToken, scope: Scope): Promise<RevokedToken>;
6
+ findByJti(jti: string, scope: Scope): Promise<RevokedTokenProps | null>;
7
7
  }
@@ -35,15 +35,16 @@ var import_database_name = require("../../../common/utils/database-name.js");
35
35
  let RevokedTokenRepository = class {
36
36
  _ = new import_repositories.MongoRepository({
37
37
  database: (0, import_database_name.getDatabaseName)(),
38
- collection: "revoked_tokens"
38
+ collection: "revoked_tokens",
39
+ scoped: true
39
40
  });
40
- async create(token) {
41
- const id = await this._.createOne({ payload: token.toDocument() });
42
- id && token.setId(id);
41
+ async create(token, scope) {
42
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: token.toDocument(), scope });
43
+ Object.assign(token, { _id, ownerId, createdAt, createdBy });
43
44
  return token;
44
45
  }
45
- async findByJti(jti) {
46
- return await this._.findOne({ where: { jti } });
46
+ async findByJti(jti, scope) {
47
+ return await this._.findOne({ scope, where: { jti } });
47
48
  }
48
49
  };
49
50
  RevokedTokenRepository = __decorateClass([
@@ -7,15 +7,16 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
7
7
  let RevokedTokenRepository = class {
8
8
  _ = new MongoRepository({
9
9
  database: getDatabaseName(),
10
- collection: "revoked_tokens"
10
+ collection: "revoked_tokens",
11
+ scoped: true
11
12
  });
12
- async create(token) {
13
- const id = await this._.createOne({ payload: token.toDocument() });
14
- id && token.setId(id);
13
+ async create(token, scope) {
14
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: token.toDocument(), scope });
15
+ Object.assign(token, { _id, ownerId, createdAt, createdBy });
15
16
  return token;
16
17
  }
17
- async findByJti(jti) {
18
- return await this._.findOne({ where: { jti } });
18
+ async findByJti(jti, scope) {
19
+ return await this._.findOne({ scope, where: { jti } });
19
20
  }
20
21
  };
21
22
  RevokedTokenRepository = __decorateClass([
@@ -33,6 +33,7 @@ module.exports = __toCommonJS(is_token_revoked_use_case_exports);
33
33
  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
+ var import_repositories2 = require("../../../common/repositories/index.js");
36
37
  let IsTokenRevokedUseCase = class extends import_base.BaseUseCase {
37
38
  constructor(revokedTokenRepository) {
38
39
  super();
@@ -41,7 +42,7 @@ let IsTokenRevokedUseCase = class extends import_base.BaseUseCase {
41
42
  revokedTokenRepository;
42
43
  async execute(params) {
43
44
  if (!params.jti) throw new Error("The jti is required");
44
- const found = await this.revokedTokenRepository.findByJti(params.jti);
45
+ const found = await this.revokedTokenRepository.findByJti(params.jti, params.scope ?? import_repositories2.Scope.system("revoked tokens are checked by jti, not by owner"));
45
46
  return { data: { revoked: Boolean(found) } };
46
47
  }
47
48
  };
@@ -5,6 +5,7 @@ import {
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { RevokedTokenRepository } from "../repositories";
8
+ import { Scope } from "../../../common/repositories/index.js";
8
9
  let IsTokenRevokedUseCase = class extends BaseUseCase {
9
10
  constructor(revokedTokenRepository) {
10
11
  super();
@@ -13,7 +14,7 @@ let IsTokenRevokedUseCase = class extends BaseUseCase {
13
14
  revokedTokenRepository;
14
15
  async execute(params) {
15
16
  if (!params.jti) throw new Error("The jti is required");
16
- const found = await this.revokedTokenRepository.findByJti(params.jti);
17
+ const found = await this.revokedTokenRepository.findByJti(params.jti, params.scope ?? Scope.system("revoked tokens are checked by jti, not by owner"));
17
18
  return { data: { revoked: Boolean(found) } };
18
19
  }
19
20
  };
@@ -43,6 +43,7 @@ var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
43
43
  var import_tsyringe = require("tsyringe");
44
44
  var import_base = require("../../../common/base/base.use-case.js");
45
45
  var import_entities = require("../../users/entities/index.js");
46
+ var import_repositories2 = require("../../../common/repositories/index.js");
46
47
  let LoginUseCase = class extends import_base.BaseUseCase {
47
48
  constructor(userRepository) {
48
49
  super();
@@ -54,7 +55,7 @@ let LoginUseCase = class extends import_base.BaseUseCase {
54
55
  if (!SECRET_KEY) throw new Error("{{jwt_secret_not_found}}");
55
56
  const { payload } = params;
56
57
  const { emailOrUsername, password } = payload;
57
- const user = await this.userRepository.findByEmailOrUsernameWithPassword(emailOrUsername);
58
+ const user = await this.userRepository.findByEmailOrUsernameWithPassword(emailOrUsername, params.scope ?? import_repositories2.Scope.system("login: the user is not known before the credentials are checked"));
58
59
  if (!user) {
59
60
  return this.throwHttpError({
60
61
  statusCode: 404,
@@ -5,6 +5,7 @@ import jwt from "jsonwebtoken";
5
5
  import { autoInjectable } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { UserRole } from "../../users/entities/index.js";
8
+ import { Scope } from "../../../common/repositories/index.js";
8
9
  let LoginUseCase = class extends BaseUseCase {
9
10
  constructor(userRepository) {
10
11
  super();
@@ -16,7 +17,7 @@ let LoginUseCase = class extends BaseUseCase {
16
17
  if (!SECRET_KEY) throw new Error("{{jwt_secret_not_found}}");
17
18
  const { payload } = params;
18
19
  const { emailOrUsername, password } = payload;
19
- const user = await this.userRepository.findByEmailOrUsernameWithPassword(emailOrUsername);
20
+ const user = await this.userRepository.findByEmailOrUsernameWithPassword(emailOrUsername, params.scope ?? Scope.system("login: the user is not known before the credentials are checked"));
20
21
  if (!user) {
21
22
  return this.throwHttpError({
22
23
  statusCode: 404,
@@ -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_repositories2 = require("../../../common/repositories/index.js");
37
38
  let RevokeTokenUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(revokedTokenRepository) {
39
40
  super();
@@ -43,12 +44,12 @@ let RevokeTokenUseCase = class extends import_base.BaseUseCase {
43
44
  async execute(params) {
44
45
  const { payload, currentUser } = params;
45
46
  if (!payload?.jti || !payload.audience || !payload.subject) throw new Error("The jti, the audience and the subject are required to revoke a token");
46
- const existing = await this.revokedTokenRepository.findByJti(payload.jti);
47
+ const scope = params.scope ?? import_repositories2.Scope.system("revoked tokens are checked by jti, not by owner");
48
+ const existing = await this.revokedTokenRepository.findByJti(payload.jti, scope);
47
49
  if (existing) return { data: existing };
48
- const revoked = import_entities.RevokedToken.fromJSON({ jti: payload.jti, audience: payload.audience, subject: payload.subject, expiresAt: payload.expiresAt });
49
- revoked.setCreatedBy(currentUser);
50
+ const revoked = import_entities.RevokedToken.fromJSON({ jti: payload.jti, audience: payload.audience, subject: payload.subject, expiresAt: payload.expiresAt, createdBy: currentUser?.userId });
50
51
  try {
51
- await this.revokedTokenRepository.create(revoked);
52
+ await this.revokedTokenRepository.create(revoked, scope);
52
53
  } catch (error) {
53
54
  if (error?.code !== 11e3) throw error;
54
55
  }
@@ -6,6 +6,7 @@ import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { RevokedToken } from "../entities";
8
8
  import { RevokedTokenRepository } from "../repositories";
9
+ import { Scope } from "../../../common/repositories/index.js";
9
10
  let RevokeTokenUseCase = class extends BaseUseCase {
10
11
  constructor(revokedTokenRepository) {
11
12
  super();
@@ -15,12 +16,12 @@ let RevokeTokenUseCase = class extends BaseUseCase {
15
16
  async execute(params) {
16
17
  const { payload, currentUser } = params;
17
18
  if (!payload?.jti || !payload.audience || !payload.subject) throw new Error("The jti, the audience and the subject are required to revoke a token");
18
- const existing = await this.revokedTokenRepository.findByJti(payload.jti);
19
+ const scope = params.scope ?? Scope.system("revoked tokens are checked by jti, not by owner");
20
+ const existing = await this.revokedTokenRepository.findByJti(payload.jti, scope);
19
21
  if (existing) return { data: existing };
20
- const revoked = RevokedToken.fromJSON({ jti: payload.jti, audience: payload.audience, subject: payload.subject, expiresAt: payload.expiresAt });
21
- revoked.setCreatedBy(currentUser);
22
+ const revoked = RevokedToken.fromJSON({ jti: payload.jti, audience: payload.audience, subject: payload.subject, expiresAt: payload.expiresAt, createdBy: currentUser?.userId });
22
23
  try {
23
- await this.revokedTokenRepository.create(revoked);
24
+ await this.revokedTokenRepository.create(revoked, scope);
24
25
  } catch (error) {
25
26
  if (error?.code !== 11e3) throw error;
26
27
  }
@@ -1,14 +1,14 @@
1
- import { MongoRepository, QueryOptions } from '../../../common/repositories/index.js';
1
+ import { MongoRepository, QueryOptions, Scope } from '../../../common/repositories/index.js';
2
2
  import { AutomationRun, AutomationRunProps, AutomationRunStatus } from '../entities';
3
3
  export declare class AutomationRunRepository {
4
4
  readonly _: MongoRepository<AutomationRunProps>;
5
- create(run: AutomationRun): Promise<AutomationRun>;
6
- update(run: AutomationRun): Promise<AutomationRunProps | null>;
7
- findById(id: string): Promise<AutomationRunProps | null>;
8
- findByAutomation(automationId: string, options?: QueryOptions): Promise<AutomationRunProps[]>;
9
- findByOwner(ownerId: string, options?: QueryOptions, status?: AutomationRunStatus): Promise<AutomationRunProps[]>;
10
- findByStatus(status: AutomationRunStatus, limit: number): Promise<AutomationRunProps[]>;
11
- findByIdempotencyKey(automationId: string, idempotencyKey: string): Promise<AutomationRunProps | null>;
12
- countQueued(automationId: string): Promise<number>;
13
- claimQueued(id: string, fields: Pick<AutomationRunProps, 'workerId' | 'startedAt' | 'instructions' | 'modelProvider' | 'modelName'>): Promise<AutomationRunProps | null>;
5
+ create(run: AutomationRun, scope: Scope): Promise<AutomationRun>;
6
+ update(run: AutomationRun, scope: Scope): Promise<AutomationRunProps | null>;
7
+ findById(id: string, scope: Scope): Promise<AutomationRunProps | null>;
8
+ findByAutomation(automationId: string, scope: Scope, options?: QueryOptions): Promise<AutomationRunProps[]>;
9
+ findByOwner(ownerId: string, scope: Scope, options?: QueryOptions, status?: AutomationRunStatus): Promise<AutomationRunProps[]>;
10
+ findByStatus(status: AutomationRunStatus, limit: number, scope: Scope): Promise<AutomationRunProps[]>;
11
+ findByIdempotencyKey(automationId: string, idempotencyKey: string, scope: Scope): Promise<AutomationRunProps | null>;
12
+ countQueued(automationId: string, scope: Scope): Promise<number>;
13
+ claimQueued(id: string, fields: Pick<AutomationRunProps, 'workerId' | 'startedAt' | 'instructions' | 'modelProvider' | 'modelName'>, scope: Scope): Promise<AutomationRunProps | null>;
14
14
  }
@@ -36,39 +36,42 @@ var import_database_name = require("../../../common/utils/database-name.js");
36
36
  let AutomationRunRepository = class {
37
37
  _ = new import_repositories.MongoRepository({
38
38
  database: (0, import_database_name.getDatabaseName)(),
39
- collection: "automation_runs"
39
+ collection: "automation_runs",
40
+ scoped: true
40
41
  });
41
- async create(run) {
42
- const id = await this._.createOne({ payload: run.toDocument() });
43
- id && run.setId(id);
42
+ async create(run, scope) {
43
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: run.toDocument(), scope });
44
+ Object.assign(run, { _id, ownerId, createdAt, createdBy });
44
45
  return run;
45
46
  }
46
- async update(run) {
47
+ async update(run, scope) {
47
48
  return await this._.updateOne({
49
+ scope,
48
50
  where: { _id: run._id },
49
51
  payload: run.toFlatDocument()
50
52
  });
51
53
  }
52
- async findById(id) {
53
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
54
+ async findById(id, scope) {
55
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
54
56
  }
55
- async findByAutomation(automationId, options) {
56
- return await this._.findMany({ where: { automationId: new import_mongodb.ObjectId(automationId) }, options });
57
+ async findByAutomation(automationId, scope, options) {
58
+ return await this._.findMany({ scope, where: { automationId: new import_mongodb.ObjectId(automationId) }, options });
57
59
  }
58
- async findByOwner(ownerId, options, status) {
59
- return await this._.findMany({ where: { ownerId: new import_mongodb.ObjectId(ownerId), ...status ? { status } : {} }, options });
60
+ async findByOwner(ownerId, scope, options, status) {
61
+ return await this._.findMany({ scope, where: { ownerId: new import_mongodb.ObjectId(ownerId), ...status ? { status } : {} }, options });
60
62
  }
61
- async findByStatus(status, limit) {
62
- return await this._.findMany({ where: { status }, options: { page: 1, limit, sortBy: { createdAt: 1 } } });
63
+ async findByStatus(status, limit, scope) {
64
+ return await this._.findMany({ scope, where: { status }, options: { page: 1, limit, sortBy: { createdAt: 1 } } });
63
65
  }
64
- async findByIdempotencyKey(automationId, idempotencyKey) {
65
- return await this._.findOne({ where: { automationId: new import_mongodb.ObjectId(automationId), idempotencyKey } });
66
+ async findByIdempotencyKey(automationId, idempotencyKey, scope) {
67
+ return await this._.findOne({ scope, where: { automationId: new import_mongodb.ObjectId(automationId), idempotencyKey } });
66
68
  }
67
- async countQueued(automationId) {
68
- return await this._.count({ where: { automationId: new import_mongodb.ObjectId(automationId), status: "queued" } });
69
+ async countQueued(automationId, scope) {
70
+ return await this._.count({ scope, where: { automationId: new import_mongodb.ObjectId(automationId), status: "queued" } });
69
71
  }
70
- async claimQueued(id, fields) {
72
+ async claimQueued(id, fields, scope) {
71
73
  return await this._.updateOne({
74
+ scope,
72
75
  where: { _id: this._.parseId(id), status: "queued" },
73
76
  payload: { ...fields, status: "running" }
74
77
  });
@@ -8,39 +8,42 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
8
8
  let AutomationRunRepository = class {
9
9
  _ = new MongoRepository({
10
10
  database: getDatabaseName(),
11
- collection: "automation_runs"
11
+ collection: "automation_runs",
12
+ scoped: true
12
13
  });
13
- async create(run) {
14
- const id = await this._.createOne({ payload: run.toDocument() });
15
- id && run.setId(id);
14
+ async create(run, scope) {
15
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: run.toDocument(), scope });
16
+ Object.assign(run, { _id, ownerId, createdAt, createdBy });
16
17
  return run;
17
18
  }
18
- async update(run) {
19
+ async update(run, scope) {
19
20
  return await this._.updateOne({
21
+ scope,
20
22
  where: { _id: run._id },
21
23
  payload: run.toFlatDocument()
22
24
  });
23
25
  }
24
- async findById(id) {
25
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
26
+ async findById(id, scope) {
27
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
26
28
  }
27
- async findByAutomation(automationId, options) {
28
- return await this._.findMany({ where: { automationId: new ObjectId(automationId) }, options });
29
+ async findByAutomation(automationId, scope, options) {
30
+ return await this._.findMany({ scope, where: { automationId: new ObjectId(automationId) }, options });
29
31
  }
30
- async findByOwner(ownerId, options, status) {
31
- return await this._.findMany({ where: { ownerId: new ObjectId(ownerId), ...status ? { status } : {} }, options });
32
+ async findByOwner(ownerId, scope, options, status) {
33
+ return await this._.findMany({ scope, where: { ownerId: new ObjectId(ownerId), ...status ? { status } : {} }, options });
32
34
  }
33
- async findByStatus(status, limit) {
34
- return await this._.findMany({ where: { status }, options: { page: 1, limit, sortBy: { createdAt: 1 } } });
35
+ async findByStatus(status, limit, scope) {
36
+ return await this._.findMany({ scope, where: { status }, options: { page: 1, limit, sortBy: { createdAt: 1 } } });
35
37
  }
36
- async findByIdempotencyKey(automationId, idempotencyKey) {
37
- return await this._.findOne({ where: { automationId: new ObjectId(automationId), idempotencyKey } });
38
+ async findByIdempotencyKey(automationId, idempotencyKey, scope) {
39
+ return await this._.findOne({ scope, where: { automationId: new ObjectId(automationId), idempotencyKey } });
38
40
  }
39
- async countQueued(automationId) {
40
- return await this._.count({ where: { automationId: new ObjectId(automationId), status: "queued" } });
41
+ async countQueued(automationId, scope) {
42
+ return await this._.count({ scope, where: { automationId: new ObjectId(automationId), status: "queued" } });
41
43
  }
42
- async claimQueued(id, fields) {
44
+ async claimQueued(id, fields, scope) {
43
45
  return await this._.updateOne({
46
+ scope,
44
47
  where: { _id: this._.parseId(id), status: "queued" },
45
48
  payload: { ...fields, status: "running" }
46
49
  });
@@ -1,4 +1,4 @@
1
- import { MongoRepository, QueryOptions } from '../../../common/repositories/index.js';
1
+ import { MongoRepository, QueryOptions, Scope } from '../../../common/repositories/index.js';
2
2
  import { Automation, AutomationProps, AutomationRunStatus } from '../entities';
3
3
  export interface ClaimParams {
4
4
  id: string;
@@ -9,19 +9,20 @@ export interface ClaimParams {
9
9
  }
10
10
  export declare class AutomationRepository {
11
11
  readonly _: MongoRepository<AutomationProps>;
12
- create(automation: Automation): Promise<Automation>;
13
- update(automation: Automation, options?: {
12
+ create(automation: Automation, scope: Scope): Promise<Automation>;
13
+ update(automation: Automation, scope: Scope, options?: {
14
14
  expectedVersion?: number;
15
15
  }): Promise<AutomationProps | null>;
16
- findById(id: string): Promise<AutomationProps | null>;
17
- findByOwner(ownerId: string, options?: QueryOptions): Promise<AutomationProps[]>;
18
- deleteById(id: string): Promise<number>;
19
- findDue(now: Date, limit: number): Promise<AutomationProps[]>;
20
- claim(params: ClaimParams): Promise<AutomationProps | null>;
21
- claimManual(params: Omit<ClaimParams, 'nextRunAt'>): Promise<AutomationProps | null>;
22
- setActiveRun(id: string, workerId: string, runId: string): Promise<AutomationProps | null>;
23
- settleRun(id: string, runId: string, status: AutomationRunStatus, at: Date): Promise<AutomationProps | null>;
24
- renewLease(id: string, workerId: string, until: Date): Promise<AutomationProps | null>;
25
- release(id: string, workerId: string, result: Pick<AutomationProps, 'lastRunAt' | 'lastRunStatus' | 'activeRunId'>): Promise<AutomationProps | null>;
16
+ findById(id: string, scope: Scope): Promise<AutomationProps | null>;
17
+ findByOwner(ownerId: string, scope: Scope, options?: QueryOptions): Promise<AutomationProps[]>;
18
+ softDeleteById(id: string, scope: Scope): Promise<AutomationProps | null>;
19
+ deleteById(id: string, scope: Scope): Promise<number>;
20
+ findDue(now: Date, limit: number, scope: Scope): Promise<AutomationProps[]>;
21
+ claim(params: ClaimParams, scope: Scope): Promise<AutomationProps | null>;
22
+ claimManual(params: Omit<ClaimParams, 'nextRunAt'>, scope: Scope): Promise<AutomationProps | null>;
23
+ setActiveRun(id: string, workerId: string, runId: string, scope: Scope): Promise<AutomationProps | null>;
24
+ settleRun(id: string, runId: string, status: AutomationRunStatus, at: Date, scope: Scope): Promise<AutomationProps | null>;
25
+ renewLease(id: string, workerId: string, until: Date, scope: Scope): Promise<AutomationProps | null>;
26
+ release(id: string, workerId: string, result: Pick<AutomationProps, 'lastRunAt' | 'lastRunStatus' | 'activeRunId'>, scope: Scope): Promise<AutomationProps | null>;
26
27
  private unlocked;
27
28
  }
@@ -36,39 +36,46 @@ var import_database_name = require("../../../common/utils/database-name.js");
36
36
  let AutomationRepository = class {
37
37
  _ = new import_repositories.MongoRepository({
38
38
  database: (0, import_database_name.getDatabaseName)(),
39
- collection: "automations"
39
+ collection: "automations",
40
+ scoped: true
40
41
  });
41
- async create(automation) {
42
- const id = await this._.createOne({ payload: automation.toDocument() });
43
- id && automation.setId(id);
42
+ async create(automation, scope) {
43
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: automation.toDocument(), scope });
44
+ Object.assign(automation, { _id, ownerId, createdAt, createdBy });
44
45
  return automation;
45
46
  }
46
- async update(automation, options) {
47
+ async update(automation, scope, options) {
47
48
  if (options && typeof options.expectedVersion !== "number") throw new Error("[AutomationRepository] The expected version is required to check for conflicts");
48
49
  return await this._.updateOne({
50
+ scope,
49
51
  where: { _id: automation._id },
50
52
  payload: automation.toFlatDocument(),
51
53
  expectedVersion: options?.expectedVersion
52
54
  });
53
55
  }
54
- async findById(id) {
55
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
56
+ async findById(id, scope) {
57
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
56
58
  }
57
- async findByOwner(ownerId, options) {
58
- return await this._.findMany({ where: { ownerId: new import_mongodb.ObjectId(ownerId) }, options });
59
+ async findByOwner(ownerId, scope, options) {
60
+ return await this._.findMany({ scope, where: { ownerId: new import_mongodb.ObjectId(ownerId) }, options });
59
61
  }
60
- async deleteById(id) {
61
- return await this._.deleteOne({ where: { _id: this._.parseId(id) } });
62
+ async softDeleteById(id, scope) {
63
+ return await this._.softDelete({ scope, where: { _id: this._.parseId(id) }, payload: { toUnset: { "triggers.schedule.nextRunAt": "" } } });
62
64
  }
63
- async findDue(now, limit) {
65
+ async deleteById(id, scope) {
66
+ return await this._.deleteOne({ scope, where: { _id: this._.parseId(id) } });
67
+ }
68
+ async findDue(now, limit, scope) {
64
69
  return await this._.findMany({
70
+ scope,
65
71
  where: { "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
66
72
  options: { page: 1, limit, sortBy: { "triggers.schedule.nextRunAt": 1 } }
67
73
  });
68
74
  }
69
- async claim(params) {
75
+ async claim(params, scope) {
70
76
  const { id, workerId, now, leaseMs, nextRunAt } = params;
71
77
  return await this._.updateOne({
78
+ scope,
72
79
  where: { _id: this._.parseId(id), "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
73
80
  payload: {
74
81
  lockedBy: workerId,
@@ -77,34 +84,39 @@ let AutomationRepository = class {
77
84
  }
78
85
  });
79
86
  }
80
- async claimManual(params) {
87
+ async claimManual(params, scope) {
81
88
  const { id, workerId, now, leaseMs } = params;
82
89
  return await this._.updateOne({
90
+ scope,
83
91
  where: { _id: this._.parseId(id), ...this.unlocked(now) },
84
92
  payload: { lockedBy: workerId, lockedUntil: new Date(now.getTime() + leaseMs) }
85
93
  });
86
94
  }
87
- async setActiveRun(id, workerId, runId) {
95
+ async setActiveRun(id, workerId, runId, scope) {
88
96
  return await this._.updateOne({
97
+ scope,
89
98
  where: { _id: this._.parseId(id), lockedBy: workerId },
90
99
  payload: { activeRunId: this._.parseId(runId) }
91
100
  });
92
101
  }
93
- async settleRun(id, runId, status, at) {
102
+ async settleRun(id, runId, status, at, scope) {
94
103
  return await this._.updateOne({
104
+ scope,
95
105
  where: { _id: this._.parseId(id), activeRunId: this._.parseId(runId) },
96
106
  payload: { lastRunStatus: status, lastRunAt: at, toUnset: { activeRunId: "" } }
97
107
  });
98
108
  }
99
- async renewLease(id, workerId, until) {
109
+ async renewLease(id, workerId, until, scope) {
100
110
  return await this._.updateOne({
111
+ scope,
101
112
  where: { _id: this._.parseId(id), lockedBy: workerId },
102
113
  payload: { lockedUntil: until }
103
114
  });
104
115
  }
105
- async release(id, workerId, result) {
116
+ async release(id, workerId, result, scope) {
106
117
  const { activeRunId, lastRunAt, lastRunStatus } = result;
107
118
  return await this._.updateOne({
119
+ scope,
108
120
  where: { _id: this._.parseId(id), lockedBy: workerId },
109
121
  payload: {
110
122
  ...lastRunAt ? { lastRunAt } : {},