From c8c6f66ed2e20756f0fa6424fc664f6cce132c5c Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Wed, 31 Jan 2024 10:36:22 +0530 Subject: [PATCH] Add mutations for edit and delete environment variables (#54) Co-authored-by: neeraj --- packages/backend/src/database.ts | 22 +++++++++++++++ packages/backend/src/resolvers.ts | 21 +++++++++++++++ packages/backend/src/schema.gql | 7 +++++ .../projects/project/OverviewTabPanel.tsx | 2 +- .../settings/EnvironmentVariablesTabPanel.tsx | 8 +++--- .../frontend/src/layouts/ProjectSearch.tsx | 1 - packages/gql-client/src/client.ts | 27 +++++++++++++++++-- packages/gql-client/src/mutations.ts | 12 +++++++++ packages/gql-client/src/types.ts | 13 +++++++++ 9 files changed, 105 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 7f39b677..c24067b8 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -201,6 +201,28 @@ export class Database { return savedEnvironmentVariables.length > 0; } + async updateEnvironmentVariable (environmentVariableId: string, update: DeepPartial): Promise { + const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable); + const updateResult = await environmentVariableRepository.update({ id: Number(environmentVariableId) }, update); + + if (updateResult.affected) { + return updateResult.affected > 0; + } else { + return false; + } + } + + async deleteEnvironmentVariable (environmentVariableId: string): Promise { + const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable); + const deleteResult = await environmentVariableRepository.delete({ id: Number(environmentVariableId) }); + + if (deleteResult.affected) { + return deleteResult.affected > 0; + } else { + return false; + } + } + async getProjectMemberByMemberId (memberId: string): Promise { const projectMemberRepository = this.dataSource.getRepository(ProjectMember); diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index e84ce91b..2c1a2528 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -139,6 +139,27 @@ export const createResolvers = async (db: Database, app: OAuthApp): Promise } }, + updateEnvironmentVariable: async (_: any, { environmentVariableId, environmentVariable }: { environmentVariableId: string, environmentVariable : { + key: string + value: string + }}) => { + try { + return db.updateEnvironmentVariable(environmentVariableId, environmentVariable); + } catch (err) { + log(err); + return false; + } + }, + + removeEnvironmentVariable: async (_: any, { environmentVariableId }: { environmentVariableId: string}) => { + try { + return db.deleteEnvironmentVariable(environmentVariableId); + } catch (err) { + log(err); + return false; + } + }, + updateDeploymentToProd: async (_: any, { deploymentId }: { deploymentId: string }) => { try { return db.updateDeploymentById(deploymentId, { diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index b98cd38b..25381cad 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -133,6 +133,8 @@ type AuthResult { type Mutation { removeMember(memberId: String!): Boolean! addEnvironmentVariables(projectId: String!, environmentVariables: [AddEnvironmentVariableInput!]): Boolean! + removeEnvironmentVariable(environmentVariableId: String!): Boolean! + updateEnvironmentVariable(environmentVariableId: String!, environmentVariable: UpdateEnvironmentVariableInput!): Boolean! updateDeploymentToProd(deploymentId: String!): Boolean! updateProject(projectId: String!, projectDetails: UpdateProjectInput): Boolean! redeployToProd(deploymentId: String!): Boolean! @@ -163,3 +165,8 @@ input UpdateDomainInput { isRedirected: Boolean branch: String } + +input UpdateEnvironmentVariableInput { + key: String + value: String +} diff --git a/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx b/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx index 8410e17d..5473f4c1 100644 --- a/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx +++ b/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx @@ -12,7 +12,7 @@ interface OverviewProps { } // TODO: Check if any live domain is set for production branch -const IS_DOMAIN_SETUP = true; +const IS_DOMAIN_SETUP = false; const OverviewTabPanel = ({ project }: OverviewProps) => { return ( diff --git a/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx b/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx index 542daa5c..3c5c4fb8 100644 --- a/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx +++ b/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx @@ -70,7 +70,7 @@ export const EnvironmentVariablesTabPanel = () => { } }, [isSubmitSuccessful, reset, id]); - const getEnvironmentVariable = useCallback( + const getEnvironmentVariables = useCallback( (environment: Environment) => { return environmentVariables.filter( (item) => item.environment === environment, @@ -226,17 +226,17 @@ export const EnvironmentVariablesTabPanel = () => {
diff --git a/packages/frontend/src/layouts/ProjectSearch.tsx b/packages/frontend/src/layouts/ProjectSearch.tsx index cc35d472..68ae1e76 100644 --- a/packages/frontend/src/layouts/ProjectSearch.tsx +++ b/packages/frontend/src/layouts/ProjectSearch.tsx @@ -51,7 +51,6 @@ const ProjectSearch = () => { ...project, // TODO: populate empty fields icon: '', - title: project.name, organization: orgName, deployments: updatedDeployments, url: '', diff --git a/packages/gql-client/src/client.ts b/packages/gql-client/src/client.ts index 1b1483a4..b66c105e 100644 --- a/packages/gql-client/src/client.ts +++ b/packages/gql-client/src/client.ts @@ -1,8 +1,8 @@ import { ApolloClient, DefaultOptions, InMemoryCache, NormalizedCacheObject } from '@apollo/client'; import { getUser, getOrganizations, getDeployments, getProjectMembers, searchProjects, getEnvironmentVariables, getProject, getDomains, getProjectsInOrganization } from './queries'; -import { AddEnvironmentVariableInput, AddEnvironmentVariablesResponse, GetDeploymentsResponse, GetEnvironmentVariablesResponse, GetOrganizationsResponse, GetProjectMembersResponse, SearchProjectsResponse, GetUserResponse, RemoveMemberResponse, UpdateDeploymentToProdResponse, GetProjectResponse, UpdateProjectResponse, UpdateProjectInput, RedeployToProdResponse, DeleteProjectResponse, GetProjectsInOrganizationResponse, RollbackDeploymentResponse, AddDomainInput, AddDomainResponse, GetDomainsResponse, UpdateDomainInput, UpdateDomainResponse, AuthenticateGithubResponse } from './types'; -import { removeMember, addEnvironmentVariables, updateDeploymentToProd, updateProjectMutation, redeployToProd, deleteProject, addDomain, rollbackDeployment, updateDomainMutation, authenticateGithub } from './mutations'; +import { AddEnvironmentVariableInput, AddEnvironmentVariablesResponse, GetDeploymentsResponse, GetEnvironmentVariablesResponse, GetOrganizationsResponse, GetProjectMembersResponse, SearchProjectsResponse, GetUserResponse, RemoveMemberResponse, UpdateDeploymentToProdResponse, GetProjectResponse, UpdateProjectResponse, UpdateProjectInput, RedeployToProdResponse, DeleteProjectResponse, GetProjectsInOrganizationResponse, RollbackDeploymentResponse, AddDomainInput, AddDomainResponse, GetDomainsResponse, UpdateDomainInput, UpdateDomainResponse, AuthenticateGithubResponse, UpdateEnvironmentVariableResponse, UpdateEnvironmentVariableInput, RemoveEnvironmentVariableResponse } from './types'; +import { removeMember, addEnvironmentVariables, updateDeploymentToProd, updateProjectMutation, redeployToProd, deleteProject, addDomain, rollbackDeployment, updateDomainMutation, authenticateGithub, updateEnvironmentVariable, removeEnvironmentVariable } from './mutations'; export interface GraphQLConfig { gqlEndpoint: string; @@ -136,6 +136,29 @@ export class GQLClient { return data; } + async updateEnvironmentVariable (environmentVariableId: string, environmentVariable: UpdateEnvironmentVariableInput): Promise { + const { data } = await this.client.mutate({ + mutation: updateEnvironmentVariable, + variables: { + environmentVariableId, + environmentVariable + } + }); + + return data; + } + + async removeEnvironmentVariable (environmentVariableId: string): Promise { + const { data } = await this.client.mutate({ + mutation: removeEnvironmentVariable, + variables: { + environmentVariableId + } + }); + + return data; + } + async updateDeploymentToProd (deploymentId: string): Promise { const { data } = await this.client.mutate({ mutation: updateDeploymentToProd, diff --git a/packages/gql-client/src/mutations.ts b/packages/gql-client/src/mutations.ts index 006b488c..5551e52f 100644 --- a/packages/gql-client/src/mutations.ts +++ b/packages/gql-client/src/mutations.ts @@ -12,6 +12,18 @@ mutation ($projectId: String!, $environmentVariables: [AddEnvironmentVariableInp } `; +export const updateEnvironmentVariable = gql` +mutation ($environmentVariableId: String!, $environmentVariable: UpdateEnvironmentVariableInput!) { + updateEnvironmentVariable(environmentVariableId: $environmentVariableId, environmentVariable: $environmentVariable) +} +`; + +export const removeEnvironmentVariable = gql` +mutation ($environmentVariableId: String!) { + removeEnvironmentVariable(environmentVariableId: $environmentVariableId) +} +`; + export const updateDeploymentToProd = gql` mutation ($deploymentId: String!) { updateDeploymentToProd(deploymentId: $deploymentId) diff --git a/packages/gql-client/src/types.ts b/packages/gql-client/src/types.ts index e47a1a31..e8cb30de 100644 --- a/packages/gql-client/src/types.ts +++ b/packages/gql-client/src/types.ts @@ -180,6 +180,19 @@ export type AddEnvironmentVariableInput = { value: string; } +export type UpdateEnvironmentVariableInput = { + key: string; + value: string; +} + +export type UpdateEnvironmentVariableResponse = { + updateEnvironmentVariable: boolean; +} + +export type RemoveEnvironmentVariableResponse = { + removeEnvironmentVariable: boolean; +} + export type UpdateDeploymentToProdResponse = { updateDeploymentToProd: boolean; }