add admin utils

This commit is contained in:
Emily
2024-06-06 03:46:12 +02:00
parent 1c26f6a1d4
commit 31dc1b2fc9

View File

@@ -4,6 +4,8 @@ export type AdminProjectsList = {
premium: boolean, premium: boolean,
created_at: Date, created_at: Date,
project_name: string, project_name: string,
premium_type: number,
_id: string,
user: { user: {
name: string, name: string,
email: string, email: string,
@@ -32,71 +34,26 @@ export default defineEventHandler(async event => {
}, },
{ {
$lookup: { $lookup: {
from: "visits", from: "project_counts",
let: { projectId: "$_id" }, localField: "_id",
pipeline: [ foreignField: "project_id",
{ as: "counts"
$match: {
$expr: {
$eq: ["$project_id", "$$projectId"]
}
}
},
{
$count: "total_visits"
}
],
as: "visits"
}
},
{
$lookup: {
from: "events",
let: { projectId: "$_id" },
pipeline: [
{
$match: {
$expr: {
$eq: ["$project_id", "$$projectId"]
}
}
},
{
$count: "total_events"
}
],
as: "events"
} }
}, },
{ {
$project: { $project: {
project_name: "$name", project_name: "$name",
premium: 1, premium: 1,
premium_type: 1,
created_at: 1, created_at: 1,
user: { user: {
$first: "$user" $first: "$user"
}, },
total_visits: { total_visits: {
$ifNull: [ $arrayElemAt: ["$counts.visits", 0]
{
$arrayElemAt: [
"$visits.total_visits",
0
]
},
0
]
}, },
total_events: { total_events: {
$ifNull: [ $arrayElemAt: ["$counts.visits", 0]
{
$arrayElemAt: [
"$events.total_events",
0
]
},
0
]
} }
} }
} }