-
-
Notifications
You must be signed in to change notification settings - Fork 131
feat(backend): unpaid org retention warnings and 95-day app archive #2929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a5778b0
feat(backend): archive unpaid apps and warn before retention deletes
cursoragent 57fcb60
test(backend): stabilize canceled org retention alert unit tests
cursoragent 0a6b159
test(db): fix retention SQL checks for pgmq and queue order
cursoragent f388a13
fix(backend): harden canceled org retention alerts and SQL tests
cursoragent 85f701f
style(db): wrap retention SQL comments for SQLFluff LT05
cursoragent e3cd89b
style(db): wrap queue_canceled_org_retention_alerts comment
cursoragent 511c072
fix(backend): validate retention org_id and fail closed on Bento errors
cursoragent d9a4abe
chore: retrigger PR CI after cancelled run
cursoragent 24238d3
ci: skip redundant push test suite when a PR covers the branch
cursoragent 891e429
fix(backend): address cubic retention alert review comments
cursoragent 2e33b63
fix(ci): harden push-suite skip PR lookup for forks
cursoragent 6b67494
chore: merge main for CI flake mitigations
cursoragent 9997c11
fix(ci): retry Kong upstream-death 502s in backend tests
cursoragent e584cda
fix(ci): tidy Kong 502 retry loop typing in fetchTestRequest
cursoragent 6480533
fix(test): dismiss support usernames prompt in compatibility e2e
cursoragent 8ad0723
fix(ci): raise Windows CLI POSIX path job timeout to 10m
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
supabase/functions/_backend/triggers/canceled_org_retention_alerts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import type { MiddlewareKeyVariables } from '../utils/hono.ts' | ||
| import { Hono } from 'hono/tiny' | ||
| import { BRES, middlewareAPISecret, parseBody, simpleError } from '../utils/hono.ts' | ||
| import { cloudlog } from '../utils/logging.ts' | ||
| import { sendEventToTracking } from '../utils/tracking.ts' | ||
|
|
||
| type RetentionAlertType = 'bundles_deletion_warning' | 'app_deletion_warning' | ||
|
|
||
| interface CanceledOrgRetentionAlertPayload { | ||
| org_id: string | ||
| org_name?: string | ||
| management_email?: string | ||
| alert_type: RetentionAlertType | ||
| access_end?: string | ||
| days_until_deletion?: number | ||
| app_ids?: string[] | ||
| } | ||
|
|
||
| const ALERT_CONFIG = { | ||
| bundles_deletion_warning: { | ||
| bentoEvent: 'org:bundles_will_be_deleted', | ||
| trackingEvent: 'Bundles will be deleted', | ||
| icon: '📦', | ||
| }, | ||
| app_deletion_warning: { | ||
| bentoEvent: 'org:apps_will_be_deleted', | ||
| trackingEvent: 'Apps will be deleted', | ||
| icon: '🗑️', | ||
| }, | ||
| } as const satisfies Record<RetentionAlertType, { | ||
| bentoEvent: string | ||
| trackingEvent: string | ||
| icon: string | ||
| }> | ||
|
|
||
| const ORG_ID_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i | ||
|
|
||
| function isRetentionAlertType(value: unknown): value is RetentionAlertType { | ||
| return value === 'bundles_deletion_warning' || value === 'app_deletion_warning' | ||
| } | ||
|
|
||
| /** Cycle key matching SQL to_char(access_end AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'). */ | ||
| function accessEndCycleKey(accessEnd: unknown): string { | ||
| if (typeof accessEnd !== 'string' || !accessEnd.trim()) | ||
| return 'unknown' | ||
| const parsed = new Date(accessEnd) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| if (Number.isNaN(parsed.getTime())) | ||
| return 'invalid' | ||
| return parsed.toISOString().replace(/\.\d{3}Z$/, 'Z') | ||
| } | ||
|
|
||
| export const app = new Hono<MiddlewareKeyVariables>() | ||
|
|
||
| app.post('/', middlewareAPISecret, async (c) => { | ||
| const payload = await parseBody<CanceledOrgRetentionAlertPayload | null>(c) | ||
| if (!payload || typeof payload !== 'object') { | ||
| throw simpleError('invalid_payload', 'Missing retention alert payload', { | ||
| hasPayload: false, | ||
| }) | ||
| } | ||
|
|
||
| const orgId = typeof payload.org_id === 'string' ? payload.org_id.trim() : '' | ||
| const alertType = payload.alert_type | ||
|
|
||
| if (!orgId || !ORG_ID_UUID_RE.test(orgId) || !isRetentionAlertType(alertType)) { | ||
| throw simpleError( | ||
| 'invalid_payload', | ||
| 'Missing or invalid org_id/alert_type in retention alert payload', | ||
| { | ||
| hasOrgId: orgId.length > 0, | ||
| orgIdValid: orgId.length > 0 && ORG_ID_UUID_RE.test(orgId), | ||
| alertType: typeof alertType === 'string' ? alertType : typeof alertType, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| const config = ALERT_CONFIG[alertType] | ||
|
|
||
| const parsedDays = Number(payload.days_until_deletion ?? 5) | ||
| const daysUntilDeletion = Number.isFinite(parsedDays) ? parsedDays : 5 | ||
| const appIds = Array.isArray(payload.app_ids) ? payload.app_ids : [] | ||
| const accessEndKey = accessEndCycleKey(payload.access_end) | ||
| const uniqId = `retention:${alertType}:${accessEndKey}` | ||
|
|
||
| const metadata = { | ||
| org_id: orgId, | ||
| org_name: payload.org_name ?? '', | ||
| management_email: payload.management_email ?? '', | ||
| alert_type: alertType, | ||
| access_end: typeof payload.access_end === 'string' ? payload.access_end : '', | ||
| days_until_deletion: daysUntilDeletion, | ||
| app_ids: appIds, | ||
| app_count: appIds.length, | ||
| } | ||
|
|
||
| cloudlog({ | ||
| requestId: c.get('requestId'), | ||
| message: 'canceled org retention alert', | ||
| eventName: config.bentoEvent, | ||
| orgId, | ||
| alertType, | ||
| daysUntilDeletion, | ||
| appCount: appIds.length, | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| await sendEventToTracking(c, { | ||
| bento: { | ||
| once: true, | ||
| data: metadata, | ||
| event: config.bentoEvent, | ||
| preferenceKey: 'usage_limit', | ||
| uniqId, | ||
| audience: 'billing', | ||
| }, | ||
| channel: 'usage', | ||
| event: config.trackingEvent, | ||
| icon: config.icon, | ||
| user_id: orgId, | ||
| groups: { organization: orgId }, | ||
| notify: false, | ||
| sentToBento: true, | ||
| tags: { | ||
| alert_type: alertType, | ||
| days_until_deletion: String(daysUntilDeletion), | ||
| app_count: String(appIds.length), | ||
| }, | ||
| }, { background: false, strict: true }) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| return c.json(BRES) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.