sync user clients to org on add/remove user org

This commit is contained in:
miloschwartz
2025-11-08 17:51:51 -08:00
parent a70799c8c0
commit 5602d8ee64
6 changed files with 332 additions and 37 deletions

View File

@@ -15,6 +15,7 @@ import { FeatureId } from "@server/lib/billing";
import { build } from "@server/build";
import { getOrgTierData } from "#dynamic/lib/billing";
import { TierId } from "@server/lib/billing/tiers";
import { calculateUserClientsForOrgs } from "@server/lib/calculateUserClientsForOrgs";
const paramsSchema = z
.object({
@@ -89,14 +90,7 @@ export async function createOrgUser(
}
const { orgId } = parsedParams.data;
const {
username,
email,
name,
type,
idpId,
roleId
} = parsedBody.data;
const { username, email, name, type, idpId, roleId } = parsedBody.data;
if (build == "saas") {
const usage = await usageService.getUsage(orgId, FeatureId.USERS);
@@ -202,7 +196,9 @@ export async function createOrgUser(
)
);
let userId: string | undefined;
if (existingUser) {
userId = existingUser.userId;
const [existingOrgUser] = await trx
.select()
.from(userOrgs)
@@ -232,7 +228,7 @@ export async function createOrgUser(
})
.returning();
} else {
const userId = generateId(15);
userId = generateId(15);
const [newUser] = await trx
.insert(users)
@@ -244,7 +240,7 @@ export async function createOrgUser(
type: "oidc",
idpId,
dateCreated: new Date().toISOString(),
emailVerified: true,
emailVerified: true
})
.returning();
@@ -264,6 +260,8 @@ export async function createOrgUser(
.select()
.from(userOrgs)
.where(eq(userOrgs.orgId, orgId));
await calculateUserClientsForOrgs(userId, trx);
});
if (orgUsers) {