diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index dbf7060d..7f39b677 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -1,7 +1,6 @@ import { DataSource, DeepPartial } from 'typeorm'; import path from 'path'; import debug from 'debug'; -import assert from 'assert'; import { DatabaseConfig } from './config'; import { User } from './entity/User'; @@ -156,9 +155,6 @@ export class Database { const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable); const environmentVariables = await environmentVariableRepository.find({ - relations: { - project: true - }, where: { project: { id: projectId @@ -181,27 +177,27 @@ export class Database { } } - async addEnvironmentVariablesByProjectId (projectId: string, environmentVariables: any[]): Promise { + async addEnvironmentVariablesByProjectId (projectId: string, environmentVariables: { + environments: string[]; + key: string; + value: string; + }[]): Promise { const environmentVariableRepository = this.dataSource.getRepository(EnvironmentVariable); - const projectRepository = this.dataSource.getRepository(Project); - const project = await projectRepository.findOneBy({ - id: projectId - }); - assert(project); + const formattedEnvironmentVariables = environmentVariables.map((environmentVariable) => { + return environmentVariable.environments.map((environment) => { + return ({ + key: environmentVariable.key, + value: environmentVariable.value, + environment: environment as Environment, + project: Object.assign(new Project(), { + id: projectId + }) + }); + }); + }).flat(); - const environmentVariablesPromises = environmentVariables.map(async environmentVariable => { - const envVar = new EnvironmentVariable(); - - envVar.key = environmentVariable.key; - envVar.value = environmentVariable.value; - envVar.environments = environmentVariable.environments; - envVar.project = project; - - return environmentVariableRepository.save(envVar); - }); - - const savedEnvironmentVariables = await Promise.all(environmentVariablesPromises); + const savedEnvironmentVariables = await environmentVariableRepository.save(formattedEnvironmentVariables); return savedEnvironmentVariables.length > 0; } diff --git a/packages/backend/src/entity/EnvironmentVariable.ts b/packages/backend/src/entity/EnvironmentVariable.ts index d148bb68..f172e1ba 100644 --- a/packages/backend/src/entity/EnvironmentVariable.ts +++ b/packages/backend/src/entity/EnvironmentVariable.ts @@ -26,9 +26,9 @@ export class EnvironmentVariable { project!: Project; @Column({ - type: 'simple-array' + enum: Environment }) - environments!: Environment[]; + environment!: Environment; @Column('varchar') key!: string; diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index 928e27f1..b2f73907 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -72,12 +72,7 @@ export const createResolvers = async (db: Database): Promise => { environmentVariables: async (_: any, { projectId }: { projectId: string }) => { const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(projectId); - - const environmentVariables = dbEnvironmentVariables.map(dbEnvironmentVariable => { - return environmentVariableToGqlType(dbEnvironmentVariable); - }); - - return environmentVariables; + return dbEnvironmentVariables; }, projectMembers: async (_: any, { projectId }: { projectId: string }) => { diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index 729ba2e5..64357655 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -106,7 +106,7 @@ type Domain { type EnvironmentVariable { id: String! - environments: [Environment!]! + environment: Environment! key: String! value: String! createdAt: String! diff --git a/packages/backend/src/utils.ts b/packages/backend/src/utils.ts index 32e48175..676cbfbd 100644 --- a/packages/backend/src/utils.ts +++ b/packages/backend/src/utils.ts @@ -74,7 +74,7 @@ export const projectMemberToGqlType = (dbProjectMember: ProjectMember): any => { export const environmentVariableToGqlType = (dbEnvironmentVariable: EnvironmentVariable): any => { return { id: dbEnvironmentVariable.id, - environments: dbEnvironmentVariable.environments, + environments: dbEnvironmentVariable.environment, key: dbEnvironmentVariable.key, value: dbEnvironmentVariable.value, createdAt: dbEnvironmentVariable.createdAt, diff --git a/packages/backend/test/fixtures/environment-variables.json b/packages/backend/test/fixtures/environment-variables.json index 02c95c49..2af52b3d 100644 --- a/packages/backend/test/fixtures/environment-variables.json +++ b/packages/backend/test/fixtures/environment-variables.json @@ -3,60 +3,90 @@ "projectIndex": 0, "key": "ABC", "value": "ABC", - "environments": ["Production", "Preview"] + "environment": "Production" + }, + { + "projectIndex": 0, + "key": "ABC", + "value": "ABC", + "environment": "Preview" }, { "projectIndex": 0, "key": "XYZ", "value": "abc3", - "environments": ["Preview"] + "environment": "Preview" }, { "projectIndex": 1, "key": "ABC", "value": "ABC", - "environments": ["Production", "Preview"] + "environment": "Production" + }, + { + "projectIndex": 1, + "key": "ABC", + "value": "ABC", + "environment": "Preview" }, { "projectIndex": 1, "key": "XYZ", "value": "abc3", - "environments": ["Preview"] + "environment": "Preview" }, { "projectIndex": 2, "key": "ABC", "value": "ABC", - "environments": ["Production", "Preview"] + "environment": "Production" + }, + { + "projectIndex": 2, + "key": "ABC", + "value": "ABC", + "environment": "Preview" }, { "projectIndex": 2, "key": "XYZ", "value": "abc3", - "environments": ["Preview"] + "environment": "Preview" }, { "projectIndex": 3, "key": "ABC", "value": "ABC", - "environments": ["Production", "Preview"] + "environment": "Production" + }, + { + "projectIndex": 3, + "key": "ABC", + "value": "ABC", + "environment": "Preview" }, { "projectIndex": 3, "key": "XYZ", "value": "abc3", - "environments": ["Preview"] + "environment": "Preview" }, { "projectIndex": 4, "key": "ABC", "value": "ABC", - "environments": ["Production", "Preview"] + "environment": "Production" + }, + { + "projectIndex": 4, + "key": "ABC", + "value": "ABC", + "environment": "Preview" }, { "projectIndex": 4, "key": "XYZ", "value": "abc3", - "environments": ["Preview"] + "environment": "Preview" } ] diff --git a/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx b/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx index 2331a6fa..8410e17d 100644 --- a/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx +++ b/packages/frontend/src/components/projects/project/OverviewTabPanel.tsx @@ -11,7 +11,7 @@ interface OverviewProps { project: Project; } -// TODO: Check any live domain is set for production branch +// TODO: Check if any live domain is set for production branch const IS_DOMAIN_SETUP = true; const OverviewTabPanel = ({ project }: OverviewProps) => { diff --git a/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx b/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx index c9ad0b65..542daa5c 100644 --- a/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx +++ b/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx @@ -72,8 +72,8 @@ export const EnvironmentVariablesTabPanel = () => { const getEnvironmentVariable = useCallback( (environment: Environment) => { - return environmentVariables.filter((item) => - item.environments.includes(environment), + return environmentVariables.filter( + (item) => item.environment === environment, ); }, [environmentVariables, id], diff --git a/packages/frontend/src/components/projects/project/settings/MembersTabPanel.tsx b/packages/frontend/src/components/projects/project/settings/MembersTabPanel.tsx index c2811451..c6d79967 100644 --- a/packages/frontend/src/components/projects/project/settings/MembersTabPanel.tsx +++ b/packages/frontend/src/components/projects/project/settings/MembersTabPanel.tsx @@ -43,7 +43,7 @@ const MembersTabPanel = ({ project }: { project: Project }) => { useEffect(() => { fetchProjectMembers(); - }, [project.id]); + }, [project.id, fetchProjectMembers]); return (
diff --git a/packages/gql-client/src/queries.ts b/packages/gql-client/src/queries.ts index 7c8864b9..4ce9eb30 100644 --- a/packages/gql-client/src/queries.ts +++ b/packages/gql-client/src/queries.ts @@ -122,14 +122,6 @@ query { email } } - environmentVariables { - id - environments - key - value - createdAt - updatedAt - } createdAt updatedAt } @@ -171,7 +163,7 @@ export const getEnvironmentVariables = gql` query ($projectId: String!) { environmentVariables(projectId: $projectId) { createdAt - environments + environment id key updatedAt diff --git a/packages/gql-client/src/types.ts b/packages/gql-client/src/types.ts index aa13d955..d77e446d 100644 --- a/packages/gql-client/src/types.ts +++ b/packages/gql-client/src/types.ts @@ -30,7 +30,7 @@ export enum DomainStatus { export type EnvironmentVariable = { id: string - environments: Environment[] + environment: Environment key: string value: string createdAt: string