mirror of
https://github.com/fosrl/pangolin.git
synced 2025-12-12 02:47:46 +00:00
Update list clients
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
|||||||
roleClients,
|
roleClients,
|
||||||
sites,
|
sites,
|
||||||
userClients,
|
userClients,
|
||||||
|
clientSites
|
||||||
} from "@server/db/schema";
|
} from "@server/db/schema";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
@@ -41,7 +42,6 @@ function queryClients(orgId: string, accessibleClientIds: number[]) {
|
|||||||
.select({
|
.select({
|
||||||
clientId: clients.clientId,
|
clientId: clients.clientId,
|
||||||
orgId: clients.orgId,
|
orgId: clients.orgId,
|
||||||
siteNiceId: sites.niceId,
|
|
||||||
name: clients.name,
|
name: clients.name,
|
||||||
pubKey: clients.pubKey,
|
pubKey: clients.pubKey,
|
||||||
subnet: clients.subnet,
|
subnet: clients.subnet,
|
||||||
@@ -49,8 +49,7 @@ function queryClients(orgId: string, accessibleClientIds: number[]) {
|
|||||||
megabytesOut: clients.megabytesOut,
|
megabytesOut: clients.megabytesOut,
|
||||||
orgName: orgs.name,
|
orgName: orgs.name,
|
||||||
type: clients.type,
|
type: clients.type,
|
||||||
online: clients.online,
|
online: clients.online
|
||||||
siteName: sites.name
|
|
||||||
})
|
})
|
||||||
.from(clients)
|
.from(clients)
|
||||||
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
||||||
@@ -62,8 +61,27 @@ function queryClients(orgId: string, accessibleClientIds: number[]) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getSiteAssociations(clientIds: number[]) {
|
||||||
|
if (clientIds.length === 0) return [];
|
||||||
|
|
||||||
|
return db
|
||||||
|
.select({
|
||||||
|
clientId: clientSites.clientId,
|
||||||
|
siteId: clientSites.siteId,
|
||||||
|
siteName: sites.name,
|
||||||
|
siteNiceId: sites.niceId
|
||||||
|
})
|
||||||
|
.from(clientSites)
|
||||||
|
.leftJoin(sites, eq(clientSites.siteId, sites.siteId))
|
||||||
|
.where(inArray(clientSites.clientId, clientIds));
|
||||||
|
}
|
||||||
|
|
||||||
export type ListClientsResponse = {
|
export type ListClientsResponse = {
|
||||||
clients: Awaited<ReturnType<typeof queryClients>>;
|
clients: Array<Awaited<ReturnType<typeof queryClients>>[0] & { sites: Array<{
|
||||||
|
siteId: number;
|
||||||
|
siteName: string | null;
|
||||||
|
siteNiceId: string | null;
|
||||||
|
}> }>;
|
||||||
pagination: { total: number; limit: number; offset: number };
|
pagination: { total: number; limit: number; offset: number };
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -121,17 +139,18 @@ export async function listClients(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const accessibleClientIds = accessibleClients.map(
|
const accessibleClientIds = accessibleClients.map(
|
||||||
(site) => site.clientId
|
(client) => client.clientId
|
||||||
);
|
);
|
||||||
const baseQuery = queryClients(orgId, accessibleClientIds);
|
const baseQuery = queryClients(orgId, accessibleClientIds);
|
||||||
|
|
||||||
let countQuery = db
|
// Get client count
|
||||||
|
const countQuery = db
|
||||||
.select({ count: count() })
|
.select({ count: count() })
|
||||||
.from(sites)
|
.from(clients)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
inArray(sites.siteId, accessibleClientIds),
|
inArray(clients.clientId, accessibleClientIds),
|
||||||
eq(sites.orgId, orgId)
|
eq(clients.orgId, orgId)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -139,9 +158,36 @@ export async function listClients(
|
|||||||
const totalCountResult = await countQuery;
|
const totalCountResult = await countQuery;
|
||||||
const totalCount = totalCountResult[0].count;
|
const totalCount = totalCountResult[0].count;
|
||||||
|
|
||||||
|
// Get associated sites for all clients
|
||||||
|
const clientIds = clientsList.map(client => client.clientId);
|
||||||
|
const siteAssociations = await getSiteAssociations(clientIds);
|
||||||
|
|
||||||
|
// Group site associations by client ID
|
||||||
|
const sitesByClient = siteAssociations.reduce((acc, association) => {
|
||||||
|
if (!acc[association.clientId]) {
|
||||||
|
acc[association.clientId] = [];
|
||||||
|
}
|
||||||
|
acc[association.clientId].push({
|
||||||
|
siteId: association.siteId,
|
||||||
|
siteName: association.siteName,
|
||||||
|
siteNiceId: association.siteNiceId
|
||||||
|
});
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<number, Array<{
|
||||||
|
siteId: number;
|
||||||
|
siteName: string | null;
|
||||||
|
siteNiceId: string | null;
|
||||||
|
}>>);
|
||||||
|
|
||||||
|
// Merge clients with their site associations
|
||||||
|
const clientsWithSites = clientsList.map(client => ({
|
||||||
|
...client,
|
||||||
|
sites: sitesByClient[client.clientId] || []
|
||||||
|
}));
|
||||||
|
|
||||||
return response<ListClientsResponse>(res, {
|
return response<ListClientsResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
clients: clientsList,
|
clients: clientsWithSites,
|
||||||
pagination: {
|
pagination: {
|
||||||
total: totalCount,
|
total: totalCount,
|
||||||
limit,
|
limit,
|
||||||
@@ -159,4 +205,4 @@ export async function listClients(
|
|||||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user