2024-01-22 09:42:08 +00:00
|
|
|
import debug from 'debug';
|
2024-02-06 08:48:06 +00:00
|
|
|
import { DeepPartial, FindOptionsWhere } from 'typeorm';
|
2024-01-22 09:42:08 +00:00
|
|
|
|
2024-01-30 14:20:53 +00:00
|
|
|
import { OAuthApp } from '@octokit/oauth-app';
|
|
|
|
|
2024-02-02 09:32:12 +00:00
|
|
|
import { Service } from './service';
|
2024-01-17 05:23:01 +00:00
|
|
|
import { Database } from './database';
|
2024-01-31 11:39:29 +00:00
|
|
|
import { Permission } from './entity/ProjectMember';
|
2024-01-31 12:25:19 +00:00
|
|
|
import { Domain } from './entity/Domain';
|
2024-02-01 08:34:07 +00:00
|
|
|
import { Project } from './entity/Project';
|
2024-02-05 10:51:55 +00:00
|
|
|
import { EnvironmentVariable } from './entity/EnvironmentVariable';
|
2024-01-16 09:36:35 +00:00
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
const log = debug('snowball:database');
|
|
|
|
|
2024-02-02 09:32:12 +00:00
|
|
|
// TODO: Remove Database argument and refactor code to Service
|
|
|
|
export const createResolvers = async (db: Database, app: OAuthApp, service: Service): 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-02-02 09:32:12 +00:00
|
|
|
return service.getUser(context.userId);
|
2024-01-17 11:29:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
organizations: async (_:any, __: any, context: any) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getOrganizationsByUserId(context.userId);
|
2024-01-18 08:36:49 +00:00
|
|
|
},
|
|
|
|
|
2024-01-25 06:28:38 +00:00
|
|
|
project: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getProjectById(projectId);
|
2024-01-29 11:01:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
projectsInOrganization: async (_: any, { organizationId }: {organizationId: string }, context: any) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getProjectsInOrganization(context.userId, organizationId);
|
2024-01-25 06:28:38 +00:00
|
|
|
},
|
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
deployments: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getDeployementsByProjectId(projectId);
|
2024-01-22 09:42:08 +00:00
|
|
|
},
|
|
|
|
|
2024-01-25 05:14:51 +00:00
|
|
|
environmentVariables: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getEnvironmentVariablesByProjectId(projectId);
|
2024-01-25 05:14:51 +00:00
|
|
|
},
|
|
|
|
|
2024-01-22 09:42:08 +00:00
|
|
|
projectMembers: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.getProjectMembersByProjectId(projectId);
|
2024-01-24 14:47:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
searchProjects: async (_: any, { searchText }: { searchText: string }, context: any) => {
|
2024-02-02 09:32:12 +00:00
|
|
|
return service.searchProjects(context.userId, searchText);
|
2024-01-29 12:48:43 +00:00
|
|
|
},
|
|
|
|
|
2024-02-06 08:48:06 +00:00
|
|
|
domains: async (_:any, { projectId, filter }: { projectId: string, filter?: FindOptionsWhere<Domain> }) => {
|
|
|
|
return service.getDomainsByProjectId(projectId, filter);
|
2024-01-22 09:42:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-01 10:04:17 +00:00
|
|
|
// TODO: Return error in GQL response
|
2024-01-22 09:42:08 +00:00
|
|
|
Mutation: {
|
2024-01-31 11:39:29 +00:00
|
|
|
removeProjectMember: async (_: any, { projectMemberId }: { projectMemberId: string }, context: any) => {
|
2024-01-22 09:42:08 +00:00
|
|
|
try {
|
2024-02-06 08:48:06 +00:00
|
|
|
return await service.removeProjectMember(context.userId, projectMemberId);
|
2024-01-23 09:31:33 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-01-31 11:39:29 +00:00
|
|
|
updateProjectMember: async (_: any, { projectMemberId, data }: {
|
|
|
|
projectMemberId: string,
|
|
|
|
data: {
|
|
|
|
permissions: Permission[]
|
|
|
|
}
|
|
|
|
}) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.updateProjectMember(projectMemberId, data);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-01 10:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addProjectMember: async (_: any, { projectId, data }: {
|
|
|
|
projectId: string,
|
|
|
|
data: {
|
|
|
|
email: string,
|
|
|
|
permissions: Permission[]
|
|
|
|
}
|
|
|
|
}) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return Boolean(await service.addProjectMember(projectId, data));
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 11:39:29 +00:00
|
|
|
},
|
|
|
|
|
2024-02-05 10:51:55 +00:00
|
|
|
addEnvironmentVariables: async (_: any, { projectId, data }: { projectId: string, data: { environments: string[], key: string, value: string}[] }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return Boolean(await service.addEnvironmentVariables(projectId, data));
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 05:47:44 +00:00
|
|
|
},
|
|
|
|
|
2024-02-05 10:51:55 +00:00
|
|
|
updateEnvironmentVariable: async (_: any, { environmentVariableId, data }: { environmentVariableId: string, data : DeepPartial<EnvironmentVariable>}) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.updateEnvironmentVariable(environmentVariableId, data);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 05:06:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeEnvironmentVariable: async (_: any, { environmentVariableId }: { environmentVariableId: string}) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.removeEnvironmentVariable(environmentVariableId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-31 05:06:22 +00:00
|
|
|
},
|
|
|
|
|
2024-01-29 12:48:43 +00:00
|
|
|
updateDeploymentToProd: async (_: any, { deploymentId }: { deploymentId: string }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.updateDeploymentToProd(deploymentId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-25 06:28:38 +00:00
|
|
|
},
|
|
|
|
|
2024-02-05 10:51:55 +00:00
|
|
|
addProject: async (_: any, { data }: { data: DeepPartial<Project> }, context: any) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return Boolean(await service.addProject(context.userId, data));
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-01 12:40:15 +00:00
|
|
|
},
|
|
|
|
|
2024-02-01 08:34:07 +00:00
|
|
|
updateProject: async (_: any, { projectId, projectDetails }: { projectId: string, projectDetails: DeepPartial<Project> }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.updateProject(projectId, projectDetails);
|
|
|
|
} 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-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.redeployToProd(context.userId, deploymentId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-29 11:01:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteProject: async (_: any, { projectId }: { projectId: string }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.deleteProject(projectId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err); return false;
|
|
|
|
}
|
2024-01-29 12:09:51 +00:00
|
|
|
},
|
|
|
|
|
2024-02-01 03:34:24 +00:00
|
|
|
deleteDomain: async (_: any, { domainId }: { domainId: string }) => {
|
2024-02-06 08:48:06 +00:00
|
|
|
try {
|
|
|
|
return await service.deleteDomain(domainId);
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-01 03:34:24 +00:00
|
|
|
},
|
|
|
|
|
2024-01-29 12:09:51 +00:00
|
|
|
rollbackDeployment: async (_: any, { projectId, deploymentId }: {deploymentId: string, projectId: string }) => {
|
|
|
|
try {
|
2024-02-06 08:48:06 +00:00
|
|
|
return await service.rollbackDeployment(projectId, deploymentId);
|
2024-01-29 12:09:51 +00:00
|
|
|
} 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 {
|
2024-02-06 08:48:06 +00:00
|
|
|
return Boolean(await service.addDomain(projectId, domainDetails));
|
2024-01-29 12:48:43 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 08:31:09 +00:00
|
|
|
},
|
|
|
|
|
2024-01-31 12:25:19 +00:00
|
|
|
updateDomain: async (_: any, { domainId, domainDetails }: { domainId: string, domainDetails: DeepPartial<Domain>}) => {
|
2024-01-30 08:31:09 +00:00
|
|
|
try {
|
2024-02-06 08:48:06 +00:00
|
|
|
return await service.updateDomain(domainId, domainDetails);
|
2024-01-30 08:31:09 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-30 14:20:53 +00:00
|
|
|
},
|
|
|
|
|
2024-01-31 13:21:53 +00:00
|
|
|
authenticateGitHub: async (_: any, { code }: { code: string }, context: any) => {
|
2024-01-30 14:20:53 +00:00
|
|
|
// TOO: Move to Service class
|
|
|
|
const { authentication: { token } } = await app.createToken({
|
|
|
|
code
|
|
|
|
});
|
|
|
|
|
2024-01-31 13:21:53 +00:00
|
|
|
await db.updateUser(context.userId, { gitHubToken: token });
|
2024-01-30 14:20:53 +00:00
|
|
|
|
|
|
|
return { token };
|
2024-01-31 13:21:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
unauthenticateGitHub: async (_: any, __: object, context: any) => {
|
|
|
|
try {
|
|
|
|
return db.updateUser(context.userId, { gitHubToken: null });
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-17 05:23:01 +00:00
|
|
|
}
|
2024-01-16 09:36:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|