Fix orgId not in queries

This commit is contained in:
Owen 2025-06-27 18:00:57 -04:00
parent 2ead5f4506
commit baee745d3c
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD

View file

@ -1,8 +1,8 @@
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
import { z } from "zod"; import { z } from "zod";
import { db } from "@server/db"; import { db, resources, sites } from "@server/db";
import { userOrgs, userResources, users, userSites } from "@server/db"; import { userOrgs, userResources, users, userSites } from "@server/db";
import { and, eq } from "drizzle-orm"; import { and, eq, exists } from "drizzle-orm";
import response from "@server/lib/response"; import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors"; import createHttpError from "http-errors";
@ -50,7 +50,7 @@ export async function removeUserOrg(
const user = await db const user = await db
.select() .select()
.from(userOrgs) .from(userOrgs)
.where(eq(userOrgs.userId, userId)); .where(and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId)));
if (!user || user.length === 0) { if (!user || user.length === 0) {
return next(createHttpError(HttpCode.NOT_FOUND, "User not found")); return next(createHttpError(HttpCode.NOT_FOUND, "User not found"));
@ -72,11 +72,42 @@ export async function removeUserOrg(
and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId)) and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId))
); );
await trx await db.delete(userResources).where(
.delete(userResources) and(
.where(eq(userResources.userId, userId)); eq(userResources.userId, userId),
exists(
db
.select()
.from(resources)
.where(
and(
eq(
resources.resourceId,
userResources.resourceId
),
eq(resources.orgId, orgId)
)
)
)
)
);
await trx.delete(userSites).where(eq(userSites.userId, userId)); await db.delete(userSites).where(
and(
eq(userSites.userId, userId),
exists(
db
.select()
.from(sites)
.where(
and(
eq(sites.siteId, userSites.siteId),
eq(sites.orgId, orgId)
)
)
)
)
);
}); });
return response(res, { return response(res, {