openloops 0.5.0 → 0.7.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 +60 -0
- package/README.md +3 -2
- package/dist/clients/llm/llm-client.mjs +0 -1
- package/dist/clients/llm/llm-error.mjs +0 -1
- package/dist/clients/llm/llm-types.mjs +0 -1
- package/dist/clients/llm/model-resolution.mjs +0 -1
- package/dist/clients/llm/pi-ai-adapter.mjs +0 -1
- package/dist/clients/llm/sampling-rules.mjs +0 -1
- package/dist/clients/serper/serper-client.mjs +0 -1
- package/dist/clients/slack/slack-client.mjs +0 -1
- package/dist/common/base/base.app.d.ts +3 -3
- package/dist/common/base/base.app.mjs +0 -1
- package/dist/common/base/base.entity.d.ts +7 -25
- package/dist/common/base/base.entity.js +26 -133
- package/dist/common/base/base.entity.mjs +26 -128
- package/dist/common/base/base.use-case.d.ts +3 -7
- package/dist/common/base/base.use-case.mjs +0 -1
- package/dist/common/base/current-session.mjs +0 -1
- package/dist/common/base/current-user.mjs +0 -1
- 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 +20 -0
- package/dist/common/base/utils/index.d.ts +1 -2
- package/dist/common/base/utils/index.js +2 -4
- package/dist/common/base/utils/index.mjs +1 -2
- package/dist/common/base/utils/is-plain-object.mjs +0 -1
- package/dist/common/base/utils/to-patch.d.ts +9 -0
- package/dist/common/base/utils/to-patch.js +46 -0
- package/dist/common/base/utils/to-patch.mjs +21 -0
- package/dist/common/constants/harness.mjs +0 -1
- package/dist/common/error/http-error.mjs +0 -1
- package/dist/common/error/reportError.mjs +0 -1
- package/dist/common/error/sentry-client.mjs +0 -1
- package/dist/common/helpers/escape-regex.mjs +0 -1
- package/dist/common/helpers/extract-json-from-text.mjs +0 -1
- package/dist/common/helpers/sanitize-string.mjs +0 -1
- package/dist/common/logger/pino.mjs +0 -1
- 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.client.mjs +0 -1
- package/dist/common/repositories/mongo-db.repository.d.ts +42 -11
- package/dist/common/repositories/mongo-db.repository.js +152 -42
- package/dist/common/repositories/mongo-db.repository.mjs +152 -43
- package/dist/common/repositories/scope.d.ts +19 -0
- package/dist/common/repositories/scope.js +63 -0
- package/dist/common/repositories/scope.mjs +39 -0
- package/dist/common/utils/database-name.mjs +0 -1
- package/dist/common/utils/number-setting.mjs +0 -1
- package/dist/common/utils/secret-manager.mjs +0 -1
- package/dist/db/indexes/agent-actions.mjs +0 -1
- package/dist/db/indexes/agent-tasks.mjs +0 -1
- package/dist/db/indexes/agent-traces.mjs +0 -1
- package/dist/db/indexes/automation-runs.mjs +0 -1
- package/dist/db/indexes/automations.mjs +0 -1
- package/dist/db/indexes/chats.mjs +0 -1
- package/dist/db/indexes/index.js +3 -1
- package/dist/db/indexes/index.mjs +3 -2
- package/dist/db/indexes/llm-calls.mjs +0 -1
- package/dist/db/indexes/mcp-servers.mjs +0 -1
- package/dist/db/indexes/refresh-tokens.d.ts +2 -0
- package/dist/{common/decorators/admin-only/index.js → db/indexes/refresh-tokens.js} +16 -5
- package/dist/db/indexes/refresh-tokens.mjs +9 -0
- package/dist/db/indexes/revoked-tokens.mjs +0 -1
- package/dist/db/indexes/workspace-items.mjs +0 -1
- package/dist/db/migrations/automations.mjs +0 -1
- package/dist/guardrails/guard.mjs +0 -1
- package/dist/guardrails/index.mjs +0 -1
- package/dist/guardrails/input/block-card.mjs +0 -1
- package/dist/guardrails/input/block-code.mjs +0 -1
- package/dist/guardrails/input/block-email.mjs +0 -1
- package/dist/guardrails/input/create-block-input-oversize.mjs +0 -1
- package/dist/guardrails/input/diagnostic-sanity.mjs +0 -1
- package/dist/guardrails/input/input-check.mjs +0 -1
- package/dist/harness/agent-loop.mjs +0 -1
- package/dist/harness/agent-state.mjs +0 -1
- package/dist/harness/agent.d.ts +0 -1
- package/dist/harness/agent.js +14 -14
- package/dist/harness/agent.mjs +14 -15
- package/dist/harness/native-tools.mjs +0 -1
- package/dist/harness/skills/approval-request.mjs +0 -1
- package/dist/harness/skills/autonomous-answer.mjs +0 -1
- package/dist/harness/skills/confirmation-gate.mjs +0 -1
- package/dist/harness/skills/mcp-change-review.mjs +0 -1
- package/dist/harness/tool-catalog.mjs +0 -1
- package/dist/harness/tool-pipeline.mjs +0 -1
- package/dist/harness/utils/activity.mjs +0 -1
- package/dist/harness/utils/describe-shape.mjs +0 -1
- package/dist/harness/utils/grounding.mjs +0 -1
- package/dist/harness/utils/rank-tools-by-keywords.mjs +0 -1
- package/dist/harness/workspace.js +7 -7
- package/dist/harness/workspace.mjs +7 -8
- package/dist/loops/agi/loop.mjs +0 -1
- package/dist/loops/agi/skills/reasoning-engine.mjs +0 -1
- package/dist/loops/shared/skills/fast-responder.mjs +0 -1
- package/dist/services/authentication/entities/index.d.ts +1 -0
- package/dist/services/authentication/entities/index.js +3 -1
- package/dist/services/authentication/entities/index.mjs +1 -0
- package/dist/services/authentication/entities/refresh-token.entity.d.ts +25 -0
- package/dist/services/authentication/entities/refresh-token.entity.js +75 -0
- package/dist/services/authentication/entities/refresh-token.entity.mjs +51 -0
- package/dist/services/authentication/entities/revoked-token.entity.mjs +0 -1
- package/dist/services/authentication/helpers/auth-config.d.ts +9 -0
- package/dist/{common/decorators/owner-only/index.js → services/authentication/helpers/auth-config.js} +19 -5
- package/dist/services/authentication/helpers/auth-config.mjs +12 -0
- package/dist/services/authentication/helpers/session-tokens.d.ts +16 -0
- package/dist/services/authentication/helpers/session-tokens.js +60 -0
- package/dist/services/authentication/helpers/session-tokens.mjs +35 -0
- package/dist/services/authentication/helpers/tokens.d.ts +43 -0
- package/dist/services/authentication/helpers/tokens.js +113 -0
- package/dist/services/authentication/helpers/tokens.mjs +71 -0
- package/dist/services/authentication/index.d.ts +3 -0
- package/dist/services/authentication/index.js +23 -0
- package/dist/services/authentication/index.mjs +11 -0
- package/dist/services/authentication/module.d.ts +12 -3
- package/dist/services/authentication/module.js +40 -7
- package/dist/services/authentication/module.mjs +34 -7
- package/dist/services/authentication/repositories/index.d.ts +1 -0
- package/dist/services/authentication/repositories/index.js +3 -1
- package/dist/services/authentication/repositories/index.mjs +1 -0
- package/dist/services/authentication/repositories/refresh-token.repository.d.ts +10 -0
- package/dist/services/authentication/repositories/refresh-token.repository.js +60 -0
- package/dist/services/authentication/repositories/refresh-token.repository.mjs +36 -0
- package/dist/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
- package/dist/services/authentication/repositories/revoked-token.repository.js +9 -20
- package/dist/services/authentication/repositories/revoked-token.repository.mjs +9 -15
- package/dist/services/authentication/use-cases/claim-refresh-token.use-case.d.ts +12 -0
- package/dist/services/authentication/use-cases/claim-refresh-token.use-case.js +40 -0
- package/dist/services/authentication/use-cases/claim-refresh-token.use-case.mjs +16 -0
- package/dist/services/authentication/use-cases/create-refresh-token.use-case.d.ts +11 -0
- package/dist/services/authentication/use-cases/create-refresh-token.use-case.js +43 -0
- package/dist/services/authentication/use-cases/create-refresh-token.use-case.mjs +19 -0
- package/dist/services/authentication/use-cases/find-refresh-token-by-hash.use-case.d.ts +11 -0
- package/dist/services/authentication/use-cases/find-refresh-token-by-hash.use-case.js +40 -0
- package/dist/services/authentication/use-cases/find-refresh-token-by-hash.use-case.mjs +16 -0
- package/dist/services/authentication/use-cases/is-token-revoked.use-case.d.ts +6 -8
- package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +6 -19
- package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +6 -14
- package/dist/services/authentication/use-cases/issue-password-reset-token.use-case.d.ts +18 -0
- package/dist/services/authentication/use-cases/issue-password-reset-token.use-case.js +49 -0
- package/dist/services/authentication/use-cases/issue-password-reset-token.use-case.mjs +25 -0
- package/dist/services/authentication/use-cases/issue-session.use-case.d.ts +13 -0
- package/dist/services/authentication/use-cases/issue-session.use-case.js +48 -0
- package/dist/services/authentication/use-cases/issue-session.use-case.mjs +24 -0
- package/dist/services/authentication/use-cases/login.use-case.d.ts +11 -11
- package/dist/services/authentication/use-cases/login.use-case.js +18 -53
- package/dist/services/authentication/use-cases/login.use-case.mjs +18 -38
- package/dist/services/authentication/use-cases/logout.use-case.d.ts +14 -0
- package/dist/services/authentication/use-cases/logout.use-case.js +48 -0
- package/dist/services/authentication/use-cases/logout.use-case.mjs +24 -0
- package/dist/services/authentication/use-cases/refresh.use-case.d.ts +23 -0
- package/dist/services/authentication/use-cases/refresh.use-case.js +84 -0
- package/dist/services/authentication/use-cases/refresh.use-case.mjs +60 -0
- package/dist/services/authentication/use-cases/reset-password.use-case.d.ts +15 -0
- package/dist/services/authentication/use-cases/reset-password.use-case.js +61 -0
- package/dist/services/authentication/use-cases/reset-password.use-case.mjs +37 -0
- package/dist/services/authentication/use-cases/revoke-refresh-token-family.use-case.d.ts +11 -0
- package/dist/services/authentication/use-cases/revoke-refresh-token-family.use-case.js +40 -0
- package/dist/services/authentication/use-cases/revoke-refresh-token-family.use-case.mjs +16 -0
- package/dist/services/authentication/use-cases/revoke-token.use-case.d.ts +4 -6
- package/dist/services/authentication/use-cases/revoke-token.use-case.js +12 -25
- package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +11 -19
- package/dist/services/authentication/use-cases/revoke-user-refresh-tokens.use-case.d.ts +13 -0
- package/dist/services/authentication/use-cases/revoke-user-refresh-tokens.use-case.js +40 -0
- package/dist/services/authentication/use-cases/revoke-user-refresh-tokens.use-case.mjs +16 -0
- package/dist/services/automations/entities/automation-run.entity.mjs +0 -1
- package/dist/services/automations/entities/automation.entity.d.ts +1 -0
- package/dist/services/automations/entities/automation.entity.js +2 -1
- package/dist/services/automations/entities/automation.entity.mjs +2 -2
- package/dist/services/automations/entities/recurrence.mjs +0 -1
- package/dist/services/automations/entities/trigger.mjs +0 -1
- package/dist/services/automations/module.d.ts +0 -1
- package/dist/services/automations/module.js +20 -22
- package/dist/services/automations/module.mjs +20 -23
- package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
- package/dist/services/automations/repositories/automation-run.repository.js +26 -38
- package/dist/services/automations/repositories/automation-run.repository.mjs +26 -33
- package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
- package/dist/services/automations/repositories/automation.repository.js +52 -54
- package/dist/services/automations/repositories/automation.repository.mjs +52 -49
- package/dist/services/automations/runner/automation-runner.d.ts +3 -0
- package/dist/services/automations/runner/automation-runner.js +8 -9
- package/dist/services/automations/runner/automation-runner.mjs +8 -10
- package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.d.ts +4 -6
- package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +8 -21
- package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +8 -16
- package/dist/services/automations/use-cases/claim-due-automations.use-case.d.ts +9 -11
- package/dist/services/automations/use-cases/claim-due-automations.use-case.js +11 -26
- package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +11 -21
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.d.ts +9 -11
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +13 -28
- package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +13 -23
- package/dist/services/automations/use-cases/create-automation.use-case.d.ts +4 -6
- package/dist/services/automations/use-cases/create-automation.use-case.js +7 -22
- package/dist/services/automations/use-cases/create-automation.use-case.mjs +6 -16
- package/dist/services/automations/use-cases/delete-automation.use-case.d.ts +6 -6
- package/dist/services/automations/use-cases/delete-automation.use-case.js +10 -27
- package/dist/services/automations/use-cases/delete-automation.use-case.mjs +9 -21
- package/dist/services/automations/use-cases/finish-automation-run.use-case.d.ts +5 -7
- package/dist/services/automations/use-cases/finish-automation-run.use-case.js +9 -23
- package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +9 -18
- package/dist/services/automations/use-cases/get-automation-by-id.use-case.d.ts +4 -6
- package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +8 -21
- package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +7 -15
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.d.ts +7 -9
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +8 -22
- package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +7 -16
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.d.ts +6 -6
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +8 -20
- package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +8 -15
- package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.d.ts +6 -6
- package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +7 -19
- package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +7 -14
- package/dist/services/automations/use-cases/list-automations-by-user.use-case.d.ts +6 -6
- package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +7 -19
- package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +7 -14
- package/dist/services/automations/use-cases/preview-automation-runs.use-case.d.ts +4 -6
- package/dist/services/automations/use-cases/preview-automation-runs.use-case.js +3 -15
- package/dist/services/automations/use-cases/preview-automation-runs.use-case.mjs +3 -10
- package/dist/services/automations/use-cases/receive-automation-event.use-case.d.ts +8 -10
- package/dist/services/automations/use-cases/receive-automation-event.use-case.js +15 -29
- package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +15 -24
- package/dist/services/automations/use-cases/renew-automation-lease.use-case.d.ts +6 -8
- package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +6 -19
- package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +6 -14
- package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.d.ts +6 -8
- package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +10 -23
- package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +10 -18
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.d.ts +4 -6
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +11 -25
- package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +10 -19
- package/dist/services/automations/use-cases/start-automation-run.use-case.d.ts +8 -10
- package/dist/services/automations/use-cases/start-automation-run.use-case.js +12 -25
- package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +12 -20
- package/dist/services/automations/use-cases/sync-automation-run.use-case.d.ts +5 -7
- package/dist/services/automations/use-cases/sync-automation-run.use-case.js +13 -27
- package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +13 -22
- package/dist/services/automations/use-cases/sync-automation-runs.use-case.d.ts +8 -10
- package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +8 -22
- package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +8 -17
- package/dist/services/automations/use-cases/update-automation.use-case.d.ts +4 -6
- package/dist/services/automations/use-cases/update-automation.use-case.js +13 -26
- package/dist/services/automations/use-cases/update-automation.use-case.mjs +12 -20
- package/dist/services/automations/utils/automation-event-error.mjs +0 -1
- package/dist/services/automations/utils/automation-fields.mjs +0 -1
- package/dist/services/automations/utils/event-message.mjs +0 -1
- package/dist/services/automations/utils/event-token.d.ts +1 -2
- package/dist/services/automations/utils/event-token.js +9 -35
- package/dist/services/automations/utils/event-token.mjs +9 -25
- package/dist/services/automations/utils/next-run.mjs +0 -1
- 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 -3
- package/dist/services/automations/utils/previous-run.js +4 -3
- package/dist/services/automations/utils/previous-run.mjs +4 -4
- package/dist/services/automations/utils/runner-scope.d.ts +3 -0
- package/dist/{common/base/utils/flatten-object.js → services/automations/utils/runner-scope.js} +10 -23
- package/dist/services/automations/utils/runner-scope.mjs +7 -0
- package/dist/services/automations/utils/settings.mjs +0 -1
- package/dist/services/chats/entities/chat-message.entity.mjs +0 -1
- package/dist/services/chats/entities/chat-settings.entity.mjs +0 -1
- package/dist/services/chats/entities/chat.entity.d.ts +1 -0
- package/dist/services/chats/entities/chat.entity.js +3 -2
- package/dist/services/chats/entities/chat.entity.mjs +3 -3
- package/dist/services/chats/module.d.ts +0 -1
- package/dist/services/chats/module.js +5 -7
- package/dist/services/chats/module.mjs +5 -8
- package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
- package/dist/services/chats/repositories/chat.repository.js +19 -29
- package/dist/services/chats/repositories/chat.repository.mjs +19 -24
- package/dist/services/chats/use-cases/create-chat.use-case.d.ts +4 -6
- package/dist/services/chats/use-cases/create-chat.use-case.js +7 -22
- package/dist/services/chats/use-cases/create-chat.use-case.mjs +6 -16
- package/dist/services/chats/use-cases/delete-chat.use-case.d.ts +6 -6
- package/dist/services/chats/use-cases/delete-chat.use-case.js +10 -26
- package/dist/services/chats/use-cases/delete-chat.use-case.mjs +9 -20
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.d.ts +4 -6
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +8 -21
- package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +8 -16
- package/dist/services/chats/use-cases/interrupt-execution.use-case.d.ts +4 -6
- package/dist/services/chats/use-cases/interrupt-execution.use-case.js +8 -24
- package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +7 -18
- package/dist/services/chats/use-cases/list-chats-by-props.use-case.d.ts +6 -6
- package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +7 -20
- package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +7 -15
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.d.ts +6 -6
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +7 -19
- package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +7 -14
- package/dist/services/chats/use-cases/update-chat.use-case.d.ts +5 -6
- package/dist/services/chats/use-cases/update-chat.use-case.js +10 -30
- package/dist/services/chats/use-cases/update-chat.use-case.mjs +9 -24
- package/dist/services/chats/utils/deprecated-activity.mjs +0 -1
- package/dist/services/llmcalls/entities/llmcall.entity.mjs +0 -1
- package/dist/services/llmcalls/module.d.ts +0 -1
- package/dist/services/llmcalls/module.js +4 -6
- package/dist/services/llmcalls/module.mjs +4 -7
- package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
- package/dist/services/llmcalls/repositories/llmcall.repository.js +19 -29
- package/dist/services/llmcalls/repositories/llmcall.repository.mjs +19 -24
- package/dist/services/llmcalls/use-cases/create-llmcall.use-case.d.ts +4 -6
- package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +7 -22
- package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +6 -16
- package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.d.ts +6 -6
- package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +10 -26
- package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +9 -20
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.d.ts +4 -6
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +8 -21
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +7 -15
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.d.ts +4 -6
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +8 -21
- package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +7 -15
- package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.d.ts +4 -6
- package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +6 -19
- package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +6 -14
- package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.d.ts +4 -6
- package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +9 -21
- package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +8 -15
- package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.d.ts +6 -6
- package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +7 -20
- package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +7 -15
- package/dist/services/llmcalls/use-cases/update-llmcall.use-case.d.ts +4 -6
- package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +9 -25
- package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +8 -19
- package/dist/services/llmcalls/utils/usage-periods.mjs +0 -1
- package/dist/services/llmcalls/utils/usage-totals.mjs +0 -1
- package/dist/services/mcp-servers/entities/mcp-manifest.mjs +0 -1
- package/dist/services/mcp-servers/entities/mcpserver.entity.mjs +0 -1
- package/dist/services/mcp-servers/module.d.ts +0 -1
- package/dist/services/mcp-servers/module.js +8 -10
- package/dist/services/mcp-servers/module.mjs +8 -11
- package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
- package/dist/services/mcp-servers/repositories/mcpserver.repository.js +21 -31
- package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +21 -26
- package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.d.ts +5 -7
- package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +7 -24
- package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +6 -18
- package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.d.ts +4 -6
- package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +11 -26
- package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +10 -20
- package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.d.ts +6 -6
- package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +10 -26
- package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +9 -20
- package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.d.ts +4 -6
- package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +8 -21
- package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +7 -15
- package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.d.ts +6 -6
- package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +7 -20
- package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +6 -14
- package/dist/services/mcp-servers/use-cases/list-mcpservers-by-user.use-case.d.ts +6 -6
- package/dist/services/mcp-servers/use-cases/list-mcpservers-by-user.use-case.js +5 -19
- package/dist/services/mcp-servers/use-cases/list-mcpservers-by-user.use-case.mjs +5 -14
- package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.d.ts +5 -7
- package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +12 -31
- package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +11 -25
- package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.d.ts +4 -6
- package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +13 -27
- package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +12 -21
- package/dist/services/tasks/entities/agent-action.entity.mjs +0 -1
- package/dist/services/tasks/entities/agent-task.entity.mjs +0 -1
- package/dist/services/tasks/module.d.ts +0 -1
- package/dist/services/tasks/module.js +7 -9
- package/dist/services/tasks/module.mjs +7 -10
- package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
- package/dist/services/tasks/repositories/agent-action.repository.js +12 -26
- package/dist/services/tasks/repositories/agent-action.repository.mjs +12 -21
- package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
- package/dist/services/tasks/repositories/agent-task.repository.js +14 -28
- package/dist/services/tasks/repositories/agent-task.repository.mjs +14 -23
- package/dist/services/tasks/use-cases/create-agent-action.use-case.d.ts +4 -6
- package/dist/services/tasks/use-cases/create-agent-action.use-case.js +7 -22
- package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +6 -16
- package/dist/services/tasks/use-cases/create-agent-task.use-case.d.ts +4 -6
- package/dist/services/tasks/use-cases/create-agent-task.use-case.js +7 -22
- package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +6 -16
- package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.d.ts +4 -6
- package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +8 -21
- package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +7 -15
- package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.d.ts +6 -6
- package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +8 -20
- package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +8 -15
- package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.d.ts +6 -6
- package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +8 -20
- package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +8 -15
- package/dist/services/tasks/use-cases/update-agent-action.use-case.d.ts +4 -6
- package/dist/services/tasks/use-cases/update-agent-action.use-case.js +8 -24
- package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +7 -18
- package/dist/services/tasks/use-cases/update-agent-task.use-case.d.ts +4 -6
- package/dist/services/tasks/use-cases/update-agent-task.use-case.js +8 -24
- package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +7 -18
- package/dist/services/traces/entities/agent-trace-entry.entity.mjs +0 -1
- package/dist/services/traces/module.d.ts +0 -1
- package/dist/services/traces/module.js +2 -4
- package/dist/services/traces/module.mjs +2 -5
- package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
- package/dist/services/traces/repositories/agent-trace.repository.js +8 -19
- package/dist/services/traces/repositories/agent-trace.repository.mjs +8 -14
- package/dist/services/traces/use-cases/create-agent-traces.use-case.d.ts +6 -8
- package/dist/services/traces/use-cases/create-agent-traces.use-case.js +8 -26
- package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +7 -20
- package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.d.ts +6 -6
- package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +8 -20
- package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +8 -15
- package/dist/services/users/entities/user-acquisition.js +4 -17
- package/dist/services/users/entities/user-acquisition.mjs +4 -12
- package/dist/services/users/entities/user-country.js +2 -2
- package/dist/services/users/entities/user-country.mjs +2 -3
- package/dist/services/users/entities/user-currency.js +2 -2
- package/dist/services/users/entities/user-currency.mjs +2 -3
- package/dist/services/users/entities/user-default-model.js +2 -2
- package/dist/services/users/entities/user-default-model.mjs +2 -3
- package/dist/services/users/entities/user-email.value-object.mjs +0 -1
- package/dist/services/users/entities/user-language.js +2 -2
- package/dist/services/users/entities/user-language.mjs +2 -3
- package/dist/services/users/entities/user-marketing.js +4 -17
- package/dist/services/users/entities/user-marketing.mjs +4 -12
- package/dist/services/users/entities/user-notification-preferences.mjs +0 -1
- package/dist/services/users/entities/user-password.value-object.mjs +0 -1
- package/dist/services/users/entities/user-permissions.mjs +0 -1
- package/dist/services/users/entities/user-preferences.mjs +0 -1
- package/dist/services/users/entities/user-report-preferences.mjs +0 -1
- package/dist/services/users/entities/user-report-schedule.mjs +0 -1
- package/dist/services/users/entities/user-timezone.js +2 -2
- package/dist/services/users/entities/user-timezone.mjs +2 -3
- package/dist/services/users/entities/user.entity.d.ts +1 -0
- package/dist/services/users/entities/user.entity.js +9 -25
- package/dist/services/users/entities/user.entity.mjs +9 -20
- package/dist/services/users/module.d.ts +2 -1
- package/dist/services/users/module.js +12 -7
- package/dist/services/users/module.mjs +11 -8
- package/dist/services/users/repositories/user.repository.d.ts +10 -7
- package/dist/services/users/repositories/user.repository.js +34 -29
- package/dist/services/users/repositories/user.repository.mjs +34 -24
- package/dist/services/users/use-cases/change-password.use-case.d.ts +15 -0
- package/dist/services/users/use-cases/change-password.use-case.js +52 -0
- package/dist/services/users/use-cases/change-password.use-case.mjs +28 -0
- package/dist/services/users/use-cases/create-user.use-case.d.ts +4 -7
- package/dist/services/users/use-cases/create-user.use-case.js +8 -22
- package/dist/services/users/use-cases/create-user.use-case.mjs +7 -16
- package/dist/services/users/use-cases/delete-user.use-case.d.ts +6 -6
- package/dist/services/users/use-cases/delete-user.use-case.js +10 -26
- package/dist/services/users/use-cases/delete-user.use-case.mjs +9 -20
- package/dist/services/users/use-cases/get-user-by-id.use-case.d.ts +4 -6
- package/dist/services/users/use-cases/get-user-by-id.use-case.js +9 -21
- package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +8 -15
- package/dist/services/users/use-cases/list-users-by-props.use-case.d.ts +6 -6
- package/dist/services/users/use-cases/list-users-by-props.use-case.js +7 -20
- package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +7 -15
- package/dist/services/users/use-cases/update-user.use-case.d.ts +4 -6
- package/dist/services/users/use-cases/update-user.use-case.js +11 -26
- package/dist/services/users/use-cases/update-user.use-case.mjs +10 -20
- package/dist/services/users/utils/default-model.mjs +0 -1
- package/dist/services/workspaces/entities/workspace-item.entity.mjs +0 -1
- package/dist/services/workspaces/module.d.ts +0 -1
- package/dist/services/workspaces/module.js +5 -7
- package/dist/services/workspaces/module.mjs +5 -8
- package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
- package/dist/services/workspaces/repositories/workspace-item.repository.js +20 -32
- package/dist/services/workspaces/repositories/workspace-item.repository.mjs +20 -27
- package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.d.ts +6 -8
- package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +11 -22
- package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +10 -16
- package/dist/services/workspaces/use-cases/get-workspace-item.use-case.d.ts +4 -6
- package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +8 -21
- package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +7 -15
- package/dist/services/workspaces/use-cases/list-workspace-items.use-case.d.ts +6 -6
- package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +7 -19
- package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +7 -14
- package/dist/services/workspaces/use-cases/save-workspace-item.use-case.d.ts +4 -6
- package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +14 -28
- package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +13 -22
- package/dist/services/workspaces/use-cases/search-workspace-items.use-case.d.ts +6 -6
- package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +7 -19
- package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +7 -14
- package/dist/skills/call-modes.mjs +0 -1
- package/dist/skills/runtime-context.mjs +0 -1
- package/dist/skills/shared/shared-rules.mjs +0 -1
- package/dist/skills/skill.js +1 -2
- package/dist/skills/skill.mjs +1 -3
- package/dist/tools/core/call-ai/call-ai.skill.mjs +0 -1
- package/dist/tools/core/call-ai/call-ai.tool.mjs +0 -1
- package/dist/tools/core/slack/send-slack-message.tool.mjs +0 -1
- package/dist/tools/core/web-search/web-search.tool.mjs +0 -1
- package/dist/tools/tool.mjs +0 -1
- package/docs/README.md +2 -1
- package/docs/authentication.md +95 -16
- package/docs/authorization.md +77 -0
- package/docs/automations.md +10 -9
- package/docs/chats.md +1 -1
- package/docs/configuration.md +1 -1
- package/docs/integration-guide.md +9 -11
- package/docs/llms.txt +1 -0
- package/docs/mcp-servers.md +1 -1
- package/docs/usage.md +5 -5
- package/docs/users.md +7 -5
- package/examples/authentication/login.ts +1 -1
- package/examples/automations/create-automation.ts +1 -1
- package/examples/automations/create-event-automation.ts +2 -2
- package/examples/automations/list-automation-runs.ts +4 -2
- package/examples/automations/list-automations.ts +1 -1
- package/examples/automations/preview-automation-runs.ts +1 -1
- package/examples/automations/receive-automation-event.ts +1 -1
- package/examples/automations/revoke-automation-event-token.ts +2 -2
- package/examples/automations/run-automation-now.ts +3 -1
- package/examples/automations/update-automation.ts +2 -2
- package/examples/chats/delete-chat.ts +1 -1
- package/examples/chats/get-chat-by-id.ts +2 -2
- package/examples/chats/list-autonomous-chats.ts +1 -1
- package/examples/chats/list-chats.ts +1 -1
- package/examples/mcps/decide-mcp.ts +3 -3
- package/examples/mcps/list-all-mcps.ts +1 -1
- package/examples/mcps/verify-mcp.ts +1 -1
- package/examples/usage/get-chat-usage.ts +2 -2
- package/examples/usage/get-message-usage.ts +2 -2
- package/examples/usage/get-usage-stats-by-range.ts +1 -1
- package/examples/usage/get-usage-stats.ts +3 -3
- package/examples/users/create-user.ts +2 -1
- package/examples/users/list-users.ts +1 -0
- package/examples/users/set-default-model.ts +1 -1
- package/package.json +2 -4
- package/dist/chunk-CF3WOMB3.mjs +0 -16
- package/dist/common/base/utils/delete-empty-fields.d.ts +0 -1
- package/dist/common/base/utils/delete-empty-fields.js +0 -66
- package/dist/common/base/utils/delete-empty-fields.mjs +0 -43
- package/dist/common/base/utils/flatten-object.d.ts +0 -1
- package/dist/common/base/utils/flatten-object.mjs +0 -22
- 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/index.d.ts +0 -3
- package/dist/common/decorators/index.js +0 -26
- package/dist/common/decorators/index.mjs +0 -3
- package/dist/common/decorators/mask-in-logs/index.d.ts +0 -1
- package/dist/common/decorators/mask-in-logs/index.js +0 -22
- package/dist/common/decorators/mask-in-logs/index.mjs +0 -1
- package/dist/common/decorators/mask-in-logs/mask-in-logs.d.ts +0 -2
- package/dist/common/decorators/mask-in-logs/mask-in-logs.js +0 -53
- package/dist/common/decorators/mask-in-logs/mask-in-logs.mjs +0 -29
- package/dist/common/decorators/owner-only/index.d.ts +0 -1
- 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
|
@@ -26,6 +26,8 @@ var import_mongodb = require("mongodb");
|
|
|
26
26
|
var import_error = require("../error/index.js");
|
|
27
27
|
var import_mongo_db = require("./mongo-db.client");
|
|
28
28
|
const DEFAULT_LIMIT = 10;
|
|
29
|
+
const PROTECTED_FIELDS = ["__v", "ownerId", "sharedWith", "createdAt", "createdBy", "updatedAt", "updatedBy", "deletedAt", "deletedBy", "archivedAt", "archivedBy"];
|
|
30
|
+
const IMMUTABLE_FIELDS = ["_id", "__v", "createdAt", "createdBy"];
|
|
29
31
|
class VersionConflictError extends Error {
|
|
30
32
|
code = "VERSION_CONFLICT";
|
|
31
33
|
statusCode = 409;
|
|
@@ -39,15 +41,65 @@ class MongoRepository {
|
|
|
39
41
|
collection;
|
|
40
42
|
dbClient;
|
|
41
43
|
_where = { deletedAt: null, archivedAt: null };
|
|
44
|
+
scoped;
|
|
45
|
+
ownerField;
|
|
46
|
+
protectedFields;
|
|
42
47
|
report = import_error.reportError;
|
|
43
48
|
constructor(props) {
|
|
44
|
-
const { database, collection, envvar } = props || {};
|
|
49
|
+
const { database, collection, envvar, scoped = false, ownerField = "ownerId", protectedFields = [] } = props || {};
|
|
45
50
|
if (!database) throw new Error("[Mongo Repository] Database name is required");
|
|
46
51
|
if (!collection) throw new Error("[Mongo Repository] Collection name is required");
|
|
47
52
|
this.dbClient = (0, import_mongo_db.getMongoDBClient)(envvar);
|
|
48
53
|
if (!this.dbClient) throw new Error("[Mongo Repository] Database client not found");
|
|
49
54
|
this.database = database;
|
|
50
55
|
this.collection = collection;
|
|
56
|
+
this.scoped = scoped;
|
|
57
|
+
this.ownerField = ownerField;
|
|
58
|
+
this.protectedFields = [...PROTECTED_FIELDS, ...protectedFields];
|
|
59
|
+
}
|
|
60
|
+
assertScope(scope) {
|
|
61
|
+
if (!scope && this.scoped) throw new Error(`[Mongo Repository] Scope is required in ${this.collection}`);
|
|
62
|
+
}
|
|
63
|
+
actorId(scope) {
|
|
64
|
+
if (!scope || scope.kind === "system") return void 0;
|
|
65
|
+
const userId = this.parseId(scope.userId);
|
|
66
|
+
if (!userId) throw new Error("[Mongo Repository] Invalid user id in scope");
|
|
67
|
+
return userId;
|
|
68
|
+
}
|
|
69
|
+
withScope(filter, scope) {
|
|
70
|
+
this.assertScope(scope);
|
|
71
|
+
if (!scope || scope.trusted) return filter;
|
|
72
|
+
return { $and: [filter, { [this.ownerField]: this.actorId(scope) }] };
|
|
73
|
+
}
|
|
74
|
+
stampCreate(payload, scope) {
|
|
75
|
+
this.assertScope(scope);
|
|
76
|
+
if (!scope) return { ...payload };
|
|
77
|
+
const actor = this.actorId(scope);
|
|
78
|
+
return {
|
|
79
|
+
...this.withoutProtected(payload, scope),
|
|
80
|
+
...scope.trusted || this.ownerField === "_id" ? {} : { [this.ownerField]: actor },
|
|
81
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
82
|
+
...actor ? { createdBy: actor } : {}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
withoutProtected(payload, scope) {
|
|
86
|
+
if (!scope || scope.trusted) return payload;
|
|
87
|
+
const kept = Object.fromEntries(Object.entries(payload).filter(([key]) => !this.protectedFields.includes(key)));
|
|
88
|
+
return this.protectedFields.filter((path) => path.includes(".")).reduce((result, path) => this.withoutPath(result, path.split(".")), kept);
|
|
89
|
+
}
|
|
90
|
+
withoutPath(node, path) {
|
|
91
|
+
const [head, ...rest] = path;
|
|
92
|
+
if (!(head in node)) return node;
|
|
93
|
+
if (!rest.length) {
|
|
94
|
+
const { [head]: removed, ...others2 } = node;
|
|
95
|
+
return others2;
|
|
96
|
+
}
|
|
97
|
+
const child = node[head];
|
|
98
|
+
if (!child || typeof child !== "object" || Array.isArray(child) || child instanceof Date || child instanceof import_mongodb.ObjectId) return node;
|
|
99
|
+
const cleaned = this.withoutPath(child, rest);
|
|
100
|
+
if (Object.keys(cleaned).length) return { ...node, [head]: cleaned };
|
|
101
|
+
const { [head]: dropped, ...others } = node;
|
|
102
|
+
return others;
|
|
51
103
|
}
|
|
52
104
|
parseId(id) {
|
|
53
105
|
return typeof id === "string" && import_mongodb.ObjectId.isValid(id) ? new import_mongodb.ObjectId(id) : void 0;
|
|
@@ -59,12 +111,13 @@ class MongoRepository {
|
|
|
59
111
|
const n = Number(limit);
|
|
60
112
|
return !Number.isFinite(n) || n <= 0 ? DEFAULT_LIMIT : n;
|
|
61
113
|
}
|
|
62
|
-
async findOne({ where, select, options }) {
|
|
114
|
+
async findOne({ where, select, options, scope }) {
|
|
115
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
63
116
|
try {
|
|
64
117
|
const db = await this.dbClient.db(this.database);
|
|
65
118
|
const collection = db.collection(this.collection);
|
|
66
119
|
const result = await collection.findOne(
|
|
67
|
-
|
|
120
|
+
filter,
|
|
68
121
|
{
|
|
69
122
|
...select ? { projection: select } : {},
|
|
70
123
|
...options
|
|
@@ -85,24 +138,26 @@ class MongoRepository {
|
|
|
85
138
|
const results = await this.query(params, _limit, _limit + 1);
|
|
86
139
|
return { items: results.slice(0, _limit), hasMore: results.length > _limit };
|
|
87
140
|
}
|
|
88
|
-
async query({ where, select, options }, pageSize, fetchSize) {
|
|
141
|
+
async query({ where, select, options, scope }, pageSize, fetchSize) {
|
|
142
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
89
143
|
try {
|
|
90
144
|
const db = await this.dbClient.db(this.database);
|
|
91
145
|
const collection = db.collection(this.collection);
|
|
92
146
|
const { page = 1 } = options || {};
|
|
93
147
|
const _skip = +page > 0 ? (+page - 1) * pageSize : 0;
|
|
94
|
-
const result = await collection.find(
|
|
148
|
+
const result = await collection.find(filter, { projection: select }).sort(options?.sortBy || { _id: -1 }).skip(_skip).limit(fetchSize).toArray();
|
|
95
149
|
return result;
|
|
96
150
|
} catch (error) {
|
|
97
151
|
await this.report(error);
|
|
98
152
|
throw error;
|
|
99
153
|
}
|
|
100
154
|
}
|
|
101
|
-
async findAll({ where, select, options }) {
|
|
155
|
+
async findAll({ where, select, options, scope }) {
|
|
156
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
102
157
|
try {
|
|
103
158
|
const db = await this.dbClient.db(this.database);
|
|
104
159
|
const collection = db.collection(this.collection);
|
|
105
|
-
const cursor = collection.find(
|
|
160
|
+
const cursor = collection.find(filter, { projection: select });
|
|
106
161
|
const result = await (options?.sortBy ? cursor.sort(options.sortBy) : cursor).toArray();
|
|
107
162
|
return result;
|
|
108
163
|
} catch (error) {
|
|
@@ -110,53 +165,96 @@ class MongoRepository {
|
|
|
110
165
|
throw error;
|
|
111
166
|
}
|
|
112
167
|
}
|
|
113
|
-
async createOne({ payload }) {
|
|
168
|
+
async createOne({ payload, scope }) {
|
|
114
169
|
if (!payload || Object.keys(payload).length === 0) throw new Error("[Mongo Repository] Payload is required for createOne operation");
|
|
170
|
+
const document = this.stampCreate(payload, scope);
|
|
115
171
|
try {
|
|
116
172
|
const db = await this.dbClient.db(this.database);
|
|
117
173
|
const collection = db.collection(this.collection);
|
|
118
|
-
const { insertedId } = await collection.insertOne({ ...
|
|
119
|
-
return insertedId;
|
|
174
|
+
const { insertedId } = await collection.insertOne({ ...document, __v: 1 }, { ignoreUndefined: true });
|
|
175
|
+
return { ...document, _id: insertedId, __v: 1 };
|
|
120
176
|
} catch (error) {
|
|
121
177
|
await this.report(error);
|
|
122
178
|
throw error;
|
|
123
179
|
}
|
|
124
180
|
}
|
|
125
|
-
async createMany({ payload }) {
|
|
181
|
+
async createMany({ payload, scope }) {
|
|
182
|
+
const documents = payload.map((item) => this.stampCreate(item, scope));
|
|
126
183
|
try {
|
|
127
184
|
const db = await this.dbClient.db(this.database);
|
|
128
185
|
const collection = db.collection(this.collection);
|
|
129
|
-
const { insertedIds } = await collection.insertMany(
|
|
186
|
+
const { insertedIds } = await collection.insertMany(documents.map((document) => ({ ...document, __v: 1 })), { ignoreUndefined: true });
|
|
130
187
|
return insertedIds;
|
|
131
188
|
} catch (error) {
|
|
132
189
|
await this.report(error);
|
|
133
190
|
throw error;
|
|
134
191
|
}
|
|
135
192
|
}
|
|
136
|
-
async
|
|
137
|
-
|
|
138
|
-
|
|
193
|
+
async patchOne({ where, set, unset, expectedVersion, scope }) {
|
|
194
|
+
return this.applyPatch({ where, set, unset, expectedVersion, scope });
|
|
195
|
+
}
|
|
196
|
+
async archiveByPatch(params) {
|
|
197
|
+
const actor = this.actorId(params.scope);
|
|
198
|
+
return this.applyPatch(params, { archivedAt: /* @__PURE__ */ new Date(), ...actor ? { archivedBy: actor } : {} });
|
|
199
|
+
}
|
|
200
|
+
async applyPatch({ where, set, unset, expectedVersion, scope }, stamps = {}) {
|
|
201
|
+
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for patchOne operation");
|
|
202
|
+
const actor = this.actorId(scope);
|
|
203
|
+
const toSet = {
|
|
204
|
+
...this.withoutProtectedPaths(set ?? {}, scope),
|
|
205
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {},
|
|
206
|
+
...stamps
|
|
207
|
+
};
|
|
208
|
+
const toUnset = this.withoutProtectedPaths(unset ?? {}, scope);
|
|
209
|
+
return this.modifyOne(where, {
|
|
210
|
+
$set: toSet,
|
|
211
|
+
...Object.keys(toUnset).length ? { $unset: toUnset } : {}
|
|
212
|
+
}, scope, expectedVersion);
|
|
213
|
+
}
|
|
214
|
+
async claimOne({ where, set, scope }) {
|
|
215
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
216
|
+
try {
|
|
217
|
+
const db = await this.dbClient.db(this.database);
|
|
218
|
+
const collection = db.collection(this.collection);
|
|
219
|
+
const result = await collection.findOneAndUpdate(
|
|
220
|
+
filter,
|
|
221
|
+
{ $set: this.withoutProtectedPaths(set ?? {}, scope) },
|
|
222
|
+
{ returnDocument: "before", ignoreUndefined: true }
|
|
223
|
+
);
|
|
224
|
+
return result;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
await this.report(error);
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
withoutProtectedPaths(fields, scope) {
|
|
231
|
+
const guarded = scope && !scope.trusted;
|
|
232
|
+
const isProtected = (key) => this.protectedFields.some((field) => key === field || key.startsWith(`${field}.`) || field.startsWith(`${key}.`));
|
|
233
|
+
return Object.fromEntries(Object.entries(fields).filter(([key]) => !IMMUTABLE_FIELDS.includes(key) && !(guarded && isProtected(key))));
|
|
234
|
+
}
|
|
235
|
+
async softDelete({ where, payload, unset, scope }) {
|
|
236
|
+
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for softDelete operation");
|
|
237
|
+
const actor = this.actorId(scope);
|
|
238
|
+
const extra = this.withoutProtected(payload ?? {}, scope);
|
|
239
|
+
const toUnset = this.withoutProtectedPaths(unset ?? {}, scope);
|
|
240
|
+
return this.modifyOne(where, {
|
|
241
|
+
$set: { ...extra, deletedAt: /* @__PURE__ */ new Date(), ...actor ? { deletedBy: actor } : {} },
|
|
242
|
+
...Object.keys(toUnset).length ? { $unset: toUnset } : {}
|
|
243
|
+
}, scope);
|
|
244
|
+
}
|
|
245
|
+
async modifyOne(where, update, scope, expectedVersion) {
|
|
139
246
|
const checkVersion = typeof expectedVersion === "number";
|
|
247
|
+
const filter = this.withScope({ ...where, ...checkVersion ? { __v: expectedVersion } : {}, ...this._where }, scope);
|
|
140
248
|
try {
|
|
141
249
|
const db = await this.dbClient.db(this.database);
|
|
142
250
|
const collection = db.collection(this.collection);
|
|
143
|
-
const { _id, __v, createdAt, createdBy, toUnset, ...toSet } = payload;
|
|
144
|
-
const __payload = {
|
|
145
|
-
...toSet ? { $set: toSet } : {},
|
|
146
|
-
...toUnset ? { $unset: toUnset } : {}
|
|
147
|
-
};
|
|
148
251
|
const result = await collection.findOneAndUpdate(
|
|
149
|
-
|
|
150
|
-
{
|
|
151
|
-
|
|
152
|
-
...__payload
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
returnDocument: "after"
|
|
156
|
-
}
|
|
252
|
+
filter,
|
|
253
|
+
{ $inc: { __v: 1 }, ...update },
|
|
254
|
+
{ returnDocument: "after", ignoreUndefined: true }
|
|
157
255
|
);
|
|
158
256
|
if (!result && checkVersion) {
|
|
159
|
-
const exists = await collection.findOne({ ...where, ...this._where }, { projection: { _id: 1 } });
|
|
257
|
+
const exists = await collection.findOne(this.withScope({ ...where, ...this._where }, scope), { projection: { _id: 1 } });
|
|
160
258
|
if (exists) throw new VersionConflictError();
|
|
161
259
|
}
|
|
162
260
|
return result;
|
|
@@ -165,13 +263,19 @@ class MongoRepository {
|
|
|
165
263
|
throw error;
|
|
166
264
|
}
|
|
167
265
|
}
|
|
168
|
-
async updateMany({ where, payload }) {
|
|
266
|
+
async updateMany({ where, payload, scope }) {
|
|
267
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
268
|
+
const actor = this.actorId(scope);
|
|
269
|
+
const toSet = {
|
|
270
|
+
...this.withoutProtected(payload ?? {}, scope),
|
|
271
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
|
|
272
|
+
};
|
|
169
273
|
try {
|
|
170
274
|
const db = await this.dbClient.db(this.database);
|
|
171
275
|
const collection = db.collection(this.collection);
|
|
172
276
|
const result = await collection.updateMany(
|
|
173
|
-
|
|
174
|
-
{ $set:
|
|
277
|
+
filter,
|
|
278
|
+
{ $set: toSet }
|
|
175
279
|
);
|
|
176
280
|
const countUpdated = result.modifiedCount;
|
|
177
281
|
return countUpdated;
|
|
@@ -180,55 +284,61 @@ class MongoRepository {
|
|
|
180
284
|
throw error;
|
|
181
285
|
}
|
|
182
286
|
}
|
|
183
|
-
async deleteOne({ where }) {
|
|
287
|
+
async deleteOne({ where, scope }) {
|
|
288
|
+
const filter = this.withScope({ ...where }, scope);
|
|
184
289
|
try {
|
|
185
290
|
const db = await this.dbClient.db(this.database);
|
|
186
291
|
const collection = db.collection(this.collection);
|
|
187
|
-
const { deletedCount } = await collection.deleteOne(
|
|
292
|
+
const { deletedCount } = await collection.deleteOne(filter);
|
|
188
293
|
return deletedCount;
|
|
189
294
|
} catch (error) {
|
|
190
295
|
await this.report(error);
|
|
191
296
|
throw error;
|
|
192
297
|
}
|
|
193
298
|
}
|
|
194
|
-
async deleteMany({ where }) {
|
|
299
|
+
async deleteMany({ where, scope }) {
|
|
300
|
+
const filter = this.withScope({ ...where }, scope);
|
|
195
301
|
try {
|
|
196
302
|
const db = await this.dbClient.db(this.database);
|
|
197
303
|
const collection = db.collection(this.collection);
|
|
198
|
-
const { deletedCount } = await collection.deleteMany(
|
|
304
|
+
const { deletedCount } = await collection.deleteMany(filter);
|
|
199
305
|
return deletedCount;
|
|
200
306
|
} catch (error) {
|
|
201
307
|
await this.report(error);
|
|
202
308
|
throw error;
|
|
203
309
|
}
|
|
204
310
|
}
|
|
205
|
-
async count({ where, options }) {
|
|
311
|
+
async count({ where, options, scope }) {
|
|
312
|
+
const filter = this.withScope({ ...where, ...options?.includeDeleted ? {} : this._where }, scope);
|
|
206
313
|
try {
|
|
207
314
|
const db = await this.dbClient.db(this.database);
|
|
208
315
|
const collection = db.collection(this.collection);
|
|
209
|
-
const result = await collection.countDocuments(
|
|
316
|
+
const result = await collection.countDocuments(filter);
|
|
210
317
|
return result;
|
|
211
318
|
} catch (error) {
|
|
212
319
|
await this.report(error);
|
|
213
320
|
throw error;
|
|
214
321
|
}
|
|
215
322
|
}
|
|
216
|
-
async aggregate(pipeline) {
|
|
323
|
+
async aggregate(pipeline, scope) {
|
|
324
|
+
const scopeFilter = this.withScope({}, scope);
|
|
325
|
+
const scopedPipeline = Object.keys(scopeFilter).length ? [{ $match: scopeFilter }, ...pipeline] : pipeline;
|
|
217
326
|
try {
|
|
218
327
|
const db = await this.dbClient.db(this.database);
|
|
219
328
|
const collection = db.collection(this.collection);
|
|
220
|
-
const result = await collection.aggregate(
|
|
329
|
+
const result = await collection.aggregate(scopedPipeline).toArray();
|
|
221
330
|
return result;
|
|
222
331
|
} catch (error) {
|
|
223
332
|
await this.report(error);
|
|
224
333
|
throw error;
|
|
225
334
|
}
|
|
226
335
|
}
|
|
227
|
-
async distinct(field, where) {
|
|
336
|
+
async distinct(field, where, scope) {
|
|
337
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
228
338
|
try {
|
|
229
339
|
const db = await this.dbClient.db(this.database);
|
|
230
340
|
const collection = db.collection(this.collection);
|
|
231
|
-
const result = await collection.distinct(field,
|
|
341
|
+
const result = await collection.distinct(field, filter);
|
|
232
342
|
return result;
|
|
233
343
|
} catch (error) {
|
|
234
344
|
await this.report(error);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import "../../chunk-CF3WOMB3.mjs";
|
|
2
1
|
import { ObjectId } from "mongodb";
|
|
3
2
|
import { reportError } from "../error/index.js";
|
|
4
3
|
import { getMongoDBClient } from "./mongo-db.client";
|
|
5
4
|
const DEFAULT_LIMIT = 10;
|
|
5
|
+
const PROTECTED_FIELDS = ["__v", "ownerId", "sharedWith", "createdAt", "createdBy", "updatedAt", "updatedBy", "deletedAt", "deletedBy", "archivedAt", "archivedBy"];
|
|
6
|
+
const IMMUTABLE_FIELDS = ["_id", "__v", "createdAt", "createdBy"];
|
|
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,96 @@ 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 }, { ignoreUndefined: true });
|
|
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.map((document) => ({ ...document, __v: 1 })), { ignoreUndefined: true });
|
|
107
163
|
return insertedIds;
|
|
108
164
|
} catch (error) {
|
|
109
165
|
await this.report(error);
|
|
110
166
|
throw error;
|
|
111
167
|
}
|
|
112
168
|
}
|
|
113
|
-
async
|
|
114
|
-
|
|
115
|
-
|
|
169
|
+
async patchOne({ where, set, unset, expectedVersion, scope }) {
|
|
170
|
+
return this.applyPatch({ where, set, unset, expectedVersion, scope });
|
|
171
|
+
}
|
|
172
|
+
async archiveByPatch(params) {
|
|
173
|
+
const actor = this.actorId(params.scope);
|
|
174
|
+
return this.applyPatch(params, { archivedAt: /* @__PURE__ */ new Date(), ...actor ? { archivedBy: actor } : {} });
|
|
175
|
+
}
|
|
176
|
+
async applyPatch({ where, set, unset, expectedVersion, scope }, stamps = {}) {
|
|
177
|
+
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for patchOne operation");
|
|
178
|
+
const actor = this.actorId(scope);
|
|
179
|
+
const toSet = {
|
|
180
|
+
...this.withoutProtectedPaths(set ?? {}, scope),
|
|
181
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {},
|
|
182
|
+
...stamps
|
|
183
|
+
};
|
|
184
|
+
const toUnset = this.withoutProtectedPaths(unset ?? {}, scope);
|
|
185
|
+
return this.modifyOne(where, {
|
|
186
|
+
$set: toSet,
|
|
187
|
+
...Object.keys(toUnset).length ? { $unset: toUnset } : {}
|
|
188
|
+
}, scope, expectedVersion);
|
|
189
|
+
}
|
|
190
|
+
async claimOne({ where, set, scope }) {
|
|
191
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
192
|
+
try {
|
|
193
|
+
const db = await this.dbClient.db(this.database);
|
|
194
|
+
const collection = db.collection(this.collection);
|
|
195
|
+
const result = await collection.findOneAndUpdate(
|
|
196
|
+
filter,
|
|
197
|
+
{ $set: this.withoutProtectedPaths(set ?? {}, scope) },
|
|
198
|
+
{ returnDocument: "before", ignoreUndefined: true }
|
|
199
|
+
);
|
|
200
|
+
return result;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
await this.report(error);
|
|
203
|
+
throw error;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
withoutProtectedPaths(fields, scope) {
|
|
207
|
+
const guarded = scope && !scope.trusted;
|
|
208
|
+
const isProtected = (key) => this.protectedFields.some((field) => key === field || key.startsWith(`${field}.`) || field.startsWith(`${key}.`));
|
|
209
|
+
return Object.fromEntries(Object.entries(fields).filter(([key]) => !IMMUTABLE_FIELDS.includes(key) && !(guarded && isProtected(key))));
|
|
210
|
+
}
|
|
211
|
+
async softDelete({ where, payload, unset, scope }) {
|
|
212
|
+
if (!where || Object.keys(where).length === 0) throw new Error("[Mongo Repository] Where is required for softDelete operation");
|
|
213
|
+
const actor = this.actorId(scope);
|
|
214
|
+
const extra = this.withoutProtected(payload ?? {}, scope);
|
|
215
|
+
const toUnset = this.withoutProtectedPaths(unset ?? {}, scope);
|
|
216
|
+
return this.modifyOne(where, {
|
|
217
|
+
$set: { ...extra, deletedAt: /* @__PURE__ */ new Date(), ...actor ? { deletedBy: actor } : {} },
|
|
218
|
+
...Object.keys(toUnset).length ? { $unset: toUnset } : {}
|
|
219
|
+
}, scope);
|
|
220
|
+
}
|
|
221
|
+
async modifyOne(where, update, scope, expectedVersion) {
|
|
116
222
|
const checkVersion = typeof expectedVersion === "number";
|
|
223
|
+
const filter = this.withScope({ ...where, ...checkVersion ? { __v: expectedVersion } : {}, ...this._where }, scope);
|
|
117
224
|
try {
|
|
118
225
|
const db = await this.dbClient.db(this.database);
|
|
119
226
|
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
227
|
const result = await collection.findOneAndUpdate(
|
|
126
|
-
|
|
127
|
-
{
|
|
128
|
-
|
|
129
|
-
...__payload
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
returnDocument: "after"
|
|
133
|
-
}
|
|
228
|
+
filter,
|
|
229
|
+
{ $inc: { __v: 1 }, ...update },
|
|
230
|
+
{ returnDocument: "after", ignoreUndefined: true }
|
|
134
231
|
);
|
|
135
232
|
if (!result && checkVersion) {
|
|
136
|
-
const exists = await collection.findOne({ ...where, ...this._where }, { projection: { _id: 1 } });
|
|
233
|
+
const exists = await collection.findOne(this.withScope({ ...where, ...this._where }, scope), { projection: { _id: 1 } });
|
|
137
234
|
if (exists) throw new VersionConflictError();
|
|
138
235
|
}
|
|
139
236
|
return result;
|
|
@@ -142,13 +239,19 @@ class MongoRepository {
|
|
|
142
239
|
throw error;
|
|
143
240
|
}
|
|
144
241
|
}
|
|
145
|
-
async updateMany({ where, payload }) {
|
|
242
|
+
async updateMany({ where, payload, scope }) {
|
|
243
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
244
|
+
const actor = this.actorId(scope);
|
|
245
|
+
const toSet = {
|
|
246
|
+
...this.withoutProtected(payload ?? {}, scope),
|
|
247
|
+
...scope ? { updatedAt: /* @__PURE__ */ new Date(), ...actor ? { updatedBy: actor } : {} } : {}
|
|
248
|
+
};
|
|
146
249
|
try {
|
|
147
250
|
const db = await this.dbClient.db(this.database);
|
|
148
251
|
const collection = db.collection(this.collection);
|
|
149
252
|
const result = await collection.updateMany(
|
|
150
|
-
|
|
151
|
-
{ $set:
|
|
253
|
+
filter,
|
|
254
|
+
{ $set: toSet }
|
|
152
255
|
);
|
|
153
256
|
const countUpdated = result.modifiedCount;
|
|
154
257
|
return countUpdated;
|
|
@@ -157,55 +260,61 @@ class MongoRepository {
|
|
|
157
260
|
throw error;
|
|
158
261
|
}
|
|
159
262
|
}
|
|
160
|
-
async deleteOne({ where }) {
|
|
263
|
+
async deleteOne({ where, scope }) {
|
|
264
|
+
const filter = this.withScope({ ...where }, scope);
|
|
161
265
|
try {
|
|
162
266
|
const db = await this.dbClient.db(this.database);
|
|
163
267
|
const collection = db.collection(this.collection);
|
|
164
|
-
const { deletedCount } = await collection.deleteOne(
|
|
268
|
+
const { deletedCount } = await collection.deleteOne(filter);
|
|
165
269
|
return deletedCount;
|
|
166
270
|
} catch (error) {
|
|
167
271
|
await this.report(error);
|
|
168
272
|
throw error;
|
|
169
273
|
}
|
|
170
274
|
}
|
|
171
|
-
async deleteMany({ where }) {
|
|
275
|
+
async deleteMany({ where, scope }) {
|
|
276
|
+
const filter = this.withScope({ ...where }, scope);
|
|
172
277
|
try {
|
|
173
278
|
const db = await this.dbClient.db(this.database);
|
|
174
279
|
const collection = db.collection(this.collection);
|
|
175
|
-
const { deletedCount } = await collection.deleteMany(
|
|
280
|
+
const { deletedCount } = await collection.deleteMany(filter);
|
|
176
281
|
return deletedCount;
|
|
177
282
|
} catch (error) {
|
|
178
283
|
await this.report(error);
|
|
179
284
|
throw error;
|
|
180
285
|
}
|
|
181
286
|
}
|
|
182
|
-
async count({ where, options }) {
|
|
287
|
+
async count({ where, options, scope }) {
|
|
288
|
+
const filter = this.withScope({ ...where, ...options?.includeDeleted ? {} : this._where }, scope);
|
|
183
289
|
try {
|
|
184
290
|
const db = await this.dbClient.db(this.database);
|
|
185
291
|
const collection = db.collection(this.collection);
|
|
186
|
-
const result = await collection.countDocuments(
|
|
292
|
+
const result = await collection.countDocuments(filter);
|
|
187
293
|
return result;
|
|
188
294
|
} catch (error) {
|
|
189
295
|
await this.report(error);
|
|
190
296
|
throw error;
|
|
191
297
|
}
|
|
192
298
|
}
|
|
193
|
-
async aggregate(pipeline) {
|
|
299
|
+
async aggregate(pipeline, scope) {
|
|
300
|
+
const scopeFilter = this.withScope({}, scope);
|
|
301
|
+
const scopedPipeline = Object.keys(scopeFilter).length ? [{ $match: scopeFilter }, ...pipeline] : pipeline;
|
|
194
302
|
try {
|
|
195
303
|
const db = await this.dbClient.db(this.database);
|
|
196
304
|
const collection = db.collection(this.collection);
|
|
197
|
-
const result = await collection.aggregate(
|
|
305
|
+
const result = await collection.aggregate(scopedPipeline).toArray();
|
|
198
306
|
return result;
|
|
199
307
|
} catch (error) {
|
|
200
308
|
await this.report(error);
|
|
201
309
|
throw error;
|
|
202
310
|
}
|
|
203
311
|
}
|
|
204
|
-
async distinct(field, where) {
|
|
312
|
+
async distinct(field, where, scope) {
|
|
313
|
+
const filter = this.withScope({ ...where, ...this._where }, scope);
|
|
205
314
|
try {
|
|
206
315
|
const db = await this.dbClient.db(this.database);
|
|
207
316
|
const collection = db.collection(this.collection);
|
|
208
|
-
const result = await collection.distinct(field,
|
|
317
|
+
const result = await collection.distinct(field, filter);
|
|
209
318
|
return result;
|
|
210
319
|
} catch (error) {
|
|
211
320
|
await this.report(error);
|