Skip to content

Commit ea0e8d0

Browse files
authored
fix: do not deduct credits if cost_per_credit is zero (#5429)
* fix: do not deduct credits if `cost_per_credit` is zero * fix: make `plan_id` not nullable * chore: run `codegen` scripts * chore: remove unnecessary `optional` chaining
1 parent b2b6973 commit ea0e8d0

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

apps/frontend/lib/clients/oso-app/oso-app.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ class OsoAppClient {
941941
: null;
942942
const cycleStartDate = lastRefill;
943943
const cycleEndDate =
944-
lastRefill && plan?.refill_cycle_days
944+
lastRefill && plan.refill_cycle_days
945945
? new Date(
946946
lastRefill.getTime() + plan.refill_cycle_days * 24 * 60 * 60 * 1000,
947947
)
@@ -956,14 +956,14 @@ class OsoAppClient {
956956
)
957957
: 0;
958958

959-
const isEnterprise = plan?.plan_name === "ENTERPRISE";
960-
const isFree = plan?.plan_name === "FREE";
959+
const isEnterprise = plan.plan_name === "ENTERPRISE";
960+
const isFree = plan.plan_name === "FREE";
961961
const isActive = true;
962962

963963
const features = {
964-
maxCreditsPerCycle: plan?.max_credits_per_cycle || 0,
965-
refillCycleDays: plan?.refill_cycle_days || 0,
966-
pricePerCredit: plan?.price_per_credit || 0,
964+
maxCreditsPerCycle: plan.max_credits_per_cycle || 0,
965+
refillCycleDays: plan.refill_cycle_days || 0,
966+
pricePerCredit: plan.price_per_credit || 0,
967967
hasUnlimitedQueries: isEnterprise,
968968
hasPrioritySupport: isEnterprise,
969969
hasAdvancedAnalytics: isEnterprise,
@@ -975,14 +975,14 @@ class OsoAppClient {
975975
return {
976976
orgId: data.id,
977977
orgName: data.org_name,
978-
planId: plan?.plan_id,
979-
planName: plan?.plan_name,
978+
planId: plan.plan_id,
979+
planName: plan.plan_name,
980980
planDescription: undefined,
981981
tier: isEnterprise ? "enterprise" : isFree ? "free" : "unknown",
982982
creditsBalance: finalCreditsBalance,
983-
maxCreditsPerCycle: plan?.max_credits_per_cycle || 0,
983+
maxCreditsPerCycle: plan.max_credits_per_cycle || 0,
984984
billingCycle: {
985-
cycleDays: plan?.refill_cycle_days || 0,
985+
cycleDays: plan.refill_cycle_days || 0,
986986
cycleStartDate: cycleStartDate?.toISOString() || null,
987987
cycleEndDate: cycleEndDate?.toISOString() || null,
988988
daysUntilRefill,

apps/frontend/lib/services/credits.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type SupabaseClient = Awaited<ReturnType<typeof createAdminClient>>;
1414
export const PLAN_NAMES = ["FREE", "STARTER", "PRO", "ENTERPRISE"] as const;
1515
export type PlanName = (typeof PLAN_NAMES)[number];
1616

17+
const DEFAULT_CREDIT_PRICE = 1;
18+
1719
export enum TransactionType {
1820
SQL_QUERY = "sql_query",
1921
GRAPHQL_QUERY = "graphql_query",
@@ -239,7 +241,7 @@ export class CreditsService {
239241
const plan = orgData.pricing_plan;
240242

241243
const nextRefillDate =
242-
credits?.last_refill_at && plan?.refill_cycle_days
244+
credits?.last_refill_at && plan.refill_cycle_days
243245
? new Date(
244246
new Date(credits.last_refill_at).getTime() +
245247
plan.refill_cycle_days * 24 * 60 * 60 * 1000,
@@ -248,7 +250,7 @@ export class CreditsService {
248250

249251
const errorContext: CreditErrorContext = {
250252
orgName: orgData.org_name,
251-
planName: plan?.plan_name || "UNKNOWN",
253+
planName: plan.plan_name || "UNKNOWN",
252254
creditsBalance: credits?.credits_balance || 0,
253255
nextRefillDate,
254256
supportUrl: orgData.enterprise_support_url || undefined,
@@ -439,11 +441,6 @@ export class CreditsService {
439441
return null;
440442
}
441443

442-
if (!data?.pricing_plan) {
443-
logger.error("Organization plan data is missing");
444-
return null;
445-
}
446-
447444
return {
448445
org_id: data.id,
449446
org_name: data.org_name,
@@ -475,7 +472,7 @@ export class CreditsService {
475472
logger.log(`Post-refill credits balance for org ${orgId}: ${credits}`);
476473

477474
const orgPlan = await CreditsService.getOrganizationPlan(orgId);
478-
const costPerCall = orgPlan?.price_per_credit || 1;
475+
const costPerCall = orgPlan?.price_per_credit ?? DEFAULT_CREDIT_PRICE;
479476

480477
try {
481478
const deductionResult = await CreditsService.deductCreditsFromBalance(

apps/frontend/lib/types/schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ export const organizationsRowSchema = z.object({
636636
enterprise_support_url: z.string().nullable(),
637637
id: z.string(),
638638
org_name: z.string(),
639-
plan_id: z.string().nullable(),
639+
plan_id: z.string(),
640640
updated_at: z.string(),
641641
});
642642

@@ -648,7 +648,7 @@ export const organizationsInsertSchema = z.object({
648648
enterprise_support_url: z.string().optional().nullable(),
649649
id: z.string().optional(),
650650
org_name: z.string(),
651-
plan_id: z.string().optional().nullable(),
651+
plan_id: z.string().optional(),
652652
updated_at: z.string().optional(),
653653
});
654654

@@ -660,7 +660,7 @@ export const organizationsUpdateSchema = z.object({
660660
enterprise_support_url: z.string().optional().nullable(),
661661
id: z.string().optional(),
662662
org_name: z.string().optional(),
663-
plan_id: z.string().optional().nullable(),
663+
plan_id: z.string().optional(),
664664
updated_at: z.string().optional(),
665665
});
666666

apps/frontend/lib/types/supabase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ export type Database = {
596596
enterprise_support_url: string | null;
597597
id: string;
598598
org_name: string;
599-
plan_id: string | null;
599+
plan_id: string;
600600
updated_at: string;
601601
};
602602
Insert: {
@@ -607,7 +607,7 @@ export type Database = {
607607
enterprise_support_url?: string | null;
608608
id?: string;
609609
org_name: string;
610-
plan_id?: string | null;
610+
plan_id?: string;
611611
updated_at?: string;
612612
};
613613
Update: {
@@ -618,7 +618,7 @@ export type Database = {
618618
enterprise_support_url?: string | null;
619619
id?: string;
620620
org_name?: string;
621-
plan_id?: string | null;
621+
plan_id?: string;
622622
updated_at?: string;
623623
};
624624
Relationships: [
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE organizations
2+
ALTER COLUMN plan_id SET NOT NULL;

0 commit comments

Comments
 (0)