2020-04-11 04:22:22 +02:00
|
|
|
package graph
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
// This file will be automatically regenerated based on the schema, any resolver implementations
|
|
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-07-05 01:02:57 +02:00
|
|
|
"errors"
|
2020-04-10 04:40:22 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2021-01-04 00:04:15 +01:00
|
|
|
"github.com/jinzhu/now"
|
2020-08-07 03:50:35 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/db"
|
2020-10-21 01:52:09 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/logger"
|
2020-04-10 04:40:22 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vektah/gqlparser/v2/gqlerror"
|
|
|
|
)
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *organizationResolver) ID(ctx context.Context, obj *db.Organization) (uuid.UUID, error) {
|
2020-06-21 00:49:11 +02:00
|
|
|
return obj.OrganizationID, nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) Organizations(ctx context.Context) ([]db.Organization, error) {
|
2020-06-21 00:49:11 +02:00
|
|
|
return r.Repository.GetAllOrganizations(ctx)
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) Users(ctx context.Context) ([]db.UserAccount, error) {
|
2020-04-10 04:40:22 +02:00
|
|
|
return r.Repository.GetAllUserAccounts(ctx)
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:52:09 +02:00
|
|
|
func (r *queryResolver) InvitedUsers(ctx context.Context) ([]InvitedUserAccount, error) {
|
|
|
|
invitedMembers, err := r.Repository.GetInvitedUserAccounts(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return []InvitedUserAccount{}, nil
|
|
|
|
}
|
|
|
|
return []InvitedUserAccount{}, err
|
|
|
|
}
|
|
|
|
members := []InvitedUserAccount{}
|
|
|
|
for _, invitedMember := range invitedMembers {
|
|
|
|
members = append(members, InvitedUserAccount{
|
|
|
|
ID: invitedMember.UserAccountInvitedID,
|
|
|
|
Email: invitedMember.Email,
|
|
|
|
InvitedOn: invitedMember.InvitedOn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) FindUser(ctx context.Context, input FindUser) (*db.UserAccount, error) {
|
2020-09-20 00:26:02 +02:00
|
|
|
account, err := r.Repository.GetUserAccountByID(ctx, input.UserID)
|
2020-04-10 04:40:22 +02:00
|
|
|
if err == sql.ErrNoRows {
|
2020-07-05 01:02:57 +02:00
|
|
|
return &db.UserAccount{}, &gqlerror.Error{
|
2020-04-10 04:40:22 +02:00
|
|
|
Message: "User not found",
|
|
|
|
Extensions: map[string]interface{}{
|
|
|
|
"code": "10-404",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &account, err
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) FindProject(ctx context.Context, input FindProject) (*db.Project, error) {
|
2021-05-01 05:55:37 +02:00
|
|
|
_, isLoggedIn := GetUser(ctx)
|
|
|
|
if !isLoggedIn {
|
|
|
|
isPublic, _ := IsProjectPublic(ctx, r.Repository, input.ProjectID)
|
|
|
|
if !isPublic {
|
|
|
|
return &db.Project{}, NotAuthorized()
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 03:01:14 +02:00
|
|
|
project, err := r.Repository.GetProjectByID(ctx, input.ProjectID)
|
2020-04-10 04:40:22 +02:00
|
|
|
if err == sql.ErrNoRows {
|
2020-07-05 01:02:57 +02:00
|
|
|
return &db.Project{}, &gqlerror.Error{
|
2020-04-10 04:40:22 +02:00
|
|
|
Message: "Project not found",
|
|
|
|
Extensions: map[string]interface{}{
|
2021-05-01 05:55:37 +02:00
|
|
|
"code": "NOT_FOUND",
|
2020-04-10 04:40:22 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 03:01:14 +02:00
|
|
|
return &project, nil
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) FindTask(ctx context.Context, input FindTask) (*db.Task, error) {
|
2020-04-20 05:02:55 +02:00
|
|
|
task, err := r.Repository.GetTaskByID(ctx, input.TaskID)
|
|
|
|
return &task, err
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) Projects(ctx context.Context, input *ProjectsFilter) ([]db.Project, error) {
|
2021-04-29 04:32:19 +02:00
|
|
|
userID, ok := GetUser(ctx)
|
2020-08-01 03:01:14 +02:00
|
|
|
if !ok {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).Info("user id was not found from middleware")
|
2020-08-01 03:01:14 +02:00
|
|
|
return []db.Project{}, nil
|
|
|
|
}
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).Info("fetching projects")
|
2020-08-01 03:01:14 +02:00
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
if input != nil {
|
2020-06-23 22:20:53 +02:00
|
|
|
return r.Repository.GetAllProjectsForTeam(ctx, *input.TeamID)
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
2020-08-01 03:01:14 +02:00
|
|
|
|
2020-09-20 03:20:36 +02:00
|
|
|
var teams []db.Team
|
|
|
|
var err error
|
2021-10-07 02:03:38 +02:00
|
|
|
teams, err = r.Repository.GetTeamsForUserIDWhereAdmin(ctx, userID)
|
2020-08-01 03:01:14 +02:00
|
|
|
|
|
|
|
projects := make(map[string]db.Project)
|
|
|
|
for _, team := range teams {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("teamID", team.TeamID).Info("found team")
|
2020-08-01 03:01:14 +02:00
|
|
|
teamProjects, err := r.Repository.GetAllProjectsForTeam(ctx, team.TeamID)
|
|
|
|
if err != sql.ErrNoRows && err != nil {
|
|
|
|
log.Info("issue getting team projects")
|
|
|
|
return []db.Project{}, nil
|
|
|
|
}
|
|
|
|
for _, project := range teamProjects {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectID", project.ProjectID).Info("adding team project")
|
2020-08-01 03:01:14 +02:00
|
|
|
projects[project.ProjectID.String()] = project
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
visibleProjects, err := r.Repository.GetAllVisibleProjectsForUserID(ctx, userID)
|
|
|
|
if err != nil {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).Info("error getting visible projects for user")
|
2020-08-01 03:01:14 +02:00
|
|
|
return []db.Project{}, nil
|
|
|
|
}
|
|
|
|
for _, project := range visibleProjects {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectID", project.ProjectID).Info("found visible project")
|
2020-08-01 03:01:14 +02:00
|
|
|
if _, ok := projects[project.ProjectID.String()]; !ok {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectID", project.ProjectID).Info("adding visible project")
|
2020-08-01 03:01:14 +02:00
|
|
|
projects[project.ProjectID.String()] = project
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectLength", len(projects)).Info("making projects")
|
2020-08-01 03:01:14 +02:00
|
|
|
allProjects := make([]db.Project, 0, len(projects))
|
|
|
|
for _, project := range projects {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectID", project.ProjectID).Info("adding project to final list")
|
2020-08-01 03:01:14 +02:00
|
|
|
allProjects = append(allProjects, project)
|
|
|
|
}
|
|
|
|
return allProjects, nil
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) FindTeam(ctx context.Context, input FindTeam) (*db.Team, error) {
|
2020-06-23 22:20:53 +02:00
|
|
|
team, err := r.Repository.GetTeamByID(ctx, input.TeamID)
|
|
|
|
if err != nil {
|
2020-07-05 01:02:57 +02:00
|
|
|
return &db.Team{}, err
|
2020-06-23 22:20:53 +02:00
|
|
|
}
|
|
|
|
return &team, nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) Teams(ctx context.Context) ([]db.Team, error) {
|
2021-04-29 04:32:19 +02:00
|
|
|
userID, ok := GetUser(ctx)
|
2020-08-01 03:01:14 +02:00
|
|
|
if !ok {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).Error("userID or org role does not exist")
|
2020-08-01 03:01:14 +02:00
|
|
|
return []db.Team{}, errors.New("internal error")
|
|
|
|
}
|
2020-09-20 03:20:36 +02:00
|
|
|
|
2020-08-01 03:01:14 +02:00
|
|
|
teams := make(map[string]db.Team)
|
|
|
|
adminTeams, err := r.Repository.GetTeamsForUserIDWhereAdmin(ctx, userID)
|
|
|
|
if err != nil {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithError(err).Error("error while getting teams for user ID")
|
2020-08-01 03:01:14 +02:00
|
|
|
return []db.Team{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, team := range adminTeams {
|
|
|
|
teams[team.TeamID.String()] = team
|
|
|
|
}
|
|
|
|
|
|
|
|
visibleProjects, err := r.Repository.GetAllVisibleProjectsForUserID(ctx, userID)
|
|
|
|
if err != nil {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithError(err).Error("error while getting visible projects for user ID")
|
2020-08-01 03:01:14 +02:00
|
|
|
return []db.Team{}, err
|
|
|
|
}
|
|
|
|
for _, project := range visibleProjects {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectID", project.ProjectID).Info("found visible project")
|
2020-08-01 03:01:14 +02:00
|
|
|
if _, ok := teams[project.ProjectID.String()]; !ok {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("projectID", project.ProjectID).Info("adding visible project")
|
2020-08-01 03:01:14 +02:00
|
|
|
team, err := r.Repository.GetTeamByID(ctx, project.TeamID)
|
|
|
|
if err != nil {
|
2020-09-20 03:20:36 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).WithField("teamID", project.TeamID).WithError(err).Error("error getting team by id")
|
2020-08-01 03:01:14 +02:00
|
|
|
return []db.Team{}, err
|
|
|
|
}
|
|
|
|
teams[project.TeamID.String()] = team
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foundTeams := make([]db.Team, 0, len(teams))
|
|
|
|
for _, team := range teams {
|
|
|
|
foundTeams = append(foundTeams, team)
|
|
|
|
}
|
|
|
|
return foundTeams, nil
|
2020-06-01 04:20:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-31 03:56:59 +01:00
|
|
|
func (r *queryResolver) MyTasks(ctx context.Context, input MyTasks) (*MyTasksPayload, error) {
|
|
|
|
userID, _ := GetUserID(ctx)
|
|
|
|
projects := []ProjectTaskMapping{}
|
|
|
|
var tasks []db.Task
|
|
|
|
var err error
|
2021-01-04 00:04:15 +01:00
|
|
|
showAll := false
|
|
|
|
if input.Status == MyTasksStatusAll {
|
|
|
|
showAll = true
|
|
|
|
}
|
|
|
|
complete := false
|
|
|
|
completedAt := sql.NullTime{Valid: false, Time: time.Time{}}
|
|
|
|
switch input.Status {
|
|
|
|
case MyTasksStatusCompleteAll:
|
|
|
|
complete = true
|
|
|
|
completedAt = sql.NullTime{Valid: true, Time: time.Time{}}
|
|
|
|
case MyTasksStatusCompleteToday:
|
|
|
|
complete = true
|
|
|
|
completedAt = sql.NullTime{Valid: true, Time: now.BeginningOfDay()}
|
|
|
|
case MyTasksStatusCompleteYesterday:
|
|
|
|
complete = true
|
|
|
|
completedAt = sql.NullTime{Valid: true, Time: now.With(time.Now().AddDate(0, 0, -1)).BeginningOfDay()}
|
|
|
|
case MyTasksStatusCompleteOneWeek:
|
|
|
|
complete = true
|
|
|
|
completedAt = sql.NullTime{Valid: true, Time: now.With(time.Now().AddDate(0, 0, -7)).BeginningOfDay()}
|
|
|
|
case MyTasksStatusCompleteTwoWeek:
|
|
|
|
complete = true
|
|
|
|
completedAt = sql.NullTime{Valid: true, Time: now.With(time.Now().AddDate(0, 0, -14)).BeginningOfDay()}
|
|
|
|
case MyTasksStatusCompleteThreeWeek:
|
|
|
|
complete = true
|
|
|
|
completedAt = sql.NullTime{Valid: true, Time: now.With(time.Now().AddDate(0, 0, -21)).BeginningOfDay()}
|
|
|
|
}
|
|
|
|
|
2020-12-31 03:56:59 +01:00
|
|
|
if input.Sort == MyTasksSortNone {
|
2021-01-04 00:04:15 +01:00
|
|
|
tasks, err = r.Repository.GetRecentlyAssignedTaskForUserID(ctx, db.GetRecentlyAssignedTaskForUserIDParams{
|
|
|
|
UserID: userID,
|
|
|
|
Complete: complete,
|
|
|
|
CompletedAt: completedAt,
|
|
|
|
Column4: showAll,
|
|
|
|
})
|
2020-12-31 03:56:59 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return &MyTasksPayload{}, err
|
|
|
|
}
|
|
|
|
} else if input.Sort == MyTasksSortProject {
|
2021-01-04 00:04:15 +01:00
|
|
|
tasks, err = r.Repository.GetAssignedTasksProjectForUserID(ctx, db.GetAssignedTasksProjectForUserIDParams{
|
|
|
|
UserID: userID,
|
|
|
|
Complete: complete,
|
|
|
|
CompletedAt: completedAt,
|
|
|
|
Column4: showAll,
|
|
|
|
})
|
2020-12-31 03:56:59 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return &MyTasksPayload{}, err
|
|
|
|
}
|
|
|
|
} else if input.Sort == MyTasksSortDueDate {
|
2021-01-04 00:04:15 +01:00
|
|
|
tasks, err = r.Repository.GetAssignedTasksDueDateForUserID(ctx, db.GetAssignedTasksDueDateForUserIDParams{
|
|
|
|
UserID: userID,
|
|
|
|
Complete: complete,
|
|
|
|
CompletedAt: completedAt,
|
|
|
|
Column4: showAll,
|
|
|
|
})
|
2020-12-31 03:56:59 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return &MyTasksPayload{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
taskIds := []uuid.UUID{}
|
|
|
|
for _, task := range tasks {
|
|
|
|
taskIds = append(taskIds, task.TaskID)
|
|
|
|
}
|
|
|
|
mappings, err := r.Repository.GetProjectIdMappings(ctx, taskIds)
|
|
|
|
for _, mapping := range mappings {
|
|
|
|
projects = append(projects, ProjectTaskMapping{ProjectID: mapping.ProjectID, TaskID: mapping.TaskID})
|
|
|
|
}
|
|
|
|
return &MyTasksPayload{Tasks: tasks, Projects: projects}, err
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) LabelColors(ctx context.Context) ([]db.LabelColor, error) {
|
2020-05-27 23:18:50 +02:00
|
|
|
return r.Repository.GetLabelColors(ctx)
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:02:57 +02:00
|
|
|
func (r *queryResolver) TaskGroups(ctx context.Context) ([]db.TaskGroup, error) {
|
2020-04-10 04:40:22 +02:00
|
|
|
return r.Repository.GetAllTaskGroups(ctx)
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:01:14 +02:00
|
|
|
func (r *queryResolver) Me(ctx context.Context) (*MePayload, error) {
|
2020-04-20 05:02:55 +02:00
|
|
|
userID, ok := GetUserID(ctx)
|
|
|
|
if !ok {
|
2021-05-01 05:55:37 +02:00
|
|
|
return nil, nil
|
2020-04-20 05:02:55 +02:00
|
|
|
}
|
|
|
|
user, err := r.Repository.GetUserAccountByID(ctx, userID)
|
2020-07-17 02:40:23 +02:00
|
|
|
if err == sql.ErrNoRows {
|
2020-10-21 01:52:09 +02:00
|
|
|
logger.New(ctx).Warning("can not find user for me query")
|
2020-08-01 03:01:14 +02:00
|
|
|
return &MePayload{}, nil
|
2020-07-17 02:40:23 +02:00
|
|
|
} else if err != nil {
|
2020-08-01 03:01:14 +02:00
|
|
|
return &MePayload{}, err
|
|
|
|
}
|
|
|
|
var projectRoles []ProjectRole
|
|
|
|
projects, err := r.Repository.GetProjectRolesForUserID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return &MePayload{}, err
|
|
|
|
}
|
|
|
|
for _, project := range projects {
|
|
|
|
projectRoles = append(projectRoles, ProjectRole{ProjectID: project.ProjectID, RoleCode: ConvertToRoleCode("admin")})
|
|
|
|
// projectRoles = append(projectRoles, ProjectRole{ProjectID: project.ProjectID, RoleCode: ConvertToRoleCode(project.RoleCode)})
|
|
|
|
}
|
|
|
|
var teamRoles []TeamRole
|
|
|
|
teams, err := r.Repository.GetTeamRolesForUserID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return &MePayload{}, err
|
|
|
|
}
|
|
|
|
for _, team := range teams {
|
|
|
|
// teamRoles = append(teamRoles, TeamRole{TeamID: team.TeamID, RoleCode: ConvertToRoleCode(team.RoleCode)})
|
|
|
|
teamRoles = append(teamRoles, TeamRole{TeamID: team.TeamID, RoleCode: ConvertToRoleCode("admin")})
|
2020-04-20 05:02:55 +02:00
|
|
|
}
|
2020-08-01 03:01:14 +02:00
|
|
|
return &MePayload{User: &user, TeamRoles: teamRoles, ProjectRoles: projectRoles}, err
|
2020-04-20 05:02:55 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 04:22:22 +02:00
|
|
|
// Mutation returns MutationResolver implementation.
|
|
|
|
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
|
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
// Organization returns OrganizationResolver implementation.
|
|
|
|
func (r *Resolver) Organization() OrganizationResolver { return &organizationResolver{r} }
|
|
|
|
|
2020-04-11 04:22:22 +02:00
|
|
|
// Query returns QueryResolver implementation.
|
|
|
|
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
|
|
|
|
|
2021-10-26 21:42:04 +02:00
|
|
|
// Subscription returns SubscriptionResolver implementation.
|
|
|
|
func (r *Resolver) Subscription() SubscriptionResolver { return &subscriptionResolver{r} }
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
type mutationResolver struct{ *Resolver }
|
2020-06-21 00:49:11 +02:00
|
|
|
type organizationResolver struct{ *Resolver }
|
2020-04-10 04:40:22 +02:00
|
|
|
type queryResolver struct{ *Resolver }
|
2021-10-26 21:42:04 +02:00
|
|
|
type subscriptionResolver struct{ *Resolver }
|