@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
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/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
1
2
|
import { CLOUD_FEATURE_KEYS, type CloudPolicyContext } from '#cloud/server/policy/keys'
|
|
2
3
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
4
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
5
|
+
import type { Kysely } from 'kysely'
|
|
3
6
|
|
|
4
7
|
/** How far back a workspace without full revision history keeps its versions. */
|
|
5
8
|
export const LIMITED_HISTORY_DAYS = 30
|
|
@@ -9,17 +12,18 @@ export const LIMITED_HISTORY_DAYS = 30
|
|
|
9
12
|
* `revisionHistory` entitlement off, only the last 30 days, as on a free plan.
|
|
10
13
|
*/
|
|
11
14
|
export async function historyStart(
|
|
15
|
+
database: Kysely<CloudDatabase>,
|
|
12
16
|
policy: CloudPolicy | undefined,
|
|
13
17
|
deploymentMode: CloudPolicyContext['deploymentMode'],
|
|
14
18
|
workspaceId: string,
|
|
15
19
|
now: Date
|
|
16
20
|
): Promise<Date | null> {
|
|
17
|
-
const
|
|
18
|
-
? await
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const context = policy
|
|
22
|
+
? await workspacePolicyContext(database, { workspaceId }, { deploymentMode })
|
|
23
|
+
: null
|
|
24
|
+
const full =
|
|
25
|
+
policy && context
|
|
26
|
+
? await policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, true, context)
|
|
27
|
+
: true
|
|
24
28
|
return full ? null : new Date(now.getTime() - LIMITED_HISTORY_DAYS * 24 * 60 * 60_000)
|
|
25
29
|
}
|
|
@@ -7,12 +7,14 @@ import type {
|
|
|
7
7
|
SaveDocumentVersionInput,
|
|
8
8
|
UpdateDocumentVersionInput
|
|
9
9
|
} from '#cloud/contract'
|
|
10
|
-
import
|
|
10
|
+
import { workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
11
|
+
import { userIdText, type CloudDatabase } from '#cloud/server/db'
|
|
11
12
|
import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
|
|
12
13
|
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
13
14
|
import { copyRevisionImages, revisionImageDownloads } from '#cloud/server/documents/images'
|
|
14
15
|
import { findDocument, insertDocument, workspaceRole } from '#cloud/server/documents/repository'
|
|
15
16
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
17
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
16
18
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
17
19
|
import type { CloudPolicyContext } from '#cloud/server/policy/keys'
|
|
18
20
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
@@ -24,7 +26,6 @@ import { historyStart } from './history'
|
|
|
24
26
|
const DOWNLOAD_LIFETIME_MS = 5 * 60 * 1000
|
|
25
27
|
const DEFAULT_PAGE_SIZE = 50
|
|
26
28
|
const MAXIMUM_PAGE_SIZE = 100
|
|
27
|
-
const WRITABLE_ROLES = new Set(['admin', 'editor'])
|
|
28
29
|
|
|
29
30
|
type VersionRow = {
|
|
30
31
|
id: string
|
|
@@ -72,15 +73,7 @@ export function createDocumentVersionService(
|
|
|
72
73
|
function versions() {
|
|
73
74
|
return database
|
|
74
75
|
.selectFrom('documentVersion')
|
|
75
|
-
.leftJoin('user', (join) =>
|
|
76
|
-
join.on((expression) =>
|
|
77
|
-
expression(
|
|
78
|
-
expression.cast(expression.ref('user.id'), 'text'),
|
|
79
|
-
'=',
|
|
80
|
-
expression.ref('documentVersion.createdBy')
|
|
81
|
-
)
|
|
82
|
-
)
|
|
83
|
-
)
|
|
76
|
+
.leftJoin('user', (join) => join.onRef(userIdText, '=', 'documentVersion.createdBy'))
|
|
84
77
|
.select([
|
|
85
78
|
'documentVersion.id',
|
|
86
79
|
'documentVersion.revisionId',
|
|
@@ -103,7 +96,13 @@ export function createDocumentVersionService(
|
|
|
103
96
|
if (!document) throw new DocumentNotFoundError()
|
|
104
97
|
return {
|
|
105
98
|
document,
|
|
106
|
-
start: await historyStart(
|
|
99
|
+
start: await historyStart(
|
|
100
|
+
database,
|
|
101
|
+
options.policy,
|
|
102
|
+
deploymentMode,
|
|
103
|
+
document.workspaceId,
|
|
104
|
+
new Date()
|
|
105
|
+
)
|
|
107
106
|
}
|
|
108
107
|
}
|
|
109
108
|
|
|
@@ -138,6 +137,41 @@ export function createDocumentVersionService(
|
|
|
138
137
|
return stored
|
|
139
138
|
}
|
|
140
139
|
|
|
140
|
+
/** A document's history for a caller that already let the reader in, and where it starts. */
|
|
141
|
+
async function sharedHistory(documentId: string) {
|
|
142
|
+
const document = await getDocumentSummaryRow(database, documentId)
|
|
143
|
+
if (!document) throw new DocumentNotFoundError()
|
|
144
|
+
const start = await historyStart(
|
|
145
|
+
database,
|
|
146
|
+
options.policy,
|
|
147
|
+
deploymentMode,
|
|
148
|
+
document.workspaceId,
|
|
149
|
+
new Date()
|
|
150
|
+
)
|
|
151
|
+
return { document, start }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A version's stored file, with where to download it and the images it names. */
|
|
155
|
+
async function downloadVersion(
|
|
156
|
+
document: Awaited<ReturnType<typeof sharedHistory>>['document'],
|
|
157
|
+
version: VersionRow
|
|
158
|
+
): Promise<DocumentVersionDownload> {
|
|
159
|
+
const stored = await storedRevision(database, version.revisionId)
|
|
160
|
+
const expiresAt = new Date(Date.now() + DOWNLOAD_LIFETIME_MS)
|
|
161
|
+
return {
|
|
162
|
+
document: {
|
|
163
|
+
document: documentSummary(document),
|
|
164
|
+
revisionId: version.revisionId,
|
|
165
|
+
byteSize: Number(stored.byteSize),
|
|
166
|
+
checksum: stored.checksum,
|
|
167
|
+
contentType: stored.contentType,
|
|
168
|
+
download: await objects.createDownload({ key: stored.objectKey, expiresAt }),
|
|
169
|
+
images: await revisionImageDownloads(database, objects, version.revisionId, expiresAt)
|
|
170
|
+
},
|
|
171
|
+
version: versionOf(version, document.currentRevisionId)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
141
175
|
return {
|
|
142
176
|
async list(
|
|
143
177
|
userId: string,
|
|
@@ -235,21 +269,51 @@ export function createDocumentVersionService(
|
|
|
235
269
|
versionId: string
|
|
236
270
|
): Promise<DocumentVersionDownload> {
|
|
237
271
|
const { document, start } = await openHistory(userId, documentId, false)
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
272
|
+
return downloadVersion(document, await findVersion(documentId, versionId, start))
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The named versions of several documents of one workspace, newest first, each list keyed by
|
|
277
|
+
* its document, for a caller that already let the reader in, such as the community gallery;
|
|
278
|
+
* the workspace's history limit still applies, and one query reads them all.
|
|
279
|
+
*/
|
|
280
|
+
async namedSharedIn(
|
|
281
|
+
workspaceId: string,
|
|
282
|
+
documents: { id: string; currentRevisionId: string | null }[]
|
|
283
|
+
): Promise<Map<string, DocumentVersion[]>> {
|
|
284
|
+
const named = new Map(documents.map((document) => [document.id, [] as DocumentVersion[]]))
|
|
285
|
+
if (documents.length === 0) return named
|
|
286
|
+
const start = await historyStart(
|
|
287
|
+
database,
|
|
288
|
+
options.policy,
|
|
289
|
+
deploymentMode,
|
|
290
|
+
workspaceId,
|
|
291
|
+
new Date()
|
|
292
|
+
)
|
|
293
|
+
let query = versions()
|
|
294
|
+
.select('documentVersion.documentId')
|
|
295
|
+
.where('documentVersion.documentId', 'in', [...named.keys()])
|
|
296
|
+
.where('documentVersion.kind', '=', 'named')
|
|
297
|
+
if (start) query = query.where('documentVersion.createdAt', '>=', start)
|
|
298
|
+
const rows = await query.orderBy('documentVersion.createdAt', 'desc').execute()
|
|
299
|
+
const current = new Map(
|
|
300
|
+
documents.map((document) => [document.id, document.currentRevisionId])
|
|
301
|
+
)
|
|
302
|
+
for (const row of rows) {
|
|
303
|
+
named.get(row.documentId)?.push(versionOf(row, current.get(row.documentId) ?? null))
|
|
252
304
|
}
|
|
305
|
+
return named
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
/** A named version with where to download it, for a caller that already let the reader in. */
|
|
309
|
+
async downloadNamedShared(
|
|
310
|
+
documentId: string,
|
|
311
|
+
versionId: string
|
|
312
|
+
): Promise<DocumentVersionDownload> {
|
|
313
|
+
const { document, start } = await sharedHistory(documentId)
|
|
314
|
+
const version = await findVersion(documentId, versionId, start)
|
|
315
|
+
if (version.kind !== 'named') throw new DocumentNotFoundError()
|
|
316
|
+
return downloadVersion(document, version)
|
|
253
317
|
},
|
|
254
318
|
|
|
255
319
|
/**
|
|
@@ -331,7 +395,9 @@ export function createDocumentVersionService(
|
|
|
331
395
|
try {
|
|
332
396
|
options.onRestored?.(documentId)
|
|
333
397
|
} catch (error) {
|
|
334
|
-
|
|
398
|
+
logCloud('error', 'Ending the rooms of a restored document failed', {
|
|
399
|
+
error: loggedError(error)
|
|
400
|
+
})
|
|
335
401
|
}
|
|
336
402
|
const document = await findDocument(database, userId, documentId)
|
|
337
403
|
if (!document) throw new DocumentNotFoundError()
|
|
@@ -351,7 +417,7 @@ export function createDocumentVersionService(
|
|
|
351
417
|
const { document, start } = await openHistory(userId, documentId, false)
|
|
352
418
|
const role = await workspaceRole(database, userId, document.workspaceId)
|
|
353
419
|
if (!role) throw new DocumentNotFoundError()
|
|
354
|
-
if (!
|
|
420
|
+
if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
|
|
355
421
|
const version = await findVersion(documentId, versionId, start)
|
|
356
422
|
const copyId = crypto.randomUUID()
|
|
357
423
|
const revisionId = crypto.randomUUID()
|
|
@@ -15,6 +15,7 @@ export function createInvitationOutbox(email: TransactionalEmailService): Invita
|
|
|
15
15
|
idempotencyKey: `document-invitation/${message.deliveryId}`,
|
|
16
16
|
kind: 'document-invitation',
|
|
17
17
|
recipientEmail: message.recipientEmail,
|
|
18
|
+
fallbackLocale: message.locale,
|
|
18
19
|
payload: {
|
|
19
20
|
inviterName: message.inviterName,
|
|
20
21
|
documentName: message.documentName,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { renderTransactionalEmail } from '#cloud/email'
|
|
2
|
-
import type { TransactionalEmailKind } from '#cloud/email'
|
|
2
|
+
import type { EmailLocale, TransactionalEmailKind } from '#cloud/email'
|
|
3
3
|
import type { CloudDatabase, TransactionalEmailStatus } from '#cloud/server/db'
|
|
4
4
|
import type { Kysely, Transaction } from 'kysely'
|
|
5
5
|
|
|
@@ -40,6 +40,7 @@ type ClaimedTransactionalEmail = {
|
|
|
40
40
|
kind: TransactionalEmailKind
|
|
41
41
|
recipientEmailNormalized: string
|
|
42
42
|
payloadEncrypted: string
|
|
43
|
+
locale: EmailLocale
|
|
43
44
|
attemptCount: number
|
|
44
45
|
}
|
|
45
46
|
|
|
@@ -93,14 +94,33 @@ export function createTransactionalEmailService(
|
|
|
93
94
|
input.payload
|
|
94
95
|
)
|
|
95
96
|
const executor = transaction ?? database
|
|
97
|
+
const recipientEmailNormalized = normalizedEmail(input.recipientEmail)
|
|
98
|
+
// An account is written to in the language it chose, whoever sends to it.
|
|
99
|
+
const recipientLocale =
|
|
100
|
+
input.recipientLocale !== undefined
|
|
101
|
+
? input.recipientLocale
|
|
102
|
+
: (
|
|
103
|
+
await executor
|
|
104
|
+
.selectFrom('user')
|
|
105
|
+
.select('locale')
|
|
106
|
+
.where((expression) =>
|
|
107
|
+
expression(
|
|
108
|
+
expression.fn('lower', [expression.ref('email')]),
|
|
109
|
+
'=',
|
|
110
|
+
recipientEmailNormalized
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
.executeTakeFirst()
|
|
114
|
+
)?.locale
|
|
96
115
|
const row = await executor
|
|
97
116
|
.insertInto('transactionalEmail')
|
|
98
117
|
.values({
|
|
99
118
|
id,
|
|
100
119
|
idempotencyKey: input.idempotencyKey,
|
|
101
120
|
kind: input.kind,
|
|
102
|
-
recipientEmailNormalized
|
|
121
|
+
recipientEmailNormalized,
|
|
103
122
|
payloadEncrypted,
|
|
123
|
+
locale: recipientLocale ?? input.fallbackLocale ?? 'en',
|
|
104
124
|
status: 'pending',
|
|
105
125
|
nextAttemptAt: new Date(),
|
|
106
126
|
claimId: null,
|
|
@@ -136,7 +156,14 @@ export function createTransactionalEmailService(
|
|
|
136
156
|
const claimed = await database.transaction().execute(async (transaction) => {
|
|
137
157
|
const candidates = await transaction
|
|
138
158
|
.selectFrom('transactionalEmail')
|
|
139
|
-
.select([
|
|
159
|
+
.select([
|
|
160
|
+
'id',
|
|
161
|
+
'kind',
|
|
162
|
+
'recipientEmailNormalized',
|
|
163
|
+
'payloadEncrypted',
|
|
164
|
+
'locale',
|
|
165
|
+
'attemptCount'
|
|
166
|
+
])
|
|
140
167
|
.where('payloadEncrypted', 'is not', null)
|
|
141
168
|
.where((expression) =>
|
|
142
169
|
expression.or([
|
|
@@ -186,7 +213,7 @@ export function createTransactionalEmailService(
|
|
|
186
213
|
message.kind,
|
|
187
214
|
message.payloadEncrypted
|
|
188
215
|
)
|
|
189
|
-
const rendered = await renderTransactionalEmail(content)
|
|
216
|
+
const rendered = await renderTransactionalEmail(content, message.locale)
|
|
190
217
|
const sent = await options.transport.send({
|
|
191
218
|
deliveryId: message.id,
|
|
192
219
|
from: options.from,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
EmailLocale,
|
|
2
3
|
RenderedTransactionalEmail,
|
|
3
4
|
TransactionalEmailKind,
|
|
4
5
|
TransactionalEmailPayloadByKind
|
|
@@ -28,6 +29,17 @@ export type EnqueueTransactionalEmailInput<Kind extends TransactionalEmailKind>
|
|
|
28
29
|
kind: Kind
|
|
29
30
|
recipientEmail: string
|
|
30
31
|
payload: TransactionalEmailPayloadByKind[Kind]
|
|
32
|
+
/**
|
|
33
|
+
* The recipient account's own language, when the sender has the account: the email is then
|
|
34
|
+
* written in it, or in English when it has none, without looking the account up. Signing up
|
|
35
|
+
* needs this, since its callbacks run inside its transaction and must not query the database.
|
|
36
|
+
*/
|
|
37
|
+
recipientLocale?: EmailLocale | null
|
|
38
|
+
/**
|
|
39
|
+
* The language to write in when the recipient has no account that chose one: the inviter's for
|
|
40
|
+
* an invitation. English otherwise.
|
|
41
|
+
*/
|
|
42
|
+
fallbackLocale?: EmailLocale | null
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
export type TransactionalEmailDeliveryOptions = {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ErrorReportContext, ErrorReporter } from '#cloud/errors'
|
|
2
|
+
import { loggedError, writeJSONLine, type CloudLogWriter } from '#cloud/server/logging'
|
|
1
3
|
import type { Context, ErrorHandler } from 'hono'
|
|
2
4
|
import { HTTPException } from 'hono/http-exception'
|
|
3
5
|
import { routePath } from 'hono/route'
|
|
@@ -14,43 +16,76 @@ export const CLOUD_ERROR_SOURCES = [
|
|
|
14
16
|
] as const
|
|
15
17
|
export type CloudErrorSource = (typeof CLOUD_ERROR_SOURCES)[number]
|
|
16
18
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
/**
|
|
20
|
+
* What the server knows about a caught error; never who made the request or what it carried. For a
|
|
21
|
+
* request: its method, the route pattern it matched (not the path with its IDs), and its ID.
|
|
22
|
+
*/
|
|
23
|
+
export type CloudErrorContext = ErrorReportContext<CloudErrorSource>
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Logs a caught error and, when the deployment configures error tracking, sends it there. Runtimes
|
|
27
|
+
* create one with `createLoggingErrorReporter`; its promise never rejects.
|
|
28
|
+
*/
|
|
29
|
+
export type CloudErrorReporter = ErrorReporter<CloudErrorSource>
|
|
24
30
|
|
|
25
31
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
32
|
+
* A reporter that writes the error as a structured line with the runtime's writer, then hands it
|
|
33
|
+
* to `send`, such as PostHog's, if the deployment has one.
|
|
28
34
|
*/
|
|
29
|
-
export
|
|
35
|
+
export function createLoggingErrorReporter(
|
|
36
|
+
log: CloudLogWriter,
|
|
37
|
+
send?: CloudErrorReporter
|
|
38
|
+
): CloudErrorReporter {
|
|
39
|
+
return async (error, context) => {
|
|
40
|
+
try {
|
|
41
|
+
log({
|
|
42
|
+
level: 'error',
|
|
43
|
+
message: context.message ?? 'Failed',
|
|
44
|
+
source: context.source,
|
|
45
|
+
method: context.method,
|
|
46
|
+
route: context.route,
|
|
47
|
+
requestId: context.requestId,
|
|
48
|
+
error: loggedError(error)
|
|
49
|
+
})
|
|
50
|
+
} catch (failure) {
|
|
51
|
+
// A writer that fails must not stop the report or reject; the console still gets both.
|
|
52
|
+
console.error('[Cloud] Writing a log line failed:', failure, error)
|
|
53
|
+
}
|
|
54
|
+
await send?.(error, context)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
30
57
|
|
|
31
|
-
/**
|
|
58
|
+
/** Without a reporter, errors are still logged, as lines of JSON. */
|
|
59
|
+
const logOnly = createLoggingErrorReporter(writeJSONLine)
|
|
60
|
+
|
|
61
|
+
/** Logs a caught error and reports it, through the runtime's reporter. */
|
|
32
62
|
export function reportCloudError(
|
|
33
63
|
reporter: CloudErrorReporter | undefined,
|
|
34
64
|
message: string,
|
|
35
65
|
error: unknown,
|
|
36
66
|
context: CloudErrorContext
|
|
37
67
|
): Promise<void> {
|
|
38
|
-
|
|
39
|
-
return reporter ? reporter(error, context) : Promise.resolve()
|
|
68
|
+
return (reporter ?? logOnly)(error, { ...context, message })
|
|
40
69
|
}
|
|
41
70
|
|
|
42
71
|
function requestContext(context: Context): CloudErrorContext {
|
|
43
|
-
|
|
72
|
+
const requestId: unknown = context.get('requestId')
|
|
73
|
+
return {
|
|
74
|
+
source: 'request',
|
|
75
|
+
method: context.req.method,
|
|
76
|
+
route: routePath(context),
|
|
77
|
+
...(typeof requestId === 'string' && { requestId })
|
|
78
|
+
}
|
|
44
79
|
}
|
|
45
80
|
|
|
46
81
|
/**
|
|
47
82
|
* The app's last error handler, as Hono's default: an `HTTPException` answers with its response,
|
|
48
|
-
* anything else is logged, reported, and answered with a 500
|
|
83
|
+
* anything else is logged, reported, and answered with a 500 naming the request's `X-Request-Id`.
|
|
49
84
|
*/
|
|
50
85
|
export function cloudErrorHandler(reporter: CloudErrorReporter | undefined): ErrorHandler {
|
|
51
86
|
return (error, context) => {
|
|
52
87
|
if (error instanceof HTTPException) return error.getResponse()
|
|
53
|
-
void reportCloudError(reporter, 'Request failed
|
|
88
|
+
void reportCloudError(reporter, 'Request failed', error, requestContext(context))
|
|
54
89
|
return context.text('Internal Server Error', 500)
|
|
55
90
|
}
|
|
56
91
|
}
|
package/src/server/index.ts
CHANGED
|
@@ -20,16 +20,23 @@ export {
|
|
|
20
20
|
type EnrollmentStatus
|
|
21
21
|
} from '#cloud/admin'
|
|
22
22
|
export {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
createBillingRoutes,
|
|
24
|
+
createBillingTokenService,
|
|
25
|
+
type BillingRouteEnvironment,
|
|
26
|
+
type BillingTokenService,
|
|
27
|
+
type BillingTokenServiceOptions
|
|
28
|
+
} from '#cloud/server/billing'
|
|
29
|
+
export {
|
|
30
|
+
ServicesNotEntitledError,
|
|
31
|
+
createServicesRoutes,
|
|
32
|
+
createServicesTokenService,
|
|
33
|
+
importServicesSigningKey,
|
|
34
|
+
loadServicesSigningKey,
|
|
35
|
+
type ServicesRouteEnvironment,
|
|
36
|
+
type ServicesSigningKey,
|
|
37
|
+
type ServicesTokenService,
|
|
38
|
+
type ServicesTokenServiceOptions
|
|
39
|
+
} from './services'
|
|
33
40
|
export {
|
|
34
41
|
createAuthenticationEmailService,
|
|
35
42
|
createAccountAuthenticationService,
|
|
@@ -45,9 +52,14 @@ export {
|
|
|
45
52
|
type CloudSessionResolver,
|
|
46
53
|
type AccountAuthenticationService,
|
|
47
54
|
type AuthenticationEmailService,
|
|
48
|
-
|
|
55
|
+
accessControl,
|
|
56
|
+
accessStatements,
|
|
57
|
+
documentRoleCan,
|
|
58
|
+
documentRoles,
|
|
59
|
+
workspaceRoleCan,
|
|
49
60
|
workspaceRoles,
|
|
50
|
-
|
|
61
|
+
type AccessRequest,
|
|
62
|
+
type DocumentRole
|
|
51
63
|
} from './auth'
|
|
52
64
|
export {
|
|
53
65
|
createCloudAPIRouter,
|
|
@@ -188,20 +200,32 @@ export {
|
|
|
188
200
|
export {
|
|
189
201
|
CLOUD_ERROR_SOURCES,
|
|
190
202
|
cloudErrorHandler,
|
|
203
|
+
createLoggingErrorReporter,
|
|
191
204
|
reportCloudError,
|
|
192
205
|
type CloudErrorContext,
|
|
193
206
|
type CloudErrorReporter,
|
|
194
207
|
type CloudErrorSource
|
|
195
208
|
} from './errors'
|
|
209
|
+
export {
|
|
210
|
+
cloudRequestId,
|
|
211
|
+
logCloud,
|
|
212
|
+
loggedError,
|
|
213
|
+
writeJSONLine,
|
|
214
|
+
type CloudLogLine,
|
|
215
|
+
type CloudLogWriter,
|
|
216
|
+
type ForwardedRequestId
|
|
217
|
+
} from './logging'
|
|
196
218
|
export {
|
|
197
219
|
createInternalRoutes,
|
|
198
220
|
createInternalService,
|
|
199
|
-
|
|
200
|
-
|
|
221
|
+
parseSetAccountEntitlements,
|
|
222
|
+
setAccountEntitlementsSchema,
|
|
201
223
|
type InternalService,
|
|
202
|
-
type
|
|
224
|
+
type SetAccountEntitlementsInput
|
|
203
225
|
} from './internal'
|
|
204
226
|
export {
|
|
227
|
+
INVITATION_BUDGET,
|
|
228
|
+
InvitationLimitError,
|
|
205
229
|
noOpInvitationDelivery,
|
|
206
230
|
type DocumentInvitationMessage,
|
|
207
231
|
type InvitationDelivery,
|
|
@@ -244,7 +268,7 @@ export {
|
|
|
244
268
|
CLOUD_FEATURE_KEYS,
|
|
245
269
|
CloudPolicy,
|
|
246
270
|
createConfiguredCloudPolicy,
|
|
247
|
-
|
|
271
|
+
createAccountEntitlementRepository,
|
|
248
272
|
DatabaseEntitlementSource,
|
|
249
273
|
EntitlementOpenFeatureProvider,
|
|
250
274
|
createDefaultCloudPolicy,
|
|
@@ -261,9 +285,13 @@ export {
|
|
|
261
285
|
type StaticEntitlementsInput
|
|
262
286
|
} from './policy'
|
|
263
287
|
export {
|
|
288
|
+
createStorageLimitNoticeService,
|
|
264
289
|
createStorageQuotaService,
|
|
265
290
|
createStorageReconciliationService,
|
|
266
291
|
StorageQuotaExceededError,
|
|
292
|
+
type StorageLimitNoticeOptions,
|
|
293
|
+
type StorageLimitNoticeResult,
|
|
294
|
+
type StorageLimitNoticeService,
|
|
267
295
|
type StorageQuotaService,
|
|
268
296
|
type StorageQuotaSnapshot,
|
|
269
297
|
type StorageReconciliation
|
|
@@ -288,3 +316,11 @@ export {
|
|
|
288
316
|
type WorkspaceRouteEnvironment,
|
|
289
317
|
type WorkspaceService
|
|
290
318
|
} from './workspaces'
|
|
319
|
+
export {
|
|
320
|
+
createDocumentReportService,
|
|
321
|
+
createPublicReportRoutes,
|
|
322
|
+
DOCUMENT_REPORT_BUDGET,
|
|
323
|
+
DocumentReportLimitError,
|
|
324
|
+
type DocumentReportService,
|
|
325
|
+
type DocumentReportServiceOptions
|
|
326
|
+
} from './reports'
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { createInternalRoutes } from './routes'
|
|
2
2
|
export {
|
|
3
3
|
createInternalService,
|
|
4
|
-
|
|
4
|
+
parseSetAccountEntitlements,
|
|
5
5
|
type InternalService,
|
|
6
|
-
type
|
|
6
|
+
type SetAccountEntitlementsInput
|
|
7
7
|
} from './service'
|
|
8
|
-
export {
|
|
8
|
+
export { setAccountEntitlementsSchema } from './schema'
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { capabilityHashMatches, hashCapability } from '#cloud/server/sharing/crypto'
|
|
2
|
-
import {
|
|
2
|
+
import { accountParams, validatedJSON } from '#cloud/server/validation'
|
|
3
3
|
import { Hono } from 'hono'
|
|
4
4
|
import { bearerAuth } from 'hono/bearer-auth'
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { parseSetAccountEntitlements, type InternalService } from './service'
|
|
7
7
|
|
|
8
8
|
/** The internal API on HTTP, for runtimes without a private transport to their services. */
|
|
9
9
|
export function createInternalRoutes(service: InternalService, serviceToken: string) {
|
|
@@ -20,15 +20,12 @@ export function createInternalRoutes(service: InternalService, serviceToken: str
|
|
|
20
20
|
})
|
|
21
21
|
)
|
|
22
22
|
.put(
|
|
23
|
-
'/
|
|
24
|
-
|
|
25
|
-
validatedJSON(
|
|
23
|
+
'/accounts/:userId/entitlements',
|
|
24
|
+
accountParams,
|
|
25
|
+
validatedJSON(parseSetAccountEntitlements),
|
|
26
26
|
async (context) => {
|
|
27
|
-
const {
|
|
28
|
-
const result = await service.
|
|
29
|
-
workspaceId,
|
|
30
|
-
context.req.valid('json')
|
|
31
|
-
)
|
|
27
|
+
const { userId } = context.req.valid('param')
|
|
28
|
+
const result = await service.setAccountEntitlements(userId, context.req.valid('json'))
|
|
32
29
|
if (!result.found) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
33
30
|
return context.json({ applied: result.applied })
|
|
34
31
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { staticEntitlementsSchema } from '#cloud/server/policy/schema'
|
|
2
2
|
import * as v from 'valibot'
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const setAccountEntitlementsSchema = v.object({
|
|
5
5
|
values: staticEntitlementsSchema,
|
|
6
6
|
/** Who decided these values, such as `billing`; stored as the row's source. */
|
|
7
7
|
source: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(64)),
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
-
import {
|
|
2
|
+
import { createAccountEntitlementRepository } from '#cloud/server/policy'
|
|
3
3
|
import type { Kysely } from 'kysely'
|
|
4
4
|
import * as v from 'valibot'
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { setAccountEntitlementsSchema } from './schema'
|
|
7
7
|
|
|
8
|
-
export type
|
|
8
|
+
export type SetAccountEntitlementsInput = v.InferInput<typeof setAccountEntitlementsSchema>
|
|
9
9
|
|
|
10
|
-
export function
|
|
11
|
-
return v.parse(
|
|
10
|
+
export function parseSetAccountEntitlements(input: unknown) {
|
|
11
|
+
return v.parse(setAccountEntitlementsSchema, input)
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -16,26 +16,20 @@ export function parseSetWorkspaceEntitlements(input: unknown) {
|
|
|
16
16
|
* their own transport: an authenticated HTTP route on Node, a service binding on Workers.
|
|
17
17
|
*/
|
|
18
18
|
export function createInternalService(database: Kysely<CloudDatabase>) {
|
|
19
|
-
const entitlements =
|
|
19
|
+
const entitlements = createAccountEntitlementRepository(database)
|
|
20
20
|
return {
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Sets an account's plan, which every workspace it owns takes. `found` is false for an
|
|
23
|
+
* unknown account; `applied` is false for an outdated version.
|
|
24
|
+
*/
|
|
25
|
+
async setAccountEntitlements(
|
|
26
|
+
userId: string,
|
|
27
|
+
input: SetAccountEntitlementsInput
|
|
25
28
|
): Promise<{ found: boolean; applied: boolean }> {
|
|
26
|
-
const { values, source, externalVersion } =
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
.where('id', '=', workspaceId)
|
|
31
|
-
.executeTakeFirst()
|
|
32
|
-
if (!workspace) return { found: false, applied: false }
|
|
33
|
-
const { applied } = await entitlements.setVersioned(
|
|
34
|
-
workspaceId,
|
|
35
|
-
values,
|
|
36
|
-
source,
|
|
37
|
-
externalVersion
|
|
38
|
-
)
|
|
29
|
+
const { values, source, externalVersion } = parseSetAccountEntitlements(input)
|
|
30
|
+
const account = await entitlements.account(userId)
|
|
31
|
+
if (!account) return { found: false, applied: false }
|
|
32
|
+
const { applied } = await entitlements.setVersioned(account, values, source, externalVersion)
|
|
39
33
|
return { found: true, applied }
|
|
40
34
|
}
|
|
41
35
|
}
|