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-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-17 05:23:01 +00:00
|
|
|
export const createResolvers = async (db: Database): 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
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const environmentVariables = dbEnvironmentVariables.map(dbEnvironmentVariable => {
|
|
|
|
return environmentVariableToGqlType(dbEnvironmentVariable);
|
|
|
|
});
|
|
|
|
|
|
|
|
return environmentVariables;
|
|
|
|
},
|
|
|
|
|
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-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
|
|
|
},
|
|
|
|
|
|
|
|
updateDeploymentToProd: async (_: any, { deploymentId }: {deploymentId: string }) => {
|
|
|
|
try {
|
|
|
|
return db.updateDeploymentById(deploymentId, {
|
|
|
|
environment: Environment.Production
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 09:36:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|