Skip to content

Commit

Permalink
Merge pull request #108 from ArhanAnsari/ArhanAnsari-patch-1
Browse files Browse the repository at this point in the history
Update askQuestions.ts
  • Loading branch information
ArhanAnsari authored Oct 14, 2024
2 parents 75d9e06 + b64496d commit ae3eb9f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions actions/askQuestions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//actions/askQuestions.ts
"use server";

import { adminDb } from "@/firebaseAdmin";
Expand Down Expand Up @@ -27,20 +28,36 @@ export async function askQuestion({
const userRef = await adminDb.collection("users").doc(userId).get();
const userData = userRef.data();

if (!userData?.hasActiveMembership && userMessages.length >= FREE_LIMIT) {
if (!userData) {
return {
success: false,
message: `You'll need to upgrade to PRO to ask more than ${FREE_LIMIT} questions! 😢`,
message: "User data not found. Please try again.",
};
}

if (userData?.hasActiveMembership && userMessages.length >= PRO_LIMIT) {
const { hasActiveMembership, isEnterprise } = userData;

// Free plan limit check
if (!hasActiveMembership && userMessages.length >= FREE_LIMIT) {
return {
success: false,
message: `You'll need to upgrade to PRO or Enterprise to ask more than ${FREE_LIMIT} questions! 😢`,
};
}

// PRO plan limit check
if (hasActiveMembership && !isEnterprise && userMessages.length >= PRO_LIMIT) {
return {
success: false,
message: `You've reached the PRO limit of ${PRO_LIMIT} questions! 😢`,
message: `You've reached the PRO limit of ${PRO_LIMIT} questions! 😢 Upgrade to Enterprise for unlimited access.`,
};
}

// No limit for Enterprise users
if (isEnterprise) {
// Enterprise users can proceed without any limit check
}

// Call the API to generate the response from Google Gemini
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/generate`, {
method: "POST",
Expand Down

0 comments on commit ae3eb9f

Please sign in to comment.