Update credit_billing.ts
This commit is contained in:
@@ -168,10 +168,11 @@ export async function supaBillTeam(team_id: string, credits: number) {
|
|||||||
export async function checkTeamCredits(team_id: string, credits: number) {
|
export async function checkTeamCredits(team_id: string, credits: number) {
|
||||||
return withAuth(supaCheckTeamCredits)(team_id, credits);
|
return withAuth(supaCheckTeamCredits)(team_id, credits);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if team has enough credits for the operation, return true, else return false
|
// if team has enough credits for the operation, return true, else return false
|
||||||
export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
||||||
if (team_id === "preview") {
|
if (team_id === "preview") {
|
||||||
return { success: true, message: "Preview team, no credits used" };
|
return { success: true, message: "Preview team, no credits used", remainingCredits: Infinity };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the team's active subscription and check for available coupons concurrently
|
// Retrieve the team's active subscription and check for available coupons concurrently
|
||||||
@@ -202,7 +203,7 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
if (subscriptionError || !subscription) {
|
if (subscriptionError || !subscription) {
|
||||||
// If there is no active subscription but there are available coupons
|
// If there is no active subscription but there are available coupons
|
||||||
if (couponCredits >= credits) {
|
if (couponCredits >= credits) {
|
||||||
return { success: true, message: "Sufficient credits available" };
|
return { success: true, message: "Sufficient credits available", remainingCredits: couponCredits };
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: creditUsages, error: creditUsageError } =
|
const { data: creditUsages, error: creditUsageError } =
|
||||||
@@ -252,9 +253,10 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: "Insufficient credits, please upgrade!",
|
message: "Insufficient credits, please upgrade!",
|
||||||
|
remainingCredits: FREE_CREDITS - totalCreditsUsed
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return { success: true, message: "Sufficient credits available" };
|
return { success: true, message: "Sufficient credits available", remainingCredits: FREE_CREDITS - totalCreditsUsed };
|
||||||
}
|
}
|
||||||
|
|
||||||
let totalCreditsUsed = 0;
|
let totalCreditsUsed = 0;
|
||||||
@@ -321,7 +323,7 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
subscription.current_period_start,
|
subscription.current_period_start,
|
||||||
subscription.current_period_end
|
subscription.current_period_end
|
||||||
);
|
);
|
||||||
return { success: false, message: "Insufficient credits, please upgrade!" };
|
return { success: false, message: "Insufficient credits, please upgrade!", remainingCredits: creditLimit - adjustedCreditsUsed };
|
||||||
} else if (creditUsagePercentage >= 0.8) {
|
} else if (creditUsagePercentage >= 0.8) {
|
||||||
// Send email notification for approaching credit limit
|
// Send email notification for approaching credit limit
|
||||||
await sendNotification(
|
await sendNotification(
|
||||||
@@ -332,7 +334,7 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true, message: "Sufficient credits available" };
|
return { success: true, message: "Sufficient credits available", remainingCredits: creditLimit - adjustedCreditsUsed };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count the total credits used by a team within the current billing period and return the remaining credits.
|
// Count the total credits used by a team within the current billing period and return the remaining credits.
|
||||||
|
|||||||
Reference in New Issue
Block a user