snowballtools-base/packages/backend/src/resolvers.ts
Nabarun Gogoi c2b997a17b Add GQL query for projects search in app (#38)
* Find projects based on search text

* Use get search projects client method in UI

* Fetch searched projects inside useCombobox hook

* Get searched project from project entity

* Remove non required search projects filtering

* Fetch projects if user is owner or project member

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
2024-02-01 11:37:57 +05:30

116 lines
3.9 KiB
TypeScript

import debug from 'debug';
import assert from 'assert';
import { Database } from './database';
import { deploymentToGqlType, projectMemberToGqlType, projectToGqlType, environmentVariableToGqlType, isUserOwner } from './utils';
const log = debug('snowball:database');
export const createResolvers = async (db: Database): Promise<any> => {
return {
Query: {
// TODO: add custom type for context
user: (_: any, __: any, context: any) => {
return db.getUser(context.userId);
},
organizations: async (_:any, __: any, context: any) => {
const organizations = await db.getOrganizationsByUserId(context.userId);
const orgsWithProjectsPromises = organizations.map(async (org) => {
const dbProjects = await db.getProjectsByOrganizationId(org.id);
const projectsPromises = dbProjects.map(async (dbProject) => {
const dbProjectMembers = await db.getProjectMembersByProjectId(dbProject.id);
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);
});
const projects = await Promise.all(projectsPromises);
return {
...org,
projects
};
});
// TODO: Add organizationMembers field when / if required
const orgsWithProjects = await Promise.all(orgsWithProjectsPromises);
return orgsWithProjects;
},
deployments: async (_: any, { projectId }: { projectId: string }) => {
const dbDeployments = await db.getDeploymentsByProjectId(projectId);
const deployments = dbDeployments.map(dbDeployment => {
return deploymentToGqlType(dbDeployment);
});
return deployments;
},
projectMembers: async (_: any, { projectId }: { projectId: string }) => {
const dbProjectMembers = await db.getProjectMembersByProjectId(projectId);
const projectMembers = dbProjectMembers.map(dbProjectMember => {
return projectMemberToGqlType(dbProjectMember);
});
return projectMembers;
},
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;
}
},
Mutation: {
removeMember: async (_: any, { memberId }: { memberId: string }, context: any) => {
try {
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))) {
return db.removeProjectMemberByMemberId(memberId);
} 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);
return false;
}
}
}
};
};