stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package resolvers
  2. // This file will be automatically regenerated based on the schema, any resolver implementations
  3. // will be copied through when generating and any unknown code will be moved to the end.
  4. import (
  5. "context"
  6. "errors"
  7. "git.aiterp.net/stufflog/server/graph/graphcore"
  8. "git.aiterp.net/stufflog/server/internal/xlerrors"
  9. "git.aiterp.net/stufflog/server/models"
  10. )
  11. func (r *mutationResolver) CreateProject(ctx context.Context, input graphcore.ProjectCreateInput) (*models.Project, error) {
  12. user := r.Auth.UserFromContext(ctx)
  13. if user == nil {
  14. return nil, xlerrors.PermissionDenied
  15. }
  16. project := &models.Project{
  17. ID: input.ID,
  18. Name: input.Name,
  19. Description: input.Description,
  20. DailyPoints: input.DailyPoints,
  21. }
  22. if !project.ValidKey() {
  23. return nil, errors.New("the project must have a valid ID (only uppercase allowed)")
  24. }
  25. if project.Name == "" || project.Description == "" {
  26. return nil, errors.New("name and description cannot be left empty")
  27. }
  28. project, err := r.Database.Projects().Insert(ctx, *project)
  29. if err != nil {
  30. return nil, err
  31. }
  32. err = r.Database.Projects().SetPermission(ctx, models.ProjectPermission{
  33. ProjectID: project.ID,
  34. UserID: user.ID,
  35. Level: models.ProjectPermissionLevelOwner,
  36. })
  37. return project, nil
  38. }
  39. func (r *mutationResolver) CreateActivity(ctx context.Context, input graphcore.ActivityCreateInput) (*models.Activity, error) {
  40. user := r.Auth.UserFromContext(ctx)
  41. if user == nil {
  42. return nil, xlerrors.PermissionDenied
  43. }
  44. project, err := r.Database.Projects().Find(ctx, input.ProjectID)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if perm, err := r.Auth.ProjectPermission(ctx, *project); err != nil || !perm.CanManageActivities() {
  49. return nil, xlerrors.PermissionDenied
  50. }
  51. activity := &models.Activity{
  52. ProjectID: project.ID,
  53. Name: input.Name,
  54. Countable: input.Countable != nil && *input.Countable,
  55. UnitIsTimeSpent: input.UnitIsTimeSpent != nil && *input.UnitIsTimeSpent,
  56. BaseValue: input.BaseValue,
  57. }
  58. if !activity.UnitIsTimeSpent && activity.Countable {
  59. if input.UnitName != nil {
  60. activity.UnitName = *input.UnitName
  61. } else {
  62. return nil, errors.New("unit name is required for countable non-time-spent activities")
  63. }
  64. }
  65. if activity.UnitIsTimeSpent || activity.Countable {
  66. if input.UnitValue != nil {
  67. activity.UnitValue = *input.UnitValue
  68. }
  69. }
  70. return r.Database.Activities().Insert(ctx, *activity)
  71. }
  72. func (r *mutationResolver) EditActivity(ctx context.Context, input graphcore.ActivityEditInput) (*models.Activity, error) {
  73. user := r.Auth.UserFromContext(ctx)
  74. if user == nil {
  75. return nil, xlerrors.PermissionDenied
  76. }
  77. activity, err := r.Database.Activities().Find(ctx, input.ActivityID)
  78. if err != nil {
  79. return nil, err
  80. }
  81. project, err := r.Database.Projects().Find(ctx, activity.ProjectID)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if perm, err := r.Auth.ProjectPermission(ctx, *project); err != nil || !perm.CanManageActivities() {
  86. return nil, xlerrors.PermissionDenied
  87. }
  88. if input.SetName != nil {
  89. activity.Name = *input.SetName
  90. }
  91. if input.SetBaseValue != nil {
  92. activity.BaseValue = *input.SetBaseValue
  93. }
  94. if input.SetUnitName != nil {
  95. activity.UnitName = *input.SetUnitName
  96. }
  97. if input.SetUnitValue != nil {
  98. activity.UnitValue = *input.SetUnitValue
  99. }
  100. err = r.Database.Activities().Save(ctx, *activity)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return activity, nil
  105. }
  106. func (r *mutationResolver) CreateIssue(ctx context.Context, input graphcore.IssueCreateInput) (*models.Issue, error) {
  107. user := r.Auth.UserFromContext(ctx)
  108. if user == nil {
  109. return nil, xlerrors.PermissionDenied
  110. }
  111. project, err := r.Database.Projects().Find(ctx, input.ProjectID)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if perm, err := r.Auth.ProjectPermission(ctx, *project); err != nil || !perm.CanManageOwnIssue() {
  116. return nil, xlerrors.PermissionDenied
  117. }
  118. status, err := r.Database.ProjectStatuses().Find(ctx, project.ID, input.StatusName)
  119. if err != nil {
  120. return nil, err
  121. }
  122. issue := &models.Issue{
  123. ProjectID: project.ID,
  124. OwnerID: user.ID,
  125. AssigneeID: "",
  126. StatusStage: status.Stage,
  127. StatusName: status.Name,
  128. Name: input.Name,
  129. Title: input.Name, // Title set below if it's in the input.
  130. Description: input.Description,
  131. }
  132. if input.Title != nil && *input.Title != "" {
  133. issue.Title = *input.Title
  134. }
  135. if input.DueTime != nil && !input.DueTime.IsZero() {
  136. issue.DueTime = *input.DueTime
  137. }
  138. if input.AssigneeID != nil && *input.AssigneeID != "" {
  139. issue.AssigneeID = *input.AssigneeID
  140. }
  141. issue, err = r.Database.Issues().Insert(ctx, *issue)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return issue, nil
  146. }
  147. func (r *mutationResolver) LoginUser(ctx context.Context, input graphcore.UserLoginInput) (*models.User, error) {
  148. return r.Auth.Login(ctx, input.Username, input.Password)
  149. }
  150. func (r *mutationResolver) LogoutUser(ctx context.Context) (*models.User, error) {
  151. return r.Auth.Logout(ctx)
  152. }
  153. func (r *mutationResolver) CreateUser(ctx context.Context, input graphcore.UserCreateInput) (*models.User, error) {
  154. active := input.Active == nil || *input.Active
  155. admin := input.Admin != nil && *input.Admin
  156. return r.Auth.CreateUser(ctx, input.Username, input.Password, input.Name, active, admin)
  157. }
  158. func (r *mutationResolver) EditUser(ctx context.Context, input graphcore.UserEditInput) (*models.User, error) {
  159. return r.Auth.EditUser(ctx, input.Username, input.SetName, input.CurrentPassword, input.SetPassword)
  160. }
  161. // Mutation returns graphcore.MutationResolver implementation.
  162. func (r *Resolver) Mutation() graphcore.MutationResolver { return &mutationResolver{r} }
  163. type mutationResolver struct{ *Resolver }