feat(cloud): set seat limit checks for partner member invitations (#506)
This commit is contained in:
@@ -10,13 +10,14 @@ import * as userRepo from "@kan/db/repository/user.repo";
|
||||
import * as workspaceRepo from "@kan/db/repository/workspace.repo";
|
||||
import {
|
||||
generateUID,
|
||||
getSeatLimit,
|
||||
getSubscriptionByPlan,
|
||||
hasUnlimitedSeats,
|
||||
} from "@kan/shared";
|
||||
import { updateSubscriptionSeats } from "@kan/stripe";
|
||||
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
|
||||
import { memberInviteResponseSchema } from "../schemas";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
|
||||
import {
|
||||
assertCanManageMember,
|
||||
assertCanManageRole,
|
||||
@@ -114,6 +115,20 @@ export const memberRouter = createTRPCRouter({
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const seatLimit = getSeatLimit(subscriptions);
|
||||
if (seatLimit !== null) {
|
||||
const memberCount = await memberRepo.getCountByWorkspaceId(
|
||||
ctx.db,
|
||||
workspace.id,
|
||||
);
|
||||
if (memberCount >= seatLimit) {
|
||||
throw new TRPCError({
|
||||
message: `SEAT_LIMIT_REACHED`,
|
||||
code: "FORBIDDEN",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const existingUser = await userRepo.getByEmail(ctx.db, input.email);
|
||||
@@ -644,6 +659,20 @@ export const memberRouter = createTRPCRouter({
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const seatLimit = getSeatLimit(subscriptions);
|
||||
if (seatLimit !== null) {
|
||||
const memberCount = await memberRepo.getCountByWorkspaceId(
|
||||
ctx.db,
|
||||
workspace.id,
|
||||
);
|
||||
if (memberCount >= seatLimit) {
|
||||
throw new TRPCError({
|
||||
message: `SEAT_LIMIT_REACHED`,
|
||||
code: "FORBIDDEN",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the workspace role to set roleId
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { and, count, eq, isNull } from "drizzle-orm";
|
||||
import { and, count, eq, isNull, or } from "drizzle-orm";
|
||||
|
||||
import type { dbClient } from "@kan/db/client";
|
||||
import type { MemberRole, MemberStatus } from "@kan/db/schema";
|
||||
@@ -19,6 +19,27 @@ export const getActiveCount = async (db: dbClient) => {
|
||||
return result[0]?.count ?? 0;
|
||||
};
|
||||
|
||||
export const getCountByWorkspaceId = async (
|
||||
db: dbClient,
|
||||
workspaceId: number,
|
||||
) => {
|
||||
const result = await db
|
||||
.select({ count: count() })
|
||||
.from(workspaceMembers)
|
||||
.where(
|
||||
and(
|
||||
eq(workspaceMembers.workspaceId, workspaceId),
|
||||
isNull(workspaceMembers.deletedAt),
|
||||
or(
|
||||
eq(workspaceMembers.status, "active"),
|
||||
eq(workspaceMembers.status, "invited"),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return result[0]?.count ?? 0;
|
||||
};
|
||||
|
||||
export const create = async (
|
||||
db: dbClient,
|
||||
memberInput: {
|
||||
|
||||
@@ -54,3 +54,14 @@ export const hasUnlimitedSeats = (
|
||||
const activeSubscriptions = getActiveSubscriptions(subscriptions);
|
||||
return activeSubscriptions.some((sub) => sub.unlimitedSeats);
|
||||
};
|
||||
|
||||
export const getSeatLimit = (
|
||||
subscriptions: Subscription[] | undefined,
|
||||
): number | null => {
|
||||
const activeSubscriptions = getActiveSubscriptions(subscriptions);
|
||||
const partnerSub = activeSubscriptions.find(
|
||||
(sub) =>
|
||||
sub.partnerTier !== null && !sub.unlimitedSeats && sub.seats !== null,
|
||||
);
|
||||
return partnerSub?.seats ?? null;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user