Skip to content

Commit

Permalink
Admin allow upgrading multiple months
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 3, 2025
1 parent dcc4788 commit 883b406
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
11 changes: 10 additions & 1 deletion apps/web/app/(app)/admin/AdminUpgradeUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const AdminUpgradeUserForm = () => {
error={errors.emailAccountsAccess}
/>
<Select
label="Period"
label="Plan"
options={[
{
label: PremiumTier.BUSINESS_ANNUALLY,
Expand Down Expand Up @@ -98,6 +98,13 @@ export const AdminUpgradeUserForm = () => {
{...register("period")}
error={errors.period}
/>
<Input
type="number"
name="count"
label="Months/Years"
registerProps={register("count", { valueAsNumber: true })}
error={errors.count}
/>
<div className="space-x-2">
<Button
type="button"
Expand All @@ -108,6 +115,7 @@ export const AdminUpgradeUserForm = () => {
lemonSqueezyCustomerId: getValues("lemonSqueezyCustomerId"),
emailAccountsAccess: getValues("emailAccountsAccess"),
period: getValues("period"),
count: getValues("count"),
upgrade: true,
});
}}
Expand All @@ -122,6 +130,7 @@ export const AdminUpgradeUserForm = () => {
onSubmit({
email: getValues("email"),
period: getValues("period"),
count: getValues("count"),
upgrade: false,
});
}}
Expand Down
1 change: 1 addition & 0 deletions apps/web/app/(app)/admin/validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const changePremiumStatusSchema = z.object({
PremiumTier.COPILOT_MONTHLY,
PremiumTier.LIFETIME,
]),
count: z.number().default(1),
upgrade: z.boolean(),
});
export type ChangePremiumStatusOptions = z.infer<
Expand Down
30 changes: 18 additions & 12 deletions apps/web/utils/actions/premium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import prisma from "@/utils/prisma";
import { env } from "@/env";
import { isAdminForPremium, isOnHigherTier, isPremium } from "@/utils/premium";
import { cancelPremium, upgradeToPremium } from "@/utils/premium/server";
import type { ChangePremiumStatusOptions } from "@/app/(app)/admin/validation";
import {
changePremiumStatusSchema,
type ChangePremiumStatusOptions,
} from "@/app/(app)/admin/validation";
import {
activateLemonLicenseKey,
getLemonCustomer,
Expand Down Expand Up @@ -256,13 +259,16 @@ export const activateLicenseKeyAction = withActionInstrumentation(

export const changePremiumStatusAction = withActionInstrumentation(
"changePremiumStatus",
async (options: ChangePremiumStatusOptions) => {
async (unsafeData: ChangePremiumStatusOptions) => {
const session = await auth();
if (!session?.user.email) return { error: "Not logged in" };
if (!isAdmin(session.user.email)) return { error: "Not admin" };

const { data, error } = changePremiumStatusSchema.safeParse(unsafeData);
if (!data) return { error };

const userToUpgrade = await prisma.user.findUnique({
where: { email: options.email },
where: { email: data.email },
select: { id: true, premiumId: true },
});

Expand All @@ -274,10 +280,10 @@ export const changePremiumStatusAction = withActionInstrumentation(
let lemonSqueezyProductId: number | null = null;
let lemonSqueezyVariantId: number | null = null;

if (options.upgrade) {
if (options.lemonSqueezyCustomerId) {
if (data.upgrade) {
if (data.lemonSqueezyCustomerId) {
const lemonCustomer = await getLemonCustomer(
options.lemonSqueezyCustomerId.toString(),
data.lemonSqueezyCustomerId.toString(),
);
if (!lemonCustomer.data) return { error: "Lemon customer not found" };
const subscription = lemonCustomer.data.included?.find(
Expand All @@ -300,28 +306,28 @@ export const changePremiumStatusAction = withActionInstrumentation(
case PremiumTier.PRO_ANNUALLY:
case PremiumTier.BUSINESS_ANNUALLY:
case PremiumTier.BASIC_ANNUALLY:
return new Date(now.getTime() + ONE_YEAR_MS);
return new Date(now.getTime() + ONE_YEAR_MS * data.count);
case PremiumTier.PRO_MONTHLY:
case PremiumTier.BUSINESS_MONTHLY:
case PremiumTier.BASIC_MONTHLY:
case PremiumTier.COPILOT_MONTHLY:
return new Date(now.getTime() + ONE_MONTH_MS);
return new Date(now.getTime() + ONE_MONTH_MS * data.count);
default:
return null;
}
};

await upgradeToPremium({
userId: userToUpgrade.id,
tier: options.period,
lemonSqueezyCustomerId: options.lemonSqueezyCustomerId || null,
tier: data.period,
lemonSqueezyCustomerId: data.lemonSqueezyCustomerId || null,
lemonSqueezySubscriptionId,
lemonSqueezySubscriptionItemId,
lemonSqueezyOrderId,
lemonSqueezyProductId,
lemonSqueezyVariantId,
lemonSqueezyRenewsAt: getRenewsAt(options.period),
emailAccountsAccess: options.emailAccountsAccess,
lemonSqueezyRenewsAt: getRenewsAt(data.period),
emailAccountsAccess: data.emailAccountsAccess,
});
} else if (userToUpgrade) {
if (userToUpgrade.premiumId) {
Expand Down

1 comment on commit 883b406

@vercel
Copy link

@vercel vercel bot commented on 883b406 Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.