Files
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

67 lines
2.5 KiB
TypeScript

import { db, workspace } from '@sim/db'
import { createLogger } from '@sim/logger'
import { getErrorMessage } from '@sim/utils/errors'
import { eq } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { verifyCronAuth } from '@/lib/auth/internal'
import { hasWorkspaceInboxGraceAccess } from '@/lib/billing/core/subscription'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { disableInbox } from '@/lib/mothership/inbox/lifecycle'
const logger = createLogger('InboxEntitlementReconcileCron')
export const dynamic = 'force-dynamic'
/**
* Periodic inbox (Sim Mailer) entitlement reconciliation. Releases the AgentMail
* inbox + webhook for any workspace whose provisioned inbox has outlived its
* plan — i.e. `inboxEnabled` is still true but the billing entity no longer
* holds an entitled (active or `past_due`) Max/Enterprise subscription.
*
* Teardown keys off {@link hasWorkspaceInboxGraceAccess}, which tolerates
* `past_due`, so a transient payment failure never destroys a paying customer's
* inbox — only a genuinely terminal plan (canceled/downgraded) is reclaimed.
* `disableInbox` swallows AgentMail delete failures, so a rerun or a race with a
* manual disable is a no-op. On self-hosted / billing-disabled deployments the
* grace check returns true for every workspace, making this sweep inert.
*
* Scheduled in helm/sim/values.yaml under cronjobs.jobs.reconcileInboxEntitlement.
*/
export const GET = withRouteHandler(async (request: NextRequest) => {
const authError = verifyCronAuth(request, 'Inbox entitlement reconciliation')
if (authError) {
return authError
}
const enabledWorkspaces = await db
.select({ id: workspace.id })
.from(workspace)
.where(eq(workspace.inboxEnabled, true))
let disabled = 0
for (const ws of enabledWorkspaces) {
try {
if (await hasWorkspaceInboxGraceAccess(ws.id)) {
continue
}
await disableInbox(ws.id)
disabled++
logger.info('Reclaimed inbox for workspace with terminated Sim Mailer entitlement', {
workspaceId: ws.id,
})
} catch (error) {
logger.error('Failed to reconcile inbox entitlement for workspace', {
workspaceId: ws.id,
error: getErrorMessage(error, 'Unknown error'),
})
}
}
logger.info('Inbox entitlement reconciliation complete', {
checked: enabledWorkspaces.length,
disabled,
})
return NextResponse.json({ checked: enabledWorkspaces.length, disabled })
})