2024-01-22 09:42:08 +00:00
|
|
|
import debug from 'debug';
|
|
|
|
|
2024-01-17 05:23:01 +00:00
|
|
|
import { Database } from './database';
|
2024-01-18 08:36:49 +00:00
|
|
|
import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, environmentVariableToGqlType } from './utils';
|
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-18 08:36:49 +00:00
|
|
|
const projectsWithPromises = 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-18 08:36:49 +00:00
|
|
|
const projects = await Promise.all(projectsWithPromises);
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
projectMembers: async (_: any, { projectId }: { projectId: string }) => {
|
|
|
|
const dbProjectMembers = await db.getProjectMembersByProjectId(projectId);
|
|
|
|
|
|
|
|
const projectMembers = dbProjectMembers.map(dbProjectMember => {
|
|
|
|
return projectMemberToGqlType(dbProjectMember);
|
|
|
|
});
|
|
|
|
|
|
|
|
return projectMembers;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Mutation: {
|
|
|
|
removeMember: async (_: any, { memberId }:{ memberId: string }) => {
|
|
|
|
try {
|
|
|
|
return await db.removeProjectMemberByMemberId(memberId);
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 09:36:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|