mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-01 16:26:39 +00:00
Also update in the assign
This commit is contained in:
@@ -14,6 +14,7 @@ import { usageService } from "@server/lib/billing/usageService";
|
|||||||
import { FeatureId } from "@server/lib/billing";
|
import { FeatureId } from "@server/lib/billing";
|
||||||
import { calculateUserClientsForOrgs } from "@server/lib/calculateUserClientsForOrgs";
|
import { calculateUserClientsForOrgs } from "@server/lib/calculateUserClientsForOrgs";
|
||||||
import { build } from "@server/build";
|
import { build } from "@server/build";
|
||||||
|
import { assignUserToOrg } from "@server/lib/userOrg";
|
||||||
|
|
||||||
const acceptInviteBodySchema = z.strictObject({
|
const acceptInviteBodySchema = z.strictObject({
|
||||||
token: z.string(),
|
token: z.string(),
|
||||||
@@ -160,12 +161,15 @@ export async function acceptInvite(
|
|||||||
}
|
}
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
await db.transaction(async (trx) => {
|
||||||
// add the user to the org
|
await assignUserToOrg(
|
||||||
await trx.insert(userOrgs).values({
|
org,
|
||||||
userId: existingUser[0].userId,
|
{
|
||||||
orgId: existingInvite.orgId,
|
userId: existingUser[0].userId,
|
||||||
roleId: existingInvite.roleId
|
orgId: existingInvite.orgId,
|
||||||
});
|
roleId: existingInvite.roleId
|
||||||
|
},
|
||||||
|
trx
|
||||||
|
);
|
||||||
|
|
||||||
// delete the invite
|
// delete the invite
|
||||||
await trx
|
await trx
|
||||||
@@ -174,40 +178,6 @@ export async function acceptInvite(
|
|||||||
|
|
||||||
await calculateUserClientsForOrgs(existingUser[0].userId, trx);
|
await calculateUserClientsForOrgs(existingUser[0].userId, trx);
|
||||||
|
|
||||||
// calculate if the user is in any other of the orgs before we count it as an add to the billing org
|
|
||||||
if (org.billingOrgId) {
|
|
||||||
const otherBillingOrgs = await trx
|
|
||||||
.select()
|
|
||||||
.from(orgs)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(orgs.billingOrgId, org.billingOrgId),
|
|
||||||
ne(orgs.orgId, existingInvite.orgId)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const billingOrgIds = otherBillingOrgs.map((o) => o.orgId);
|
|
||||||
|
|
||||||
const orgsInBillingDomainThatTheUserIsStillIn = await trx
|
|
||||||
.select()
|
|
||||||
.from(userOrgs)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(userOrgs.userId, existingUser[0].userId),
|
|
||||||
inArray(userOrgs.orgId, billingOrgIds)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (orgsInBillingDomainThatTheUserIsStillIn.length === 0) {
|
|
||||||
await usageService.add(
|
|
||||||
existingInvite.orgId,
|
|
||||||
FeatureId.USERS,
|
|
||||||
1,
|
|
||||||
trx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`User ${existingUser[0].userId} accepted invite to org ${existingInvite.orgId}`
|
`User ${existingUser[0].userId} accepted invite to org ${existingInvite.orgId}`
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user