almost finished
This commit is contained in:
parent
9c481e5e83
commit
06675d1fe3
@ -46,11 +46,11 @@ export async function scrapeHelper(
|
|||||||
return { success: true, error: "No page found", returnCode: 200 };
|
return { success: true, error: "No page found", returnCode: 200 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const { success, credit_usage } = await billTeam(
|
const billingResult = await billTeam(
|
||||||
team_id,
|
team_id,
|
||||||
filteredDocs.length
|
filteredDocs.length
|
||||||
);
|
);
|
||||||
if (!success) {
|
if (!billingResult.success) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error:
|
error:
|
||||||
|
@ -83,11 +83,11 @@ export async function searchHelper(
|
|||||||
return { success: true, error: "No page found", returnCode: 200 };
|
return { success: true, error: "No page found", returnCode: 200 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const { success, credit_usage } = await billTeam(
|
const billingResult = await billTeam(
|
||||||
team_id,
|
team_id,
|
||||||
filteredDocs.length
|
filteredDocs.length
|
||||||
);
|
);
|
||||||
if (!success) {
|
if (!billingResult.success) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error:
|
error:
|
||||||
|
@ -89,12 +89,12 @@ export async function runWebScraper({
|
|||||||
: docs.filter((doc) => doc.content.trim().length > 0);
|
: docs.filter((doc) => doc.content.trim().length > 0);
|
||||||
|
|
||||||
|
|
||||||
const { success, credit_usage } = await billTeam(
|
const billingResult = await billTeam(
|
||||||
team_id,
|
team_id,
|
||||||
filteredDocs.length
|
filteredDocs.length
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!success) {
|
if (!billingResult.success) {
|
||||||
// throw new Error("Failed to bill team, no subscription was found");
|
// throw new Error("Failed to bill team, no subscription was found");
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -18,7 +18,6 @@ export async function supaBillTeam(team_id: string, credits: number) {
|
|||||||
// created_at: The timestamp of the API usage.
|
// created_at: The timestamp of the API usage.
|
||||||
|
|
||||||
// 1. get the subscription
|
// 1. get the subscription
|
||||||
|
|
||||||
const { data: subscription } = await supabase_service
|
const { data: subscription } = await supabase_service
|
||||||
.from("subscriptions")
|
.from("subscriptions")
|
||||||
.select("*")
|
.select("*")
|
||||||
@ -26,51 +25,81 @@ export async function supaBillTeam(team_id: string, credits: number) {
|
|||||||
.eq("status", "active")
|
.eq("status", "active")
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (!subscription) {
|
|
||||||
const { data: credit_usage } = await supabase_service
|
|
||||||
.from("credit_usage")
|
|
||||||
.insert([
|
|
||||||
{
|
|
||||||
team_id,
|
|
||||||
credits_used: credits,
|
|
||||||
created_at: new Date(),
|
|
||||||
},
|
|
||||||
])
|
|
||||||
.select();
|
|
||||||
|
|
||||||
return { success: true, credit_usage };
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Check for available coupons
|
// 2. Check for available coupons
|
||||||
const { data: coupons } = await supabase_service
|
const { data: coupons } = await supabase_service
|
||||||
.from("coupons")
|
.from("coupons")
|
||||||
.select("credits")
|
.select("id, credits")
|
||||||
.eq("team_id", team_id)
|
.eq("team_id", team_id)
|
||||||
.eq("status", "active");
|
.eq("status", "active");
|
||||||
|
|
||||||
let couponValue = 0;
|
let couponCredits = 0;
|
||||||
if (coupons && coupons.length > 0) {
|
if (coupons && coupons.length > 0) {
|
||||||
couponValue = coupons[0].credits; // Assuming only one active coupon can be used at a time
|
couponCredits = coupons.reduce((total, coupon) => total + coupon.credits, 0);
|
||||||
console.log(`Applying coupon of ${couponValue} credits`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate final credits used after applying coupon
|
let sortedCoupons = coupons.sort((a, b) => b.credits - a.credits);
|
||||||
const finalCreditsUsed = Math.max(0, credits - couponValue);
|
|
||||||
|
|
||||||
// 3. Log the credit usage
|
// using coupon credits:
|
||||||
const { data: credit_usage } = await supabase_service
|
if (couponCredits > 0) {
|
||||||
.from("credit_usage")
|
// using only coupon credits:
|
||||||
.insert([
|
if (couponCredits > credits && !subscription) {
|
||||||
{
|
// remove credits from coupon credits
|
||||||
team_id,
|
let usedCredits = credits;
|
||||||
subscription_id: subscription ? subscription.id : null,
|
while (usedCredits > 0) {
|
||||||
credits_used: finalCreditsUsed,
|
// update coupons
|
||||||
created_at: new Date(),
|
if (sortedCoupons[0].credits < usedCredits) {
|
||||||
},
|
usedCredits = usedCredits - sortedCoupons[0].credits;
|
||||||
])
|
// update coupon credits
|
||||||
.select();
|
await supabase_service
|
||||||
|
.from("coupons")
|
||||||
|
.update({
|
||||||
|
credits: 0
|
||||||
|
})
|
||||||
|
.eq("id", sortedCoupons[0].id);
|
||||||
|
sortedCoupons.shift();
|
||||||
|
|
||||||
return { success: true, credit_usage };
|
} else {
|
||||||
|
// update coupon credits
|
||||||
|
await supabase_service
|
||||||
|
.from("coupons")
|
||||||
|
.update({
|
||||||
|
credits: sortedCoupons[0].credits - usedCredits
|
||||||
|
})
|
||||||
|
.eq("id", sortedCoupons[0].id);
|
||||||
|
usedCredits = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await createCreditUsage({ team_id, credits: 0 });
|
||||||
|
|
||||||
|
// @nick ??? HOW TO HANDLE THIS CASE?
|
||||||
|
// not enough coupon credits but no subscription
|
||||||
|
} else if (!subscription) {
|
||||||
|
return await createCreditUsage({ team_id, credits });
|
||||||
|
}
|
||||||
|
|
||||||
|
// using coupon + subscription credits:
|
||||||
|
if (credits > couponCredits) {
|
||||||
|
// update coupon credits
|
||||||
|
for (let i = 0; i < sortedCoupons.length; i++) {
|
||||||
|
await supabase_service
|
||||||
|
.from("coupons")
|
||||||
|
.update({
|
||||||
|
credits: 0
|
||||||
|
})
|
||||||
|
.eq("id", sortedCoupons[i].id);
|
||||||
|
}
|
||||||
|
const usedCredits = credits - couponCredits;
|
||||||
|
return await createCreditUsage({ team_id, subscription_id: subscription.id, credits: usedCredits });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// not using coupon credits
|
||||||
|
if (!subscription) {
|
||||||
|
return await createCreditUsage({ team_id, credits });
|
||||||
|
}
|
||||||
|
|
||||||
|
return await createCreditUsage({ team_id, subscription_id: subscription.id, credits });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkTeamCredits(team_id: string, credits: number) {
|
export async function checkTeamCredits(team_id: string, credits: number) {
|
||||||
@ -90,10 +119,6 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
.eq("status", "active")
|
.eq("status", "active")
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (subscriptionError || !subscription) {
|
|
||||||
return { success: false, message: "No active subscription found" };
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for available coupons
|
// Check for available coupons
|
||||||
const { data: coupons } = await supabase_service
|
const { data: coupons } = await supabase_service
|
||||||
.from("coupons")
|
.from("coupons")
|
||||||
@ -101,9 +126,18 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
.eq("team_id", team_id)
|
.eq("team_id", team_id)
|
||||||
.eq("status", "active");
|
.eq("status", "active");
|
||||||
|
|
||||||
let couponValue = 0;
|
let couponCredits = 0;
|
||||||
if (coupons && coupons.length > 0) {
|
if (coupons && coupons.length > 0) {
|
||||||
couponValue = coupons[0].credits;
|
couponCredits = coupons.reduce((total, coupon) => total + coupon.credits, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subscriptionError || (!subscription && couponCredits <= 0)) {
|
||||||
|
return { success: false, message: "No active subscription or coupons found" };
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is no active subscription but there are available coupons
|
||||||
|
if (couponCredits >= credits) {
|
||||||
|
return { success: true, message: "Sufficient credits available" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the total credits used by the team within the current billing period
|
// Calculate the total credits used by the team within the current billing period
|
||||||
@ -121,7 +155,7 @@ export async function supaCheckTeamCredits(team_id: string, credits: number) {
|
|||||||
const totalCreditsUsed = creditUsages.reduce((acc, usage) => acc + usage.credits_used, 0);
|
const totalCreditsUsed = creditUsages.reduce((acc, usage) => acc + usage.credits_used, 0);
|
||||||
|
|
||||||
// Adjust total credits used by subtracting coupon value
|
// Adjust total credits used by subtracting coupon value
|
||||||
const adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponValue);
|
const adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponCredits);
|
||||||
|
|
||||||
// Get the price details
|
// Get the price details
|
||||||
const { data: price, error: priceError } = await supabase_service
|
const { data: price, error: priceError } = await supabase_service
|
||||||
@ -154,19 +188,18 @@ export async function countCreditsAndRemainingForCurrentBillingPeriod(
|
|||||||
.eq("team_id", team_id)
|
.eq("team_id", team_id)
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (subscriptionError || !subscription) {
|
|
||||||
// Check for available coupons even if there's no subscription
|
|
||||||
const { data: coupons } = await supabase_service
|
const { data: coupons } = await supabase_service
|
||||||
.from("coupons")
|
.from("coupons")
|
||||||
.select("value")
|
.select("credits")
|
||||||
.eq("team_id", team_id)
|
.eq("team_id", team_id)
|
||||||
.eq("status", "active");
|
.eq("status", "active");
|
||||||
|
|
||||||
let couponValue = 0;
|
let couponCredits = 0;
|
||||||
if (coupons && coupons.length > 0) {
|
if (coupons && coupons.length > 0) {
|
||||||
couponValue = coupons[0].value;
|
couponCredits = coupons.reduce((total, coupon) => total + coupon.credits, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (subscriptionError || !subscription) {
|
||||||
// Free
|
// Free
|
||||||
const { data: creditUsages, error: creditUsageError } =
|
const { data: creditUsages, error: creditUsageError } =
|
||||||
await supabase_service
|
await supabase_service
|
||||||
@ -184,25 +217,8 @@ export async function countCreditsAndRemainingForCurrentBillingPeriod(
|
|||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
// Adjust total credits used by subtracting coupon value
|
const remainingCredits = FREE_CREDITS + couponCredits - totalCreditsUsed;
|
||||||
const adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponValue);
|
return { totalCreditsUsed: totalCreditsUsed, remainingCredits, totalCredits: FREE_CREDITS + couponCredits };
|
||||||
|
|
||||||
// 4. Calculate remaining credits.
|
|
||||||
const remainingCredits = FREE_CREDITS - adjustedCreditsUsed;
|
|
||||||
|
|
||||||
return { totalCreditsUsed: adjustedCreditsUsed, remainingCredits, totalCredits: FREE_CREDITS };
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there is an active subscription
|
|
||||||
const { data: coupons } = await supabase_service
|
|
||||||
.from("coupons")
|
|
||||||
.select("credits")
|
|
||||||
.eq("team_id", team_id)
|
|
||||||
.eq("status", "active");
|
|
||||||
|
|
||||||
let couponValue = 0;
|
|
||||||
if (coupons && coupons.length > 0) {
|
|
||||||
couponValue = coupons[0].credits;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: creditUsages, error: creditUsageError } = await supabase_service
|
const { data: creditUsages, error: creditUsageError } = await supabase_service
|
||||||
@ -216,13 +232,10 @@ export async function countCreditsAndRemainingForCurrentBillingPeriod(
|
|||||||
throw new Error(`Failed to retrieve credit usage for subscription_id: ${subscription.id}`);
|
throw new Error(`Failed to retrieve credit usage for subscription_id: ${subscription.id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalCreditsUsed = creditUsages.reduce(
|
const totalCreditsUsed = creditUsages.reduce((acc, usage) => acc + usage.credits_used, 0);
|
||||||
(acc, usage) => acc + usage.credits_used,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
// Adjust total credits used by subtracting coupon value
|
// Adjust total credits used by subtracting coupon value
|
||||||
const adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponValue);
|
// const adjustedCreditsUsed = Math.max(0, totalCreditsUsed - couponCredits);
|
||||||
|
|
||||||
const { data: price, error: priceError } = await supabase_service
|
const { data: price, error: priceError } = await supabase_service
|
||||||
.from("prices")
|
.from("prices")
|
||||||
@ -235,11 +248,27 @@ export async function countCreditsAndRemainingForCurrentBillingPeriod(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate remaining credits.
|
// Calculate remaining credits.
|
||||||
const remainingCredits = price.credits - adjustedCreditsUsed;
|
const remainingCredits = price.credits + couponCredits - totalCreditsUsed;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
totalCreditsUsed: adjustedCreditsUsed,
|
totalCreditsUsed,
|
||||||
remainingCredits,
|
remainingCredits,
|
||||||
totalCredits: price.credits
|
totalCredits: price.credits
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createCreditUsage({ team_id, subscription_id, credits }: { team_id: string, subscription_id?: string, credits: number }) {
|
||||||
|
const { data: credit_usage } = await supabase_service
|
||||||
|
.from("credit_usage")
|
||||||
|
.insert([
|
||||||
|
{
|
||||||
|
team_id,
|
||||||
|
credits_used: credits,
|
||||||
|
subscription_id: subscription_id || null,
|
||||||
|
created_at: new Date(),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.select();
|
||||||
|
|
||||||
|
return { success: true, credit_usage };
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user