diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 329bbe8f..dce51f7b 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -2,8 +2,11 @@ import { DataSource } from 'typeorm'; import path from 'path'; import debug from 'debug'; -import { User } from './entity/User'; import { DatabaseConfig } from './config'; +import { User } from './entity/User'; +import { Organization } from './entity/Organization'; +import { UserOrganization } from './entity/UserOrganization'; +import { Project } from './entity/Project'; const log = debug('snowball:database'); @@ -33,4 +36,41 @@ export class Database { return user; } + + async getOrganizationsbyUserId (userId: number) : Promise { + const userOrganizationRepository = this.dataSource.getRepository(UserOrganization); + + const userOrgs = await userOrganizationRepository.find({ + relations: { + user: true, + organization: true + }, + where: { + user: { + id: userId + } + } + }); + + const organizations = userOrgs.map(userOrg => userOrg.organization); + return organizations; + } + + async getProjectsbyOrganizationId (organizationId: number): Promise { + const projectRepository = this.dataSource.getRepository(Project); + + const projects = await projectRepository.find({ + relations: { + organization: true, + owner: true + }, + where: { + organization: { + id: organizationId + } + } + }); + + return projects; + } } diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index 60ec3cfd..c7511a6e 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -1,15 +1,34 @@ import { Database } from './database'; +import { projectToGqlType } from './utils'; export const createResolvers = async (db: Database): Promise => { return { Query: { - user: ( - _: any, - __: any, - // TODO: add custom type for context - context: any - ) => { + // TODO: add custom type for context + user: (_: any, __: any, context: any) => { return db.getUser(context.userId); + }, + + organizations: async (_:any, __: any, context: any) => { + const organizations = await db.getOrganizationsbyUserId(context.userId); + + const orgsWithProjectsPromises = organizations.map(async (org) => { + const dbProjects = await db.getProjectsbyOrganizationId(org.id); + + const projects = dbProjects.map(dbProject => { + return projectToGqlType(dbProject); + }); + + return { + ...org, + projects + }; + }); + + const orgsWithProjects = await Promise.all(orgsWithProjectsPromises); + + // TODO: Populate members field when / if required + return orgsWithProjects; } } }; diff --git a/packages/backend/src/utils.ts b/packages/backend/src/utils.ts index f33963f9..6e276d73 100644 --- a/packages/backend/src/utils.ts +++ b/packages/backend/src/utils.ts @@ -3,6 +3,8 @@ import path from 'path'; import toml from 'toml'; import debug from 'debug'; +import { Project } from './entity/Project'; + const log = debug('snowball:utils'); export const getConfig = async ( @@ -19,3 +21,19 @@ export const getConfig = async ( return config; }; + +export const projectToGqlType = (dbProject: Project): any => { + return { + id: dbProject.id, + owner: dbProject.owner, + name: dbProject.name, + repository: dbProject.repository, + prodBranch: dbProject.prodBranch, + description: dbProject.description, + template: dbProject.template, + framework: dbProject.framework, + webhooks: dbProject.webhooks, + createdAt: dbProject.createdAt, + updatedAt: dbProject.updatedAt + }; +};