@cosmicdrift/kumiko-samples 0.405.0 → 0.409.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/package.json +1 -1
- package/packages/bundled-features/package.json +26 -4
- package/packages/bundled-features/src/account-security/feature.ts +9 -1
- package/packages/bundled-features/src/admin-shell/changes.json +6 -0
- package/packages/bundled-features/src/admin-shell/feature.ts +4 -1
- package/packages/bundled-features/src/agent-tools/agent-manifest.ts +1 -0
- package/packages/bundled-features/src/agent-tools/changes.json +6 -0
- package/packages/bundled-features/src/agent-tools/tool-catalog.ts +1 -0
- package/packages/bundled-features/src/agent-tools/types.ts +4 -0
- package/packages/bundled-features/src/audit/event-classification.ts +1 -0
- package/packages/bundled-features/src/auth-email-password/bootstrap.ts +1 -0
- package/packages/bundled-features/src/auth-email-password/errors.ts +18 -66
- package/packages/bundled-features/src/auth-email-password/web/auth-client.ts +30 -64
- package/packages/bundled-features/src/auth-email-password/web/auth-redirect.ts +2 -2
- package/packages/bundled-features/src/auth-email-password/web/confirm-account-unlock-screen.tsx +20 -66
- package/packages/bundled-features/src/auth-email-password/web/index.ts +1 -0
- package/packages/bundled-features/src/auth-email-password/web/read-path-param.ts +8 -0
- package/packages/bundled-features/src/auth-email-password/web/return-path.ts +3 -2
- package/packages/bundled-features/src/auth-email-password/web/token-confirm-screen.tsx +97 -0
- package/packages/bundled-features/src/auth-email-password/web/verify-email-screen.tsx +20 -68
- package/packages/bundled-features/src/auth-foundation/boot-checks.ts +22 -15
- package/packages/bundled-features/src/auth-foundation/boot-rules.ts +33 -0
- package/packages/bundled-features/src/auth-mfa/errors.ts +12 -42
- package/packages/bundled-features/src/auth-mfa/feature-exports.ts +6 -45
- package/packages/bundled-features/src/auth-mfa/feature.ts +8 -19
- package/packages/bundled-features/src/auth-mfa/web/mfa-client.ts +19 -33
- package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-notify.ts +1 -0
- package/packages/bundled-features/src/billing-foundation/web/plan-price.ts +20 -7
- package/packages/bundled-features/src/cap-counter/stock-cap-guard.ts +1 -0
- package/packages/bundled-features/src/channel-in-app/changes.json +7 -0
- package/packages/bundled-features/src/channel-in-app/feature.ts +2 -0
- package/packages/bundled-features/src/channel-in-app/handlers/read-state-by-keys.query.ts +35 -0
- package/packages/bundled-features/src/channel-in-app/in-app-channel.ts +1 -0
- package/packages/bundled-features/src/channel-in-app/tables.ts +5 -1
- package/packages/bundled-features/src/contact/challenge.ts +41 -0
- package/packages/bundled-features/src/contact/changes.json +27 -0
- package/packages/bundled-features/src/contact/client/contact-client-source.ts +203 -0
- package/packages/bundled-features/src/contact/entity.ts +168 -0
- package/packages/bundled-features/src/contact/feature.ts +171 -0
- package/packages/bundled-features/src/contact/handlers/access.ts +17 -0
- package/packages/bundled-features/src/contact/handlers/attempts.ts +76 -0
- package/packages/bundled-features/src/contact/handlers/challenge.query.ts +44 -0
- package/packages/bundled-features/src/contact/handlers/forward-request.write.ts +98 -0
- package/packages/bundled-features/src/contact/handlers/mark-done.write.ts +40 -0
- package/packages/bundled-features/src/contact/handlers/notify-reporter-in-app.write.ts +48 -0
- package/packages/bundled-features/src/contact/handlers/open-count.query.ts +15 -0
- package/packages/bundled-features/src/contact/handlers/open-count.ts +17 -0
- package/packages/bundled-features/src/contact/handlers/replies.query.ts +29 -0
- package/packages/bundled-features/src/contact/handlers/reply.write.ts +197 -0
- package/packages/bundled-features/src/contact/handlers/report.write.ts +126 -0
- package/packages/bundled-features/src/contact/handlers/request-delivery.query.ts +143 -0
- package/packages/bundled-features/src/contact/handlers/request-detail.query.ts +50 -0
- package/packages/bundled-features/src/contact/handlers/request-list.query.ts +14 -0
- package/packages/bundled-features/src/contact/handlers/send-reply-mail.write.ts +124 -0
- package/packages/bundled-features/src/contact/handlers/send-test.write.ts +45 -0
- package/packages/bundled-features/src/contact/handlers/submit.write.ts +194 -0
- package/packages/bundled-features/src/contact/handlers/take-over-uploads.ts +47 -0
- package/packages/bundled-features/src/contact/i18n.ts +189 -0
- package/packages/bundled-features/src/contact/index.ts +29 -0
- package/packages/bundled-features/src/contact/lib.ts +176 -0
- package/packages/bundled-features/src/contact/mail.ts +401 -0
- package/packages/bundled-features/src/contact/options.ts +95 -0
- package/packages/bundled-features/src/contact/panels.ts +28 -0
- package/packages/bundled-features/src/contact/payloads.ts +106 -0
- package/packages/bundled-features/src/contact/pow.ts +22 -0
- package/packages/bundled-features/src/contact/screens.ts +506 -0
- package/packages/bundled-features/src/contact/subject.ts +13 -0
- package/packages/bundled-features/src/contact/user-agent.ts +39 -0
- package/packages/bundled-features/src/contact/user-data-hooks.ts +180 -0
- package/packages/bundled-features/src/contact/web/constants.ts +160 -0
- package/packages/bundled-features/src/contact/web/index.ts +29 -0
- package/packages/bundled-features/src/crypto-shredding/handlers/forget-subject.write.ts +5 -16
- package/packages/bundled-features/src/data-retention/changes.json +6 -0
- package/packages/bundled-features/src/data-retention/feature.ts +16 -2
- package/packages/bundled-features/src/data-retention/index.ts +5 -0
- package/packages/bundled-features/src/data-retention/run-retention-cleanup.ts +138 -41
- package/packages/bundled-features/src/data-retention/tenant-destruction-hold.ts +196 -0
- package/packages/bundled-features/src/delivery/address-opt-out.ts +1 -0
- package/packages/bundled-features/src/delivery/changes.json +7 -0
- package/packages/bundled-features/src/delivery/channel-context.ts +2 -0
- package/packages/bundled-features/src/delivery/delivery-service.ts +29 -5
- package/packages/bundled-features/src/delivery/events.ts +2 -0
- package/packages/bundled-features/src/delivery/feature.ts +3 -2
- package/packages/bundled-features/src/delivery/handlers/attempts-by-keys.query.ts +55 -0
- package/packages/bundled-features/src/delivery/handlers/log.query.ts +1 -1
- package/packages/bundled-features/src/delivery/jobs.ts +5 -0
- package/packages/bundled-features/src/delivery/tables.ts +13 -2
- package/packages/bundled-features/src/delivery/types.ts +3 -0
- package/packages/bundled-features/src/delivery/unsubscribe-page.ts +2 -0
- package/packages/bundled-features/src/delivery/web/delivery-channel-cell.tsx +1 -1
- package/packages/bundled-features/src/delivery/web/delivery-error-cell.tsx +1 -1
- package/packages/bundled-features/src/delivery/web/delivery-status-cell.tsx +1 -1
- package/packages/bundled-features/src/delivery/web/delivery-time-cell.tsx +3 -2
- package/packages/bundled-features/src/delivery/web/delivery-type-cell.tsx +1 -1
- package/packages/bundled-features/src/delivery/web/public-names.ts +1 -0
- package/packages/bundled-features/src/document-ingest-foundation/boot-checks.ts +14 -9
- package/packages/bundled-features/src/document-ingest-foundation/boot-rules.ts +22 -0
- package/packages/bundled-features/src/document-ingest-foundation/changes.json +6 -0
- package/packages/bundled-features/src/document-ingest-foundation/events.ts +17 -0
- package/packages/bundled-features/src/document-ingest-foundation/feature.ts +39 -1
- package/packages/bundled-features/src/document-ingest-foundation/index.ts +10 -0
- package/packages/bundled-features/src/document-ingest-foundation/report-ingest-progress.ts +43 -0
- package/packages/bundled-features/src/document-ingest-foundation/tables.ts +43 -0
- package/packages/bundled-features/src/files-tenant-data/changes.json +6 -0
- package/packages/bundled-features/src/files-tenant-data/hooks.ts +4 -1
- package/packages/bundled-features/src/folders/web/folder-manager.tsx +8 -9
- package/packages/bundled-features/src/folders/web/folder-section.tsx +1 -1
- package/packages/bundled-features/src/form-draft-user-data/index.ts +2 -0
- package/packages/bundled-features/src/inbound-mail-foundation/changes.json +13 -0
- package/packages/bundled-features/src/inbound-mail-foundation/entities.ts +2 -0
- package/packages/bundled-features/src/inbound-mail-foundation/events.ts +2 -0
- package/packages/bundled-features/src/inbound-mail-foundation/feature.ts +6 -6
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/attach-message-content.write.ts +7 -0
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/ingest-message.write.ts +6 -0
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/list-messages.query.ts +6 -2
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/message-content.query.ts +3 -0
- package/packages/bundled-features/src/inbound-mail-foundation/handlers/message-content.ts +19 -0
- package/packages/bundled-features/src/inbound-mail-foundation/projection.ts +2 -0
- package/packages/bundled-features/src/inbound-mail-foundation/sanitize-inbound-html.ts +150 -0
- package/packages/bundled-features/src/inbound-mail-foundation/types.ts +1 -0
- package/packages/bundled-features/src/jobs/job-run-logger.ts +24 -25
- package/packages/bundled-features/src/ledger/handlers/reports.query.ts +46 -31
- package/packages/bundled-features/src/ledger/recurring.ts +1 -0
- package/packages/bundled-features/src/notes-history/web/notes-section.tsx +3 -1
- package/packages/bundled-features/src/personal-access-tokens/feature.ts +9 -17
- package/packages/bundled-features/src/personal-access-tokens/handlers/create.write.ts +2 -7
- package/packages/bundled-features/src/secrets/changes.json +6 -0
- package/packages/bundled-features/src/secrets/handlers/delete.write.ts +8 -4
- package/packages/bundled-features/src/secrets/handlers/list.query.ts +6 -3
- package/packages/bundled-features/src/secrets/handlers/set.write.ts +9 -4
- package/packages/bundled-features/src/secrets/secret-scope.ts +33 -0
- package/packages/bundled-features/src/sessions/changes.json +6 -0
- package/packages/bundled-features/src/sessions/feature.ts +21 -18
- package/packages/bundled-features/src/sessions/handlers/last-activity-probe.query.ts +53 -0
- package/packages/bundled-features/src/sessions/handlers/last-activity-probe.route.ts +39 -0
- package/packages/bundled-features/src/sessions/screens.ts +1 -0
- package/packages/bundled-features/src/sessions/session-callbacks.ts +13 -12
- package/packages/bundled-features/src/sessions/web/i18n.ts +4 -1
- package/packages/bundled-features/src/shared/assert-billing-live.ts +14 -0
- package/packages/bundled-features/src/shared/collect-extension-options.ts +12 -0
- package/packages/bundled-features/src/shared/index.ts +16 -0
- package/packages/bundled-features/src/shared/late-bound-slot.ts +19 -0
- package/packages/bundled-features/src/shared/mail-locale-templates.ts +7 -1
- package/packages/bundled-features/src/shared/price-env-shape.ts +44 -0
- package/packages/bundled-features/src/shared/purge-row-file-refs.ts +1 -1
- package/packages/bundled-features/src/shared/read-feature-export-fn.ts +16 -0
- package/packages/bundled-features/src/shared/signup-handover.ts +3 -4
- package/packages/bundled-features/src/shared/tenant-destruction-hold.ts +49 -0
- package/packages/bundled-features/src/shared/unprocessable.ts +19 -0
- package/packages/bundled-features/src/shared/web/index.ts +3 -0
- package/packages/bundled-features/src/subscription-paddle/feature.ts +7 -15
- package/packages/bundled-features/src/subscription-paddle/runtime.ts +4 -7
- package/packages/bundled-features/src/subscription-stripe/feature.ts +7 -15
- package/packages/bundled-features/src/subscription-stripe/runtime.ts +4 -7
- package/packages/bundled-features/src/tags/entity.ts +1 -0
- package/packages/bundled-features/src/tags/web/tag-chip.tsx +14 -0
- package/packages/bundled-features/src/tags/web/tag-filter.tsx +4 -5
- package/packages/bundled-features/src/tags/web/tag-manager.tsx +1 -1
- package/packages/bundled-features/src/tags/web/tag-section.tsx +3 -6
- package/packages/bundled-features/src/tags/web/tags-cell.tsx +4 -5
- package/packages/bundled-features/src/template-resolver/compile-document-body.ts +27 -15
- package/packages/bundled-features/src/template-resolver/handlers/by-tenant.query.ts +11 -8
- package/packages/bundled-features/src/template-resolver/handlers/discard-draft.write.ts +1 -0
- package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +1 -0
- package/packages/bundled-features/src/template-resolver/handlers/send-test.write.ts +5 -5
- package/packages/bundled-features/src/template-resolver/handlers/toggle-status.write.ts +3 -0
- package/packages/bundled-features/src/template-resolver/linked-resources.ts +11 -6
- package/packages/bundled-features/src/template-resolver/store-template-resource.ts +15 -12
- package/packages/bundled-features/src/template-resolver/web/document-images.ts +6 -6
- package/packages/bundled-features/src/template-resolver/web/document-insert-fields.tsx +41 -36
- package/packages/bundled-features/src/template-resolver/web/document-schema.ts +11 -12
- package/packages/bundled-features/src/template-resolver/web/tiptap-attribute-parse-rule.ts +19 -0
- package/packages/bundled-features/src/tenant/i18n.ts +3 -0
- package/packages/bundled-features/src/tenant/schema/tenant.ts +1 -0
- package/packages/bundled-features/src/tenant/web/member-roles-cell.tsx +1 -1
- package/packages/bundled-features/src/tenant/web/member-status-cell.tsx +1 -1
- package/packages/bundled-features/src/tenant-inactivity/changes.json +8 -0
- package/packages/bundled-features/src/tenant-inactivity/check-inactive-tenants.ts +260 -0
- package/packages/bundled-features/src/tenant-inactivity/constants.ts +12 -0
- package/packages/bundled-features/src/tenant-inactivity/email-templates.ts +24 -0
- package/packages/bundled-features/src/tenant-inactivity/feature.ts +66 -0
- package/packages/bundled-features/src/tenant-inactivity/handlers/inactivity-state.query.ts +49 -0
- package/packages/bundled-features/src/tenant-inactivity/handlers/keep-account.write.ts +56 -0
- package/packages/bundled-features/src/tenant-inactivity/inactivity-plan.ts +187 -0
- package/packages/bundled-features/src/tenant-inactivity/index.ts +5 -0
- package/packages/bundled-features/src/tenant-inactivity/keep-token.ts +44 -0
- package/packages/bundled-features/src/tenant-inactivity/mail-notifications.ts +62 -0
- package/packages/bundled-features/src/tenant-inactivity/options.ts +38 -0
- package/packages/bundled-features/src/tenant-inactivity/remote-activity.ts +32 -0
- package/packages/bundled-features/src/tenant-inactivity/state-store.ts +71 -0
- package/packages/bundled-features/src/tenant-inactivity/state-table.ts +50 -0
- package/packages/bundled-features/src/tenant-lifecycle/boot-checks.ts +8 -6
- package/packages/bundled-features/src/tenant-lifecycle/boot-rules.ts +16 -0
- package/packages/bundled-features/src/tenant-lifecycle/changes.json +30 -0
- package/packages/bundled-features/src/tenant-lifecycle/constants.ts +6 -0
- package/packages/bundled-features/src/tenant-lifecycle/events.ts +2 -0
- package/packages/bundled-features/src/tenant-lifecycle/feature.ts +25 -2
- package/packages/bundled-features/src/tenant-lifecycle/handlers/request-destruction.write.ts +12 -87
- package/packages/bundled-features/src/tenant-lifecycle/index.ts +6 -0
- package/packages/bundled-features/src/tenant-lifecycle/request-tenant-destruction.ts +108 -0
- package/packages/bundled-features/src/tenant-lifecycle/retention-config-destroy-hooks.ts +62 -0
- package/packages/bundled-features/src/tenant-lifecycle/run-tenant-destroy.ts +131 -11
- package/packages/bundled-features/src/tenant-lifecycle/stages.ts +39 -4
- package/packages/bundled-features/src/tenant-lifecycle/testing.ts +1 -1
- package/packages/bundled-features/src/tier-engine/caps-for-tier-from-map.ts +16 -0
- package/packages/bundled-features/src/tier-engine/changes.json +6 -0
- package/packages/bundled-features/src/tier-engine/compose-app.ts +21 -20
- package/packages/bundled-features/src/tier-engine/index.ts +1 -0
- package/packages/bundled-features/src/user-data-rights/feature.ts +2 -0
- package/packages/bundled-features/src/user-data-rights/run-forget-cleanup.ts +2 -0
- package/packages/bundled-features/src/user-data-rights/web/privacy-center-screen.tsx +1 -1
- package/packages/bundled-features/src/user-data-rights/zip-path.ts +1 -0
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/config-value.userdata-hook.ts +8 -27
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/delivery-attempt.userdata-hook.ts +11 -8
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/forget-own-rows-delete-hook.ts +42 -0
- package/packages/bundled-features/src/user-data-rights-defaults/hooks/notification-preference.userdata-hook.ts +8 -27
- package/packages/bundled-features/src/user-profile/handlers/change-email.write.ts +2 -6
- package/packages/bundled-features/src/user-profile/web/profile-screen.tsx +2 -3
- package/samples/apps/styleguide/src/features/widgets/i18n.ts +6 -0
- package/samples/apps/styleguide/src/features/widgets/web/InboxReaderExamples.tsx +158 -0
- package/samples/apps/styleguide/src/features/widgets/web/Widgets.tsx +2 -0
- package/samples/apps/styleguide/src/features/widgets/web/sample-pdf.ts +28 -0
- package/samples/apps/use-all-bundled/feature-manifest.json +134 -2
- package/samples/apps/use-all-bundled/kumiko/migrations/0063_contact.rebuild.json +4 -0
- package/samples/apps/use-all-bundled/package.json +1 -0
- package/samples/apps/use-all-bundled/playwright.config.ts +2 -0
- package/samples/apps/use-all-bundled/src/app/client.tsx +3 -0
- package/samples/apps/use-all-bundled/src/app/screens-feature.ts +46 -2
- package/samples/apps/use-all-bundled/src/run-config.ts +38 -4
- package/samples/recipes/contact/README.md +46 -0
- package/samples/recipes/contact/package.json +12 -0
- package/samples/recipes/contact/src/feature.ts +27 -0
- package/samples/recipes/delivery-notifications/src/feature.ts +3 -0
- package/samples/recipes/lane-routing/README.md +1 -0
- package/samples/recipes/lane-routing/src/feature.ts +8 -0
- package/samples/recipes/record-detail-layout/src/feature.ts +9 -1
- package/samples/recipes/search/README.md +2 -0
- package/samples/recipes/search/src/feature.ts +27 -2
- package/samples/recipes/secrets-demo/README.md +1 -0
- package/samples/recipes/secrets-demo/src/feature.ts +56 -0
- package/samples/recipes/tenant-inactivity/README.md +21 -0
- package/samples/recipes/tenant-inactivity/package.json +12 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { deleteMany, deriveEntityTableMeta } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import {
|
|
3
|
+
defineFeature,
|
|
4
|
+
EXT_TENANT_DATA,
|
|
5
|
+
type FeatureDefinition,
|
|
6
|
+
type TenantDataDestroyHook,
|
|
7
|
+
} from "@cosmicdrift/kumiko-framework/engine";
|
|
8
|
+
import { checkTenantInactivity } from "./check-inactive-tenants.ts";
|
|
9
|
+
import { TENANT_INACTIVITY_FEATURE, TENANT_INACTIVITY_STATE_ENTITY } from "./constants.ts";
|
|
10
|
+
import { createInactivityStateQuery } from "./handlers/inactivity-state.query.ts";
|
|
11
|
+
import { createKeepAccountHandler } from "./handlers/keep-account.write.ts";
|
|
12
|
+
import { registerInactivityMails } from "./mail-notifications.ts";
|
|
13
|
+
import type { TenantInactivityOptions } from "./options.ts";
|
|
14
|
+
import { tenantInactivityStateEntity, tenantInactivityStateTable } from "./state-table.ts";
|
|
15
|
+
|
|
16
|
+
const CHECK_JOB_ESCAPE_HATCH_REASON =
|
|
17
|
+
"reads the tenant's lifecycle status on the system tenant and files the deletion request through the shared tenant-lifecycle core, which writes the tenant row and its event stream with a raw runner";
|
|
18
|
+
|
|
19
|
+
const tenantInactivityStateDestroyHook: TenantDataDestroyHook = async (ctx) => {
|
|
20
|
+
await deleteMany(ctx.db, tenantInactivityStateTable, { tenantId: ctx.tenantId });
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Opt-in by mounting. Free accounts without activity get warning mails, then a normal deletion
|
|
25
|
+
* request through tenant-lifecycle; paid tiers are exempt. The option value carries functions
|
|
26
|
+
* (rules per tier, activity sources), so the feature is never deduplicated by options.
|
|
27
|
+
*/
|
|
28
|
+
export function createTenantInactivityFeature<TTier extends string = string>(
|
|
29
|
+
options: TenantInactivityOptions<TTier>,
|
|
30
|
+
): FeatureDefinition {
|
|
31
|
+
return defineFeature(TENANT_INACTIVITY_FEATURE, (r) => {
|
|
32
|
+
r.describe(
|
|
33
|
+
"Deletes abandoned free accounts: warns the tenant owners before the deadline, then files the regular tenant-lifecycle deletion request. Activity or the keep-account link restarts the period; tiers without a rule are exempt.",
|
|
34
|
+
);
|
|
35
|
+
r.uiHints({
|
|
36
|
+
displayLabel: "Tenant Inactivity · Deletion",
|
|
37
|
+
category: "compliance",
|
|
38
|
+
recommended: false,
|
|
39
|
+
});
|
|
40
|
+
r.requires({ features: ["sessions", "tenant", "tenant-lifecycle", "delivery"] });
|
|
41
|
+
|
|
42
|
+
r.storeTable(
|
|
43
|
+
deriveEntityTableMeta(TENANT_INACTIVITY_STATE_ENTITY, tenantInactivityStateEntity, {
|
|
44
|
+
source: "unmanaged",
|
|
45
|
+
}),
|
|
46
|
+
{ reason: "read_side.tenant_inactivity_state_direct_write" },
|
|
47
|
+
);
|
|
48
|
+
r.useExtension(EXT_TENANT_DATA, TENANT_INACTIVITY_STATE_ENTITY, {
|
|
49
|
+
destroy: tenantInactivityStateDestroyHook,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
registerInactivityMails(r);
|
|
53
|
+
|
|
54
|
+
r.job({
|
|
55
|
+
name: "check-inactive-tenants",
|
|
56
|
+
trigger: { maintenance: true, wakesSleepingApp: false },
|
|
57
|
+
perTenant: true,
|
|
58
|
+
concurrency: "skip",
|
|
59
|
+
escapeHatch: { grants: ["unsafeRaw"], reason: CHECK_JOB_ESCAPE_HATCH_REASON },
|
|
60
|
+
handler: async (_payload, ctx) => checkTenantInactivity(ctx, options),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
r.writeHandler(createKeepAccountHandler(options));
|
|
64
|
+
r.queryHandler(createInactivityStateQuery(options));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createTenantDb, type TenantDb } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import { defineQueryHandler, parseTenantId } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import { AccessDeniedError, UnprocessableError } from "@cosmicdrift/kumiko-framework/errors";
|
|
4
|
+
import * as z from "zod";
|
|
5
|
+
import { describeOutlook, foldActivity } from "../inactivity-plan.ts";
|
|
6
|
+
import { resolveInactivityRule, type TenantInactivityOptions } from "../options.ts";
|
|
7
|
+
import { loadInactivityState, loadNewestSessionActivity } from "../state-store.ts";
|
|
8
|
+
|
|
9
|
+
const STATE_QUERY_ESCAPE_HATCH_REASON =
|
|
10
|
+
"a SystemAdmin reads the inactivity state of any tenant through a database handle pinned to that tenant";
|
|
11
|
+
|
|
12
|
+
// Folds the sessions' newest activity in like the daily job does, so a login shows up before
|
|
13
|
+
// the next job run.
|
|
14
|
+
export function createInactivityStateQuery<TTier extends string>(
|
|
15
|
+
options: TenantInactivityOptions<TTier>,
|
|
16
|
+
) {
|
|
17
|
+
return defineQueryHandler({
|
|
18
|
+
name: "state",
|
|
19
|
+
schema: z.object({ tenantIdOverride: z.string().min(1).max(36).optional() }),
|
|
20
|
+
access: { roles: ["TenantOwner", "TenantAdmin", "SystemAdmin"] },
|
|
21
|
+
escapeHatch: { grants: ["unsafeRaw"], reason: STATE_QUERY_ESCAPE_HATCH_REASON },
|
|
22
|
+
description:
|
|
23
|
+
"Returns whether the tenant is exempt from inactivity deletion, its last activity, the warnings already sent and the next step with its date; owners and admins see their own tenant, a SystemAdmin any tenant.",
|
|
24
|
+
handler: async (query, ctx) => {
|
|
25
|
+
const requested = query.payload.tenantIdOverride;
|
|
26
|
+
const isOtherTenant = requested !== undefined && requested !== query.user.tenantId;
|
|
27
|
+
let db: TenantDb = ctx.db;
|
|
28
|
+
if (isOtherTenant) {
|
|
29
|
+
if (!query.user.roles.includes("SystemAdmin")) throw new AccessDeniedError();
|
|
30
|
+
const tenantId = parseTenantId(requested);
|
|
31
|
+
if (tenantId === null) throw new UnprocessableError("invalid_tenant_id");
|
|
32
|
+
db = createTenantDb({ db: ctx.db.unsafeRaw(), tenantId });
|
|
33
|
+
}
|
|
34
|
+
const rule = await resolveInactivityRule(options, db);
|
|
35
|
+
const stored = await loadInactivityState(db);
|
|
36
|
+
const folded = stored
|
|
37
|
+
? foldActivity(stored, await loadNewestSessionActivity(db)).state
|
|
38
|
+
: null;
|
|
39
|
+
const outlook = describeOutlook(folded, rule);
|
|
40
|
+
return {
|
|
41
|
+
exempt: outlook.exempt,
|
|
42
|
+
lastActivityAt: outlook.lastActivityAt?.toString() ?? null,
|
|
43
|
+
warningsSent: outlook.warningsSent,
|
|
44
|
+
nextStep: outlook.nextStep,
|
|
45
|
+
nextStepAt: outlook.nextStepAt?.toString() ?? null,
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { fetchOne, updateMany } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import { UnprocessableError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
4
|
+
import * as z from "zod";
|
|
5
|
+
import { redeemKeepAccountToken } from "../keep-token.ts";
|
|
6
|
+
import { tenantInactivityStateTable } from "../state-table.ts";
|
|
7
|
+
|
|
8
|
+
const KEEP_ACCOUNT_ESCAPE_HATCH_REASON =
|
|
9
|
+
"the anonymous link holder has no tenant scope; the tenant comes from the signed token and every statement filters by that tenantId";
|
|
10
|
+
|
|
11
|
+
export function createKeepAccountHandler(opts: { readonly keepAccountTokenSecret?: string }) {
|
|
12
|
+
return defineWriteHandler({
|
|
13
|
+
name: "keep-account",
|
|
14
|
+
schema: z.object({ token: z.string().min(1) }),
|
|
15
|
+
access: { roles: ["anonymous", "Member", "User", "TenantAdmin", "TenantOwner", "SystemAdmin"] },
|
|
16
|
+
escapeHatch: { grants: ["unsafeRaw"], reason: KEEP_ACCOUNT_ESCAPE_HATCH_REASON },
|
|
17
|
+
agent: { expose: false },
|
|
18
|
+
rateLimit: { per: "ip+handler", limit: 10, windowSeconds: 60 },
|
|
19
|
+
description:
|
|
20
|
+
"Redeems the keep-account link from an inactivity warning mail: counts as activity, resets the warning cycle and invalidates every earlier link.",
|
|
21
|
+
handler: async (event, ctx) => {
|
|
22
|
+
const runner = ctx.db.unsafeRaw();
|
|
23
|
+
const now = ctx.tz.now();
|
|
24
|
+
const redeemed = await redeemKeepAccountToken({
|
|
25
|
+
token: event.payload.token,
|
|
26
|
+
secret: opts.keepAccountTokenSecret,
|
|
27
|
+
now,
|
|
28
|
+
// The tenant id comes from the not-yet-verified token; a malformed value must end in the
|
|
29
|
+
// generic 422, so the lookup error is swallowed by the grant's anchor-lookup guard.
|
|
30
|
+
loadCycleId: async (tenantId) => {
|
|
31
|
+
const row = await fetchOne<{ cycleId: string }>(runner, tenantInactivityStateTable, {
|
|
32
|
+
tenantId,
|
|
33
|
+
});
|
|
34
|
+
return row?.cycleId ?? null;
|
|
35
|
+
},
|
|
36
|
+
commitKeep: async (tenantId, cycleId) => {
|
|
37
|
+
const updated = await updateMany(
|
|
38
|
+
runner,
|
|
39
|
+
tenantInactivityStateTable,
|
|
40
|
+
{
|
|
41
|
+
lastActivityAt: now,
|
|
42
|
+
warningsSent: 0,
|
|
43
|
+
lastWarnedAt: null,
|
|
44
|
+
postponedDueAt: null,
|
|
45
|
+
cycleId: crypto.randomUUID(),
|
|
46
|
+
},
|
|
47
|
+
{ tenantId, cycleId },
|
|
48
|
+
);
|
|
49
|
+
return updated.length === 1;
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
if (!redeemed.ok) return writeFailure(new UnprocessableError("invalid_or_expired_token"));
|
|
53
|
+
return { isSuccess: true as const, data: { kept: true } };
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addDurationSpec,
|
|
3
|
+
type DurationSpec,
|
|
4
|
+
type RetentionSpec,
|
|
5
|
+
} from "@cosmicdrift/kumiko-framework/compliance";
|
|
6
|
+
import { Temporal } from "@cosmicdrift/kumiko-types/temporal";
|
|
7
|
+
|
|
8
|
+
type Instant = Temporal.Instant;
|
|
9
|
+
|
|
10
|
+
export type InactivityRule = {
|
|
11
|
+
readonly after: RetentionSpec;
|
|
12
|
+
// Lead times before the deadline, in any order.
|
|
13
|
+
readonly warnings: readonly DurationSpec[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type InactivityState = {
|
|
17
|
+
readonly firstSeenAt: Instant;
|
|
18
|
+
readonly lastActivityAt: Instant | null;
|
|
19
|
+
readonly lastExemptAt: Instant | null;
|
|
20
|
+
readonly warningsSent: number;
|
|
21
|
+
readonly lastWarnedAt: Instant | null;
|
|
22
|
+
// Set when a deadline passed without all warnings out: the last warning is sent late and
|
|
23
|
+
// the deletion request moves behind it.
|
|
24
|
+
readonly postponedDueAt: Instant | null;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type InactivityStep = "warning" | "destructionRequest";
|
|
28
|
+
|
|
29
|
+
export type InactivityPlan = {
|
|
30
|
+
readonly step: InactivityStep;
|
|
31
|
+
readonly at: Instant;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type InactivityDecision =
|
|
35
|
+
| { readonly kind: "none" }
|
|
36
|
+
| {
|
|
37
|
+
readonly kind: "warn";
|
|
38
|
+
readonly warningsSentAfter: number;
|
|
39
|
+
readonly deadline: Instant;
|
|
40
|
+
readonly postponedDueAt: Instant | null;
|
|
41
|
+
}
|
|
42
|
+
| { readonly kind: "destroy" };
|
|
43
|
+
|
|
44
|
+
const EPOCH = Temporal.Instant.fromEpochMilliseconds(0);
|
|
45
|
+
|
|
46
|
+
export function addRetentionSpec(from: Instant, spec: RetentionSpec): Instant {
|
|
47
|
+
if ("months" in spec) {
|
|
48
|
+
return from.toZonedDateTimeISO("UTC").add({ months: spec.months }).toInstant();
|
|
49
|
+
}
|
|
50
|
+
if ("years" in spec) {
|
|
51
|
+
return from.toZonedDateTimeISO("UTC").add({ years: spec.years }).toInstant();
|
|
52
|
+
}
|
|
53
|
+
return addDurationSpec(from, spec);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function leadMs(spec: DurationSpec): number {
|
|
57
|
+
return addDurationSpec(EPOCH, spec).epochMilliseconds;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Longest lead first, so index 0 is the earliest warning.
|
|
61
|
+
export function sortedWarnings(rule: InactivityRule): readonly DurationSpec[] {
|
|
62
|
+
return [...rule.warnings].sort((a, b) => leadMs(b) - leadMs(a));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function laterOf(instants: readonly (Instant | null)[]): Instant | null {
|
|
66
|
+
let latest: Instant | null = null;
|
|
67
|
+
for (const instant of instants) {
|
|
68
|
+
if (instant !== null && (latest === null || Temporal.Instant.compare(instant, latest) > 0)) {
|
|
69
|
+
latest = instant;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return latest;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function activityBase(state: InactivityState): Instant {
|
|
76
|
+
return (
|
|
77
|
+
laterOf([state.firstSeenAt, state.lastActivityAt, state.lastExemptAt]) ?? state.firstSeenAt
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Activity after the last warning starts a new cycle.
|
|
82
|
+
export function isNewCycle(state: InactivityState): boolean {
|
|
83
|
+
return (
|
|
84
|
+
state.warningsSent > 0 &&
|
|
85
|
+
state.lastWarnedAt !== null &&
|
|
86
|
+
Temporal.Instant.compare(activityBase(state), state.lastWarnedAt) > 0
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function resetWarnings(state: InactivityState): InactivityState {
|
|
91
|
+
return { ...state, warningsSent: 0, lastWarnedAt: null, postponedDueAt: null };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function dueAt(state: InactivityState, rule: InactivityRule): Instant {
|
|
95
|
+
const regular = addRetentionSpec(activityBase(state), rule.after);
|
|
96
|
+
return laterOf([regular, state.postponedDueAt]) ?? regular;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Expects a state that already went through the new-cycle reset.
|
|
100
|
+
export function planNextStep(state: InactivityState, rule: InactivityRule): InactivityPlan {
|
|
101
|
+
const due = dueAt(state, rule);
|
|
102
|
+
const warnings = sortedWarnings(rule);
|
|
103
|
+
const nextWarning = warnings[state.warningsSent];
|
|
104
|
+
if (nextWarning === undefined) return { step: "destructionRequest", at: due };
|
|
105
|
+
const warnAt = Temporal.Instant.fromEpochMilliseconds(
|
|
106
|
+
due.epochMilliseconds - leadMs(nextWarning),
|
|
107
|
+
);
|
|
108
|
+
return { step: "warning", at: warnAt };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// At most one warning per run: the latest one that is due. A deadline that passed with warnings
|
|
112
|
+
// missing sends the final warning late and moves the deletion request behind it.
|
|
113
|
+
export function decideInactivityAction(
|
|
114
|
+
state: InactivityState,
|
|
115
|
+
rule: InactivityRule,
|
|
116
|
+
now: Instant,
|
|
117
|
+
): InactivityDecision {
|
|
118
|
+
const due = dueAt(state, rule);
|
|
119
|
+
const warnings = sortedWarnings(rule);
|
|
120
|
+
const total = warnings.length;
|
|
121
|
+
if (state.warningsSent >= total) {
|
|
122
|
+
return Temporal.Instant.compare(now, due) >= 0 ? { kind: "destroy" } : { kind: "none" };
|
|
123
|
+
}
|
|
124
|
+
const finalLead = warnings[total - 1];
|
|
125
|
+
if (finalLead !== undefined && Temporal.Instant.compare(now, due) >= 0) {
|
|
126
|
+
const postponedDueAt = addDurationSpec(now, finalLead);
|
|
127
|
+
return {
|
|
128
|
+
kind: "warn",
|
|
129
|
+
warningsSentAfter: total,
|
|
130
|
+
deadline: postponedDueAt,
|
|
131
|
+
postponedDueAt,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
let lastDue = -1;
|
|
135
|
+
for (let index = state.warningsSent; index < total; index++) {
|
|
136
|
+
const lead = warnings[index];
|
|
137
|
+
if (lead !== undefined && now.epochMilliseconds >= due.epochMilliseconds - leadMs(lead)) {
|
|
138
|
+
lastDue = index;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (lastDue < 0) return { kind: "none" };
|
|
142
|
+
return { kind: "warn", warningsSentAfter: lastDue + 1, deadline: due, postponedDueAt: null };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Raises lastActivityAt to the newest known activity; activity after the last warning starts a
|
|
146
|
+
// new cycle, which also drops a postponed deadline.
|
|
147
|
+
export function foldActivity(
|
|
148
|
+
state: InactivityState,
|
|
149
|
+
newest: Instant | null,
|
|
150
|
+
): { readonly state: InactivityState; readonly newCycle: boolean } {
|
|
151
|
+
const lastActivityAt = laterOf([state.lastActivityAt, newest]);
|
|
152
|
+
const raised: InactivityState = { ...state, lastActivityAt };
|
|
153
|
+
if (!isNewCycle(raised)) return { state: raised, newCycle: false };
|
|
154
|
+
return { state: resetWarnings(raised), newCycle: true };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type InactivityOutlook = {
|
|
158
|
+
readonly exempt: boolean;
|
|
159
|
+
readonly lastActivityAt: Instant | null;
|
|
160
|
+
readonly warningsSent: number;
|
|
161
|
+
readonly nextStep: InactivityStep | null;
|
|
162
|
+
readonly nextStepAt: Instant | null;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// Shared by the daily job and the state query so both name the same next step.
|
|
166
|
+
export function describeOutlook(
|
|
167
|
+
state: InactivityState | null,
|
|
168
|
+
rule: InactivityRule | null,
|
|
169
|
+
): InactivityOutlook {
|
|
170
|
+
if (rule === null || state === null) {
|
|
171
|
+
return {
|
|
172
|
+
exempt: rule === null,
|
|
173
|
+
lastActivityAt: state?.lastActivityAt ?? null,
|
|
174
|
+
warningsSent: state?.warningsSent ?? 0,
|
|
175
|
+
nextStep: null,
|
|
176
|
+
nextStepAt: null,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const plan = planNextStep(state, rule);
|
|
180
|
+
return {
|
|
181
|
+
exempt: false,
|
|
182
|
+
lastActivityAt: state.lastActivityAt,
|
|
183
|
+
warningsSent: state.warningsSent,
|
|
184
|
+
nextStep: plan.step,
|
|
185
|
+
nextStepAt: plan.at,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { TenantInactivityHandlers, TenantInactivityJobs } from "./constants.ts";
|
|
2
|
+
export { createTenantInactivityFeature } from "./feature.ts";
|
|
3
|
+
export type { InactivityRule } from "./inactivity-plan.ts";
|
|
4
|
+
export type { TenantActivitySource, TenantInactivityOptions } from "./options.ts";
|
|
5
|
+
export { fetchRemoteAppLastActivity } from "./remote-activity.ts";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Temporal } from "@cosmicdrift/kumiko-types/temporal";
|
|
2
|
+
import {
|
|
3
|
+
type RowBoundGrantResult,
|
|
4
|
+
redeemRowBoundGrant,
|
|
5
|
+
signRowBoundGrant,
|
|
6
|
+
} from "../shared/index.ts";
|
|
7
|
+
|
|
8
|
+
const KEEP_ACCOUNT_PURPOSE = "keep-account";
|
|
9
|
+
|
|
10
|
+
// Subject is the tenant, anchor the state row's cycleId: any activity or a redeemed link rotates
|
|
11
|
+
// the cycleId, so every earlier link stops verifying.
|
|
12
|
+
export function signKeepAccountToken(args: {
|
|
13
|
+
readonly tenantId: string;
|
|
14
|
+
readonly cycleId: string;
|
|
15
|
+
readonly ttlMinutes: number;
|
|
16
|
+
readonly secret: string;
|
|
17
|
+
readonly now: Temporal.Instant;
|
|
18
|
+
}): string {
|
|
19
|
+
return signRowBoundGrant({
|
|
20
|
+
subject: args.tenantId,
|
|
21
|
+
purpose: KEEP_ACCOUNT_PURPOSE,
|
|
22
|
+
anchor: args.cycleId,
|
|
23
|
+
ttlMinutes: args.ttlMinutes,
|
|
24
|
+
secret: args.secret,
|
|
25
|
+
now: args.now,
|
|
26
|
+
}).token;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function redeemKeepAccountToken(args: {
|
|
30
|
+
readonly token: string;
|
|
31
|
+
readonly secret: string | undefined;
|
|
32
|
+
readonly loadCycleId: (tenantId: string) => Promise<string | null>;
|
|
33
|
+
readonly commitKeep: (tenantId: string, cycleId: string) => Promise<boolean>;
|
|
34
|
+
readonly now: Temporal.Instant;
|
|
35
|
+
}): Promise<RowBoundGrantResult> {
|
|
36
|
+
return redeemRowBoundGrant({
|
|
37
|
+
token: args.token,
|
|
38
|
+
purpose: KEEP_ACCOUNT_PURPOSE,
|
|
39
|
+
secret: args.secret,
|
|
40
|
+
loadAnchor: args.loadCycleId,
|
|
41
|
+
commitAnchor: args.commitKeep,
|
|
42
|
+
now: args.now,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type FeatureRegistrar,
|
|
3
|
+
QnTypes,
|
|
4
|
+
qn,
|
|
5
|
+
type TemplateVariables,
|
|
6
|
+
templateVariable,
|
|
7
|
+
} from "@cosmicdrift/kumiko-framework/engine";
|
|
8
|
+
import { mailT } from "@cosmicdrift/kumiko-framework/i18n";
|
|
9
|
+
import { escapeHtml } from "@cosmicdrift/kumiko-renderer/headless";
|
|
10
|
+
import { createMailLocaleTemplates, mailParagraph } from "../shared/index.ts";
|
|
11
|
+
import { TENANT_INACTIVITY_FEATURE } from "./constants.ts";
|
|
12
|
+
|
|
13
|
+
const WARNING_MAIL_NAME = "warning";
|
|
14
|
+
|
|
15
|
+
export const InactivityWarningNotificationType = qn(
|
|
16
|
+
TENANT_INACTIVITY_FEATURE,
|
|
17
|
+
QnTypes.notify,
|
|
18
|
+
WARNING_MAIL_NAME,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
const WARNING_SUBJECT_KEY = "inactivity.mail.warning.subject";
|
|
22
|
+
|
|
23
|
+
// Translation placeholder -> template variable.
|
|
24
|
+
// The translation slot is named account because locale-de rejects the word tenant in values.
|
|
25
|
+
const SLOTS = { app: "{app}", account: "{tenant}", when: "{when}" } as const;
|
|
26
|
+
const SUBJECT_SLOTS = { app: "{{app}}", account: "{{tenant}}", when: "{{when}}" } as const;
|
|
27
|
+
|
|
28
|
+
const WARNING_VARIABLES: TemplateVariables = {
|
|
29
|
+
app: templateVariable.text({ required: true }),
|
|
30
|
+
tenant: templateVariable.text({ required: true }),
|
|
31
|
+
when: templateVariable.text({ required: true }),
|
|
32
|
+
keepUrl: templateVariable.link({ required: true }),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function warningBody(locale: string): string {
|
|
36
|
+
const paragraph = (part: string) =>
|
|
37
|
+
mailParagraph(mailT(locale, `inactivity.mail.warning.${part}`, SLOTS));
|
|
38
|
+
return [
|
|
39
|
+
mailParagraph(mailT(locale, "gdpr.mail.greeting")),
|
|
40
|
+
paragraph("intro"),
|
|
41
|
+
paragraph("keep"),
|
|
42
|
+
`<p><a data-button href="{{keepUrl}}">${escapeHtml(mailT(locale, "inactivity.mail.warning.button"))}</a></p>`,
|
|
43
|
+
paragraph("after"),
|
|
44
|
+
].join("");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const mailLocaleTemplates = createMailLocaleTemplates({
|
|
48
|
+
kinds: [WARNING_MAIL_NAME],
|
|
49
|
+
notificationTypes: { [WARNING_MAIL_NAME]: InactivityWarningNotificationType },
|
|
50
|
+
subjectKey: () => WARNING_SUBJECT_KEY,
|
|
51
|
+
bodyFor: (_kind, locale) => warningBody(locale),
|
|
52
|
+
subjectSlots: () => SUBJECT_SLOTS,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export const inactivityMailTemplateLocale = mailLocaleTemplates.templateLocale;
|
|
56
|
+
|
|
57
|
+
export function registerInactivityMails(r: FeatureRegistrar): void {
|
|
58
|
+
r.notification(WARNING_MAIL_NAME, {
|
|
59
|
+
variables: WARNING_VARIABLES,
|
|
60
|
+
localizedTemplate: mailLocaleTemplates.localizedTemplate(WARNING_MAIL_NAME),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { TenantDb } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import type { TenantId } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import type { Temporal } from "@cosmicdrift/kumiko-types/temporal";
|
|
4
|
+
import type { InactivityRule } from "./inactivity-plan.ts";
|
|
5
|
+
|
|
6
|
+
/** Newest activity an app knows outside this app's sessions (for example a sibling app); null when none. */
|
|
7
|
+
export type TenantActivitySource = (args: {
|
|
8
|
+
readonly tenantId: TenantId;
|
|
9
|
+
readonly now: Temporal.Instant;
|
|
10
|
+
}) => Promise<Temporal.Instant | null>;
|
|
11
|
+
|
|
12
|
+
type SharedOptions = {
|
|
13
|
+
/** HMAC key of the keep-account links. Unset: mails link to keepAccountUrl without a token. */
|
|
14
|
+
readonly keepAccountTokenSecret?: string;
|
|
15
|
+
/** App page that reads the `#token=` fragment and calls the keep-account handler. */
|
|
16
|
+
readonly keepAccountUrl: string;
|
|
17
|
+
/** Name shown in the mails; defaults to a generic word per locale. */
|
|
18
|
+
readonly appName?: string;
|
|
19
|
+
readonly activitySources?: readonly TenantActivitySource[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type TenantInactivityOptions<TTier extends string = string> = SharedOptions &
|
|
23
|
+
(
|
|
24
|
+
| { readonly rule: InactivityRule }
|
|
25
|
+
| {
|
|
26
|
+
readonly resolveTier: (db: TenantDb) => Promise<TTier>;
|
|
27
|
+
/** null exempts the tier (paid plans). */
|
|
28
|
+
readonly rulesByTier: Readonly<Record<TTier, InactivityRule | null>>;
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export async function resolveInactivityRule<TTier extends string>(
|
|
33
|
+
options: TenantInactivityOptions<TTier>,
|
|
34
|
+
db: TenantDb,
|
|
35
|
+
): Promise<InactivityRule | null> {
|
|
36
|
+
if ("rule" in options) return options.rule;
|
|
37
|
+
return options.rulesByTier[await options.resolveTier(db)];
|
|
38
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { egress } from "@cosmicdrift/kumiko-framework/http";
|
|
2
|
+
import { Temporal } from "@cosmicdrift/kumiko-types/temporal";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
|
|
5
|
+
const probeResponseSchema = z.object({ lastActivityAt: z.string().nullable() });
|
|
6
|
+
|
|
7
|
+
type SendRequest = (url: string, init: RequestInit) => Promise<Response>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Asks a sibling app for its newest session activity (the sessions `lastActivityProbeSecret`
|
|
11
|
+
* route). Use it inside a `TenantActivitySource` so a user active in the other app keeps the
|
|
12
|
+
* account alive. Fails closed: any error throws, and the job then sends no mail and files no
|
|
13
|
+
* deletion request for that run. `send` exists for tests; production uses the internal egress
|
|
14
|
+
* pinned to the origin of `url`.
|
|
15
|
+
*/
|
|
16
|
+
export async function fetchRemoteAppLastActivity(args: {
|
|
17
|
+
readonly url: string;
|
|
18
|
+
readonly secret: string;
|
|
19
|
+
readonly send?: SendRequest;
|
|
20
|
+
}): Promise<Temporal.Instant | null> {
|
|
21
|
+
// Sibling apps sit in the same cluster on private addresses, which the external policy blocks.
|
|
22
|
+
const send = args.send ?? egress({ kind: "internal", allowOrigins: [new URL(args.url).origin] });
|
|
23
|
+
const response = await send(args.url, {
|
|
24
|
+
method: "GET",
|
|
25
|
+
headers: { authorization: `Bearer ${args.secret}` },
|
|
26
|
+
});
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error(`remote activity probe answered ${response.status}`);
|
|
29
|
+
}
|
|
30
|
+
const body = probeResponseSchema.parse(await response.json());
|
|
31
|
+
return body.lastActivityAt === null ? null : Temporal.Instant.from(body.lastActivityAt);
|
|
32
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { TenantDb } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import { Temporal } from "@cosmicdrift/kumiko-types/temporal";
|
|
3
|
+
import { userSessionTable } from "../sessions/index.ts";
|
|
4
|
+
import type { InactivityState } from "./inactivity-plan.ts";
|
|
5
|
+
import { tenantInactivityStateTable } from "./state-table.ts";
|
|
6
|
+
|
|
7
|
+
export type StoredInactivityState = InactivityState & { readonly cycleId: string };
|
|
8
|
+
|
|
9
|
+
type StateRow = StoredInactivityState & { readonly id: string };
|
|
10
|
+
|
|
11
|
+
const EPOCH = Temporal.Instant.fromEpochMilliseconds(0);
|
|
12
|
+
|
|
13
|
+
export async function loadInactivityState(db: TenantDb): Promise<StoredInactivityState | null> {
|
|
14
|
+
const row = await db.fetchOne<StateRow>(tenantInactivityStateTable, { tenantId: db.tenantId });
|
|
15
|
+
if (!row) return null;
|
|
16
|
+
return {
|
|
17
|
+
cycleId: row.cycleId,
|
|
18
|
+
firstSeenAt: row.firstSeenAt,
|
|
19
|
+
lastActivityAt: row.lastActivityAt,
|
|
20
|
+
lastExemptAt: row.lastExemptAt,
|
|
21
|
+
lastWarnedAt: row.lastWarnedAt,
|
|
22
|
+
postponedDueAt: row.postponedDueAt,
|
|
23
|
+
warningsSent: row.warningsSent,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function sameInstant(a: Temporal.Instant | null, b: Temporal.Instant | null): boolean {
|
|
28
|
+
if (a === null || b === null) return a === b;
|
|
29
|
+
return a.epochMilliseconds === b.epochMilliseconds;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isSameInactivityState(
|
|
33
|
+
a: StoredInactivityState | null,
|
|
34
|
+
b: StoredInactivityState,
|
|
35
|
+
): boolean {
|
|
36
|
+
return (
|
|
37
|
+
a !== null &&
|
|
38
|
+
a.cycleId === b.cycleId &&
|
|
39
|
+
a.warningsSent === b.warningsSent &&
|
|
40
|
+
sameInstant(a.firstSeenAt, b.firstSeenAt) &&
|
|
41
|
+
sameInstant(a.lastActivityAt, b.lastActivityAt) &&
|
|
42
|
+
sameInstant(a.lastExemptAt, b.lastExemptAt) &&
|
|
43
|
+
sameInstant(a.lastWarnedAt, b.lastWarnedAt) &&
|
|
44
|
+
sameInstant(a.postponedDueAt, b.postponedDueAt)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Writes only when something changed, so a run that decides nothing stays read-only.
|
|
49
|
+
export async function saveInactivityState(
|
|
50
|
+
db: TenantDb,
|
|
51
|
+
previous: StoredInactivityState | null,
|
|
52
|
+
next: StoredInactivityState,
|
|
53
|
+
): Promise<void> {
|
|
54
|
+
// skip: nothing changed, so the run stays read-only
|
|
55
|
+
if (isSameInactivityState(previous, next)) return;
|
|
56
|
+
if (previous === null) {
|
|
57
|
+
await db.insertOne(tenantInactivityStateTable, { tenantId: db.tenantId, ...next });
|
|
58
|
+
} else {
|
|
59
|
+
await db.updateMany(tenantInactivityStateTable, next, { tenantId: db.tenantId });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The sessions table keeps the activity marker; reading it here avoids a write per request.
|
|
64
|
+
export async function loadNewestSessionActivity(db: TenantDb): Promise<Temporal.Instant | null> {
|
|
65
|
+
const [newest] = await db.selectMany<{ lastSeenAt: Temporal.Instant | null }>(
|
|
66
|
+
userSessionTable,
|
|
67
|
+
{ tenantId: db.tenantId, lastSeenAt: { gte: EPOCH } },
|
|
68
|
+
{ limit: 1, orderBy: { col: "lastSeenAt", direction: "desc" } },
|
|
69
|
+
);
|
|
70
|
+
return newest?.lastSeenAt ?? null;
|
|
71
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { deriveEntityTableMeta } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import {
|
|
3
|
+
access,
|
|
4
|
+
createEntity,
|
|
5
|
+
createNumberField,
|
|
6
|
+
createTextField,
|
|
7
|
+
createTimestampField,
|
|
8
|
+
} from "@cosmicdrift/kumiko-framework/engine";
|
|
9
|
+
import { TENANT_INACTIVITY_STATE_ENTITY } from "./constants.ts";
|
|
10
|
+
|
|
11
|
+
// One row per tenant. Direct-write store: the daily job and the keep-account handler mutate
|
|
12
|
+
// columns, there is no aggregate or event stream behind it.
|
|
13
|
+
export const tenantInactivityStateEntity = createEntity({
|
|
14
|
+
table: "store_tenant_inactivity_state",
|
|
15
|
+
softDelete: false,
|
|
16
|
+
fields: {
|
|
17
|
+
tenantId: createTextField({
|
|
18
|
+
required: true,
|
|
19
|
+
maxLength: 36,
|
|
20
|
+
access: { write: access.privileged },
|
|
21
|
+
personal: false,
|
|
22
|
+
reason: "pseudonymous_fk",
|
|
23
|
+
}),
|
|
24
|
+
cycleId: createTextField({
|
|
25
|
+
required: true,
|
|
26
|
+
maxLength: 36,
|
|
27
|
+
access: { write: access.privileged },
|
|
28
|
+
personal: false,
|
|
29
|
+
reason: "technical_reference",
|
|
30
|
+
}),
|
|
31
|
+
firstSeenAt: createTimestampField({ required: true, access: { write: access.privileged } }),
|
|
32
|
+
lastActivityAt: createTimestampField({ access: { write: access.privileged } }),
|
|
33
|
+
lastExemptAt: createTimestampField({ access: { write: access.privileged } }),
|
|
34
|
+
lastWarnedAt: createTimestampField({ access: { write: access.privileged } }),
|
|
35
|
+
postponedDueAt: createTimestampField({ access: { write: access.privileged } }),
|
|
36
|
+
warningsSent: createNumberField({
|
|
37
|
+
required: true,
|
|
38
|
+
integer: true,
|
|
39
|
+
min: 0,
|
|
40
|
+
access: { write: access.privileged },
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
indexes: [{ unique: true, columns: ["tenantId"], name: "store_tenant_inactivity_state_tenant" }],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export const tenantInactivityStateTable = deriveEntityTableMeta(
|
|
47
|
+
TENANT_INACTIVITY_STATE_ENTITY,
|
|
48
|
+
tenantInactivityStateEntity,
|
|
49
|
+
{ source: "unmanaged" },
|
|
50
|
+
);
|