Skip to content

Commit

Permalink
feat: add create feed and website limit which in max tier limit
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Nov 15, 2024
1 parent c33d5bb commit c8d4063
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/server/model/billing/limit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { WorkspaceSubscriptionTier } from '@prisma/client';
import { z } from 'zod';
import { prisma } from '../_client.js';
import { env } from '../../utils/env.js';

export const TierLimitSchema = z.object({
maxWebsiteCount: z.number(),
Expand Down Expand Up @@ -59,3 +61,32 @@ export function getTierLimit(tier: WorkspaceSubscriptionTier): TierLimit {
maxFeedEventCount: -1,
};
}

export async function getWorkspaceTierLimit(
workspaceId: string
): Promise<TierLimit> {
if (!env.billing.enable) {
return getTierLimit(WorkspaceSubscriptionTier.UNLIMITED);
}

const workspace = await prisma.workspace.findUnique({
where: {
id: workspaceId,
},
select: {
subscription: {
select: {
tier: true,
},
},
},
});

if (!workspace) {
return getTierLimit(WorkspaceSubscriptionTier.FREE);
}

return getTierLimit(
workspace.subscription?.tier ?? WorkspaceSubscriptionTier.FREE
);
}
16 changes: 16 additions & 0 deletions src/server/trpc/routers/feed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './integration.js';
import { fetchDataByCursor } from '../../../utils/prisma.js';
import { delFeedEventNotifyCache } from '../../../model/feed/event.js';
import { getWorkspaceTierLimit } from '../../../model/billing/limit.js';

export const feedRouter = router({
channels: workspaceProcedure
Expand Down Expand Up @@ -253,6 +254,21 @@ export const feedRouter = router({
.mutation(async ({ input }) => {
const { name, workspaceId, notifyFrequency, notificationIds } = input;

const [limit, feedChannelCount] = await Promise.all([
getWorkspaceTierLimit(workspaceId),
prisma.feedChannel.count({
where: {
workspaceId,
},
}),
]);
if (
limit.maxFeedChannelCount !== -1 &&
feedChannelCount >= limit.maxFeedChannelCount
) {
throw new Error('You have reached your website limit');
}

const channel = await prisma.feedChannel.create({
data: {
workspaceId,
Expand Down
16 changes: 16 additions & 0 deletions src/server/trpc/routers/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { WebsiteLighthouseReportStatus } from '@prisma/client';
import { WebsiteLighthouseReportModelSchema } from '../../prisma/zod/websitelighthousereport.js';
import { buildCursorResponseSchema } from '../../utils/schema.js';
import { sendBuildLighthouseMessageQueue } from '../../mq/producer.js';
import { getWorkspaceTierLimit } from '../../model/billing/limit.js';

const websiteNameSchema = z.string().max(100);
const websiteDomainSchema = z.union([
Expand Down Expand Up @@ -512,6 +513,21 @@ export const websiteRouter = router({
.mutation(async ({ input }) => {
const { workspaceId, name, domain } = input;

const [limit, websiteCount] = await Promise.all([
getWorkspaceTierLimit(workspaceId),
prisma.website.count({
where: {
workspaceId,
},
}),
]);
if (
limit.maxWebsiteCount !== -1 &&
websiteCount >= limit.maxWebsiteCount
) {
throw new Error('You have reached your website limit');
}

const website = await prisma.website.create({
data: {
name,
Expand Down

0 comments on commit c8d4063

Please sign in to comment.