@terreno/api 57.3.0 → 57.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/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
- package/README.md +11 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/adminTypes.d.ts +7 -0
- package/dist/adminTypes.js +6 -1
- package/dist/api.d.ts +10 -0
- package/dist/api.js +601 -792
- package/dist/audit/auditApp.d.ts +30 -0
- package/dist/audit/auditApp.js +114 -0
- package/dist/audit/auditEventModel.d.ts +29 -0
- package/dist/audit/auditEventModel.js +101 -0
- package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
- package/dist/audit/cloudTasksEnqueue.js +69 -0
- package/dist/audit/diff.d.ts +10 -0
- package/dist/audit/diff.js +140 -0
- package/dist/audit/processQueue.d.ts +6 -0
- package/dist/audit/processQueue.js +56 -0
- package/dist/audit/rbacSink.d.ts +2 -0
- package/dist/audit/rbacSink.js +41 -0
- package/dist/audit/record.d.ts +56 -0
- package/dist/audit/record.js +251 -0
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuth.d.ts +6 -0
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.d.ts +3 -1
- package/dist/collectionRegistry.js +43 -33
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +28 -1
- package/dist/index.js +53 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.d.ts +0 -13
- package/dist/mcp/query.js +57 -151
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +9 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +9 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +7 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +7 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +7 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +9 -0
- package/dist/migrations/generate.d.ts +17 -0
- package/dist/migrations/generate.js +155 -0
- package/dist/migrations/load.d.ts +20 -0
- package/dist/migrations/load.js +110 -0
- package/dist/migrations/lock.d.ts +13 -0
- package/dist/migrations/lock.js +73 -0
- package/dist/migrations/runner.d.ts +42 -0
- package/dist/migrations/runner.js +249 -0
- package/dist/migrations/schemaCatalog.d.ts +44 -0
- package/dist/migrations/schemaCatalog.js +185 -0
- package/dist/migrations/types.d.ts +87 -0
- package/dist/migrations/types.js +7 -0
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.d.ts +2 -0
- package/dist/models/notification.js +62 -0
- package/dist/models/notificationPreference.d.ts +2 -0
- package/dist/models/notificationPreference.js +49 -0
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.d.ts +40 -0
- package/dist/notifications/notificationService.js +183 -0
- package/dist/notifications/notificationsApp.d.ts +18 -0
- package/dist/notifications/notificationsApp.js +156 -0
- package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
- package/dist/notifications/notificationsBeforeSend.js +32 -0
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.d.ts +1 -7
- package/dist/openApi.js +113 -127
- package/dist/openApiBuilder.d.ts +14 -3
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.d.ts +13 -1
- package/dist/openApiValidator.js +301 -246
- package/dist/orgs/orgContext.d.ts +23 -0
- package/dist/orgs/orgContext.js +128 -0
- package/dist/orgs/orgPermissions.d.ts +4 -0
- package/dist/orgs/orgPermissions.js +38 -0
- package/dist/orgs/orgPlugin.d.ts +13 -0
- package/dist/orgs/orgPlugin.js +66 -0
- package/dist/orgs/organizationModel.d.ts +13 -0
- package/dist/orgs/organizationModel.js +200 -0
- package/dist/orgs/organizationSettings.d.ts +19 -0
- package/dist/orgs/organizationSettings.js +113 -0
- package/dist/orgs/orgsApp.d.ts +28 -0
- package/dist/orgs/orgsApp.js +416 -0
- package/dist/permissions.d.ts +7 -13
- package/dist/permissions.js +95 -211
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.d.ts +0 -8
- package/dist/rateLimit/middleware.js +49 -101
- package/dist/rateLimit/mongoStore.d.ts +1 -0
- package/dist/rateLimit/mongoStore.js +55 -112
- package/dist/rateLimit/policies.d.ts +0 -4
- package/dist/rateLimit/policies.js +44 -80
- package/dist/rateLimit/redisStore.d.ts +1 -0
- package/dist/rateLimit/redisStore.js +40 -133
- package/dist/rbac/access.js +122 -253
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
- package/dist/rbac/membershipOrgAdminSource.js +51 -0
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.d.ts +3 -0
- package/dist/rbac/modelRouterAccess.js +133 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +154 -319
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.d.ts +2 -5
- package/dist/rbac/roleModel.js +60 -173
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.d.ts +10 -1
- package/dist/rbac/statements.js +42 -93
- package/dist/rbac/types.d.ts +5 -0
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.d.ts +10 -4
- package/dist/realtime/changeStreamWatcher.js +499 -764
- package/dist/realtime/deprecation.d.ts +1 -0
- package/dist/realtime/deprecation.js +9 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.d.ts +1 -0
- package/dist/realtime/realtimeApp.js +248 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +181 -265
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.d.ts +40 -0
- package/dist/sync/adminBroadcastScope.js +82 -0
- package/dist/sync/adminWindowAccess.d.ts +13 -0
- package/dist/sync/adminWindowAccess.js +25 -0
- package/dist/sync/adminWindowMutation.d.ts +88 -0
- package/dist/sync/adminWindowMutation.js +176 -0
- package/dist/sync/executors.d.ts +9 -3
- package/dist/sync/executors.js +425 -598
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.d.ts +5 -2
- package/dist/sync/mutationHandler.js +549 -697
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +31 -47
- package/dist/sync/routes.d.ts +13 -0
- package/dist/sync/routes.js +484 -760
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.d.ts +8 -2
- package/dist/sync/socketHandlers.js +271 -402
- package/dist/sync/streams.d.ts +2 -0
- package/dist/sync/streams.js +54 -116
- package/dist/sync/syncApp.d.ts +2 -1
- package/dist/sync/syncApp.js +14 -16
- package/dist/sync/syncBatch.d.ts +5 -2
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/sync/types.d.ts +18 -0
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.d.ts +29 -0
- package/dist/terrenoApp.js +184 -257
- package/dist/tests/createTestData.d.ts +1 -3
- package/dist/tests/createTestData.js +147 -259
- package/dist/tests/models.d.ts +1 -0
- package/dist/tests/models.js +23 -56
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.d.ts +0 -5
- package/dist/tests/testHelper.js +32 -101
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/types/authToken.d.ts +3 -3
- package/dist/types/mcpServiceToken.d.ts +2 -2
- package/dist/types/membership.d.ts +23 -0
- package/dist/types/membership.js +2 -0
- package/dist/types/notification.d.ts +28 -0
- package/dist/types/notification.js +2 -0
- package/dist/types/notificationPreference.d.ts +20 -0
- package/dist/types/notificationPreference.js +2 -0
- package/dist/types/organization.d.ts +36 -0
- package/dist/types/organization.js +2 -0
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
- package/dist/webhooks/idempotency/webhookReceipt.js +14 -14
- package/dist/webhooks/rawBody.d.ts +1 -0
- package/dist/webhooks/rawBody.js +2 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +34 -28
- package/src/__snapshots__/openApi.test.ts.snap +192 -54
- package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
- package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/adminTypes.ts +8 -0
- package/src/api.test.ts +39 -2
- package/src/api.ts +68 -0
- package/src/audit/auditApp.test.ts +243 -0
- package/src/audit/auditApp.ts +139 -0
- package/src/audit/auditEventModel.ts +160 -0
- package/src/audit/auditRouter.test.ts +456 -0
- package/src/audit/cloudTasksEnqueue.test.ts +124 -0
- package/src/audit/cloudTasksEnqueue.ts +104 -0
- package/src/audit/diff.ts +163 -0
- package/src/audit/processQueue.ts +69 -0
- package/src/audit/rbacSink.test.ts +86 -0
- package/src/audit/rbacSink.ts +42 -0
- package/src/audit/record.test.ts +402 -0
- package/src/audit/record.ts +329 -0
- package/src/auth.test.ts +1 -1
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/betterAuth.ts +7 -0
- package/src/betterAuthSetup.test.ts +27 -0
- package/src/betterAuthSetup.ts +1 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/collectionRegistry.test.ts +8 -9
- package/src/collectionRegistry.ts +21 -1
- package/src/configurationPlugin.test.ts +1 -11
- package/src/express.d.ts +2 -0
- package/src/expressServer.test.ts +50 -2
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +76 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/mcp/handlers.test.ts +275 -0
- package/src/mcp/query.ts +3 -3
- package/src/mcp/toolGenerator.test.ts +81 -0
- package/src/migrations/cli.test.ts +353 -0
- package/src/migrations/cli.ts +283 -0
- package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
- package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
- package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
- package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
- package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
- package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
- package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
- package/src/migrations/generate.test.ts +333 -0
- package/src/migrations/generate.ts +216 -0
- package/src/migrations/load.test.ts +103 -0
- package/src/migrations/load.ts +124 -0
- package/src/migrations/lock.test.ts +133 -0
- package/src/migrations/lock.ts +118 -0
- package/src/migrations/runner.test.ts +504 -0
- package/src/migrations/runner.ts +358 -0
- package/src/migrations/schemaCatalog.test.ts +159 -0
- package/src/migrations/schemaCatalog.ts +262 -0
- package/src/migrations/types.ts +94 -0
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +69 -0
- package/src/models/notificationPreference.ts +62 -0
- package/src/notifications/notificationService.ts +254 -0
- package/src/notifications/notificationsApp.test.ts +528 -0
- package/src/notifications/notificationsApp.ts +200 -0
- package/src/notifications/notificationsBeforeSend.ts +51 -0
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/openApi.test.ts +0 -1
- package/src/openApi.ts +2 -1
- package/src/openApiBuilder.ts +14 -3
- package/src/openApiValidator.test.ts +221 -2
- package/src/openApiValidator.ts +218 -33
- package/src/orgs/orgAccessOptIn.test.ts +87 -0
- package/src/orgs/orgContext.test.ts +259 -0
- package/src/orgs/orgContext.ts +151 -0
- package/src/orgs/orgPermissions.test.ts +105 -0
- package/src/orgs/orgPermissions.ts +40 -0
- package/src/orgs/orgPlugin.test.ts +130 -0
- package/src/orgs/orgPlugin.ts +70 -0
- package/src/orgs/orgRbac.test.ts +267 -0
- package/src/orgs/orgSecurityDefects.test.ts +415 -0
- package/src/orgs/organizationModel.test.ts +205 -0
- package/src/orgs/organizationModel.ts +262 -0
- package/src/orgs/organizationSettings.test.ts +113 -0
- package/src/orgs/organizationSettings.ts +126 -0
- package/src/orgs/orgsApp.test.ts +382 -0
- package/src/orgs/orgsApp.ts +563 -0
- package/src/permissions.test.ts +62 -36
- package/src/permissions.ts +8 -30
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/rateLimit/middleware.ts +1 -1
- package/src/rateLimit/mongoStore.ts +1 -0
- package/src/rateLimit/policies.ts +2 -2
- package/src/rateLimit/redisStore.ts +1 -0
- package/src/rbac/access.ts +11 -3
- package/src/rbac/membershipOrgAdminSource.ts +59 -0
- package/src/rbac/modelRouterAccess.ts +3 -0
- package/src/rbac/permissionUtils.test.ts +26 -5
- package/src/rbac/resolve.test.ts +34 -2
- package/src/rbac/resolve.ts +26 -7
- package/src/rbac/roleModel.test.ts +34 -1
- package/src/rbac/roleModel.ts +17 -1
- package/src/rbac/statements.test.ts +19 -0
- package/src/rbac/statements.ts +19 -1
- package/src/rbac/types.ts +5 -0
- package/src/realtime/changeStreamWatcher.ts +73 -33
- package/src/realtime/deprecation.ts +1 -0
- package/src/realtime/realtime.test.ts +357 -185
- package/src/realtime/realtimeApp.ts +2 -1
- package/src/schemaMetadata.test.ts +14 -0
- package/src/schemaMetadata.ts +5 -5
- package/src/sync/adminBroadcastConfig.test.ts +96 -0
- package/src/sync/adminBroadcastScope.test.ts +155 -0
- package/src/sync/adminBroadcastScope.ts +123 -0
- package/src/sync/adminWindowAccess.test.ts +57 -0
- package/src/sync/adminWindowAccess.ts +32 -0
- package/src/sync/adminWindowMutation.test.ts +450 -0
- package/src/sync/adminWindowMutation.ts +283 -0
- package/src/sync/executors.ts +33 -6
- package/src/sync/integration.test.ts +2 -2
- package/src/sync/mutationHandler.test.ts +277 -1
- package/src/sync/mutationHandler.ts +171 -29
- package/src/sync/registry.ts +4 -0
- package/src/sync/routes.ts +52 -8
- package/src/sync/socketHandlers.ts +154 -58
- package/src/sync/streams.ts +6 -0
- package/src/sync/sync.test.ts +3 -2
- package/src/sync/syncApp.ts +2 -1
- package/src/sync/syncBatch.ts +7 -2
- package/src/sync/syncRoutes.test.ts +297 -3
- package/src/sync/syncSocket.test.ts +229 -0
- package/src/sync/types.ts +20 -0
- package/src/terrenoApp.migrations.test.ts +114 -0
- package/src/terrenoApp.test.ts +41 -0
- package/src/terrenoApp.ts +97 -31
- package/src/tests/createTestData.ts +2 -2
- package/src/tests/models.ts +10 -2
- package/src/tests/testHelper.ts +1 -7
- package/src/types/authToken.ts +2 -4
- package/src/types/mcpServiceToken.ts +1 -7
- package/src/types/membership.ts +48 -0
- package/src/types/notification.ts +34 -0
- package/src/types/notificationPreference.ts +28 -0
- package/src/types/organization.ts +49 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +8 -10
- package/src/webhooks/rawBody.test.ts +33 -0
- package/src/webhooks/rawBody.ts +1 -0
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +4 -1
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
- package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
package/dist/sync/executors.js
CHANGED
|
@@ -1,99 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
28
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
-
function step(op) {
|
|
31
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
-
switch (op[0]) {
|
|
36
|
-
case 0: case 1: t = op; break;
|
|
37
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
-
default:
|
|
41
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
-
if (t[2]) _.ops.pop();
|
|
46
|
-
_.trys.pop(); continue;
|
|
47
|
-
}
|
|
48
|
-
op = body.call(thisArg, _);
|
|
49
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
54
|
-
var t = {};
|
|
55
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
56
|
-
t[p] = s[p];
|
|
57
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
58
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
59
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
60
|
-
t[p[i]] = s[p[i]];
|
|
61
|
-
}
|
|
62
|
-
return t;
|
|
63
|
-
};
|
|
64
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
65
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
66
4
|
};
|
|
67
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
68
6
|
exports.runPostDelete = exports.executeDelete = exports.runPostUpdate = exports.executeUpdate = exports.runPostCreate = exports.executeCreate = exports.isVersionError = exports.isExecutorConflictError = exports.ExecutorConflictError = void 0;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
7
|
+
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
8
|
+
const luxon_1 = require("luxon");
|
|
9
|
+
const api_1 = require("../api");
|
|
10
|
+
const auth_1 = require("../auth");
|
|
11
|
+
const docLoader_1 = require("../docLoader");
|
|
12
|
+
const errors_1 = require("../errors");
|
|
13
|
+
const permissions_1 = require("../permissions");
|
|
14
|
+
const modelRouterAccess_1 = require("../rbac/modelRouterAccess");
|
|
15
|
+
const transformers_1 = require("../transformers");
|
|
78
16
|
/**
|
|
79
17
|
* Thrown by executeUpdate when a concurrency check fails. Carries the current server document
|
|
80
18
|
* (canonical copy) so callers can serialize it into a conflict response: the REST handler
|
|
81
19
|
* turns timestamp conflicts into the legacy 409 `{data, error, message}` body, and the sync
|
|
82
20
|
* mutation channel returns the doc + seq in its conflict nack.
|
|
83
21
|
*/
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
22
|
+
class ExecutorConflictError extends errors_1.APIError {
|
|
23
|
+
/** Which concurrency mode detected the conflict. */
|
|
24
|
+
conflictType;
|
|
25
|
+
/** The current server document at conflict time. */
|
|
26
|
+
doc;
|
|
27
|
+
/** The doc's `_syncSeq` at conflict time (seq mode only). */
|
|
28
|
+
serverSeq;
|
|
29
|
+
constructor({ conflictType, doc, serverSeq, ...apiErrorData }) {
|
|
30
|
+
super(apiErrorData);
|
|
31
|
+
this.conflictType = conflictType;
|
|
32
|
+
this.doc = doc;
|
|
33
|
+
this.serverSeq = serverSeq;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
97
36
|
exports.ExecutorConflictError = ExecutorConflictError;
|
|
98
37
|
/**
|
|
99
38
|
* Duck-typed guard for {@link ExecutorConflictError}. The package compiles to ES5, where
|
|
@@ -102,11 +41,11 @@ exports.ExecutorConflictError = ExecutorConflictError;
|
|
|
102
41
|
* source directly is unaffected — which is why unit/integration tests never caught it).
|
|
103
42
|
* Always use this guard instead of `instanceof ExecutorConflictError`.
|
|
104
43
|
*/
|
|
105
|
-
|
|
44
|
+
const isExecutorConflictError = (error) => {
|
|
106
45
|
if (error instanceof ExecutorConflictError) {
|
|
107
46
|
return true;
|
|
108
47
|
}
|
|
109
|
-
|
|
48
|
+
const candidate = error;
|
|
110
49
|
return (!!candidate &&
|
|
111
50
|
typeof candidate === "object" &&
|
|
112
51
|
(candidate.conflictType === "timestamp" || candidate.conflictType === "seq") &&
|
|
@@ -117,16 +56,14 @@ exports.isExecutorConflictError = isExecutorConflictError;
|
|
|
117
56
|
* Minimal stand-in for an Express request when executors run outside HTTP. Only `user` is
|
|
118
57
|
* populated — see the module doc comment for the hook compatibility contract.
|
|
119
58
|
*/
|
|
120
|
-
|
|
59
|
+
const stubRequest = (user) => ({ user });
|
|
121
60
|
/**
|
|
122
61
|
* True for the Mongoose error thrown when an `optimisticConcurrency` save matched no
|
|
123
62
|
* document (its `__v` filter failed) — i.e. another write landed since the load.
|
|
124
63
|
* Name-checked rather than `instanceof` for the same ES5-dist reason as
|
|
125
64
|
* {@link isExecutorConflictError}.
|
|
126
65
|
*/
|
|
127
|
-
|
|
128
|
-
return (error === null || error === void 0 ? void 0 : error.name) === "VersionError";
|
|
129
|
-
};
|
|
66
|
+
const isVersionError = (error) => error?.name === "VersionError";
|
|
130
67
|
exports.isVersionError = isVersionError;
|
|
131
68
|
/**
|
|
132
69
|
* Re-read the document a version-conflicted save lost to, so the conflict response carries
|
|
@@ -135,153 +72,119 @@ exports.isVersionError = isVersionError;
|
|
|
135
72
|
* client needs to learn that too. Returns undefined if the read fails — the caller falls
|
|
136
73
|
* back to its own instance rather than failing the request.
|
|
137
74
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return [2 /*return*/, found[0]];
|
|
148
|
-
case 2:
|
|
149
|
-
_a = _b.sent();
|
|
150
|
-
return [2 /*return*/, undefined];
|
|
151
|
-
case 3: return [2 /*return*/];
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}); };
|
|
75
|
+
const reloadDocForConflict = async (model, id) => {
|
|
76
|
+
try {
|
|
77
|
+
const found = await model.find({ _id: id, deleted: { $in: [true, false] } }).limit(1);
|
|
78
|
+
return found[0];
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
155
84
|
/**
|
|
156
85
|
* Create a document through the same pipeline as `POST /`: method-level permissions,
|
|
157
86
|
* `transformer.transform`, `preCreate`, `Model.create` (Mongoose validation), population,
|
|
158
87
|
* and `postCreate`. Throws APIErrors with the same statuses/titles as the REST handler.
|
|
159
88
|
*/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
if (!options.preCreate) return [3 /*break*/, 6];
|
|
186
|
-
_c.label = 2;
|
|
187
|
-
case 2:
|
|
188
|
-
_c.trys.push([2, 4, , 5]);
|
|
189
|
-
return [4 /*yield*/, options.preCreate(cleanedBody, request)];
|
|
190
|
-
case 3:
|
|
191
|
-
cleanedBody = _c.sent();
|
|
192
|
-
return [3 /*break*/, 5];
|
|
193
|
-
case 4:
|
|
194
|
-
error_1 = _c.sent();
|
|
195
|
-
throw (0, errors_1.passthroughOrWrap)(error_1, {
|
|
196
|
-
code: "pre-create-hook-error",
|
|
197
|
-
status: 400,
|
|
198
|
-
title: "preCreate hook error",
|
|
199
|
-
});
|
|
200
|
-
case 5:
|
|
201
|
-
if (cleanedBody === undefined) {
|
|
202
|
-
throw new errors_1.ForbiddenError({
|
|
203
|
-
code: "create-not-allowed",
|
|
204
|
-
detail: "A body must be returned from preCreate",
|
|
205
|
-
title: "Create not allowed",
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
if (cleanedBody === null) {
|
|
209
|
-
throw new errors_1.ForbiddenError({
|
|
210
|
-
code: "create-not-allowed",
|
|
211
|
-
detail: "preCreate hook returned null",
|
|
212
|
-
title: "Create not allowed",
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
_c.label = 6;
|
|
216
|
-
case 6:
|
|
217
|
-
cleanedBody = (0, auth_1.omitUserRolesFromWriteBody)(writeModelName !== null && writeModelName !== void 0 ? writeModelName : model.modelName, options.accessControl, cleanedBody, request
|
|
218
|
-
.terrenoAllowUserAdminWrite === true);
|
|
219
|
-
if (cleanedBody === undefined) {
|
|
220
|
-
throw new errors_1.BadRequestError({
|
|
221
|
-
code: "invalid-request-body",
|
|
222
|
-
detail: "Body is undefined",
|
|
223
|
-
title: "Invalid request body",
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
return [4 /*yield*/, (0, modelRouterAccess_1.validateAccessWritePayload)({
|
|
227
|
-
body: cleanedBody,
|
|
228
|
-
options: options,
|
|
229
|
-
phase: "create",
|
|
230
|
-
user: user,
|
|
231
|
-
})];
|
|
232
|
-
case 7:
|
|
233
|
-
_c.sent();
|
|
234
|
-
_c.label = 8;
|
|
235
|
-
case 8:
|
|
236
|
-
_c.trys.push([8, 10, , 11]);
|
|
237
|
-
return [4 /*yield*/, model.create(cleanedBody)];
|
|
238
|
-
case 9:
|
|
239
|
-
data = (_c.sent());
|
|
240
|
-
return [3 /*break*/, 11];
|
|
241
|
-
case 10:
|
|
242
|
-
error_2 = _c.sent();
|
|
243
|
-
throw (0, errors_1.passthroughOrWrapWrite)(error_2, {
|
|
244
|
-
code: "create-error",
|
|
245
|
-
status: 400,
|
|
246
|
-
title: "Create error",
|
|
247
|
-
});
|
|
248
|
-
case 11:
|
|
249
|
-
if (!options.populatePaths) return [3 /*break*/, 15];
|
|
250
|
-
_c.label = 12;
|
|
251
|
-
case 12:
|
|
252
|
-
_c.trys.push([12, 14, , 15]);
|
|
253
|
-
populateQuery = (0, api_1.addPopulateToQuery)(model.findById(data._id), options.populatePaths);
|
|
254
|
-
return [4 /*yield*/, populateQuery.exec()];
|
|
255
|
-
case 13:
|
|
256
|
-
data = (_c.sent());
|
|
257
|
-
return [3 /*break*/, 15];
|
|
258
|
-
case 14:
|
|
259
|
-
error_3 = _c.sent();
|
|
260
|
-
throw (0, errors_1.passthroughOrWrap)(error_3, {
|
|
261
|
-
code: "populate-error",
|
|
262
|
-
status: 400,
|
|
263
|
-
title: "Populate error",
|
|
264
|
-
});
|
|
265
|
-
case 15:
|
|
266
|
-
if (!(options.postCreate && !skipPostHooks)) return [3 /*break*/, 19];
|
|
267
|
-
_c.label = 16;
|
|
268
|
-
case 16:
|
|
269
|
-
_c.trys.push([16, 18, , 19]);
|
|
270
|
-
return [4 /*yield*/, options.postCreate(data, request)];
|
|
271
|
-
case 17:
|
|
272
|
-
_c.sent();
|
|
273
|
-
return [3 /*break*/, 19];
|
|
274
|
-
case 18:
|
|
275
|
-
error_4 = _c.sent();
|
|
276
|
-
throw (0, errors_1.passthroughOrWrap)(error_4, {
|
|
277
|
-
code: "post-create-hook-error",
|
|
278
|
-
status: 400,
|
|
279
|
-
title: "postCreate hook error",
|
|
280
|
-
});
|
|
281
|
-
case 19: return [2 /*return*/, { doc: data }];
|
|
89
|
+
const executeCreate = async ({ model, options, user, body, req, skipPermissionChecks, skipPostHooks, writeModelName, }) => {
|
|
90
|
+
const request = req ?? stubRequest(user);
|
|
91
|
+
if (!skipPermissionChecks &&
|
|
92
|
+
!(await (0, permissions_1.checkPermissions)("create", options.permissions.create, user))) {
|
|
93
|
+
throw new errors_1.APIError({
|
|
94
|
+
status: 405,
|
|
95
|
+
title: `Access to CREATE on ${model.modelName} denied for ${user?.id}`,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
let cleanedBody;
|
|
99
|
+
try {
|
|
100
|
+
cleanedBody = (0, transformers_1.transform)(options, body, "create", user);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
104
|
+
code: "transform-error",
|
|
105
|
+
status: 400,
|
|
106
|
+
title: "Transform error",
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (options.preCreate) {
|
|
110
|
+
try {
|
|
111
|
+
cleanedBody = await options.preCreate(cleanedBody, request);
|
|
282
112
|
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
115
|
+
code: "pre-create-hook-error",
|
|
116
|
+
status: 400,
|
|
117
|
+
title: "preCreate hook error",
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (cleanedBody === undefined) {
|
|
121
|
+
throw new errors_1.ForbiddenError({
|
|
122
|
+
code: "create-not-allowed",
|
|
123
|
+
detail: "A body must be returned from preCreate",
|
|
124
|
+
title: "Create not allowed",
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (cleanedBody === null) {
|
|
128
|
+
throw new errors_1.ForbiddenError({
|
|
129
|
+
code: "create-not-allowed",
|
|
130
|
+
detail: "preCreate hook returned null",
|
|
131
|
+
title: "Create not allowed",
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
cleanedBody = (0, auth_1.omitUserRolesFromWriteBody)(writeModelName ?? model.modelName, options.accessControl, cleanedBody, request
|
|
136
|
+
.terrenoAllowUserAdminWrite === true);
|
|
137
|
+
if (cleanedBody === undefined) {
|
|
138
|
+
throw new errors_1.BadRequestError({
|
|
139
|
+
code: "invalid-request-body",
|
|
140
|
+
detail: "Body is undefined",
|
|
141
|
+
title: "Invalid request body",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
await (0, modelRouterAccess_1.validateAccessWritePayload)({
|
|
145
|
+
body: cleanedBody,
|
|
146
|
+
options,
|
|
147
|
+
phase: "create",
|
|
148
|
+
user,
|
|
283
149
|
});
|
|
284
|
-
|
|
150
|
+
let data;
|
|
151
|
+
try {
|
|
152
|
+
data = (await model.create(cleanedBody));
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
throw (0, errors_1.passthroughOrWrapWrite)(error, {
|
|
156
|
+
code: "create-error",
|
|
157
|
+
status: 400,
|
|
158
|
+
title: "Create error",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (options.populatePaths) {
|
|
162
|
+
try {
|
|
163
|
+
const populateQuery = (0, api_1.addPopulateToQuery)(model.findById(data._id), options.populatePaths);
|
|
164
|
+
data = (await populateQuery.exec());
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
168
|
+
code: "populate-error",
|
|
169
|
+
status: 400,
|
|
170
|
+
title: "Populate error",
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (options.postCreate && !skipPostHooks) {
|
|
175
|
+
try {
|
|
176
|
+
await options.postCreate(data, request);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
180
|
+
code: "post-create-hook-error",
|
|
181
|
+
status: 400,
|
|
182
|
+
title: "postCreate hook error",
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { doc: data };
|
|
187
|
+
};
|
|
285
188
|
exports.executeCreate = executeCreate;
|
|
286
189
|
/**
|
|
287
190
|
* C5 (FIX 6): run `postCreate` outside the write transaction/ledger-finalize
|
|
@@ -290,20 +193,11 @@ exports.executeCreate = executeCreate;
|
|
|
290
193
|
* (the document write already committed and the ledger already finalized
|
|
291
194
|
* `applied`).
|
|
292
195
|
*/
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (!options.postCreate) return [3 /*break*/, 2];
|
|
299
|
-
return [4 /*yield*/, options.postCreate(doc, request)];
|
|
300
|
-
case 1:
|
|
301
|
-
_c.sent();
|
|
302
|
-
_c.label = 2;
|
|
303
|
-
case 2: return [2 /*return*/];
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
}); };
|
|
196
|
+
const runPostCreate = async ({ doc, options, request, }) => {
|
|
197
|
+
if (options.postCreate) {
|
|
198
|
+
await options.postCreate(doc, request);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
307
201
|
exports.runPostCreate = runPostCreate;
|
|
308
202
|
/**
|
|
309
203
|
* Update a document through the same pipeline as `PATCH /:id`: method-level and object-level
|
|
@@ -312,229 +206,193 @@ exports.runPostCreate = runPostCreate;
|
|
|
312
206
|
* population, and `postUpdate`. Throws APIErrors with the same statuses/titles as the REST
|
|
313
207
|
* handler; concurrency conflicts throw `ExecutorConflictError` (409) carrying the server doc.
|
|
314
208
|
*/
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
// raw and transformed bodies before preUpdate processes them (mirrors the REST handler).
|
|
361
|
-
if (body && typeof body === "object") {
|
|
362
|
-
delete body._updatedAt;
|
|
363
|
-
}
|
|
364
|
-
if (cleanedBody && typeof cleanedBody === "object") {
|
|
365
|
-
delete cleanedBody._updatedAt;
|
|
366
|
-
}
|
|
367
|
-
if (!options.preUpdate) return [3 /*break*/, 10];
|
|
368
|
-
_h.label = 6;
|
|
369
|
-
case 6:
|
|
370
|
-
_h.trys.push([6, 8, , 9]);
|
|
371
|
-
return [4 /*yield*/, options.preUpdate(cleanedBody, request)];
|
|
372
|
-
case 7:
|
|
373
|
-
cleanedBody = _h.sent();
|
|
374
|
-
return [3 /*break*/, 9];
|
|
375
|
-
case 8:
|
|
376
|
-
error_5 = _h.sent();
|
|
377
|
-
throw (0, errors_1.passthroughOrWrap)(error_5, {
|
|
378
|
-
code: "pre-update-hook-error",
|
|
379
|
-
detail: "preUpdate hook error on ".concat(id, ": ").concat((0, errors_1.errorMessage)(error_5)),
|
|
380
|
-
status: 400,
|
|
381
|
-
title: "preUpdate hook error",
|
|
382
|
-
});
|
|
383
|
-
case 9:
|
|
384
|
-
if (cleanedBody === undefined) {
|
|
385
|
-
throw new errors_1.ForbiddenError({
|
|
386
|
-
code: "update-not-allowed",
|
|
387
|
-
detail: "A body must be returned from preUpdate",
|
|
388
|
-
title: "Update not allowed",
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
if (cleanedBody === null) {
|
|
392
|
-
throw new errors_1.ForbiddenError({
|
|
393
|
-
code: "update-not-allowed",
|
|
394
|
-
detail: "preUpdate hook on ".concat(id, " returned null"),
|
|
395
|
-
title: "Update not allowed",
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
_h.label = 10;
|
|
399
|
-
case 10:
|
|
400
|
-
cleanedBody = (0, auth_1.omitUserRolesFromWriteBody)(writeModelName !== null && writeModelName !== void 0 ? writeModelName : model.modelName, options.accessControl, cleanedBody, request
|
|
401
|
-
.terrenoAllowUserAdminWrite === true);
|
|
402
|
-
return [4 /*yield*/, (0, modelRouterAccess_1.validateAccessWritePayload)({
|
|
403
|
-
body: cleanedBody,
|
|
404
|
-
doc: doc,
|
|
405
|
-
options: options,
|
|
406
|
-
phase: "write",
|
|
407
|
-
user: user,
|
|
408
|
-
})];
|
|
409
|
-
case 11:
|
|
410
|
-
_h.sent();
|
|
411
|
-
// Conflict detection runs after preUpdate so that unauthorized mutations
|
|
412
|
-
// are rejected before we leak document data in a conflict response.
|
|
413
|
-
if ((concurrencyCheck === null || concurrencyCheck === void 0 ? void 0 : concurrencyCheck.type) === "timestamp") {
|
|
414
|
-
clientTimestamp = luxon_1.DateTime.fromJSDate(concurrencyCheck.ifUnmodifiedSince);
|
|
415
|
-
if (!clientTimestamp.isValid) {
|
|
416
|
-
throw new errors_1.APIError({
|
|
417
|
-
detail: (_d = concurrencyCheck.invalidTimestampDetail) !== null && _d !== void 0 ? _d : "Conflict-detection timestamp could not be parsed as a date",
|
|
418
|
-
status: 400,
|
|
419
|
-
title: "Invalid conflict-detection timestamp",
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
docRecord = doc;
|
|
423
|
-
serverTimestamp = null;
|
|
424
|
-
serverTimestampValue = (_e = docRecord.updated) !== null && _e !== void 0 ? _e : docRecord.created;
|
|
425
|
-
if (serverTimestampValue instanceof Date) {
|
|
426
|
-
serverTimestamp = luxon_1.DateTime.fromJSDate(serverTimestampValue);
|
|
427
|
-
}
|
|
428
|
-
else if (typeof serverTimestampValue === "string") {
|
|
429
|
-
serverTimestamp = luxon_1.DateTime.fromISO(serverTimestampValue);
|
|
430
|
-
}
|
|
431
|
-
if (serverTimestamp && !serverTimestamp.isValid) {
|
|
432
|
-
throw new errors_1.APIError({
|
|
433
|
-
detail: "Document timestamp could not be parsed as a date",
|
|
434
|
-
status: 400,
|
|
435
|
-
title: "Invalid server timestamp",
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
if (serverTimestamp && clientTimestamp < serverTimestamp) {
|
|
439
|
-
throw new ExecutorConflictError({
|
|
440
|
-
conflictType: "timestamp",
|
|
441
|
-
detail: "Document was modified since your last read",
|
|
442
|
-
disableExternalErrorTracking: true,
|
|
443
|
-
doc: doc,
|
|
444
|
-
status: 409,
|
|
445
|
-
title: "Conflict",
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
else if ((concurrencyCheck === null || concurrencyCheck === void 0 ? void 0 : concurrencyCheck.type) === "seq") {
|
|
450
|
-
serverSeq = (_f = doc._syncSeq) !== null && _f !== void 0 ? _f : 0;
|
|
451
|
-
if (concurrencyCheck.baseSeq !== serverSeq) {
|
|
452
|
-
throw new ExecutorConflictError({
|
|
453
|
-
conflictType: "seq",
|
|
454
|
-
detail: "Document was modified since your last read",
|
|
455
|
-
disableExternalErrorTracking: true,
|
|
456
|
-
doc: doc,
|
|
457
|
-
serverSeq: serverSeq,
|
|
458
|
-
status: 409,
|
|
459
|
-
title: "Sync conflict on ".concat(model.modelName, ":").concat(id, ": baseSeq ").concat(concurrencyCheck.baseSeq, " does not match server seq ").concat(serverSeq),
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
prevDoc = (0, cloneDeep_1.default)(doc);
|
|
464
|
-
_h.label = 12;
|
|
465
|
-
case 12:
|
|
466
|
-
_h.trys.push([12, 14, , 17]);
|
|
467
|
-
doc.set(cleanedBody);
|
|
468
|
-
return [4 /*yield*/, doc.save()];
|
|
469
|
-
case 13:
|
|
470
|
-
_h.sent();
|
|
471
|
-
return [3 /*break*/, 17];
|
|
472
|
-
case 14:
|
|
473
|
-
error_6 = _h.sent();
|
|
474
|
-
if (!((concurrencyCheck === null || concurrencyCheck === void 0 ? void 0 : concurrencyCheck.type) === "seq" && (0, exports.isVersionError)(error_6))) return [3 /*break*/, 16];
|
|
475
|
-
return [4 /*yield*/, reloadDocForConflict(model, id)];
|
|
476
|
-
case 15:
|
|
477
|
-
current = _h.sent();
|
|
478
|
-
serverSeq = (_g = current === null || current === void 0 ? void 0 : current._syncSeq) !== null && _g !== void 0 ? _g : 0;
|
|
479
|
-
throw new ExecutorConflictError({
|
|
480
|
-
conflictType: "seq",
|
|
481
|
-
detail: "Document was modified since your last read",
|
|
482
|
-
disableExternalErrorTracking: true,
|
|
483
|
-
doc: current !== null && current !== void 0 ? current : doc,
|
|
484
|
-
serverSeq: serverSeq,
|
|
485
|
-
status: 409,
|
|
486
|
-
title: "Sync conflict on ".concat(model.modelName, ":").concat(id, ": a concurrent write landed while this update was in flight (baseSeq ").concat(concurrencyCheck.baseSeq, ")"),
|
|
487
|
-
});
|
|
488
|
-
case 16: throw (0, errors_1.passthroughOrWrapWrite)(error_6, {
|
|
489
|
-
code: "update-save-error",
|
|
490
|
-
detail: "preUpdate hook save error on ".concat(id, ": ").concat((0, errors_1.errorMessage)(error_6)),
|
|
209
|
+
const executeUpdate = async ({ model, options, user, id, body, concurrencyCheck, existingDoc, req, skipPermissionChecks, skipPostHooks, writeModelName, }) => {
|
|
210
|
+
const request = req ?? stubRequest(user);
|
|
211
|
+
if (!skipPermissionChecks &&
|
|
212
|
+
!(await (0, permissions_1.checkPermissions)("update", options.permissions.update, user))) {
|
|
213
|
+
throw new errors_1.APIError({
|
|
214
|
+
status: 405,
|
|
215
|
+
title: `Access to UPDATE on ${model.modelName} denied for ${user?.id}`,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
let doc = existingDoc ?? (await (0, docLoader_1.loadDocOr404)(model, id, options.populatePaths));
|
|
219
|
+
if (!skipPermissionChecks &&
|
|
220
|
+
!(await (0, permissions_1.checkPermissions)("update", options.permissions.update, user, doc))) {
|
|
221
|
+
throw new errors_1.APIError({
|
|
222
|
+
status: 403,
|
|
223
|
+
title: `Access to GET on ${model.modelName}:${id} denied for ${user?.id}`,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
let cleanedBody;
|
|
227
|
+
try {
|
|
228
|
+
cleanedBody = (0, transformers_1.transform)(options, body, "update", user);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
232
|
+
code: "transform-error",
|
|
233
|
+
detail: `PATCH failed on ${id} for user ${user?.id}: ${(0, errors_1.errorMessage)(error)}`,
|
|
234
|
+
status: 403,
|
|
235
|
+
title: "PATCH failed",
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
// `_updatedAt` is conflict-detection metadata, not a model field: strip it from both the
|
|
239
|
+
// raw and transformed bodies before preUpdate processes them (mirrors the REST handler).
|
|
240
|
+
if (body && typeof body === "object") {
|
|
241
|
+
delete body._updatedAt;
|
|
242
|
+
}
|
|
243
|
+
if (cleanedBody && typeof cleanedBody === "object") {
|
|
244
|
+
delete cleanedBody._updatedAt;
|
|
245
|
+
}
|
|
246
|
+
if (options.preUpdate) {
|
|
247
|
+
try {
|
|
248
|
+
cleanedBody = await options.preUpdate(cleanedBody, request);
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
252
|
+
code: "pre-update-hook-error",
|
|
253
|
+
detail: `preUpdate hook error on ${id}: ${(0, errors_1.errorMessage)(error)}`,
|
|
491
254
|
status: 400,
|
|
492
|
-
title: "preUpdate hook
|
|
255
|
+
title: "preUpdate hook error",
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
if (cleanedBody === undefined) {
|
|
259
|
+
throw new errors_1.ForbiddenError({
|
|
260
|
+
code: "update-not-allowed",
|
|
261
|
+
detail: "A body must be returned from preUpdate",
|
|
262
|
+
title: "Update not allowed",
|
|
493
263
|
});
|
|
494
|
-
case 17:
|
|
495
|
-
if (!options.populatePaths) return [3 /*break*/, 19];
|
|
496
|
-
populateQuery = (0, api_1.addPopulateToQuery)(model.findById(doc._id), options.populatePaths);
|
|
497
|
-
return [4 /*yield*/, populateQuery.exec()];
|
|
498
|
-
case 18:
|
|
499
|
-
doc = (_h.sent());
|
|
500
|
-
_h.label = 19;
|
|
501
|
-
case 19:
|
|
502
|
-
if (!(options.postUpdate && !skipPostHooks)) return [3 /*break*/, 23];
|
|
503
|
-
_h.label = 20;
|
|
504
|
-
case 20:
|
|
505
|
-
_h.trys.push([20, 22, , 23]);
|
|
506
|
-
return [4 /*yield*/, options.postUpdate(doc, cleanedBody, request, prevDoc)];
|
|
507
|
-
case 21:
|
|
508
|
-
_h.sent();
|
|
509
|
-
return [3 /*break*/, 23];
|
|
510
|
-
case 22:
|
|
511
|
-
error_7 = _h.sent();
|
|
512
|
-
throw (0, errors_1.passthroughOrWrap)(error_7, {
|
|
513
|
-
code: "post-update-hook-error",
|
|
514
|
-
detail: "postUpdate hook error on ".concat(id, ": ").concat((0, errors_1.errorMessage)(error_7)),
|
|
515
|
-
status: 400,
|
|
516
|
-
title: "postUpdate hook error",
|
|
517
|
-
});
|
|
518
|
-
case 23: return [2 /*return*/, { cleanedBody: cleanedBody, doc: doc, prevDoc: prevDoc }];
|
|
519
264
|
}
|
|
265
|
+
if (cleanedBody === null) {
|
|
266
|
+
throw new errors_1.ForbiddenError({
|
|
267
|
+
code: "update-not-allowed",
|
|
268
|
+
detail: `preUpdate hook on ${id} returned null`,
|
|
269
|
+
title: "Update not allowed",
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
cleanedBody = (0, auth_1.omitUserRolesFromWriteBody)(writeModelName ?? model.modelName, options.accessControl, cleanedBody, request
|
|
274
|
+
.terrenoAllowUserAdminWrite === true);
|
|
275
|
+
await (0, modelRouterAccess_1.validateAccessWritePayload)({
|
|
276
|
+
body: cleanedBody,
|
|
277
|
+
doc,
|
|
278
|
+
options,
|
|
279
|
+
phase: "write",
|
|
280
|
+
user,
|
|
520
281
|
});
|
|
521
|
-
|
|
282
|
+
// Conflict detection runs after preUpdate so that unauthorized mutations
|
|
283
|
+
// are rejected before we leak document data in a conflict response.
|
|
284
|
+
if (concurrencyCheck?.type === "timestamp") {
|
|
285
|
+
const clientTimestamp = luxon_1.DateTime.fromJSDate(concurrencyCheck.ifUnmodifiedSince);
|
|
286
|
+
if (!clientTimestamp.isValid) {
|
|
287
|
+
throw new errors_1.APIError({
|
|
288
|
+
detail: concurrencyCheck.invalidTimestampDetail ??
|
|
289
|
+
"Conflict-detection timestamp could not be parsed as a date",
|
|
290
|
+
status: 400,
|
|
291
|
+
title: "Invalid conflict-detection timestamp",
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
const docRecord = doc;
|
|
295
|
+
let serverTimestamp = null;
|
|
296
|
+
const serverTimestampValue = docRecord.updated ?? docRecord.created;
|
|
297
|
+
if (serverTimestampValue instanceof Date) {
|
|
298
|
+
serverTimestamp = luxon_1.DateTime.fromJSDate(serverTimestampValue);
|
|
299
|
+
}
|
|
300
|
+
else if (typeof serverTimestampValue === "string") {
|
|
301
|
+
serverTimestamp = luxon_1.DateTime.fromISO(serverTimestampValue);
|
|
302
|
+
}
|
|
303
|
+
if (serverTimestamp && !serverTimestamp.isValid) {
|
|
304
|
+
throw new errors_1.APIError({
|
|
305
|
+
detail: "Document timestamp could not be parsed as a date",
|
|
306
|
+
status: 400,
|
|
307
|
+
title: "Invalid server timestamp",
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (serverTimestamp && clientTimestamp < serverTimestamp) {
|
|
311
|
+
throw new ExecutorConflictError({
|
|
312
|
+
conflictType: "timestamp",
|
|
313
|
+
detail: "Document was modified since your last read",
|
|
314
|
+
disableExternalErrorTracking: true,
|
|
315
|
+
doc,
|
|
316
|
+
status: 409,
|
|
317
|
+
title: "Conflict",
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
else if (concurrencyCheck?.type === "seq") {
|
|
322
|
+
const serverSeq = doc._syncSeq ?? 0;
|
|
323
|
+
if (concurrencyCheck.baseSeq !== serverSeq) {
|
|
324
|
+
throw new ExecutorConflictError({
|
|
325
|
+
conflictType: "seq",
|
|
326
|
+
detail: "Document was modified since your last read",
|
|
327
|
+
disableExternalErrorTracking: true,
|
|
328
|
+
doc,
|
|
329
|
+
serverSeq,
|
|
330
|
+
status: 409,
|
|
331
|
+
title: `Sync conflict on ${model.modelName}:${id}: baseSeq ${concurrencyCheck.baseSeq} does not match server seq ${serverSeq}`,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
// Make a copy for passing pre-saved values to hooks.
|
|
336
|
+
const prevDoc = (0, cloneDeep_1.default)(doc);
|
|
337
|
+
// Using .save here runs the risk of a versioning error if you try to make two simultaneous
|
|
338
|
+
// updates. We won't wind up with corrupted data, just an API error.
|
|
339
|
+
try {
|
|
340
|
+
doc.set(cleanedBody);
|
|
341
|
+
await doc.save();
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
// Task 9.13: `syncPlugin` enables Mongoose `optimisticConcurrency` on synced schemas, so
|
|
345
|
+
// this save is conditional on the `__v` the document was loaded at. A concurrent write
|
|
346
|
+
// that landed between the seq check above and this save therefore throws a VersionError
|
|
347
|
+
// instead of silently overwriting it — the same lost update the seq check exists to
|
|
348
|
+
// prevent, just detected at write time. Report it as the conflict the seq check would
|
|
349
|
+
// have produced had the other write landed a moment earlier.
|
|
350
|
+
if (concurrencyCheck?.type === "seq" && (0, exports.isVersionError)(error)) {
|
|
351
|
+
const current = await reloadDocForConflict(model, id);
|
|
352
|
+
const serverSeq = current?._syncSeq ?? 0;
|
|
353
|
+
throw new ExecutorConflictError({
|
|
354
|
+
conflictType: "seq",
|
|
355
|
+
detail: "Document was modified since your last read",
|
|
356
|
+
disableExternalErrorTracking: true,
|
|
357
|
+
doc: current ?? doc,
|
|
358
|
+
serverSeq,
|
|
359
|
+
status: 409,
|
|
360
|
+
title: `Sync conflict on ${model.modelName}:${id}: a concurrent write landed while this update was in flight (baseSeq ${concurrencyCheck.baseSeq})`,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
throw (0, errors_1.passthroughOrWrapWrite)(error, {
|
|
364
|
+
code: "update-save-error",
|
|
365
|
+
detail: `preUpdate hook save error on ${id}: ${(0, errors_1.errorMessage)(error)}`,
|
|
366
|
+
status: 400,
|
|
367
|
+
title: "preUpdate hook save error",
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
if (options.populatePaths) {
|
|
371
|
+
const populateQuery = (0, api_1.addPopulateToQuery)(model.findById(doc._id), options.populatePaths);
|
|
372
|
+
doc = (await populateQuery.exec());
|
|
373
|
+
}
|
|
374
|
+
if (options.postUpdate && !skipPostHooks) {
|
|
375
|
+
try {
|
|
376
|
+
await options.postUpdate(doc, cleanedBody, request, prevDoc);
|
|
377
|
+
}
|
|
378
|
+
catch (error) {
|
|
379
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
380
|
+
code: "post-update-hook-error",
|
|
381
|
+
detail: `postUpdate hook error on ${id}: ${(0, errors_1.errorMessage)(error)}`,
|
|
382
|
+
status: 400,
|
|
383
|
+
title: "postUpdate hook error",
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return { cleanedBody: cleanedBody, doc, prevDoc: prevDoc };
|
|
388
|
+
};
|
|
522
389
|
exports.executeUpdate = executeUpdate;
|
|
523
390
|
/** C5 (FIX 6): run `postUpdate` outside the write/ledger-finalize window — see `runPostCreate`. */
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
if (!options.postUpdate) return [3 /*break*/, 2];
|
|
530
|
-
return [4 /*yield*/, options.postUpdate(doc, cleanedBody, request, prevDoc)];
|
|
531
|
-
case 1:
|
|
532
|
-
_c.sent();
|
|
533
|
-
_c.label = 2;
|
|
534
|
-
case 2: return [2 /*return*/];
|
|
535
|
-
}
|
|
536
|
-
});
|
|
537
|
-
}); };
|
|
391
|
+
const runPostUpdate = async ({ doc, cleanedBody, prevDoc, options, request, }) => {
|
|
392
|
+
if (options.postUpdate) {
|
|
393
|
+
await options.postUpdate(doc, cleanedBody, request, prevDoc);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
538
396
|
exports.runPostUpdate = runPostUpdate;
|
|
539
397
|
/**
|
|
540
398
|
* Delete a document through the same pipeline as `DELETE /:id`: method-level and object-level
|
|
@@ -542,150 +400,119 @@ exports.runPostUpdate = runPostUpdate;
|
|
|
542
400
|
* schema has a Boolean `deleted` path, else `doc.deleteOne()`), and `postDelete`. Throws
|
|
543
401
|
* APIErrors with the same statuses/titles as the REST handler.
|
|
544
402
|
*/
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
case 4:
|
|
571
|
-
error_8 = _d.sent();
|
|
572
|
-
alreadyDeleted = (0, errors_1.isAPIError)(error_8) &&
|
|
573
|
-
error_8.status === 404 &&
|
|
574
|
-
((_c = error_8.meta) === null || _c === void 0 ? void 0 : _c.deleted) === "true";
|
|
575
|
-
if (!alreadyDeleted) return [3 /*break*/, 7];
|
|
576
|
-
return [4 /*yield*/, model.find({ _id: id, deleted: { $in: [true, false] } }).limit(1)];
|
|
577
|
-
case 5:
|
|
578
|
-
tombstone = _d.sent();
|
|
579
|
-
resolved = tombstone[0];
|
|
403
|
+
const executeDelete = async ({ model, options, user, id, existingDoc, req, skipPermissionChecks, skipPostHooks, }) => {
|
|
404
|
+
const request = req ?? stubRequest(user);
|
|
405
|
+
if (!skipPermissionChecks &&
|
|
406
|
+
!(await (0, permissions_1.checkPermissions)("delete", options.permissions.delete, user))) {
|
|
407
|
+
throw new errors_1.APIError({
|
|
408
|
+
status: 405,
|
|
409
|
+
title: `Access to DELETE on ${model.modelName} denied for ${user?.id}`,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
// C8: delete of an already-deleted doc is idempotent — loadDocOr404 auto-filters
|
|
413
|
+
// soft-deleted docs and would 404 (a `validation` nack over sync). Load the tombstone
|
|
414
|
+
// directly and return it as an idempotent success (its existing seq) instead.
|
|
415
|
+
let doc = existingDoc;
|
|
416
|
+
if (!doc) {
|
|
417
|
+
try {
|
|
418
|
+
doc = (await (0, docLoader_1.loadDocOr404)(model, id, options.populatePaths));
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
const alreadyDeleted = (0, errors_1.isAPIError)(error) &&
|
|
422
|
+
error.status === 404 &&
|
|
423
|
+
error.meta?.deleted === "true";
|
|
424
|
+
if (alreadyDeleted) {
|
|
425
|
+
// Hydrate the tombstone bypassing the isDeletedPlugin find filter.
|
|
426
|
+
const tombstone = await model.find({ _id: id, deleted: { $in: [true, false] } }).limit(1);
|
|
427
|
+
const resolved = tombstone[0];
|
|
580
428
|
if (!resolved) {
|
|
581
|
-
throw
|
|
582
|
-
}
|
|
583
|
-
return [4 /*yield*/, (0, permissions_1.checkPermissions)("delete", options.permissions.delete, user, resolved)];
|
|
584
|
-
case 6:
|
|
585
|
-
if (!(_d.sent())) {
|
|
586
|
-
throw new errors_1.APIError({
|
|
587
|
-
status: 403,
|
|
588
|
-
title: "Access to GET on ".concat(model.modelName, ":").concat(id, " denied for ").concat(user === null || user === void 0 ? void 0 : user.id),
|
|
589
|
-
});
|
|
429
|
+
throw error;
|
|
590
430
|
}
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
case 8: return [4 /*yield*/, (0, permissions_1.checkPermissions)("delete", options.permissions.delete, user, doc)];
|
|
594
|
-
case 9:
|
|
595
|
-
if (!(_d.sent())) {
|
|
431
|
+
if (!skipPermissionChecks &&
|
|
432
|
+
!(await (0, permissions_1.checkPermissions)("delete", options.permissions.delete, user, resolved))) {
|
|
596
433
|
throw new errors_1.APIError({
|
|
597
434
|
status: 403,
|
|
598
|
-
title:
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
|
-
if (!options.preDelete) return [3 /*break*/, 14];
|
|
602
|
-
body = void 0;
|
|
603
|
-
_d.label = 10;
|
|
604
|
-
case 10:
|
|
605
|
-
_d.trys.push([10, 12, , 13]);
|
|
606
|
-
return [4 /*yield*/, options.preDelete(doc, request)];
|
|
607
|
-
case 11:
|
|
608
|
-
body = _d.sent();
|
|
609
|
-
return [3 /*break*/, 13];
|
|
610
|
-
case 12:
|
|
611
|
-
error_9 = _d.sent();
|
|
612
|
-
throw (0, errors_1.passthroughOrWrap)(error_9, {
|
|
613
|
-
code: "pre-delete-hook-error",
|
|
614
|
-
detail: "preDelete hook error on ".concat(id, ": ").concat((0, errors_1.errorMessage)(error_9)),
|
|
615
|
-
status: 403,
|
|
616
|
-
title: "preDelete hook error",
|
|
617
|
-
});
|
|
618
|
-
case 13:
|
|
619
|
-
if (body === undefined) {
|
|
620
|
-
throw new errors_1.ForbiddenError({
|
|
621
|
-
code: "delete-not-allowed",
|
|
622
|
-
detail: "A body must be returned from preDelete",
|
|
623
|
-
title: "Delete not allowed",
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
if (body === null) {
|
|
627
|
-
throw new errors_1.ForbiddenError({
|
|
628
|
-
code: "delete-not-allowed",
|
|
629
|
-
detail: "preDelete hook for ".concat(id, " returned null"),
|
|
630
|
-
title: "Delete not allowed",
|
|
435
|
+
title: `Access to GET on ${model.modelName}:${id} denied for ${user?.id}`,
|
|
631
436
|
});
|
|
632
437
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
model.schema.paths.deleted.instance === "Boolean")) return [3 /*break*/, 16];
|
|
637
|
-
doc.deleted = true;
|
|
638
|
-
return [4 /*yield*/, doc.save()];
|
|
639
|
-
case 15:
|
|
640
|
-
_d.sent();
|
|
641
|
-
return [3 /*break*/, 19];
|
|
642
|
-
case 16:
|
|
643
|
-
_d.trys.push([16, 18, , 19]);
|
|
644
|
-
return [4 /*yield*/, doc.deleteOne()];
|
|
645
|
-
case 17:
|
|
646
|
-
_d.sent();
|
|
647
|
-
return [3 /*break*/, 19];
|
|
648
|
-
case 18:
|
|
649
|
-
error_10 = _d.sent();
|
|
650
|
-
throw (0, errors_1.passthroughOrWrap)(error_10, {
|
|
651
|
-
code: "delete-error",
|
|
652
|
-
status: 400,
|
|
653
|
-
title: "Delete error",
|
|
654
|
-
});
|
|
655
|
-
case 19:
|
|
656
|
-
if (!(options.postDelete && !skipPostHooks)) return [3 /*break*/, 23];
|
|
657
|
-
_d.label = 20;
|
|
658
|
-
case 20:
|
|
659
|
-
_d.trys.push([20, 22, , 23]);
|
|
660
|
-
return [4 /*yield*/, options.postDelete(request, doc)];
|
|
661
|
-
case 21:
|
|
662
|
-
_d.sent();
|
|
663
|
-
return [3 /*break*/, 23];
|
|
664
|
-
case 22:
|
|
665
|
-
error_11 = _d.sent();
|
|
666
|
-
throw (0, errors_1.passthroughOrWrap)(error_11, {
|
|
667
|
-
code: "post-delete-hook-error",
|
|
668
|
-
status: 400,
|
|
669
|
-
title: "postDelete hook error",
|
|
670
|
-
});
|
|
671
|
-
case 23: return [2 /*return*/, { doc: doc }];
|
|
438
|
+
return { doc: resolved };
|
|
439
|
+
}
|
|
440
|
+
throw error;
|
|
672
441
|
}
|
|
673
|
-
}
|
|
674
|
-
|
|
442
|
+
}
|
|
443
|
+
if (!skipPermissionChecks &&
|
|
444
|
+
!(await (0, permissions_1.checkPermissions)("delete", options.permissions.delete, user, doc))) {
|
|
445
|
+
throw new errors_1.APIError({
|
|
446
|
+
status: 403,
|
|
447
|
+
title: `Access to GET on ${model.modelName}:${id} denied for ${user?.id}`,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
if (options.preDelete) {
|
|
451
|
+
let body;
|
|
452
|
+
try {
|
|
453
|
+
body = await options.preDelete(doc, request);
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
457
|
+
code: "pre-delete-hook-error",
|
|
458
|
+
detail: `preDelete hook error on ${id}: ${(0, errors_1.errorMessage)(error)}`,
|
|
459
|
+
status: 403,
|
|
460
|
+
title: "preDelete hook error",
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
if (body === undefined) {
|
|
464
|
+
throw new errors_1.ForbiddenError({
|
|
465
|
+
code: "delete-not-allowed",
|
|
466
|
+
detail: "A body must be returned from preDelete",
|
|
467
|
+
title: "Delete not allowed",
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
if (body === null) {
|
|
471
|
+
throw new errors_1.ForbiddenError({
|
|
472
|
+
code: "delete-not-allowed",
|
|
473
|
+
detail: `preDelete hook for ${id} returned null`,
|
|
474
|
+
title: "Delete not allowed",
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
// Support .deleted from isDeleted plugin
|
|
479
|
+
if (Object.keys(model.schema.paths).includes("deleted") &&
|
|
480
|
+
model.schema.paths.deleted.instance === "Boolean") {
|
|
481
|
+
doc.deleted = true;
|
|
482
|
+
await doc.save();
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
// For models without the isDeleted plugin
|
|
486
|
+
try {
|
|
487
|
+
await doc.deleteOne();
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
491
|
+
code: "delete-error",
|
|
492
|
+
status: 400,
|
|
493
|
+
title: "Delete error",
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
if (options.postDelete && !skipPostHooks) {
|
|
498
|
+
try {
|
|
499
|
+
await options.postDelete(request, doc);
|
|
500
|
+
}
|
|
501
|
+
catch (error) {
|
|
502
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
503
|
+
code: "post-delete-hook-error",
|
|
504
|
+
status: 400,
|
|
505
|
+
title: "postDelete hook error",
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return { doc };
|
|
510
|
+
};
|
|
675
511
|
exports.executeDelete = executeDelete;
|
|
676
512
|
/** C5 (FIX 6): run `postDelete` outside the write/ledger-finalize window — see `runPostCreate`. */
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
if (!options.postDelete) return [3 /*break*/, 2];
|
|
683
|
-
return [4 /*yield*/, options.postDelete(request, doc)];
|
|
684
|
-
case 1:
|
|
685
|
-
_c.sent();
|
|
686
|
-
_c.label = 2;
|
|
687
|
-
case 2: return [2 /*return*/];
|
|
688
|
-
}
|
|
689
|
-
});
|
|
690
|
-
}); };
|
|
513
|
+
const runPostDelete = async ({ doc, options, request, }) => {
|
|
514
|
+
if (options.postDelete) {
|
|
515
|
+
await options.postDelete(request, doc);
|
|
516
|
+
}
|
|
517
|
+
};
|
|
691
518
|
exports.runPostDelete = runPostDelete;
|