From 58e20c15db3cf76a06e039d66d2efb7a2634a6d7 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:12:08 +0530 Subject: [PATCH] Add a mutation to remove project member by member id (#30) * Add mutation to remove project member by member id * Add query to fetch project members by project id * Handle review changes --------- Co-authored-by: neeraj --- packages/backend/src/database.ts | 14 +++++++++++++- packages/backend/src/resolvers.ts | 27 ++++++++++++++++++++++++++- packages/backend/src/schema.gql | 5 +++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 17bd29c8..a9fa27e4 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -95,7 +95,7 @@ export class Database { return deployments; } - async getProjectMembers (projectId: string): Promise { + async getProjectMembersByProjectId (projectId: string): Promise { const projectMemberRepository = this.dataSource.getRepository(ProjectMember); const projectMembers = await projectMemberRepository.find({ @@ -129,4 +129,16 @@ export class Database { return environmentVariables; } + + async removeProjectMemberByMemberId (memberId: string): Promise { + // TODO: Check if user is authorized to delete members + const projectMemberRepository = this.dataSource.getRepository(ProjectMember); + const deleted = await projectMemberRepository.delete(memberId); + + if (deleted.affected) { + return deleted.affected > 0; + } else { + return false; + } + } } diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index f687b786..2f9da799 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -1,6 +1,10 @@ +import debug from 'debug'; + import { Database } from './database'; import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, environmentVariableToGqlType } from './utils'; +const log = debug('snowball:database'); + export const createResolvers = async (db: Database): Promise => { return { Query: { @@ -16,7 +20,7 @@ export const createResolvers = async (db: Database): Promise => { const dbProjects = await db.getProjectsByOrganizationId(org.id); const projectsWithPromises = dbProjects.map(async (dbProject) => { - const dbProjectMembers = await db.getProjectMembers(dbProject.id); + const dbProjectMembers = await db.getProjectMembersByProjectId(dbProject.id); const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(dbProject.id); const projectMembers = dbProjectMembers.map(dbProjectMember => { @@ -51,6 +55,27 @@ export const createResolvers = async (db: Database): Promise => { }); return deployments; + }, + + projectMembers: async (_: any, { projectId }: { projectId: string }) => { + const dbProjectMembers = await db.getProjectMembersByProjectId(projectId); + + const projectMembers = dbProjectMembers.map(dbProjectMember => { + return projectMemberToGqlType(dbProjectMember); + }); + + return projectMembers; + } + }, + + Mutation: { + removeMember: async (_: any, { memberId }:{ memberId: string }) => { + try { + return await db.removeProjectMemberByMemberId(memberId); + } catch (error) { + log(error); + return false; + } } } }; diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index b8e8cf70..30f13989 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -115,4 +115,9 @@ type Query { organizations: [Organization!] projects: [Project!] deployments(projectId: String!): [Deployment!] + projectMembers(projectId: String!): [ProjectMember!] +} + +type Mutation { + removeMember(memberId: String!): Boolean! }