mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-02 08:46:38 +00:00
♻️ make code cleanrer
This commit is contained in:
@@ -29,6 +29,7 @@ import { OpenAPITags, registry } from "@server/openApi";
|
|||||||
import NodeCache from "node-cache";
|
import NodeCache from "node-cache";
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import { getUserDeviceName } from "@server/db/names";
|
import { getUserDeviceName } from "@server/db/names";
|
||||||
|
import type { PaginatedResponse } from "@server/types/Pagination";
|
||||||
|
|
||||||
const olmVersionCache = new NodeCache({ stdTTL: 3600 });
|
const olmVersionCache = new NodeCache({ stdTTL: 3600 });
|
||||||
|
|
||||||
@@ -89,38 +90,29 @@ const listClientsParamsSchema = z.strictObject({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const listClientsSchema = z.object({
|
const listClientsSchema = z.object({
|
||||||
limit: z
|
pageSize: z.coerce
|
||||||
.string()
|
.number<string>() // for prettier formatting
|
||||||
|
.int()
|
||||||
|
.positive()
|
||||||
.optional()
|
.optional()
|
||||||
.default("1000")
|
.catch(20)
|
||||||
.transform(Number)
|
.default(20),
|
||||||
.pipe(z.int().positive()),
|
page: z.coerce
|
||||||
offset: z
|
.number<string>() // for prettier formatting
|
||||||
.string()
|
.int()
|
||||||
|
.min(0)
|
||||||
.optional()
|
.optional()
|
||||||
.default("0")
|
.catch(1)
|
||||||
.transform(Number)
|
.default(1),
|
||||||
.pipe(z.int().nonnegative()),
|
query: z.string().optional(),
|
||||||
|
sort_by: z
|
||||||
|
.enum(["megabytesIn", "megabytesOut"])
|
||||||
|
.optional()
|
||||||
|
.catch(undefined),
|
||||||
filter: z.enum(["user", "machine"]).optional()
|
filter: z.enum(["user", "machine"]).optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
function queryClients(
|
function queryClientsBase() {
|
||||||
orgId: string,
|
|
||||||
accessibleClientIds: number[],
|
|
||||||
filter?: "user" | "machine"
|
|
||||||
) {
|
|
||||||
const conditions = [
|
|
||||||
inArray(clients.clientId, accessibleClientIds),
|
|
||||||
eq(clients.orgId, orgId)
|
|
||||||
];
|
|
||||||
|
|
||||||
// Add filter condition based on filter type
|
|
||||||
if (filter === "user") {
|
|
||||||
conditions.push(isNotNull(clients.userId));
|
|
||||||
} else if (filter === "machine") {
|
|
||||||
conditions.push(isNull(clients.userId));
|
|
||||||
}
|
|
||||||
|
|
||||||
return db
|
return db
|
||||||
.select({
|
.select({
|
||||||
clientId: clients.clientId,
|
clientId: clients.clientId,
|
||||||
@@ -156,8 +148,7 @@ function queryClients(
|
|||||||
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
||||||
.leftJoin(olms, eq(clients.clientId, olms.clientId))
|
.leftJoin(olms, eq(clients.clientId, olms.clientId))
|
||||||
.leftJoin(users, eq(clients.userId, users.userId))
|
.leftJoin(users, eq(clients.userId, users.userId))
|
||||||
.leftJoin(currentFingerprint, eq(olms.olmId, currentFingerprint.olmId))
|
.leftJoin(currentFingerprint, eq(olms.olmId, currentFingerprint.olmId));
|
||||||
.where(and(...conditions));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getSiteAssociations(clientIds: number[]) {
|
async function getSiteAssociations(clientIds: number[]) {
|
||||||
@@ -175,7 +166,7 @@ async function getSiteAssociations(clientIds: number[]) {
|
|||||||
.where(inArray(clientSitesAssociationsCache.clientId, clientIds));
|
.where(inArray(clientSitesAssociationsCache.clientId, clientIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientWithSites = Awaited<ReturnType<typeof queryClients>>[0] & {
|
type ClientWithSites = Awaited<ReturnType<typeof queryClientsBase>>[0] & {
|
||||||
sites: Array<{
|
sites: Array<{
|
||||||
siteId: number;
|
siteId: number;
|
||||||
siteName: string | null;
|
siteName: string | null;
|
||||||
@@ -186,10 +177,9 @@ type ClientWithSites = Awaited<ReturnType<typeof queryClients>>[0] & {
|
|||||||
|
|
||||||
type OlmWithUpdateAvailable = ClientWithSites;
|
type OlmWithUpdateAvailable = ClientWithSites;
|
||||||
|
|
||||||
export type ListClientsResponse = {
|
export type ListClientsResponse = PaginatedResponse<{
|
||||||
clients: Array<ClientWithSites>;
|
clients: Array<ClientWithSites>;
|
||||||
pagination: { total: number; limit: number; offset: number };
|
}>;
|
||||||
};
|
|
||||||
|
|
||||||
registry.registerPath({
|
registry.registerPath({
|
||||||
method: "get",
|
method: "get",
|
||||||
@@ -218,7 +208,7 @@ export async function listClients(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const { limit, offset, filter } = parsedQuery.data;
|
const { page, pageSize, query, filter } = parsedQuery.data;
|
||||||
|
|
||||||
const parsedParams = listClientsParamsSchema.safeParse(req.params);
|
const parsedParams = listClientsParamsSchema.safeParse(req.params);
|
||||||
if (!parsedParams.success) {
|
if (!parsedParams.success) {
|
||||||
@@ -267,28 +257,31 @@ export async function listClients(
|
|||||||
const accessibleClientIds = accessibleClients.map(
|
const accessibleClientIds = accessibleClients.map(
|
||||||
(client) => client.clientId
|
(client) => client.clientId
|
||||||
);
|
);
|
||||||
const baseQuery = queryClients(orgId, accessibleClientIds, filter);
|
const baseQuery = queryClientsBase();
|
||||||
|
|
||||||
// Get client count with filter
|
// Get client count with filter
|
||||||
const countConditions = [
|
const conditions = [
|
||||||
inArray(clients.clientId, accessibleClientIds),
|
inArray(clients.clientId, accessibleClientIds),
|
||||||
eq(clients.orgId, orgId)
|
eq(clients.orgId, orgId)
|
||||||
];
|
];
|
||||||
|
|
||||||
if (filter === "user") {
|
if (filter === "user") {
|
||||||
countConditions.push(isNotNull(clients.userId));
|
conditions.push(isNotNull(clients.userId));
|
||||||
} else if (filter === "machine") {
|
} else if (filter === "machine") {
|
||||||
countConditions.push(isNull(clients.userId));
|
conditions.push(isNull(clients.userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
const countQuery = db
|
const countQuery = db.$count(
|
||||||
.select({ count: count() })
|
queryClientsBase().where(and(...conditions))
|
||||||
.from(clients)
|
);
|
||||||
.where(and(...countConditions));
|
|
||||||
|
|
||||||
const clientsList = await baseQuery.limit(limit).offset(offset);
|
const [clientsList, totalCount] = await Promise.all([
|
||||||
const totalCountResult = await countQuery;
|
baseQuery
|
||||||
const totalCount = totalCountResult[0].count;
|
.where(and(...conditions))
|
||||||
|
.limit(page)
|
||||||
|
.offset(pageSize * (page - 1)),
|
||||||
|
countQuery
|
||||||
|
]);
|
||||||
|
|
||||||
// Get associated sites for all clients
|
// Get associated sites for all clients
|
||||||
const clientIds = clientsList.map((client) => client.clientId);
|
const clientIds = clientsList.map((client) => client.clientId);
|
||||||
@@ -368,8 +361,8 @@ export async function listClients(
|
|||||||
clients: olmsWithUpdates,
|
clients: olmsWithUpdates,
|
||||||
pagination: {
|
pagination: {
|
||||||
total: totalCount,
|
total: totalCount,
|
||||||
limit,
|
page,
|
||||||
offset
|
pageSize
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import {
|
|||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { fromZodError } from "zod-validation-error";
|
import { fromZodError } from "zod-validation-error";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
import type { PaginatedResponse } from "@server/types/Pagination";
|
||||||
|
|
||||||
const listResourcesParamsSchema = z.strictObject({
|
const listResourcesParamsSchema = z.strictObject({
|
||||||
orgId: z.string()
|
orgId: z.string()
|
||||||
@@ -171,10 +172,9 @@ function queryResourcesBase() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ListResourcesResponse = {
|
export type ListResourcesResponse = PaginatedResponse<{
|
||||||
resources: ResourceWithTargets[];
|
resources: ResourceWithTargets[];
|
||||||
pagination: { total: number; pageSize: number; page: number };
|
}>;
|
||||||
};
|
|
||||||
|
|
||||||
registry.registerPath({
|
registry.registerPath({
|
||||||
method: "get",
|
method: "get",
|
||||||
@@ -268,16 +268,15 @@ export async function listResources(
|
|||||||
(resource) => resource.resourceId
|
(resource) => resource.resourceId
|
||||||
);
|
);
|
||||||
|
|
||||||
let conditions = and(
|
const conditions = [
|
||||||
and(
|
and(
|
||||||
inArray(resources.resourceId, accessibleResourceIds),
|
inArray(resources.resourceId, accessibleResourceIds),
|
||||||
eq(resources.orgId, orgId)
|
eq(resources.orgId, orgId)
|
||||||
)
|
)
|
||||||
);
|
];
|
||||||
|
|
||||||
if (query) {
|
if (query) {
|
||||||
conditions = and(
|
conditions.push(
|
||||||
conditions,
|
|
||||||
or(
|
or(
|
||||||
ilike(resources.name, "%" + query + "%"),
|
ilike(resources.name, "%" + query + "%"),
|
||||||
ilike(resources.fullDomain, "%" + query + "%")
|
ilike(resources.fullDomain, "%" + query + "%")
|
||||||
@@ -285,17 +284,16 @@ export async function listResources(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (typeof enabled !== "undefined") {
|
if (typeof enabled !== "undefined") {
|
||||||
conditions = and(conditions, eq(resources.enabled, enabled));
|
conditions.push(eq(resources.enabled, enabled));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof authState !== "undefined") {
|
if (typeof authState !== "undefined") {
|
||||||
switch (authState) {
|
switch (authState) {
|
||||||
case "none":
|
case "none":
|
||||||
conditions = and(conditions, eq(resources.http, false));
|
conditions.push(eq(resources.http, false));
|
||||||
break;
|
break;
|
||||||
case "protected":
|
case "protected":
|
||||||
conditions = and(
|
conditions.push(
|
||||||
conditions,
|
|
||||||
or(
|
or(
|
||||||
eq(resources.sso, true),
|
eq(resources.sso, true),
|
||||||
eq(resources.emailWhitelistEnabled, true),
|
eq(resources.emailWhitelistEnabled, true),
|
||||||
@@ -306,8 +304,7 @@ export async function listResources(
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case "not_protected":
|
case "not_protected":
|
||||||
conditions = and(
|
conditions.push(
|
||||||
conditions,
|
|
||||||
not(eq(resources.sso, true)),
|
not(eq(resources.sso, true)),
|
||||||
not(eq(resources.emailWhitelistEnabled, true)),
|
not(eq(resources.emailWhitelistEnabled, true)),
|
||||||
isNull(resourceHeaderAuth.headerAuthId),
|
isNull(resourceHeaderAuth.headerAuthId),
|
||||||
@@ -318,7 +315,7 @@ export async function listResources(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let aggregateFilters: SQL<any> | null | undefined = null;
|
let aggregateFilters: SQL<any> | undefined = sql`1 = 1`;
|
||||||
|
|
||||||
if (typeof healthStatus !== "undefined") {
|
if (typeof healthStatus !== "undefined") {
|
||||||
switch (healthStatus) {
|
switch (healthStatus) {
|
||||||
@@ -354,8 +351,8 @@ export async function listResources(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const baseQuery = queryResourcesBase()
|
const baseQuery = queryResourcesBase()
|
||||||
.where(conditions)
|
.where(and(...conditions))
|
||||||
.having(aggregateFilters ?? sql`1 = 1`);
|
.having(aggregateFilters);
|
||||||
|
|
||||||
// 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(baseQuery.as("filtered_resources"));
|
const countQuery = db.$count(baseQuery.as("filtered_resources"));
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import { fromError } from "zod-validation-error";
|
|||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import semver from "semver";
|
import semver from "semver";
|
||||||
import cache from "@server/lib/cache";
|
import cache from "@server/lib/cache";
|
||||||
|
import type { PaginatedResponse } from "@server/types/Pagination";
|
||||||
|
|
||||||
async function getLatestNewtVersion(): Promise<string | null> {
|
async function getLatestNewtVersion(): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
@@ -111,9 +112,6 @@ const listSitesSchema = z.object({
|
|||||||
.catch(undefined)
|
.catch(undefined)
|
||||||
});
|
});
|
||||||
|
|
||||||
function countSitesBase() {
|
|
||||||
return db.select({ count: count() }).from(sites);
|
|
||||||
}
|
|
||||||
function querySitesBase() {
|
function querySitesBase() {
|
||||||
return db
|
return db
|
||||||
.select({
|
.select({
|
||||||
@@ -148,10 +146,9 @@ type SiteWithUpdateAvailable = Awaited<ReturnType<typeof querySitesBase>>[0] & {
|
|||||||
newtUpdateAvailable?: boolean;
|
newtUpdateAvailable?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ListSitesResponse = {
|
export type ListSitesResponse = PaginatedResponse<{
|
||||||
sites: SiteWithUpdateAvailable[];
|
sites: SiteWithUpdateAvailable[];
|
||||||
pagination: { total: number; pageSize: number; page: number };
|
}>;
|
||||||
};
|
|
||||||
|
|
||||||
registry.registerPath({
|
registry.registerPath({
|
||||||
method: "get",
|
method: "get",
|
||||||
@@ -227,13 +224,14 @@ export async function listSites(
|
|||||||
|
|
||||||
const accessibleSiteIds = accessibleSites.map((site) => site.siteId);
|
const accessibleSiteIds = accessibleSites.map((site) => site.siteId);
|
||||||
|
|
||||||
let conditions = and(
|
const conditions = [
|
||||||
inArray(sites.siteId, accessibleSiteIds),
|
and(
|
||||||
eq(sites.orgId, orgId)
|
inArray(sites.siteId, accessibleSiteIds),
|
||||||
);
|
eq(sites.orgId, orgId)
|
||||||
|
)
|
||||||
|
];
|
||||||
if (query) {
|
if (query) {
|
||||||
conditions = and(
|
conditions.push(
|
||||||
conditions,
|
|
||||||
or(
|
or(
|
||||||
ilike(sites.name, "%" + query + "%"),
|
ilike(sites.name, "%" + query + "%"),
|
||||||
ilike(sites.niceId, "%" + query + "%")
|
ilike(sites.niceId, "%" + query + "%")
|
||||||
@@ -241,13 +239,15 @@ export async function listSites(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (typeof online !== "undefined") {
|
if (typeof online !== "undefined") {
|
||||||
conditions = and(conditions, eq(sites.online, online));
|
conditions.push(eq(sites.online, online));
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseQuery = querySitesBase().where(conditions);
|
const baseQuery = querySitesBase().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(querySitesBase().where(conditions));
|
const countQuery = db.$count(
|
||||||
|
querySitesBase().where(and(...conditions))
|
||||||
|
);
|
||||||
|
|
||||||
const siteListQuery = baseQuery
|
const siteListQuery = baseQuery
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { eq, and, asc, ilike, or } from "drizzle-orm";
|
|||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
import type { PaginatedResponse } from "@server/types/Pagination";
|
||||||
|
|
||||||
const listAllSiteResourcesByOrgParamsSchema = z.strictObject({
|
const listAllSiteResourcesByOrgParamsSchema = z.strictObject({
|
||||||
orgId: z.string()
|
orgId: z.string()
|
||||||
@@ -33,14 +34,13 @@ const listAllSiteResourcesByOrgQuerySchema = z.object({
|
|||||||
mode: z.enum(["host", "cidr"]).optional().catch(undefined)
|
mode: z.enum(["host", "cidr"]).optional().catch(undefined)
|
||||||
});
|
});
|
||||||
|
|
||||||
export type ListAllSiteResourcesByOrgResponse = {
|
export type ListAllSiteResourcesByOrgResponse = PaginatedResponse<{
|
||||||
siteResources: (SiteResource & {
|
siteResources: (SiteResource & {
|
||||||
siteName: string;
|
siteName: string;
|
||||||
siteNiceId: string;
|
siteNiceId: string;
|
||||||
siteAddress: string | null;
|
siteAddress: string | null;
|
||||||
})[];
|
})[];
|
||||||
pagination: { total: number; pageSize: number; page: number };
|
}>;
|
||||||
};
|
|
||||||
|
|
||||||
function querySiteResourcesBase() {
|
function querySiteResourcesBase() {
|
||||||
return db
|
return db
|
||||||
@@ -114,10 +114,9 @@ export async function listAllSiteResourcesByOrg(
|
|||||||
const { orgId } = parsedParams.data;
|
const { orgId } = parsedParams.data;
|
||||||
const { page, pageSize, query, mode } = parsedQuery.data;
|
const { page, pageSize, query, mode } = parsedQuery.data;
|
||||||
|
|
||||||
let conditions = and(eq(siteResources.orgId, orgId));
|
const conditions = [and(eq(siteResources.orgId, orgId))];
|
||||||
if (query) {
|
if (query) {
|
||||||
conditions = and(
|
conditions.push(
|
||||||
conditions,
|
|
||||||
or(
|
or(
|
||||||
ilike(siteResources.name, "%" + query + "%"),
|
ilike(siteResources.name, "%" + query + "%"),
|
||||||
ilike(siteResources.destination, "%" + query + "%"),
|
ilike(siteResources.destination, "%" + query + "%"),
|
||||||
@@ -129,16 +128,15 @@ export async function listAllSiteResourcesByOrg(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mode) {
|
if (mode) {
|
||||||
conditions = and(conditions, eq(siteResources.mode, mode));
|
conditions.push(eq(siteResources.mode, mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseQuery = querySiteResourcesBase().where(conditions);
|
const baseQuery = querySiteResourcesBase().where(and(...conditions));
|
||||||
|
|
||||||
const countQuery = db.$count(
|
const countQuery = db.$count(
|
||||||
querySiteResourcesBase().where(conditions)
|
querySiteResourcesBase().where(and(...conditions))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get all site resources for the org with site names
|
|
||||||
const [siteResourcesList, totalCount] = await Promise.all([
|
const [siteResourcesList, totalCount] = await Promise.all([
|
||||||
baseQuery
|
baseQuery
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
|
|||||||
5
server/types/Pagination.ts
Normal file
5
server/types/Pagination.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export type Pagination = { total: number; pageSize: number; page: number };
|
||||||
|
|
||||||
|
export type PaginatedResponse<T> = T & {
|
||||||
|
pagination: Pagination;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user