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