Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

more logic hardening for revenuecat

daniel31x13ba356555d02c
diff --git a/apps/mobile/app/subscribe.tsx b/apps/mobile/app/subscribe.tsx
index 7048f4d2..9ace3f71 100644
--- a/apps/mobile/app/subscribe.tsx
+++ b/apps/mobile/app/subscribe.tsx
@@ -217,12 +217,19 @@ export default function SubscribeScreen() {
};
const purchase = async () => {
- if (!selectedPlan.package || purchaseLoading) return;
+ if (!selectedPlan.package || purchaseLoading || !user?.uuid) return;
setPurchaseLoading(true);
try {
- if (user?.uuid) await Purchases.logIn(user.uuid);
+ await Purchases.logIn(user.uuid);
+ await Purchases.invalidateCustomerInfoCache();
+
+ const existingCustomerInfo = await Purchases.getCustomerInfo();
+ const alreadySubscribed =
+ await activateSubscription(existingCustomerInfo);
+
+ if (alreadySubscribed) return;
const { customerInfo } = await Purchases.purchasePackage(
selectedPlan.package
@@ -365,7 +372,7 @@ export default function SubscribeScreen() {
accessibilityRole="button"
accessibilityLabel={paymentButton.accessibilityLabel}
className="w-full flex-row bg-black px-4"
- disabled={!selectedPlan.package || purchaseLoading}
+ disabled={!selectedPlan.package || purchaseLoading || !user?.uuid}
onPress={purchase}
isLoading={purchaseLoading}
>
diff --git a/apps/web/lib/api/stripe/handleSubscription.ts b/apps/web/lib/api/stripe/handleSubscription.ts
index 376ecec7..a6cb7cb4 100644
--- a/apps/web/lib/api/stripe/handleSubscription.ts
+++ b/apps/web/lib/api/stripe/handleSubscription.ts
@@ -88,6 +88,7 @@ export default async function handleSubscription({
},
update: {
active,
+ provider: "STRIPE",
stripeSubscriptionId: id,
quantity,
currentPeriodStart: new Date(periodStart * 1000),
diff --git a/apps/web/lib/api/stripe/verifySubscription.ts b/apps/web/lib/api/stripe/verifySubscription.ts
index edc34036..2f6e5fbb 100644
--- a/apps/web/lib/api/stripe/verifySubscription.ts
+++ b/apps/web/lib/api/stripe/verifySubscription.ts
@@ -78,6 +78,7 @@ export default async function verifySubscription(
},
update: {
active,
+ provider: "STRIPE",
stripeSubscriptionId,
currentPeriodStart: new Date(currentPeriodStart),
currentPeriodEnd: new Date(currentPeriodEnd),
@@ -95,6 +96,14 @@ export default async function verifySubscription(
!subscription.revenueCatSubscriptionId ||
!subscription.currentPeriodStart
) {
+ if (user.subscriptions?.active)
+ await prisma.subscription.update({
+ where: {
+ revenueCatAppUserId: user.uuid,
+ },
+ data: { active: false },
+ });
+
return null;
}
@@ -116,6 +125,7 @@ export default async function verifySubscription(
},
update: {
active,
+ provider: "REVENUECAT",
revenueCatAppUserId: user.uuid,
currentPeriodStart,
currentPeriodEnd,
diff --git a/apps/web/pages/api/v1/auth/verify-email.ts b/apps/web/pages/api/v1/auth/verify-email.ts
index d1fb0a67..b75f9415 100644
--- a/apps/web/pages/api/v1/auth/verify-email.ts
+++ b/apps/web/pages/api/v1/auth/verify-email.ts
@@ -89,7 +89,7 @@ export default async function verifyEmail(
});
// Update email in db
- await prisma.user.update({
+ const user = await prisma.user.update({
where: {
email: oldEmail,
},
@@ -97,12 +97,16 @@ export default async function verifyEmail(
email: newEmail.toLowerCase().trim(),
unverifiedNewEmail: null,
},
+ include: {
+ subscriptions: true,
+ },
});
// Apply to Stripe
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
- if (STRIPE_SECRET_KEY) await updateCustomerEmail(oldEmail, newEmail);
+ if (STRIPE_SECRET_KEY && user.subscriptions?.provider === "STRIPE")
+ await updateCustomerEmail(oldEmail, newEmail);
// Clean up existing tokens
await prisma.verificationToken.delete({
diff --git a/apps/web/pages/api/v1/webhooks/revenuecat.ts b/apps/web/pages/api/v1/webhooks/revenuecat.ts
index 3d54391c..7d3dfb6e 100644
--- a/apps/web/pages/api/v1/webhooks/revenuecat.ts
+++ b/apps/web/pages/api/v1/webhooks/revenuecat.ts
@@ -123,6 +123,7 @@ export default async function revenueCatWebhook(
},
update: {
active,
+ provider: "REVENUECAT",
revenueCatAppUserId: event.original_app_user_id,
currentPeriodStart,
currentPeriodEnd,
diff --git a/packages/lib/verifyCapacity.ts b/packages/lib/verifyCapacity.ts
index b57baad2..f4248ca8 100644
--- a/packages/lib/verifyCapacity.ts
+++ b/packages/lib/verifyCapacity.ts
@@ -9,7 +9,16 @@ export const hasPassedLimit = async (
userId: number,
numberOfImports: number
) => {
- if (!stripeEnabled) {
+ const user = await prisma.user.findUnique({
+ where: { id: userId },
+ select: {
+ parentSubscriptionId: true,
+ subscriptions: { select: { id: true, quantity: true, provider: true } },
+ createdAt: true,
+ },
+ });
+
+ if (!stripeEnabled && user?.subscriptions?.provider !== "STRIPE") {
const totalLinks = await prisma.link.count({
where: {
createdById: userId,
@@ -19,15 +28,6 @@ export const hasPassedLimit = async (
return MAX_LINKS_PER_USER - (numberOfImports + totalLinks) < 0;
}
- const user = await prisma.user.findUnique({
- where: { id: userId },
- select: {
- parentSubscriptionId: true,
- subscriptions: { select: { id: true, quantity: true } },
- createdAt: true,
- },
- });
-
if (!user) {
return true;
}