Skip to content

Commit

Permalink
Set access to null upon expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 4, 2025
1 parent ca09c9d commit 009a0d3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion apps/web/app/api/lemon-squeezy/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const POST = withError(async (request: Request) => {
return await subscriptionPlanChanged({ payload, userId });
}

// cancellation
// cancelled or expired
if (payload.data.attributes.ends_at) {
return await subscriptionCancelled({
payload,
Expand Down Expand Up @@ -414,6 +414,7 @@ async function subscriptionCancelled({
premiumId,
variantId,
lemonSqueezyEndsAt: new Date(endsAt),
expired: payload.data.attributes.status === "expired",
});

if (!updatedPremium) return NextResponse.json({ ok: true });
Expand Down
1 change: 1 addition & 0 deletions apps/web/utils/actions/premium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export const changePremiumStatusAction = withActionInstrumentation(
await cancelPremium({
premiumId: userToUpgrade.premiumId,
lemonSqueezyEndsAt: new Date(),
expired: true,
});
} else {
return { error: "User not premium." };
Expand Down
12 changes: 9 additions & 3 deletions apps/web/utils/premium/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ export async function cancelPremium({
premiumId,
lemonSqueezyEndsAt,
variantId,
expired,
}: {
premiumId: string;
lemonSqueezyEndsAt: Date;
variantId?: number;
expired: boolean;
}) {
if (variantId) {
// Check if the premium exists for the given variant
Expand All @@ -95,9 +97,13 @@ export async function cancelPremium({
where: { id: premiumId },
data: {
lemonSqueezyRenewsAt: lemonSqueezyEndsAt,
bulkUnsubscribeAccess: null,
aiAutomationAccess: null,
coldEmailBlockerAccess: null,
...(expired
? {
bulkUnsubscribeAccess: null,
aiAutomationAccess: null,
coldEmailBlockerAccess: null,
}
: {}),
},
select: {
users: {
Expand Down

0 comments on commit 009a0d3

Please sign in to comment.