GraphQL API and utilities for the rpdata project
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.

70 lines
1.4 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/internal/auth"
  7. "git.aiterp.net/rpdata/api/model/change"
  8. "git.aiterp.net/rpdata/api/model/log"
  9. )
  10. // LogAddArgs is args for the addLog mutation
  11. type LogAddArgs struct {
  12. Input *struct {
  13. Date string
  14. Channel string
  15. Title *string
  16. Open *bool
  17. Event *string
  18. Description *string
  19. }
  20. }
  21. // AddLog resolves the addLog mutation
  22. func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*types.LogResolver, error) {
  23. input := args.Input
  24. token := auth.TokenFromContext(ctx)
  25. if !token.Permitted("log.add") {
  26. return nil, ErrUnauthorized
  27. }
  28. date, err := time.Parse(time.RFC3339Nano, args.Input.Date)
  29. if err != nil {
  30. return nil, err
  31. }
  32. title := ""
  33. if input.Title != nil {
  34. title = *input.Title
  35. }
  36. event := ""
  37. if input.Event != nil {
  38. event = *input.Event
  39. }
  40. description := ""
  41. if input.Description != nil {
  42. description = *input.Description
  43. }
  44. open := input.Open != nil && *input.Open == true
  45. log, err := log.New(date, input.Channel, title, event, description, open)
  46. if err != nil {
  47. return nil, err
  48. }
  49. go change.Submit("Log", "add", token.UserID, log.ID, map[string]interface{}{
  50. "channel": log.Channel,
  51. "title": log.Title,
  52. "event": log.Event,
  53. "description": log.Description,
  54. "open": log.Open,
  55. })
  56. return &types.LogResolver{L: log}, nil
  57. }