diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index d2ec61d2..e54f6336 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -11,6 +11,7 @@ import { createResolvers } from './resolvers'; import { getConfig } from './utils'; import { Config } from './config'; import { DEFAULT_CONFIG_FILE_PATH } from './constants'; +import { Service } from './service'; const log = debug('snowball:server'); @@ -20,6 +21,7 @@ export const main = async (): Promise => { const db = new Database(database); await db.init(); + const service = new Service(db); // TODO: Move to Service class const app = new OAuthApp({ @@ -29,7 +31,7 @@ export const main = async (): Promise => { }); const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString(); - const resolvers = await createResolvers(db, app); + const resolvers = await createResolvers(db, app, service); await createAndStartServer(typeDefs, resolvers, server); }; diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index a6d0de31..bec308a5 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -4,8 +4,9 @@ import { DeepPartial } from 'typeorm'; import { OAuthApp } from '@octokit/oauth-app'; +import { Service } from './service'; import { Database } from './database'; -import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, isUserOwner } from './utils'; +import { isUserOwner } from './utils'; import { Environment } from './entity/Deployment'; import { Permission } from './entity/ProjectMember'; import { Domain } from './entity/Domain'; @@ -13,71 +14,45 @@ import { Project } from './entity/Project'; const log = debug('snowball:database'); -export const createResolvers = async (db: Database, app: OAuthApp): Promise => { +// TODO: Remove Database argument and refactor code to Service +export const createResolvers = async (db: Database, app: OAuthApp, service: Service): Promise => { return { Query: { // TODO: add custom type for context user: (_: any, __: any, context: any) => { - return db.getUser(context.userId); + return service.getUser(context.userId); }, organizations: async (_:any, __: any, context: any) => { - return db.getOrganizationsByUserId(context.userId); + return service.getOrganizationsByUserId(context.userId); }, project: async (_: any, { projectId }: { projectId: string }) => { - const dbProject = await db.getProjectById(projectId); - - return dbProject || null; + return service.getProjectById(projectId); }, projectsInOrganization: async (_: any, { organizationId }: {organizationId: string }, context: any) => { - const dbProjects = await db.getProjectsInOrganization(context.userId, organizationId); - return dbProjects; + return service.getProjectsInOrganization(context.userId, organizationId); }, deployments: async (_: any, { projectId }: { projectId: string }) => { - const dbDeployments = await db.getDeploymentsByProjectId(projectId); - - const deployments = dbDeployments.map(dbDeployment => { - return deploymentToGqlType(dbDeployment); - }); - - return deployments; + return service.getDeployementsByProjectId(projectId); }, environmentVariables: async (_: any, { projectId }: { projectId: string }) => { - const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(projectId); - return dbEnvironmentVariables; + return service.getEnvironmentVariablesByProjectId(projectId); }, projectMembers: async (_: any, { projectId }: { projectId: string }) => { - const dbProjectMembers = await db.getProjectMembersByProjectId(projectId); - - const projectMembers = dbProjectMembers.map(dbProjectMember => { - return projectMemberToGqlType(dbProjectMember); - }); - - return projectMembers; + return service.getProjectMembersByProjectId(projectId); }, searchProjects: async (_: any, { searchText }: { searchText: string }, context: any) => { - const dbProjects = await db.getProjectsBySearchText(context.userId, searchText); - - const projects = dbProjects.map((project) => { - return projectToGqlType(project, [], []); - }); - - return projects; + return service.searchProjects(context.userId, searchText); }, domains: async (_:any, { projectId }: { projectId: string }) => { - try { - return db.getDomainsByProjectId(projectId); - } catch (err) { - log(err); - return false; - } + return service.getDomainsByProjectId(projectId); } }, diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts new file mode 100644 index 00000000..8c3798c4 --- /dev/null +++ b/packages/backend/src/service.ts @@ -0,0 +1,60 @@ +import { Database } from './database'; +import { Deployment } from './entity/Deployment'; +import { Domain } from './entity/Domain'; +import { EnvironmentVariable } from './entity/EnvironmentVariable'; +import { Organization } from './entity/Organization'; +import { Project } from './entity/Project'; +import { ProjectMember } from './entity/ProjectMember'; +import { User } from './entity/User'; + +export class Service { + private db: Database; + + constructor (db: Database) { + this.db = db; + } + + async getUser (userId: number): Promise { + return this.db.getUser(userId); + } + + async getOrganizationsByUserId (userId: number): Promise { + const dbOrganizations = await this.db.getOrganizationsByUserId(userId); + return dbOrganizations; + } + + async getProjectById (projectId: string): Promise { + const dbProject = await this.db.getProjectById(projectId); + return dbProject; + } + + async getProjectsInOrganization (userId:string, organizationId: string): Promise { + const dbProjects = await this.db.getProjectsInOrganization(userId, organizationId); + return dbProjects; + } + + async getDeployementsByProjectId (projectId: string): Promise { + const dbDeployments = await this.db.getDeploymentsByProjectId(projectId); + return dbDeployments; + } + + async getEnvironmentVariablesByProjectId (projectId: string): Promise { + const dbEnvironmentVariables = await this.db.getEnvironmentVariablesByProjectId(projectId); + return dbEnvironmentVariables; + } + + async getProjectMembersByProjectId (projectId: string): Promise { + const dbProjectMembers = await this.db.getProjectMembersByProjectId(projectId); + return dbProjectMembers; + } + + async searchProjects (userId:string, searchText: string): Promise { + const dbProjects = await this.db.getProjectsBySearchText(Number(userId), searchText); + return dbProjects; + } + + async getDomainsByProjectId (projectId: string): Promise { + const dbDomains = await this.db.getDomainsByProjectId(projectId); + return dbDomains; + } +}