update user schema, limit rts to client users

This commit is contained in:
2025-05-06 14:15:36 +05:30
parent c7e6b209e9
commit 16428cf895
4 changed files with 38 additions and 42 deletions

View File

@@ -13,7 +13,7 @@ const userSchema = new mongoose.Schema({
unique: true,
required: true,
},
orgId: mongoose.Types.ObjectId,
orgId: { type: mongoose.Types.ObjectId, ref: "organization" },
firstName: String,
lastName: String,
name: String,
@@ -28,10 +28,6 @@ const userSchema = new mongoose.Schema({
type: String,
required: true,
},
defaultClient: {
type: mongoose.Types.ObjectId,
ref: "organization",
},
passKeys: [new mongoose.Schema({}, { _id: false, strict: false })],
challenge: new mongoose.Schema(
{
@@ -92,13 +88,19 @@ const updateUserInput = z.object({
.optional(),
avatar: z.string().url().optional(),
role: z.enum(roles).optional(),
defaultClient: z.string().optional(),
orgId: z.string().optional(),
});
const userResponse = z.object({
_id: z.string(),
pid: z.string(),
orgId: z.string().optional(),
orgId: z
.object({
_id: z.string(),
pid: z.string(),
name: z.string(),
})
.optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
name: z.string().optional(),

View File

@@ -54,12 +54,16 @@ export async function createUser(
export async function getUser(userId: string) {
if (mongoose.Types.ObjectId.isValid(userId)) {
return await userModel.findById(userId);
return await userModel
.findById(userId)
.populate({ path: "orgId", select: "_id pid name" });
}
return await userModel.findOne({
$and: [{ pid: userId }],
});
return await userModel
.findOne({
$and: [{ pid: userId }],
})
.populate({ path: "orgId", select: "_id pid name" });
}
export async function getUserByToken(token: string) {