Slug projects and filter type

This commit is contained in:
Daniel Hougaard
2024-03-17 18:48:09 +01:00
parent 9473de2212
commit 8e3fc044ca
5 changed files with 131 additions and 73 deletions
+31 -2
View File
@@ -5,6 +5,8 @@ import { ProjectsSchema, ProjectUpgradeStatus, ProjectVersion, TableName, TProje
import { BadRequestError, DatabaseError } from "@app/lib/errors";
import { ormify, selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
import { ProjectFilterType } from "./project-types";
export type TProjectDALFactory = ReturnType<typeof projectDALFactory>;
export const projectDALFactory = (db: TDbClient) => {
@@ -139,7 +141,7 @@ export const projectDALFactory = (db: TDbClient) => {
{ column: `${TableName.Project}.name`, order: "asc" },
{ column: `${TableName.Environment}.position`, order: "asc" }
]);
return sqlNestRelationships({
const project = sqlNestRelationships({
data: workspaces,
key: "id",
parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }),
@@ -155,6 +157,12 @@ export const projectDALFactory = (db: TDbClient) => {
}
]
})?.[0];
if (!project) {
throw new BadRequestError({ message: "Project not found" });
}
return project;
} catch (error) {
throw new DatabaseError({ error, name: "Find all projects" });
}
@@ -177,7 +185,7 @@ export const projectDALFactory = (db: TDbClient) => {
{ column: `${TableName.Project}.name`, order: "asc" },
{ column: `${TableName.Environment}.position`, order: "asc" }
]);
return sqlNestRelationships({
const project = sqlNestRelationships({
data: projects,
key: "id",
parentMapper: ({ _id, ...el }) => ({ _id, ...ProjectsSchema.parse(el) }),
@@ -193,11 +201,31 @@ export const projectDALFactory = (db: TDbClient) => {
}
]
})?.[0];
if (!project) {
throw new BadRequestError({ message: "Project not found" });
}
return project;
} catch (error) {
throw new DatabaseError({ error, name: "Find project by slug" });
}
};
const findProjectByFilter = async (filter: string, type: ProjectFilterType) => {
try {
if (type === ProjectFilterType.ID) {
return await findProjectById(filter);
}
if (type === ProjectFilterType.SLUG) {
return await findProjectBySlug(filter);
}
throw new BadRequestError({ message: "Invalid filter type" });
} catch (error) {
throw new DatabaseError({ error, name: `Failed to find project by ${type}` });
}
};
const checkProjectUpgradeStatus = async (projectId: string) => {
const project = await projectOrm.findById(projectId);
const upgradeInProgress =
@@ -217,6 +245,7 @@ export const projectDALFactory = (db: TDbClient) => {
findAllProjectsByIdentity,
findProjectGhostUser,
findProjectById,
findProjectByFilter,
findProjectBySlug,
checkProjectUpgradeStatus
};