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