mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 13:25:11 +00:00
Remove trial subscriptions from Khoj cloud
This commit is contained in:
@@ -460,51 +460,6 @@ export default function SettingsView() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const enableFreeTrial = async () => {
|
|
||||||
const formatDate = (dateString: Date) => {
|
|
||||||
const date = new Date(dateString);
|
|
||||||
return new Intl.DateTimeFormat("en-US", {
|
|
||||||
day: "2-digit",
|
|
||||||
month: "short",
|
|
||||||
year: "numeric",
|
|
||||||
}).format(date);
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/subscription/trial`, {
|
|
||||||
method: "POST",
|
|
||||||
});
|
|
||||||
if (!response.ok) throw new Error("Failed to enable free trial");
|
|
||||||
|
|
||||||
const responseBody = await response.json();
|
|
||||||
|
|
||||||
// Set updated user settings
|
|
||||||
if (responseBody.trial_enabled && userConfig) {
|
|
||||||
let newUserConfig = userConfig;
|
|
||||||
newUserConfig.subscription_state = SubscriptionStates.TRIAL;
|
|
||||||
const renewalDate = new Date(
|
|
||||||
Date.now() + userConfig.length_of_free_trial * 24 * 60 * 60 * 1000,
|
|
||||||
);
|
|
||||||
newUserConfig.subscription_renewal_date = formatDate(renewalDate);
|
|
||||||
newUserConfig.subscription_enabled_trial_at = new Date().toISOString();
|
|
||||||
setUserConfig(newUserConfig);
|
|
||||||
|
|
||||||
// Notify user of free trial
|
|
||||||
toast({
|
|
||||||
title: "🎉 Trial Enabled",
|
|
||||||
description: `Your free trial will end on ${newUserConfig.subscription_renewal_date}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error enabling free trial:", error);
|
|
||||||
toast({
|
|
||||||
title: "⚠️ Failed to Enable Free Trial",
|
|
||||||
description:
|
|
||||||
"Failed to enable free trial. Try again or contact us at team@khoj.dev",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const saveName = async () => {
|
const saveName = async () => {
|
||||||
if (!name) return;
|
if (!name) return;
|
||||||
try {
|
try {
|
||||||
@@ -912,7 +867,7 @@ export default function SettingsView() {
|
|||||||
Resubscribe
|
Resubscribe
|
||||||
</Button>
|
</Button>
|
||||||
)) ||
|
)) ||
|
||||||
(userConfig.subscription_enabled_trial_at && (
|
(
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
className="text-primary/80 hover:text-primary"
|
className="text-primary/80 hover:text-primary"
|
||||||
@@ -930,18 +885,6 @@ export default function SettingsView() {
|
|||||||
/>
|
/>
|
||||||
Subscribe
|
Subscribe
|
||||||
</Button>
|
</Button>
|
||||||
)) || (
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
className="text-primary/80 hover:text-primary"
|
|
||||||
onClick={enableFreeTrial}
|
|
||||||
>
|
|
||||||
<ArrowCircleUp
|
|
||||||
weight="bold"
|
|
||||||
className="h-5 w-5 mr-2"
|
|
||||||
/>
|
|
||||||
Enable Trial
|
|
||||||
</Button>
|
|
||||||
)}
|
)}
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
@@ -265,25 +265,6 @@ async def aget_or_create_user_by_email(input_email: str, check_deliverability=Fa
|
|||||||
return user, is_new
|
return user, is_new
|
||||||
|
|
||||||
|
|
||||||
@arequire_valid_user
|
|
||||||
async def astart_trial_subscription(user: KhojUser) -> Subscription:
|
|
||||||
subscription = await Subscription.objects.filter(user=user).afirst()
|
|
||||||
if not subscription:
|
|
||||||
raise HTTPException(status_code=400, detail="User does not have a subscription")
|
|
||||||
|
|
||||||
if subscription.type == Subscription.Type.TRIAL:
|
|
||||||
raise HTTPException(status_code=400, detail="User already has a trial subscription")
|
|
||||||
|
|
||||||
if subscription.enabled_trial_at:
|
|
||||||
raise HTTPException(status_code=400, detail="User already has a trial subscription")
|
|
||||||
|
|
||||||
subscription.type = Subscription.Type.TRIAL
|
|
||||||
subscription.enabled_trial_at = datetime.now(tz=timezone.utc)
|
|
||||||
subscription.renewal_date = datetime.now(tz=timezone.utc) + timedelta(days=LENGTH_OF_FREE_TRIAL)
|
|
||||||
await subscription.asave()
|
|
||||||
return subscription
|
|
||||||
|
|
||||||
|
|
||||||
async def aget_user_validated_by_email_verification_code(code: str, email: str) -> tuple[Optional[KhojUser], bool]:
|
async def aget_user_validated_by_email_verification_code(code: str, email: str) -> tuple[Optional[KhojUser], bool]:
|
||||||
# Normalize the email address
|
# Normalize the email address
|
||||||
normalized_email, _ = normalize_email(email)
|
normalized_email, _ = normalize_email(email)
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
from fastapi import APIRouter, Request, Response
|
from fastapi import APIRouter, Request
|
||||||
from starlette.authentication import requires
|
from starlette.authentication import requires
|
||||||
|
|
||||||
from khoj.database import adapters
|
from khoj.database import adapters
|
||||||
from khoj.database.models import KhojUser, Subscription
|
from khoj.database.models import Subscription
|
||||||
from khoj.routers.helpers import update_telemetry_state
|
from khoj.routers.helpers import update_telemetry_state
|
||||||
from khoj.utils import state
|
from khoj.utils import state
|
||||||
|
|
||||||
@@ -102,9 +101,9 @@ async def subscribe(request: Request):
|
|||||||
)
|
)
|
||||||
success = user is not None
|
success = user is not None
|
||||||
elif event_type in {"customer.subscription.deleted"}:
|
elif event_type in {"customer.subscription.deleted"}:
|
||||||
# Reset the user to trial state
|
# Reset user subscription state when subscription is deleted
|
||||||
user, is_new = await adapters.set_user_subscription(
|
user, is_new = await adapters.set_user_subscription(
|
||||||
customer_email, is_recurring=False, renewal_date=None, type=Subscription.Type.TRIAL
|
customer_email, is_recurring=False, renewal_date=None, type=Subscription.Type.STANDARD
|
||||||
)
|
)
|
||||||
success = user is not None
|
success = user is not None
|
||||||
|
|
||||||
@@ -148,19 +147,3 @@ async def update_subscription(request: Request, operation: str):
|
|||||||
return {"success": False, "message": "No subscription found that is set to cancel"}
|
return {"success": False, "message": "No subscription found that is set to cancel"}
|
||||||
|
|
||||||
return {"success": False, "message": "Invalid operation"}
|
return {"success": False, "message": "Invalid operation"}
|
||||||
|
|
||||||
|
|
||||||
@subscription_router.post("/trial", response_class=Response)
|
|
||||||
@requires(["authenticated"])
|
|
||||||
async def start_trial(request: Request) -> Response:
|
|
||||||
user: KhojUser = request.user.object
|
|
||||||
|
|
||||||
# Start a trial for the user
|
|
||||||
updated_subscription = await adapters.astart_trial_subscription(user)
|
|
||||||
|
|
||||||
# Return trial status as a JSON response
|
|
||||||
return Response(
|
|
||||||
content=json.dumps({"trial_enabled": updated_subscription is not None}),
|
|
||||||
media_type="application/json",
|
|
||||||
status_code=200,
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user