♻️ show list of resources on policy list

This commit is contained in:
Fred KISSIE
2026-03-06 04:03:25 +01:00
parent 136c3eff0c
commit 38aa2dace8
2 changed files with 60 additions and 7 deletions

View File

@@ -11,11 +11,20 @@
* 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 logger from "@server/logger";
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 { and, asc, eq, inArray, like, or, sql } from "drizzle-orm";
import { NextFunction, Request, Response } from "express";
@@ -191,9 +200,48 @@ export async function listResourcePolicies(
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, {
data: {
policies: rows,
policies: policiesList,
pagination: {
total: totalCount,
pageSize,

View File

@@ -1,4 +1,4 @@
import type { ResourcePolicy } from "@server/db";
import type { Resource, ResourcePolicy } from "@server/db";
import type { PaginatedResponse } from "@server/types/Pagination";
export type GetMaintenanceInfoResponse = {
@@ -12,8 +12,13 @@ export type GetMaintenanceInfoResponse = {
maintenanceEstimatedTime: string | null;
};
export type ResourcePolicyWithResources = Pick<
ResourcePolicy,
"resourcePolicyId" | "niceId" | "name" | "orgId"
> & {
resources: Array<Pick<Resource, "resourceId" | "name" | "fullDomain">>;
};
export type ListResourcePoliciesResponse = PaginatedResponse<{
policies: Array<
Pick<ResourcePolicy, "resourcePolicyId" | "niceId" | "name" | "orgId">
>;
policies: Array<ResourcePolicyWithResources>;
}>;