Skip to content

Commit

Permalink
Slug projects and filter type
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Feb 23, 2024
1 parent 20497a9 commit 66ecf9d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 73 deletions.
24 changes: 14 additions & 10 deletions backend/src/server/routes/v1/project-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@app/db/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { ProjectFilterType } from "@app/services/project/project-types";

import { integrationAuthPubSchema } from "../sanitizedSchemas";
import { sanitizedServiceTokenSchema } from "../v2/service-token-router";
Expand Down Expand Up @@ -120,10 +121,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
onRequest: verifyAuth([AuthMode.JWT, AuthMode.SERVICE_TOKEN, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
const workspace = await server.services.project.getAProject({
filterType: ProjectFilterType.ID,
filter: req.params.workspaceId,
actorId: req.permission.id,
actor: req.permission.type,
actorOrgId: req.permission.orgId,
projectId: req.params.workspaceId
actorOrgId: req.permission.orgId
});
return { workspace };
}
Expand Down Expand Up @@ -172,10 +174,11 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
const workspace = await server.services.project.deleteProject({
filterType: ProjectFilterType.ID,
filter: req.params.workspaceId,
actorId: req.permission.id,
actor: req.permission.type,
actorOrgId: req.permission.orgId,
projectId: req.params.workspaceId
actorOrgId: req.permission.orgId
});
return { workspace };
}
Expand Down Expand Up @@ -231,17 +234,18 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
})
}
},
onRequest: verifyAuth([AuthMode.JWT]),
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
const workspace = await server.services.project.updateProject({
actorId: req.permission.id,
actor: req.permission.type,
actorOrgId: req.permission.orgId,
projectId: req.params.workspaceId,
filterType: ProjectFilterType.ID,
filter: req.params.workspaceId,
update: {
name: req.body.name,
autoCapitalization: req.body.autoCapitalization
}
},
actorId: req.permission.id,
actor: req.permission.type,
actorOrgId: req.permission.orgId
});
return {
workspace
Expand Down
54 changes: 48 additions & 6 deletions backend/src/server/routes/v2/project-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { authRateLimit } from "@app/server/config/rateLimiter";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { ProjectFilterType } from "@app/services/project/project-types";

const projectWithEnv = ProjectsSchema.merge(
z.object({
Expand Down Expand Up @@ -159,6 +160,7 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
}
});

/* Delete a project by slug */
server.route({
method: "DELETE",
url: "/:slug",
Expand All @@ -167,21 +169,25 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
slug: slugSchema.describe("The slug of the project to delete.")
}),
response: {
200: z.void()
200: ProjectsSchema
}
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY, AuthMode.IDENTITY_ACCESS_TOKEN]),

handler: async (req) => {
await server.services.project.deleteProjectBySlug({
const project = await server.services.project.deleteProject({
filterType: ProjectFilterType.SLUG,
filter: req.params.slug,
actorId: req.permission.id,
actorOrgId: req.permission.orgId,
actor: req.permission.type,
slug: req.params.slug
actor: req.permission.type
});

return project;
}
});

/* Get a project by slug */
server.route({
method: "GET",
url: "/:slug",
Expand All @@ -195,11 +201,47 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
const project = await server.services.project.getProjectBySlug({
const project = await server.services.project.getAProject({
filter: req.params.slug,
filterType: ProjectFilterType.SLUG,
actorId: req.permission.id,
actorOrgId: req.permission.orgId,
actor: req.permission.type
});

return project;
}
});

/* Update a project by slug */
server.route({
method: "PATCH",
url: "/:slug",
schema: {
params: z.object({
slug: slugSchema.describe("The slug of the project to update.")
}),
body: z.object({
name: z.string().trim().optional().describe("The new name of the project."),
autoCapitalization: z.boolean().optional().describe("The new auto-capitalization setting.")
}),
response: {
200: ProjectsSchema
}
},

onRequest: verifyAuth([AuthMode.JWT, AuthMode.API_KEY, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
const project = await server.services.project.updateProject({
filterType: ProjectFilterType.SLUG,
filter: req.params.slug,
update: {
name: req.body.name,
autoCapitalization: req.body.autoCapitalization
},
actorId: req.permission.id,
actor: req.permission.type,
slug: req.params.slug
actorOrgId: req.permission.orgId
});

return project;
Expand Down
33 changes: 31 additions & 2 deletions backend/src/services/project/project-dal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) }),
Expand All @@ -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" });
}
Expand All @@ -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) }),
Expand All @@ -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 =
Expand All @@ -217,6 +245,7 @@ export const projectDALFactory = (db: TDbClient) => {
findAllProjectsByIdentity,
findProjectGhostUser,
findProjectById,
findProjectByFilter,
findProjectBySlug,
checkProjectUpgradeStatus
};
Expand Down
49 changes: 13 additions & 36 deletions backend/src/services/project/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import { assignWorkspaceKeysToMembers, createProjectKey } from "./project-fns";
import { TProjectQueueFactory } from "./project-queue";
import {
TCreateProjectDTO,
TDeleteProjectBySlugDTO,
TDeleteProjectDTO,
TGetProjectBySlugDTO,
TGetProjectDTO,
TToggleProjectAutoCapitalizationDTO,
TUpdateProjectDTO,
TUpdateProjectNameDTO,
TUpgradeProjectDTO
} from "./project-types";

Expand Down Expand Up @@ -289,27 +289,8 @@ export const projectServiceFactory = ({
return results;
};

const deleteProject = async ({ actor, actorId, actorOrgId, projectId }: TDeleteProjectDTO) => {
const { permission } = await permissionService.getProjectPermission(actor, actorId, projectId, actorOrgId);
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Delete, ProjectPermissionSub.Project);

const deletedProject = await projectDAL.transaction(async (tx) => {
const project = await projectDAL.deleteById(projectId, tx);
const projectGhostUser = await projectMembershipDAL.findProjectGhostUser(projectId).catch(() => null);

// Delete the org membership for the ghost user if it's found.
if (projectGhostUser) {
await userDAL.deleteById(projectGhostUser.id, tx);
}

return project;
});

return deletedProject;
};

const deleteProjectBySlug = async ({ actor, actorId, actorOrgId, slug }: TDeleteProjectBySlugDTO) => {
const project = await projectDAL.findOne({ slug });
const deleteProject = async ({ actor, actorId, actorOrgId, filter, filterType }: TDeleteProjectDTO) => {
const project = await projectDAL.findProjectByFilter(filter, filterType);

const { permission } = await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Delete, ProjectPermissionSub.Project);
Expand All @@ -334,22 +315,20 @@ export const projectServiceFactory = ({
return workspaces;
};

const getAProject = async ({ actorId, actorOrgId, projectId, actor }: TGetProjectDTO) => {
await permissionService.getProjectPermission(actor, actorId, projectId, actorOrgId);
return projectDAL.findProjectById(projectId);
};
const getAProject = async ({ actorId, actorOrgId, filter, filterType, actor }: TGetProjectDTO) => {
const project = await projectDAL.findProjectByFilter(filter, filterType);

const getProjectBySlug = async ({ actorId, actorOrgId, slug, actor }: TGetProjectBySlugDTO) => {
const project = await projectDAL.findProjectBySlug(slug);
await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
return project;
};

const updateProject = async ({ projectId, actor, actorId, actorOrgId, update }: TUpdateProjectDTO) => {
const { permission } = await permissionService.getProjectPermission(actor, actorId, projectId, actorOrgId);
const updateProject = async ({ actor, actorId, actorOrgId, update, filter, filterType }: TUpdateProjectDTO) => {
const project = await projectDAL.findProjectByFilter(filter, filterType);

const { permission } = await permissionService.getProjectPermission(actor, actorId, project.id, actorOrgId);
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Settings);

const updatedProject = await projectDAL.updateById(projectId, {
const updatedProject = await projectDAL.updateById(project.id, {
name: update.name,
autoCapitalization: update.autoCapitalization
});
Expand All @@ -362,15 +341,15 @@ export const projectServiceFactory = ({
actorId,
actorOrgId,
autoCapitalization
}: TGetProjectDTO & { autoCapitalization: boolean }) => {
}: TToggleProjectAutoCapitalizationDTO) => {
const { permission } = await permissionService.getProjectPermission(actor, actorId, projectId, actorOrgId);
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Settings);

const updatedProject = await projectDAL.updateById(projectId, { autoCapitalization });
return updatedProject;
};

const updateName = async ({ projectId, actor, actorId, actorOrgId, name }: TGetProjectDTO & { name: string }) => {
const updateName = async ({ projectId, actor, actorId, actorOrgId, name }: TUpdateProjectNameDTO) => {
const { permission } = await permissionService.getProjectPermission(actor, actorId, projectId, actorOrgId);
ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Settings);

Expand Down Expand Up @@ -423,10 +402,8 @@ export const projectServiceFactory = ({
deleteProject,
getProjects,
updateProject,
deleteProjectBySlug,
getProjectUpgradeStatus,
getAProject,
getProjectBySlug,
toggleAutoCapitalization,
updateName,
upgradeProject
Expand Down
Loading

0 comments on commit 66ecf9d

Please sign in to comment.