Working on complete auth flow

This commit is contained in:
Owen
2026-02-04 16:32:39 -08:00
parent b5d76f73e8
commit 1bc4480d84
5 changed files with 125 additions and 22 deletions

View File

@@ -26,6 +26,7 @@ import logger from "@server/logger";
import { getFeatureIdByMetricId } from "@server/lib/billing/features";
import stripe from "#private/lib/stripe";
import { handleSubscriptionLifesycle } from "../subscriptionLifecycle";
import { getSubType } from "./getSubType";
export async function handleSubscriptionUpdated(
subscription: Stripe.Subscription,
@@ -74,11 +75,6 @@ export async function handleSubscriptionUpdated(
})
.where(eq(subscriptions.subscriptionId, subscription.id));
await handleSubscriptionLifesycle(
existingCustomer.orgId,
subscription.status
);
// Upsert subscription items
if (Array.isArray(fullSubscription.items?.data)) {
const itemsToUpsert = fullSubscription.items.data.map((item) => ({
@@ -141,14 +137,14 @@ export async function handleSubscriptionUpdated(
// This item has cycled
const meterId = item.plan.meter;
if (!meterId) {
logger.warn(
logger.debug(
`No meterId found for subscription item ${item.id}. Skipping usage reset.`
);
continue;
}
const featureId = getFeatureIdByMetricId(meterId);
if (!featureId) {
logger.warn(
logger.debug(
`No featureId found for meterId ${meterId}. Skipping usage reset.`
);
continue;
@@ -236,6 +232,20 @@ export async function handleSubscriptionUpdated(
}
}
// --- end usage update ---
const type = getSubType(fullSubscription);
if (type === "saas") {
logger.debug(`Handling SAAS subscription lifecycle for org ${existingCustomer.orgId}`);
// we only need to handle the limit lifecycle for saas subscriptions not for the licenses
await handleSubscriptionLifesycle(
existingCustomer.orgId,
subscription.status
);
} else {
logger.debug(
`Subscription ${subscription.id} is of type ${type}. No lifecycle handling needed.`
);
}
}
} catch (error) {
logger.error(