filter users for clients

This commit is contained in:
2025-06-05 10:24:00 +05:30
parent f258029d29
commit 01b40dd757
2 changed files with 17 additions and 3 deletions

View File

@@ -84,7 +84,7 @@ export async function getUserHandler(req: FastifyRequest, res: FastifyReply) {
export async function listUserHandler(req: FastifyRequest, res: FastifyReply) {
try {
const users = await listUsers(req.user.tenantId);
const users = await listUsers(req.user);
return res.code(200).send({ users: users });
} catch (err) {
return err;

View File

@@ -120,9 +120,23 @@ export async function getUserByEmail(email: string) {
return await userModel.findOne({ email: email });
}
export async function listUsers(tenantId: string) {
export async function listUsers(user: AuthenticatedUser) {
if (user.role === "client") {
return await userModel
.find({
$and: [
{ tenantId: user.tenantId, orgId: user.orgId },
{ dev: { $ne: true } },
],
})
.select(
"_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin"
)
.populate({ path: "orgId", select: "_id pid name avatar" });
}
return await userModel
.find({ $and: [{ tenantId: tenantId }, { dev: { $ne: true } }] })
.find({ $and: [{ tenantId: user.tenantId }, { dev: { $ne: true } }] })
.select(
"_id pid orgId firstName lastName name email role avatar status createdAt createdBy lastLogin"
)