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

@@ -38,7 +38,7 @@ export async function listRtsHandler(req: FastifyRequest, res: FastifyReply) {
const queryParams = req.query as PageQueryParams; const queryParams = req.query as PageQueryParams;
try { try {
const rtsList = await listRts(queryParams, req.user.tenantId); const rtsList = await listRts(queryParams, req.user);
return res.code(200).send(rtsList); return res.code(200).send(rtsList);
} catch (err) { } catch (err) {
return err; return err;

View File

@@ -11,6 +11,7 @@ import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
import { getUser } from "../user/user.service"; import { getUser } from "../user/user.service";
import { orgModel } from "../organization/organization.schema"; import { orgModel } from "../organization/organization.schema";
import { userModel } from "../user/user.schema"; import { userModel } from "../user/user.schema";
import mongoose from "mongoose";
export async function createRts( export async function createRts(
input: CreateRtsInput, input: CreateRtsInput,
@@ -18,36 +19,18 @@ export async function createRts(
) { ) {
let defaultClient = null; let defaultClient = null;
const userInDb = await getUser(user.userId); const userInDb = await getUser(user.userId);
if (userInDb && userInDb.defaultClient) { if (userInDb && userInDb.orgId) {
defaultClient = userInDb.defaultClient; defaultClient = userInDb.orgId;
} }
if (!input.files) { return await rtsModel.create({
return await rtsModel.create({ ...input,
...input, tenantId: user.tenantId,
tenantId: user.tenantId, pid: generateId(),
pid: generateId(), client: defaultClient,
client: defaultClient, createdAt: new Date(),
createdAt: new Date(), createdBy: user.userId ?? null,
createdBy: user.userId ?? null, });
});
} else {
return await rtsModel.create({
tenantId: user.tenantId,
pid: generateId(),
county: input.county,
client: input.client,
documents: [
{
files: input.files,
createdAt: new Date(),
createdBy: user.userId ?? null,
},
],
createdAt: new Date(),
createdBy: user.userId ?? null,
});
}
} }
export async function getRts(id: string, tenantId: string) { export async function getRts(id: string, tenantId: string) {
@@ -59,15 +42,22 @@ export async function getRts(id: string, tenantId: string) {
.populate({ path: "assignedTo", select: "pid name avatar" }); .populate({ path: "assignedTo", select: "pid name avatar" });
} }
export async function listRts(params: PageQueryParams, tenantId: string) { export async function listRts(
params: PageQueryParams,
user: AuthenticatedUser
) {
const page = params.page || 1; const page = params.page || 1;
const pageSize = params.pageSize || 10; const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, rtsFields); const sortObj = getSortObject(params, rtsFields);
const filterObj = getFilterObject(params) || []; const filterObj = getFilterObject(params) || [];
if (user.role === "client") {
filterObj.push({ client: new mongoose.Types.ObjectId(user.orgId) });
}
const rtsList = await rtsModel.aggregate([ const rtsList = await rtsModel.aggregate([
{ {
$match: { $and: [{ tenantId: tenantId }, ...filterObj] }, $match: { $and: [{ tenantId: user.tenantId }, ...filterObj] },
}, },
{ {
$lookup: { $lookup: {

View File

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

View File

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