2024-01-22 09:42:08 +00:00
|
|
|
import debug from 'debug';
|
2024-01-23 09:31:33 +00:00
|
|
|
import assert from 'assert';
|
2024-01-22 09:42:08 +00:00
|
|
|
|
2024-01-30 14:20:53 +00:00
|
|
|
import { OAuthApp } from '@octokit/oauth-app';
|
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
import { Database } from './database';
|
2024-01-23 09:31:33 +00:00
|
|
|
import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, environmentVariableToGqlType, isUserOwner } from './utils';
|
2024-01-25 05:47:44 +00:00
|
|
|
import { Environment } from './entity/Deployment';
|
2024-01-16 09:36:35 +00:00
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
const log = debug('snowball:database');
|
|
|
|
|
2024-01-30 14:20:53 +00:00
|
|
|
export const createResolvers = async (db: Database, app: OAuthApp): Promise<any> => {
|
2024-01-16 09:36:35 +00:00
|
|
|
return {
|
|
|
|
Query: {
|
2024-01-17 11:29:59 +00:00
|
|
|
// TODO: add custom type for context
|
|
|
|
user: (_: any, __: any, context: any) => {
|
2024-01-17 05:23:01 +00:00
|
|
|
return db.getUser(context.userId);
|
2024-01-17 11:29:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
organizations: async (_:any, __: any, context: any) => {
|
2024-01-18 08:36:49 +00:00
|
|
|
const organizations = await db.getOrganizationsByUserId(context.userId);
|
2024-01-17 11:29:59 +00:00
|
|
|
|
|
|
|
const orgsWithProjectsPromises = organizations.map(async (org) => {
|
2024-01-18 08:36:49 +00:00
|
|
|
const dbProjects = await db.getProjectsByOrganizationId(org.id);
|
2024-01-17 11:29:59 +00:00
|
|
|
|
2024-01-23 09:31:33 +00:00
|
|
|
const projectsPromises = dbProjects.map(async (dbProject) => {
|
2024-01-22 09:42:08 +00:00
|
|
|
const dbProjectMembers = await db.getProjectMembersByProjectId(dbProject.id);
|
2024-01-18 08:36:49 +00:00
|
|
|
const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(dbProject.id);
|
|
|
|
|
|
|
|
const projectMembers = dbProjectMembers.map(dbProjectMember => {
|
|
|
|
return projectMemberToGqlType(dbProjectMember);
|
|
|
|
});
|
|
|
|
|
|
|
|
const environmentVariables = dbEnvironmentVariables.map(dbEnvironmentVariable => {
|
|
|
|
return environmentVariableToGqlType(dbEnvironmentVariable);
|
|
|
|
});
|
|
|
|
|
|
|
|
return projectToGqlType(dbProject, projectMembers, environmentVariables);
|
2024-01-17 11:29:59 +00:00
|
|
|
});
|
|
|
|
|
2024-01-23 09:31:33 +00:00
|
|
|
const projects = await Promise.all(projectsPromises);
|
2024-01-18 08:36:49 +00:00
|
|
|
|
2024-01-17 11:29:59 +00:00
|
|
|
return {
|
|
|
|
...org,
|
|
|
|
projects
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
// TODO: Add organizationMembers field when / if required
|
2024-01-17 11:29:59 +00:00
|
|
|
const orgsWithProjects = await Promise.all(orgsWithProjectsPromises);
|
|
|
|
return orgsWithProjects;
|
2024-01-18 08:36:49 +00:00
|
|
|
},
|
|
|
|
|
2024-01-25 06:28:38 +00:00
|
|
|
project: async (_: any, { projectId }: { projectId: string }) => {
|
2024-01-29 11:01:03 +00:00
|
|
|
const dbProject = await db.getProjectById(projectId);
|
2024-01-25 06:28:38 +00:00
|
|
|
|
2024-01-29 11:01:03 +00:00
|
|
|
return dbProject || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
projectsInOrganization: async (_: any, { organizationId }: {organizationId: string }, context: any) => {
|
2024-01-30 09:10:04 +00:00
|
|
|
const dbProjects = await db.getProjectsInOrganization(context.userId, organizationId);
|
|
|
|
return dbProjects;
|
2024-01-25 06:28:38 +00:00
|
|
|
},
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
deployments: async (_: any, { projectId }: { projectId: string }) => {
|
|
|
|
const dbDeployments = await db.getDeploymentsByProjectId(projectId);
|
|
|
|
|
|
|
|
const deployments = dbDeployments.map(dbDeployment => {
|
|
|
|
return deploymentToGqlType(dbDeployment);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deployments;
|
2024-01-22 09:42:08 +00:00
|
|
|
},
|
|
|
|
|
2024-01-25 05:14:51 +00:00
|
|
|
environmentVariables: async (_: any, { projectId }: { projectId: string }) => {
|
|
|
|
const dbEnvironmentVariables = await db.getEnvironmentVariablesByProjectId(projectId);
|
2024-01-30 13:47:55 +00:00
|
|
|
return dbEnvironmentVariables;
|
2024-01-25 05:14:51 +00:00
|
|
|
},
|
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
projectMembers: async (_: any, { projectId }: { projectId: string }) => {
|
|
|
|
const dbProjectMembers = await db.getProjectMembersByProjectId(projectId);
|
|
|
|
|
|
|
|
const projectMembers = dbProjectMembers.map(dbProjectMember => {
|
|
|
|
return projectMemberToGqlType(dbProjectMember);
|
|
|
|
});
|
|
|
|
|
|
|
|
return projectMembers;
|
2024-01-24 14:47:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
2024-01-29 12:48:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
domains: async (_:any, { projectId }: { projectId: string }) => {
|
|
|
|
try {
|
|
|
|
return db.getDomainsByProjectId(projectId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-22 09:42:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Mutation: {
|
2024-01-23 09:31:33 +00:00
|
|
|
removeMember: async (_: any, { memberId }: { memberId: string }, context: any) => {
|
2024-01-22 09:42:08 +00:00
|
|
|
try {
|
2024-01-23 09:31:33 +00:00
|
|
|
const member = await db.getProjectMemberByMemberId(memberId);
|
|
|
|
|
|
|
|
if (member.member.id === context.userId) {
|
|
|
|
throw new Error('Invalid operation: cannot remove self');
|
|
|
|
}
|
|
|
|
|
|
|
|
const memberProject = member.project;
|
|
|
|
assert(memberProject);
|
|
|
|
|
|
|
|
if (isUserOwner(String(context.userId), String(memberProject.owner.id))) {
|
2024-01-25 05:47:44 +00:00
|
|
|
return db.removeProjectMemberById(memberId);
|
2024-01-23 09:31:33 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Invalid operation: not authorized');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addEnvironmentVariables: async (_: any, { projectId, environmentVariables }: { projectId: string, environmentVariables: { environments: string[], key: string, value: string}[] }) => {
|
|
|
|
try {
|
|
|
|
return db.addEnvironmentVariablesByProjectId(projectId, environmentVariables);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
2024-01-22 09:42:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 05:47:44 +00:00
|
|
|
},
|
|
|
|
|
2024-01-29 12:48:43 +00:00
|
|
|
updateDeploymentToProd: async (_: any, { deploymentId }: { deploymentId: string }) => {
|
2024-01-25 05:47:44 +00:00
|
|
|
try {
|
|
|
|
return db.updateDeploymentById(deploymentId, {
|
|
|
|
environment: Environment.Production
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 06:28:38 +00:00
|
|
|
},
|
|
|
|
|
2024-01-30 08:31:09 +00:00
|
|
|
updateProject: async (_: any, { projectId, projectDetails }: { projectId: string, projectDetails: { name: string, description: string } }) => {
|
2024-01-25 06:28:38 +00:00
|
|
|
try {
|
2024-01-30 08:31:09 +00:00
|
|
|
return db.updateProjectById(projectId, projectDetails);
|
2024-01-25 06:28:38 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 12:04:13 +00:00
|
|
|
},
|
|
|
|
|
2024-01-30 10:18:50 +00:00
|
|
|
redeployToProd: async (_: any, { deploymentId }: { deploymentId: string }, context: any) => {
|
2024-01-25 12:04:13 +00:00
|
|
|
try {
|
2024-01-30 10:18:50 +00:00
|
|
|
await db.redeployToProdById(context.userId, deploymentId);
|
2024-01-29 12:48:43 +00:00
|
|
|
return true;
|
2024-01-25 12:04:13 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-29 11:01:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteProject: async (_: any, { projectId }: { projectId: string }) => {
|
|
|
|
try {
|
|
|
|
return db.deleteProjectById(projectId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-29 12:09:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
rollbackDeployment: async (_: any, { projectId, deploymentId }: {deploymentId: string, projectId: string }) => {
|
|
|
|
try {
|
|
|
|
return db.rollbackDeploymentById(projectId, deploymentId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-29 12:48:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addDomain: async (_: any, { projectId, domainDetails }: { projectId: string, domainDetails: { name: string } }) => {
|
|
|
|
try {
|
|
|
|
await db.addDomainByProjectId(projectId, domainDetails);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 08:31:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateDomain: async (_: any, { domainId, domainDetails }: { domainId: string, domainDetails: {name?: string, isRedirected?: boolean, branch?: string }}) => {
|
|
|
|
try {
|
|
|
|
await db.updateDomainById(domainId, domainDetails);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 14:20:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
authenticateGithub: async (_: any, { code }: { code: string }) => {
|
|
|
|
// TOO: Move to Service class
|
|
|
|
const { authentication: { token } } = await app.createToken({
|
|
|
|
code
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Save bearer token in DB for user
|
|
|
|
|
|
|
|
return { token };
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 09:36:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|