mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-06 12:19:50 +00:00
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import { db, launcherViews } from "@server/db";
|
|
import { response } from "@server/lib/response";
|
|
import { getFirstString } from "@server/lib/requestParams";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import { and, eq, isNull, or } from "drizzle-orm";
|
|
import { NextFunction, Request, Response } from "express";
|
|
import createHttpError from "http-errors";
|
|
import { launcherViewConfigSchema, type LauncherViewRecord } from "./types";
|
|
import { verifyLauncherOrgMembership } from "./launcherResourceAccess";
|
|
|
|
function mapViewRow(
|
|
row: typeof launcherViews.$inferSelect
|
|
): LauncherViewRecord {
|
|
return {
|
|
viewId: row.viewId,
|
|
orgId: row.orgId,
|
|
userId: row.userId,
|
|
name: row.name,
|
|
config: launcherViewConfigSchema.parse(JSON.parse(row.config)),
|
|
createdAt: row.createdAt,
|
|
updatedAt: row.updatedAt,
|
|
isOrgWide: row.userId == null
|
|
};
|
|
}
|
|
|
|
export async function listLauncherViews(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
): Promise<any> {
|
|
try {
|
|
const orgId = getFirstString(req.params.orgId);
|
|
const userId = req.user?.userId;
|
|
|
|
if (!userId) {
|
|
return next(
|
|
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated")
|
|
);
|
|
}
|
|
|
|
if (!orgId) {
|
|
return next(
|
|
createHttpError(HttpCode.BAD_REQUEST, "Invalid organization ID")
|
|
);
|
|
}
|
|
|
|
await verifyLauncherOrgMembership(orgId, userId);
|
|
|
|
const rows = await db
|
|
.select()
|
|
.from(launcherViews)
|
|
.where(
|
|
and(
|
|
eq(launcherViews.orgId, orgId),
|
|
or(
|
|
eq(launcherViews.userId, userId),
|
|
isNull(launcherViews.userId)
|
|
)
|
|
)
|
|
);
|
|
|
|
return response(res, {
|
|
data: {
|
|
views: rows.map(mapViewRow)
|
|
},
|
|
success: true,
|
|
error: false,
|
|
message: "Launcher views retrieved successfully",
|
|
status: HttpCode.OK
|
|
});
|
|
} catch (error) {
|
|
if (createHttpError.isHttpError(error)) {
|
|
return next(error);
|
|
}
|
|
console.error("Error listing launcher views:", error);
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
"Internal server error"
|
|
)
|
|
);
|
|
}
|
|
}
|