Look to the right org to test is subscribed

This commit is contained in:
Owen
2026-02-17 20:06:58 -08:00
parent 6661a76aa8
commit f591cf8601

View File

@@ -12,7 +12,8 @@
*/ */
import { build } from "@server/build"; import { build } from "@server/build";
import { db, customers, subscriptions } from "@server/db"; import { db, customers, subscriptions, orgs } from "@server/db";
import logger from "@server/logger";
import { Tier } from "@server/types/Tiers"; import { Tier } from "@server/types/Tiers";
import { eq, and, ne } from "drizzle-orm"; import { eq, and, ne } from "drizzle-orm";
@@ -27,37 +28,60 @@ export async function getOrgTierData(
} }
try { try {
const [org] = await db
.select()
.from(orgs)
.where(eq(orgs.orgId, orgId))
.limit(1);
if (!org) {
return { tier, active };
}
let orgIdToUse = org.orgId;
if (!org.isBillingOrg) {
if (!org.billingOrgId) {
logger.warn(
`Org ${orgId} is not a billing org and does not have a billingOrgId`
);
return { tier, active };
}
orgIdToUse = org.billingOrgId;
}
// Get customer for org // Get customer for org
const [customer] = await db const [customer] = await db
.select() .select()
.from(customers) .from(customers)
.where(eq(customers.orgId, orgId)) .where(eq(customers.orgId, orgIdToUse))
.limit(1); .limit(1);
if (customer) { if (!customer) {
// Query for active subscriptions that are not license type return { tier, active };
const [subscription] = await db }
.select()
.from(subscriptions)
.where(
and(
eq(subscriptions.customerId, customer.customerId),
eq(subscriptions.status, "active"),
ne(subscriptions.type, "license")
)
)
.limit(1);
if (subscription) { // Query for active subscriptions that are not license type
// Validate that subscription.type is one of the expected tier values const [subscription] = await db
if ( .select()
subscription.type === "tier1" || .from(subscriptions)
subscription.type === "tier2" || .where(
subscription.type === "tier3" and(
) { eq(subscriptions.customerId, customer.customerId),
tier = subscription.type; eq(subscriptions.status, "active"),
active = true; ne(subscriptions.type, "license")
} )
)
.limit(1);
if (subscription) {
// Validate that subscription.type is one of the expected tier values
if (
subscription.type === "tier1" ||
subscription.type === "tier2" ||
subscription.type === "tier3"
) {
tier = subscription.type;
active = true;
} }
} }
} catch (error) { } catch (error) {