mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-11 23:04:59 +00:00
♻️ show list of resources on policy list
This commit is contained in:
@@ -11,11 +11,20 @@
|
|||||||
* This file is not licensed under the AGPLv3.
|
* This file is not licensed under the AGPLv3.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { db, resourcePolicies, rolePolicies, userPolicies } from "@server/db";
|
import {
|
||||||
|
db,
|
||||||
|
resourcePolicies,
|
||||||
|
resources,
|
||||||
|
rolePolicies,
|
||||||
|
userPolicies
|
||||||
|
} from "@server/db";
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import type { ListResourcePoliciesResponse } from "@server/routers/resource/types";
|
import type {
|
||||||
|
ListResourcePoliciesResponse,
|
||||||
|
ResourcePolicyWithResources
|
||||||
|
} from "@server/routers/resource/types";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import { and, asc, eq, inArray, like, or, sql } from "drizzle-orm";
|
import { and, asc, eq, inArray, like, or, sql } from "drizzle-orm";
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
@@ -191,9 +200,48 @@ export async function listResourcePolicies(
|
|||||||
countQuery
|
countQuery
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const attachedResources =
|
||||||
|
rows.length === 0
|
||||||
|
? []
|
||||||
|
: await db
|
||||||
|
.select({
|
||||||
|
resourceId: resources.resourceId,
|
||||||
|
name: resources.name,
|
||||||
|
fullDomain: resources.fullDomain,
|
||||||
|
resourcePolicyId: resources.resourcePolicyId
|
||||||
|
})
|
||||||
|
.from(resources)
|
||||||
|
.where(
|
||||||
|
inArray(
|
||||||
|
resources.resourcePolicyId,
|
||||||
|
rows.map((row) => row.resourcePolicyId)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const entries: ResourcePolicyWithResources[] = [];
|
||||||
|
|
||||||
|
// avoids TS issues with reduce/never[]
|
||||||
|
const map = new Map<number, ResourcePolicyWithResources>();
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
let entry = map.get(row.resourcePolicyId);
|
||||||
|
if (!entry) {
|
||||||
|
entry = {
|
||||||
|
...row,
|
||||||
|
resources: []
|
||||||
|
};
|
||||||
|
map.set(row.resourcePolicyId, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.resources = attachedResources.filter(
|
||||||
|
(r) => r.resourcePolicyId === entry?.resourcePolicyId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const policiesList = Array.from(map.values());
|
||||||
|
|
||||||
return response<ListResourcePoliciesResponse>(res, {
|
return response<ListResourcePoliciesResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
policies: rows,
|
policies: policiesList,
|
||||||
pagination: {
|
pagination: {
|
||||||
total: totalCount,
|
total: totalCount,
|
||||||
pageSize,
|
pageSize,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { ResourcePolicy } from "@server/db";
|
import type { Resource, ResourcePolicy } from "@server/db";
|
||||||
import type { PaginatedResponse } from "@server/types/Pagination";
|
import type { PaginatedResponse } from "@server/types/Pagination";
|
||||||
|
|
||||||
export type GetMaintenanceInfoResponse = {
|
export type GetMaintenanceInfoResponse = {
|
||||||
@@ -12,8 +12,13 @@ export type GetMaintenanceInfoResponse = {
|
|||||||
maintenanceEstimatedTime: string | null;
|
maintenanceEstimatedTime: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ResourcePolicyWithResources = Pick<
|
||||||
|
ResourcePolicy,
|
||||||
|
"resourcePolicyId" | "niceId" | "name" | "orgId"
|
||||||
|
> & {
|
||||||
|
resources: Array<Pick<Resource, "resourceId" | "name" | "fullDomain">>;
|
||||||
|
};
|
||||||
|
|
||||||
export type ListResourcePoliciesResponse = PaginatedResponse<{
|
export type ListResourcePoliciesResponse = PaginatedResponse<{
|
||||||
policies: Array<
|
policies: Array<ResourcePolicyWithResources>;
|
||||||
Pick<ResourcePolicy, "resourcePolicyId" | "niceId" | "name" | "orgId">
|
|
||||||
>;
|
|
||||||
}>;
|
}>;
|
||||||
|
|||||||
Reference in New Issue
Block a user