update user schema, limit rts to client users
This commit is contained in:
@@ -38,7 +38,7 @@ export async function listRtsHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const queryParams = req.query as PageQueryParams;
|
||||
|
||||
try {
|
||||
const rtsList = await listRts(queryParams, req.user.tenantId);
|
||||
const rtsList = await listRts(queryParams, req.user);
|
||||
return res.code(200).send(rtsList);
|
||||
} catch (err) {
|
||||
return err;
|
||||
|
||||
@@ -11,6 +11,7 @@ import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { getUser } from "../user/user.service";
|
||||
import { orgModel } from "../organization/organization.schema";
|
||||
import { userModel } from "../user/user.schema";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export async function createRts(
|
||||
input: CreateRtsInput,
|
||||
@@ -18,36 +19,18 @@ export async function createRts(
|
||||
) {
|
||||
let defaultClient = null;
|
||||
const userInDb = await getUser(user.userId);
|
||||
if (userInDb && userInDb.defaultClient) {
|
||||
defaultClient = userInDb.defaultClient;
|
||||
if (userInDb && userInDb.orgId) {
|
||||
defaultClient = userInDb.orgId;
|
||||
}
|
||||
|
||||
if (!input.files) {
|
||||
return await rtsModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
client: defaultClient,
|
||||
createdAt: new Date(),
|
||||
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,
|
||||
});
|
||||
}
|
||||
return await rtsModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
client: defaultClient,
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
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" });
|
||||
}
|
||||
|
||||
export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
export async function listRts(
|
||||
params: PageQueryParams,
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
const page = params.page || 1;
|
||||
const pageSize = params.pageSize || 10;
|
||||
const sortObj = getSortObject(params, rtsFields);
|
||||
const filterObj = getFilterObject(params) || [];
|
||||
|
||||
if (user.role === "client") {
|
||||
filterObj.push({ client: new mongoose.Types.ObjectId(user.orgId) });
|
||||
}
|
||||
|
||||
const rtsList = await rtsModel.aggregate([
|
||||
{
|
||||
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
|
||||
$match: { $and: [{ tenantId: user.tenantId }, ...filterObj] },
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
|
||||
Reference in New Issue
Block a user