notification_preferences_update.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class User::Current::NotificationPreferencesUpdate < BaseMutation
  4. description 'Update user notification settings'
  5. argument :group_ids, [GraphQL::Types::ID, { null: false }], required: false, description: 'Limit notifications to specified groups, if any'
  6. argument :matrix, Gql::Types::Input::User::NotificationMatrixInputType, description: 'Ticket notification preference matrix'
  7. argument :sound, Gql::Types::Input::User::NotificationSoundInputType, description: 'Ticket notification sound preference'
  8. field :user, Gql::Types::UserType, null: false, description: 'Updated user object'
  9. def self.authorize(_obj, ctx)
  10. ctx.current_user.permissions?('user_preferences.notifications+ticket.agent')
  11. end
  12. def resolve(matrix:, sound:, group_ids: nil)
  13. user = context.current_user
  14. user.with_lock do
  15. user.preferences.merge! new_config(group_ids:, matrix:, sound:)
  16. user.save!
  17. end
  18. { user: user.reload }
  19. end
  20. def new_config(matrix:, sound:, group_ids: nil)
  21. config = { matrix: matrix.to_h }
  22. config[:group_ids] = group_internal_ids(group_ids) if group_ids.present?
  23. {
  24. notification_config: config,
  25. notification_sound: sound.to_h,
  26. }
  27. end
  28. def group_internal_ids(group_ids)
  29. group_ids.map do |gid|
  30. Gql::ZammadSchema.authorized_object_from_id(gid, type: Group, user: context.current_user).id
  31. end
  32. end
  33. end
  34. end