mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
Refactor registration and dashboard features to support invite codes and Bunny uploads
- Moved registration logic to a new client component for better separation of concerns. - Integrated invite code requirement based on feature flags in the registration process. - Enhanced dashboard functionality to conditionally enable Bunny uploads based on feature flags. - Updated various components and API routes to check for Bunny uploads and Stripe billing feature flags. - Added new feature flag utilities for managing feature toggles in the application.
This commit is contained in:
@@ -2,6 +2,7 @@ import { unstable_cache } from 'next/cache';
|
||||
import { db } from '@/lib/db';
|
||||
import { r2Client, R2_BUCKET_NAME } from '@/lib/r2';
|
||||
import { ListObjectsV2Command, type ListObjectsV2CommandInput } from '@aws-sdk/client-s3';
|
||||
import { isBunnyUploadsFeatureEnabled } from '@/lib/feature-flags';
|
||||
|
||||
const BUNNY_API_BASE = 'https://video.bunnycdn.com';
|
||||
const STORAGE_CACHE_SECONDS = 600;
|
||||
@@ -116,6 +117,10 @@ export async function refreshR2StorageSnapshot(): Promise<string> {
|
||||
}
|
||||
|
||||
async function fetchBunnyStorageStats(): Promise<BunnyStorageStats> {
|
||||
if (!isBunnyUploadsFeatureEnabled()) {
|
||||
return { totalBytes: 0, byVideoId: {} };
|
||||
}
|
||||
|
||||
const { apiKey, libraryId } = getBunnyConfig();
|
||||
const byVideoId: Record<string, number> = {};
|
||||
let totalBytes = 0;
|
||||
|
||||
+11
-2
@@ -3,6 +3,7 @@ import type Stripe from 'stripe';
|
||||
import { BillingSubscriptionStatus } from '@prisma/client';
|
||||
import { db } from '@/lib/db';
|
||||
import { getStripe, getStripePriceId } from '@/lib/stripe';
|
||||
import { isStripeFeatureEnabled } from '@/lib/feature-flags';
|
||||
|
||||
const ACTIVE_SUBSCRIPTION_STATUSES = new Set<BillingSubscriptionStatus>([
|
||||
BillingSubscriptionStatus.ACTIVE,
|
||||
@@ -35,6 +36,10 @@ export function hasActiveSubscription(status: BillingSubscriptionStatus | null |
|
||||
}
|
||||
|
||||
export function hasBillingAccess(subject: BillingAccessSubject, now: Date = new Date()) {
|
||||
if (!isStripeFeatureEnabled()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hasActiveSubscription(subject.subscriptionStatus)) {
|
||||
return true;
|
||||
}
|
||||
@@ -68,6 +73,10 @@ export function getStorageCleanupEligibleAt(subject: BillingAccessSubject) {
|
||||
}
|
||||
|
||||
export function buildBillingAccessWhereInput(now: Date = new Date()): Prisma.UserWhereInput {
|
||||
if (!isStripeFeatureEnabled()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
OR: [
|
||||
{ subscriptionStatus: { in: [BillingSubscriptionStatus.ACTIVE, BillingSubscriptionStatus.TRIALING] } },
|
||||
@@ -217,10 +226,10 @@ export async function getWorkspaceCreationEligibility(userId: string) {
|
||||
const billingAccess = hasBillingAccess(user);
|
||||
const collaborationCount = invitedWorkspaceCount + projectOnlyCollaborationCount;
|
||||
const canCreateWorkspace =
|
||||
billingAccess || (ownedWorkspaceCount === 0 && collaborationCount === 0);
|
||||
!isStripeFeatureEnabled() || billingAccess || (ownedWorkspaceCount === 0 && collaborationCount === 0);
|
||||
|
||||
let reason: string | null = null;
|
||||
if (!canCreateWorkspace) {
|
||||
if (!canCreateWorkspace && isStripeFeatureEnabled()) {
|
||||
if (collaborationCount > 0 && ownedWorkspaceCount === 0) {
|
||||
reason =
|
||||
'You are currently collaborating in someone else’s workspace or project. Start a subscription to create a workspace of your own.';
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
function readBooleanEnv(name: string, defaultValue: boolean): boolean {
|
||||
const value = process.env[name];
|
||||
if (!value) return defaultValue;
|
||||
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === 'true') return true;
|
||||
if (normalized === 'false') return false;
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export function isStripeFeatureEnabled() {
|
||||
return readBooleanEnv('OPENFRAME_ENABLE_STRIPE', true);
|
||||
}
|
||||
|
||||
export function hasStripeConfig() {
|
||||
return Boolean(process.env.STRIPE_SECRET_KEY && process.env.STRIPE_PRICE_ID);
|
||||
}
|
||||
|
||||
export function isStripeBillingEnabled() {
|
||||
return isStripeFeatureEnabled() && hasStripeConfig();
|
||||
}
|
||||
|
||||
export function isBunnyUploadsFeatureEnabled() {
|
||||
return readBooleanEnv('OPENFRAME_ENABLE_BUNNY_UPLOADS', true);
|
||||
}
|
||||
|
||||
export function hasBunnyUploadsConfig() {
|
||||
return Boolean(
|
||||
process.env.BUNNY_STREAM_API_KEY &&
|
||||
(process.env.BUNNY_STREAM_LIBRARY_ID || process.env.NEXT_PUBLIC_BUNNY_STREAM_LIBRARY_ID)
|
||||
);
|
||||
}
|
||||
|
||||
export function isBunnyUploadsEnabled() {
|
||||
return isBunnyUploadsFeatureEnabled() && hasBunnyUploadsConfig();
|
||||
}
|
||||
|
||||
export function isInviteCodeRequired() {
|
||||
return readBooleanEnv('OPENFRAME_REQUIRE_INVITE_CODE', true);
|
||||
}
|
||||
+3
-15
@@ -1,6 +1,6 @@
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
import { auth, checkProjectAccess, checkWorkspaceAccess } from '@/lib/auth';
|
||||
import { hasBillingAccess } from '@/lib/billing';
|
||||
import { buildBillingAccessWhereInput, hasBillingAccess } from '@/lib/billing';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
type AccessIntent = 'view' | 'manage';
|
||||
@@ -98,13 +98,7 @@ export async function hasCollaboratorBillingBackedAccess(userId: string) {
|
||||
const [workspaceCount, projectCount] = await Promise.all([
|
||||
db.workspace.count({
|
||||
where: {
|
||||
owner: {
|
||||
OR: [
|
||||
{ subscriptionStatus: { in: ['ACTIVE', 'TRIALING'] } },
|
||||
{ trialEndsAt: { gt: now } },
|
||||
{ stripeCurrentPeriodEnd: { gt: now } },
|
||||
],
|
||||
},
|
||||
owner: buildBillingAccessWhereInput(now),
|
||||
OR: [
|
||||
{ ownerId: userId },
|
||||
{ members: { some: { userId } } },
|
||||
@@ -114,13 +108,7 @@ export async function hasCollaboratorBillingBackedAccess(userId: string) {
|
||||
db.project.count({
|
||||
where: {
|
||||
workspace: {
|
||||
owner: {
|
||||
OR: [
|
||||
{ subscriptionStatus: { in: ['ACTIVE', 'TRIALING'] } },
|
||||
{ trialEndsAt: { gt: now } },
|
||||
{ stripeCurrentPeriodEnd: { gt: now } },
|
||||
],
|
||||
},
|
||||
owner: buildBillingAccessWhereInput(now),
|
||||
},
|
||||
OR: [
|
||||
{ ownerId: userId },
|
||||
|
||||
+6
-1
@@ -1,9 +1,14 @@
|
||||
import Stripe from 'stripe';
|
||||
import { hasStripeConfig, isStripeBillingEnabled } from '@/lib/feature-flags';
|
||||
|
||||
let stripeClient: Stripe | null = null;
|
||||
|
||||
export function isStripeConfigured() {
|
||||
return Boolean(process.env.STRIPE_SECRET_KEY && process.env.STRIPE_PRICE_ID);
|
||||
return isStripeBillingEnabled();
|
||||
}
|
||||
|
||||
export function hasStripeRuntimeConfig() {
|
||||
return hasStripeConfig();
|
||||
}
|
||||
|
||||
export function getStripe() {
|
||||
|
||||
Reference in New Issue
Block a user