mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-27 01:29:14 +00:00
Improve efficiency of calculateUserClientsForOrgs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { db, primaryDb } from "@server/db";
|
||||
import { and, count, eq } from "drizzle-orm";
|
||||
import {
|
||||
domains,
|
||||
@@ -233,6 +233,7 @@ export async function createOrg(
|
||||
let error = "";
|
||||
let org: Org | null = null;
|
||||
let numOrgs: number | null = null;
|
||||
let ownerUserId: string | null = null;
|
||||
|
||||
await db.transaction(async (trx) => {
|
||||
const allDomains = await trx
|
||||
@@ -326,7 +327,6 @@ export async function createOrg(
|
||||
);
|
||||
}
|
||||
|
||||
let ownerUserId: string | null = null;
|
||||
if (req.user) {
|
||||
await trx.insert(userOrgs).values({
|
||||
userId: req.user!.userId,
|
||||
@@ -382,8 +382,6 @@ export async function createOrg(
|
||||
}))
|
||||
);
|
||||
|
||||
await calculateUserClientsForOrgs(ownerUserId, trx);
|
||||
|
||||
if (billingOrgIdForNewOrg) {
|
||||
const [numOrgsResult] = await trx
|
||||
.select({ count: count() })
|
||||
@@ -396,6 +394,14 @@ export async function createOrg(
|
||||
}
|
||||
});
|
||||
|
||||
if (ownerUserId) {
|
||||
calculateUserClientsForOrgs(ownerUserId).catch((e) => {
|
||||
logger.error(
|
||||
`Failed to calculate user clients after creating org ${orgId} for user ${ownerUserId}: ${e}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (!org) {
|
||||
return next(
|
||||
createHttpError(
|
||||
|
||||
Reference in New Issue
Block a user