diff --git a/services/api/src/resolvers.js b/services/api/src/resolvers.js index 674add53ff..43caeab7b2 100644 --- a/services/api/src/resolvers.js +++ b/services/api/src/resolvers.js @@ -248,6 +248,7 @@ const { updateOrganization, deleteOrganization, getOrganizationById, + getOrganizationByName, addDeployTargetToOrganization, removeDeployTargetFromOrganization, getDeployTargetsByOrganizationId, @@ -577,6 +578,7 @@ const resolvers = { deployTargetConfigsByDeployTarget: getDeployTargetConfigsByDeployTarget, allOrganizations: getAllOrganizations, organizationById: getOrganizationById, + organizationByName: getOrganizationByName, getGroupProjectOrganizationAssociation, getProjectGroupOrganizationAssociation, getEnvVariablesByProjectEnvironmentName, diff --git a/services/api/src/resources/organization/resolvers.ts b/services/api/src/resources/organization/resolvers.ts index 67d9b0b4e0..adb3582db9 100644 --- a/services/api/src/resources/organization/resolvers.ts +++ b/services/api/src/resources/organization/resolvers.ts @@ -242,6 +242,30 @@ export const getOrganizationById: ResolverFn = async ( return orgResult; }; +export const getOrganizationByName: ResolverFn = async ( + name, + args, + { sqlClientPool, hasPermission } +) => { + let orgName = args.name; + if (name) { + orgName = name; + } + + const rows = await query(sqlClientPool, Sql.selectOrganizationByName(orgName)); + const orgResult = rows[0]; + + if (!orgResult) { + return null; + } + + await hasPermission('organization', 'view', { + organization: orgResult.id, + }); + + return orgResult; +}; + export const getAllOrganizations: ResolverFn = async ( root, args, diff --git a/services/api/src/resources/organization/sql.ts b/services/api/src/resources/organization/sql.ts index 273558fd62..d05fca12dd 100644 --- a/services/api/src/resources/organization/sql.ts +++ b/services/api/src/resources/organization/sql.ts @@ -74,6 +74,10 @@ export const Sql = { knex('organization') .where('id', '=', id) .toString(), + selectOrganizationByName: (name: string) => + knex('organization') + .where('name', '=', name) + .toString(), selectOrganizationProjects: (id: number) => knex('project') .where('organization', '=', id) diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index 633b887e51..13ba89ede2 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -1416,6 +1416,7 @@ const typeDefs = gql` Get an organization by its ID """ organizationById(organization: Int!): Organization + organizationByName(name: String!): Organization getGroupProjectOrganizationAssociation(input: AddGroupInput!): String getProjectGroupOrganizationAssociation(input: ProjectOrgGroupsInput!): String getEnvVariablesByProjectEnvironmentName(input: EnvVariableByProjectEnvironmentNameInput!): [EnvKeyValue]