Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 54 additions & 62 deletions apps/api/src/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9993,6 +9993,7 @@ admin.openapi(getDevpassSubscribers, async (c) => {
.from(tables.organization)
.where(
and(
eq(tables.organization.kind, "devpass"),
ne(tables.organization.devPlan, "none"),
or(
isNull(tables.organization.devPlanExpiresAt),
Expand Down Expand Up @@ -10038,6 +10039,7 @@ admin.openapi(getDevpassSubscribers, async (c) => {
)
.where(
and(
eq(tables.organization.kind, "devpass"),
eq(tables.transaction.type, "dev_plan_start"),
eq(tables.organization.devPlan, "none"),
),
Comment on lines 10040 to 10045

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Include legacy starts in the churned KPI.

Now that this query is gated by organization.kind = "devpass", pre-rename subscription_start rows are safe to count here too. As written, legacy churned DevPass orgs can appear in the subscriber list via subscribedSinceSub, but are omitted from kpis.churned.

Proposed fix
 				eq(tables.organization.kind, "devpass"),
-				eq(tables.transaction.type, "dev_plan_start"),
+				inArray(tables.transaction.type, [
+					"dev_plan_start",
+					"subscription_start",
+				]),
 				eq(tables.organization.devPlan, "none"),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.where(
and(
eq(tables.organization.kind, "devpass"),
eq(tables.transaction.type, "dev_plan_start"),
eq(tables.organization.devPlan, "none"),
),
.where(
and(
eq(tables.organization.kind, "devpass"),
inArray(tables.transaction.type, [
"dev_plan_start",
"subscription_start",
]),
eq(tables.organization.devPlan, "none"),
),
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/api/src/routes/admin.ts` around lines 10040 - 10045, The WHERE clause in
the query is filtering for only "dev_plan_start" transaction types, which
excludes legacy "subscription_start" rows from the churned KPI count. Modify the
condition that checks `tables.transaction.type` to include both the new
"dev_plan_start" type and the legacy "subscription_start" type so that
pre-rename transaction rows are counted in the churned KPI calculation. Replace
the single equality check with a condition that accepts multiple transaction
type values.

Expand All @@ -10047,8 +10049,13 @@ admin.openapi(getDevpassSubscribers, async (c) => {
const [startsRow] = await db
.select({ count: sql<number>`COUNT(*)` })
.from(tables.transaction)
.innerJoin(
tables.organization,
eq(tables.transaction.organizationId, tables.organization.id),
)
.where(
and(
eq(tables.organization.kind, "devpass"),
eq(tables.transaction.type, "dev_plan_start"),
gte(tables.transaction.createdAt, monthStart),
),
Expand All @@ -10058,8 +10065,13 @@ admin.openapi(getDevpassSubscribers, async (c) => {
const [endsRow] = await db
.select({ count: sql<number>`COUNT(*)` })
.from(tables.transaction)
.innerJoin(
tables.organization,
eq(tables.transaction.organizationId, tables.organization.id),
)
.where(
and(
eq(tables.organization.kind, "devpass"),
inArray(tables.transaction.type, ["dev_plan_cancel", "dev_plan_end"]),
gte(tables.transaction.createdAt, monthStart),
),
Expand All @@ -10068,7 +10080,7 @@ admin.openapi(getDevpassSubscribers, async (c) => {

// Refunds this month for DevPass transactions. Mirrors the timeseries
// refund query (joins credit_refund rows to their original tx and filters
// to dev plan types, plus legacy subscription_* rows on personal orgs) but
// to dev plan types, plus legacy subscription_* rows on DevPass orgs) but
// aggregates to a single month total so the KPI strip reflects refund
// activity that the snapshot-based MRR cards can't show.
const refundOriginalTx = aliasedTable(
Expand All @@ -10094,13 +10106,11 @@ admin.openapi(getDevpassSubscribers, async (c) => {
eq(tables.transaction.type, "credit_refund"),
eq(tables.transaction.status, "completed"),
gte(tables.transaction.createdAt, monthStart),
or(
inArray(refundOriginalTx.type, [...DEV_PLAN_TX_TYPES]),
and(
inArray(refundOriginalTx.type, [...LEGACY_DEV_PLAN_TX_TYPES]),
eq(tables.organization.kind, "devpass"),
),
),
eq(tables.organization.kind, "devpass"),
inArray(refundOriginalTx.type, [
...DEV_PLAN_TX_TYPES,
...LEGACY_DEV_PLAN_TX_TYPES,
]),
),
);
const refundsThisMonth = Number(refundsRow?.count ?? 0);
Expand All @@ -10115,6 +10125,7 @@ admin.openapi(getDevpassSubscribers, async (c) => {
.from(tables.organization)
.where(
and(
eq(tables.organization.kind, "devpass"),
ne(tables.organization.devPlan, "none"),
or(
isNull(tables.organization.devPlanExpiresAt),
Expand All @@ -10138,7 +10149,12 @@ admin.openapi(getDevpassSubscribers, async (c) => {
realCostSub,
eq(tables.organization.id, realCostSub.organizationId),
)
.where(ne(tables.organization.devPlan, "none"));
.where(
and(
eq(tables.organization.kind, "devpass"),
ne(tables.organization.devPlan, "none"),
),
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const totalRealCostCycle = Number(universeRow?.totalCost ?? 0);
const totalMrrCycle = Number(universeRow?.totalMrr ?? 0);
const totalMargin = totalMrrCycle - totalRealCostCycle;
Expand Down Expand Up @@ -10255,8 +10271,9 @@ admin.openapi(getDevpassTimeseries, async (c) => {

// Resolve range. When no from/to is provided, default to all-time
// (anchored to the earliest dev plan start, falling back to today).
// Includes legacy `subscription_start` rows for personal orgs so subscribers
// from before the dev_plan_* rename also extend the chart range.
// Scoped to DevPass orgs (kind = 'devpass') so legacy `subscription_start`
// rows written before the dev_plan_* rename also extend the chart range,
// while org Pro `subscription_*` rows are excluded.
let startDate: Date;
let endDate: Date;
if (query.from && query.to) {
Expand All @@ -10275,12 +10292,12 @@ admin.openapi(getDevpassTimeseries, async (c) => {
eq(tables.transaction.organizationId, tables.organization.id),
)
.where(
or(
eq(tables.transaction.type, "dev_plan_start"),
and(
eq(tables.transaction.type, "subscription_start"),
eq(tables.organization.kind, "devpass"),
),
and(
eq(tables.organization.kind, "devpass"),
inArray(tables.transaction.type, [
"dev_plan_start",
"subscription_start",
]),
),
);
startDate = oldest?.minDate ? new Date(oldest.minDate) : now;
Expand All @@ -10295,8 +10312,8 @@ admin.openapi(getDevpassTimeseries, async (c) => {
}

// Revenue per day from completed DevPass transactions. Joins organization
// so legacy `subscription_*` rows can be counted only when the org is
// personal (where they are pre-rename dev plan rows, not org Pro).
// and scopes to kind = 'devpass' so legacy `subscription_*` rows are counted
// only on DevPass orgs (where they are pre-rename dev plan rows, not org Pro).
// Sums `amount` (actual dollars paid) — `creditAmount` is the credits
// granted (price × DEV_PLAN_CREDITS_MULTIPLIER) and would over-report
// revenue, and is null on legacy `subscription_*` rows so they would
Expand Down Expand Up @@ -10328,13 +10345,11 @@ admin.openapi(getDevpassTimeseries, async (c) => {
eq(tables.transaction.status, "completed"),
gte(tables.transaction.createdAt, startDate),
lte(tables.transaction.createdAt, endDate),
or(
inArray(tables.transaction.type, [...DEV_PLAN_TX_TYPES]),
and(
inArray(tables.transaction.type, [...LEGACY_DEV_PLAN_TX_TYPES]),
eq(tables.organization.kind, "devpass"),
),
),
eq(tables.organization.kind, "devpass"),
inArray(tables.transaction.type, [
...DEV_PLAN_TX_TYPES,
...LEGACY_DEV_PLAN_TX_TYPES,
]),
sql`NOT EXISTS (
SELECT 1 FROM ${tables.transaction} dup
WHERE dup.stripe_invoice_id = ${tables.transaction.stripeInvoiceId}
Expand Down Expand Up @@ -10385,23 +10400,20 @@ admin.openapi(getDevpassTimeseries, async (c) => {
eq(tables.transaction.status, "completed"),
gte(tables.transaction.createdAt, startDate),
lte(tables.transaction.createdAt, endDate),
or(
inArray(originalTx.type, [...DEV_PLAN_TX_TYPES]),
and(
inArray(originalTx.type, [...LEGACY_DEV_PLAN_TX_TYPES]),
eq(tables.organization.kind, "devpass"),
),
),
eq(tables.organization.kind, "devpass"),
inArray(originalTx.type, [
...DEV_PLAN_TX_TYPES,
...LEGACY_DEV_PLAN_TX_TYPES,
]),
),
)
.groupBy(sql`DATE(${tables.transaction.createdAt})`)
.orderBy(asc(sql`DATE(${tables.transaction.createdAt})`));

// Provider cost per day for projects belonging to orgs that are or were
// ever on a DevPass plan (i.e. currently devPlan != 'none' OR have a
// historical dev_plan_start, OR a legacy subscription_start while personal).
// This approximates "DevPass usage" without reconstructing daily plan
// membership.
// Provider cost per day for projects belonging to DevPass orgs
// (kind = 'devpass'), which is stable across the subscription lifecycle so
// churned orgs (devPlan = 'none') are still included without reconstructing
// daily plan membership.
const costPerDay = await db
.select({
date: sql<string>`DATE(${projectHourlyStats.hourTimestamp})`.as("date"),
Expand All @@ -10423,17 +10435,7 @@ admin.openapi(getDevpassTimeseries, async (c) => {
and(
gte(projectHourlyStats.hourTimestamp, startDate),
lte(projectHourlyStats.hourTimestamp, endDate),
or(
ne(tables.organization.devPlan, "none"),
sql`EXISTS (
SELECT 1 FROM ${tables.transaction} t
WHERE t.organization_id = ${tables.organization.id}
AND (
t.type = 'dev_plan_start'
OR (t.type = 'subscription_start' AND ${tables.organization.kind} = 'devpass')
)
)`,
)!,
eq(tables.organization.kind, "devpass"),
),
)
.groupBy(sql`DATE(${projectHourlyStats.hourTimestamp})`)
Expand Down Expand Up @@ -10523,19 +10525,9 @@ admin.openapi(getDevpassUsage, async (c) => {
endDate.setUTCHours(23, 59, 59, 999);
}

// Filter: only orgs that are or were ever on a DevPass plan.
// Mirrors the cost-per-day query in /devpass/timeseries.
const devpassOrgFilter = or(
ne(tables.organization.devPlan, "none"),
sql`EXISTS (
SELECT 1 FROM ${tables.transaction} t
WHERE t.organization_id = ${tables.organization.id}
AND (
t.type = 'dev_plan_start'
OR (t.type = 'subscription_start' AND ${tables.organization.kind} = 'devpass')
)
)`,
)!;
// Filter: only DevPass orgs (kind = 'devpass'). Stable across the
// subscription lifecycle, so churned orgs (devPlan = 'none') stay included.
const devpassOrgFilter = eq(tables.organization.kind, "devpass");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve subscription-history guard on DevPass usage

When a code-app signup or /dev-plans/personal-org call runs, getOrCreatePersonalOrg() creates a kind='devpass' org before any subscription while devPlan is still none; the previous predicate also required an active plan or a start transaction. With this kind-only filter, /devpass/usage (and the same kind-only cost filter above) can include rollups from never-subscribed personal orgs if they have project usage from migrated/personal credits, inflating DevPass usage/cost. Keep the kind check but retain a current-or-history DevPass predicate.

Useful? React with 👍 / 👎.


// Models + providers: use the per-project hourly model aggregator so the
// dashboard reads from rollups instead of the raw `log` table. Joins
Expand Down
Loading