feat(server): support selfhost licenses (#8947)

This commit is contained in:
forehalo
2025-01-22 10:21:07 +00:00
parent 22e424d7de
commit 994d758c07
31 changed files with 1653 additions and 127 deletions
@@ -16,11 +16,14 @@ import type { User } from '@prisma/client';
import { PrismaClient } from '@prisma/client';
import { GraphQLJSONObject } from 'graphql-scalars';
import { groupBy } from 'lodash-es';
import Stripe from 'stripe';
import { z } from 'zod';
import {
AccessDenied,
AuthenticationRequired,
FailedToCheckout,
Throttle,
WorkspaceIdRequiredToUpdateTeamSubscription,
} from '../../base';
import { CurrentUser, Public } from '../../core/auth';
@@ -193,7 +196,7 @@ class CreateCheckoutSessionInput implements z.infer<typeof CheckoutParams> {
idempotencyKey?: string;
@Field(() => GraphQLJSONObject, { nullable: true })
args!: { workspaceId?: string };
args!: { workspaceId?: string; quantity?: number };
}
@Resolver(() => SubscriptionType)
@@ -261,19 +264,33 @@ export class SubscriptionResolver {
}, [] as SubscriptionPrice[]);
}
@Public()
@Mutation(() => String, {
description: 'Create a subscription checkout link of stripe',
})
async createCheckoutSession(
@CurrentUser() user: CurrentUser,
@CurrentUser() user: CurrentUser | null,
@Args({ name: 'input', type: () => CreateCheckoutSessionInput })
input: CreateCheckoutSessionInput
) {
const session = await this.service.checkout(input, {
plan: input.plan as any,
user,
workspaceId: input.args?.workspaceId,
});
let session: Stripe.Checkout.Session;
if (input.plan === SubscriptionPlan.SelfHostedTeam) {
session = await this.service.checkout(input, {
plan: input.plan as any,
quantity: input.args.quantity ?? 10,
});
} else {
if (!user) {
throw new AuthenticationRequired();
}
session = await this.service.checkout(input, {
plan: input.plan as any,
user,
workspaceId: input.args?.workspaceId,
});
}
if (!session.url) {
throw new FailedToCheckout();
@@ -415,6 +432,15 @@ export class SubscriptionResolver {
idempotencyKey
);
}
@Public()
@Throttle('strict')
@Mutation(() => String)
async generateLicenseKey(
@Args('sessionId', { type: () => String }) sessionId: string
) {
return this.service.generateLicenseKey(sessionId);
}
}
@Resolver(() => UserType)