improve query performance
This commit is contained in:
@@ -22,9 +22,9 @@ export async function getPermit(permitId: string, tenantId: string) {
|
||||
.findOne({
|
||||
$and: [{ tenantId: tenantId }, { pid: permitId }],
|
||||
})
|
||||
.populate("county")
|
||||
.populate("client")
|
||||
.populate("assignedTo");
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
}
|
||||
|
||||
export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||
@@ -55,7 +55,7 @@ export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "permits",
|
||||
from: "users",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedRec",
|
||||
@@ -63,15 +63,47 @@ export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
_id: 1,
|
||||
pid: 1,
|
||||
permitNumber: 1,
|
||||
county: { $arrayElemAt: ["$countyRec", 0] },
|
||||
client: { $arrayElemAt: ["$clientRec", 0] },
|
||||
permitDate: 1,
|
||||
stage: 1,
|
||||
status: 1,
|
||||
assignedTo: { $arrayElemAt: ["$assignedRec", 0] },
|
||||
county: {
|
||||
$let: {
|
||||
vars: { county: { $arrayElemAt: ["$countyRec", 0] } },
|
||||
in: {
|
||||
_id: "$$county._id",
|
||||
pid: "$$county.pid",
|
||||
name: "$$county.name",
|
||||
type: "$$county.type",
|
||||
avatar: "$$county.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
client: {
|
||||
$let: {
|
||||
vars: { client: { $arrayElemAt: ["$clientRec", 0] } },
|
||||
in: {
|
||||
_id: "$$client._id",
|
||||
pid: "$$client.pid",
|
||||
name: "$$client.name",
|
||||
type: "$$client.type",
|
||||
avatar: "$$client.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
||||
in: {
|
||||
_id: "$$assigned._id",
|
||||
pid: "$$assigned.pid",
|
||||
name: "$$assigned.name",
|
||||
avatar: "$$assigned.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -112,9 +144,9 @@ export async function updatePermit(
|
||||
{ ...input, updatedAt: new Date() },
|
||||
{ new: true }
|
||||
)
|
||||
.populate("county")
|
||||
.populate("client")
|
||||
.populate("assignedTo");
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
|
||||
return updatePermitResult;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ export async function updateRtsHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const updatedRts = await updateRts(rtsId, input, req.user.tenantId);
|
||||
if (!updatedRts) return res.code(404).send({ error: "resource not found" });
|
||||
|
||||
return res.code(200).send(updateRts);
|
||||
return res.code(200).send(updatedRts);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ export async function createRts(
|
||||
export async function getRts(id: string, tenantId: string) {
|
||||
return await rtsModel
|
||||
.findOne({ pid: id, tenantId: tenantId })
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
}
|
||||
|
||||
@@ -62,7 +64,6 @@ export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
localField: "county",
|
||||
foreignField: "_id",
|
||||
as: "countyRec",
|
||||
pipeline: [{ $project: { _id: 1, pid: 1, name: 1, type: 1 } }],
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -71,7 +72,6 @@ export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
localField: "client",
|
||||
foreignField: "_id",
|
||||
as: "clientRec",
|
||||
pipeline: [{ $project: { _id: 1, pid: 1, name: 1, type: 1 } }],
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -80,18 +80,47 @@ export async function listRts(params: PageQueryParams, tenantId: string) {
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdRec",
|
||||
pipeline: [{ $project: { _id: 1, pid: 1, name: 1, avatar: 1 } }],
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
pid: 1,
|
||||
county: { $arrayElemAt: ["$countyRec", 0] },
|
||||
client: { $arrayElemAt: ["$clientRec", 0] },
|
||||
statusPipeline: 1,
|
||||
createdAt: 1,
|
||||
createdBy: { $arrayElemAt: ["$createdRec", 0] },
|
||||
county: {
|
||||
$let: {
|
||||
vars: { county: { $arrayElemAt: ["$countyRec", 0] } },
|
||||
in: {
|
||||
_id: "$$county._id",
|
||||
pid: "$$county.pid",
|
||||
name: "$$county.name",
|
||||
type: "$$county.type",
|
||||
},
|
||||
},
|
||||
},
|
||||
client: {
|
||||
$let: {
|
||||
vars: { client: { $arrayElemAt: ["$clientRec", 0] } },
|
||||
in: {
|
||||
_id: "$$client._id",
|
||||
pid: "$$client.pid",
|
||||
name: "$$client.name",
|
||||
type: "$$client.type",
|
||||
},
|
||||
},
|
||||
},
|
||||
createdBy: {
|
||||
$let: {
|
||||
vars: { created: { $arrayElemAt: ["$createdRec", 0] } },
|
||||
in: {
|
||||
_id: "$$created._id",
|
||||
pid: "$$created.pid",
|
||||
name: "$$created.name",
|
||||
avatar: "$$created.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -124,10 +153,13 @@ export async function updateRts(
|
||||
input: UpdateRtsInput,
|
||||
tenantId: string
|
||||
) {
|
||||
return await rtsModel.findOneAndUpdate(
|
||||
{ pid: id, tenantId: tenantId },
|
||||
input
|
||||
);
|
||||
const updatedRts = await rtsModel
|
||||
.findOneAndUpdate({ pid: id, tenantId: tenantId }, input, { new: true })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" });
|
||||
|
||||
return updatedRts;
|
||||
}
|
||||
|
||||
export async function deleteRts(id: string, tenantId: string) {
|
||||
|
||||
Reference in New Issue
Block a user