mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-21 08:15:17 +00:00
✨ search list by labels too
This commit is contained in:
@@ -190,9 +190,9 @@ const listSitesSchema = z.object({
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
function querySitesBase() {
|
function querySitesBase(isLabelFeatureEnabled: boolean) {
|
||||||
return db
|
let query = db
|
||||||
.select({
|
.selectDistinct({
|
||||||
siteId: sites.siteId,
|
siteId: sites.siteId,
|
||||||
niceId: sites.niceId,
|
niceId: sites.niceId,
|
||||||
name: sites.name,
|
name: sites.name,
|
||||||
@@ -231,6 +231,14 @@ function querySitesBase() {
|
|||||||
remoteExitNodes,
|
remoteExitNodes,
|
||||||
eq(remoteExitNodes.exitNodeId, sites.exitNodeId)
|
eq(remoteExitNodes.exitNodeId, sites.exitNodeId)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (isLabelFeatureEnabled) {
|
||||||
|
query = query
|
||||||
|
.leftJoin(siteLabels, eq(siteLabels.siteId, sites.siteId))
|
||||||
|
.leftJoin(labels, eq(labels.labelId, siteLabels.labelId));
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
type SiteRowBase = Awaited<ReturnType<typeof querySitesBase>>[0];
|
type SiteRowBase = Awaited<ReturnType<typeof querySitesBase>>[0];
|
||||||
@@ -314,6 +322,11 @@ export async function listSites(
|
|||||||
.where(eq(sites.orgId, orgId));
|
.where(eq(sites.orgId, orgId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isLabelFeatureEnabled = await isLicensedOrSubscribed(
|
||||||
|
orgId,
|
||||||
|
tierMatrix.labels
|
||||||
|
);
|
||||||
|
|
||||||
const { pageSize, page, query, sort_by, order, online, status } =
|
const { pageSize, page, query, sort_by, order, online, status } =
|
||||||
parsedQuery.data;
|
parsedQuery.data;
|
||||||
|
|
||||||
@@ -325,31 +338,43 @@ export async function listSites(
|
|||||||
eq(sites.orgId, orgId)
|
eq(sites.orgId, orgId)
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
if (query) {
|
|
||||||
conditions.push(
|
|
||||||
or(
|
|
||||||
like(
|
|
||||||
sql`LOWER(${sites.name})`,
|
|
||||||
"%" + query.toLowerCase() + "%"
|
|
||||||
),
|
|
||||||
like(
|
|
||||||
sql`LOWER(${sites.niceId})`,
|
|
||||||
"%" + query.toLowerCase() + "%"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (typeof online !== "undefined") {
|
if (typeof online !== "undefined") {
|
||||||
conditions.push(eq(sites.online, online));
|
conditions.push(eq(sites.online, online));
|
||||||
}
|
}
|
||||||
if (typeof status !== "undefined") {
|
if (typeof status !== "undefined") {
|
||||||
conditions.push(eq(sites.status, status));
|
conditions.push(eq(sites.status, status));
|
||||||
}
|
}
|
||||||
const baseQuery = querySitesBase().where(and(...conditions));
|
if (query) {
|
||||||
|
const queryList = [
|
||||||
|
like(
|
||||||
|
sql`LOWER(${sites.name})`,
|
||||||
|
"%" + query.toLowerCase() + "%"
|
||||||
|
),
|
||||||
|
like(
|
||||||
|
sql`LOWER(${sites.niceId})`,
|
||||||
|
"%" + query.toLowerCase() + "%"
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
if (isLabelFeatureEnabled) {
|
||||||
|
queryList.push(
|
||||||
|
like(
|
||||||
|
sql`LOWER(${labels.name})`,
|
||||||
|
"%" + query.toLowerCase() + "%"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
conditions.push(or(...queryList));
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseQuery = querySitesBase(isLabelFeatureEnabled).where(
|
||||||
|
and(...conditions)
|
||||||
|
);
|
||||||
|
|
||||||
// we need to add `as` so that drizzle filters the result as a subquery
|
// we need to add `as` so that drizzle filters the result as a subquery
|
||||||
const countQuery = db.$count(
|
const countQuery = db.$count(
|
||||||
querySitesBase()
|
querySitesBase(isLabelFeatureEnabled)
|
||||||
.where(and(...conditions))
|
.where(and(...conditions))
|
||||||
.as("filtered_sites")
|
.as("filtered_sites")
|
||||||
);
|
);
|
||||||
@@ -382,25 +407,24 @@ export async function listSites(
|
|||||||
siteId: number;
|
siteId: number;
|
||||||
}> = [];
|
}> = [];
|
||||||
|
|
||||||
// The label feature should be added in the tiers
|
if (isLabelFeatureEnabled) {
|
||||||
// if (await isLicensedOrSubscribed(orgId, tierMatrix.fullRbac)) {
|
labelsForSites =
|
||||||
// }
|
siteIds.length === 0
|
||||||
labelsForSites =
|
? []
|
||||||
siteIds.length === 0
|
: await db
|
||||||
? []
|
.select({
|
||||||
: await db
|
labelId: labels.labelId,
|
||||||
.select({
|
name: labels.name,
|
||||||
labelId: labels.labelId,
|
color: labels.color,
|
||||||
name: labels.name,
|
siteId: siteLabels.siteId
|
||||||
color: labels.color,
|
})
|
||||||
siteId: siteLabels.siteId
|
.from(labels)
|
||||||
})
|
.innerJoin(
|
||||||
.from(labels)
|
siteLabels,
|
||||||
.innerJoin(
|
eq(siteLabels.labelId, labels.labelId)
|
||||||
siteLabels,
|
)
|
||||||
eq(siteLabels.labelId, labels.labelId)
|
.where(inArray(siteLabels.siteId, siteIds));
|
||||||
)
|
}
|
||||||
.where(inArray(siteLabels.siteId, siteIds));
|
|
||||||
|
|
||||||
const sitesWithUpdates: SiteWithUpdateAvailable[] = rows.map((site) => {
|
const sitesWithUpdates: SiteWithUpdateAvailable[] = rows.map((site) => {
|
||||||
const siteWithUpdate: SiteWithUpdateAvailable = { ...site };
|
const siteWithUpdate: SiteWithUpdateAvailable = { ...site };
|
||||||
|
|||||||
Reference in New Issue
Block a user