delete.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class User::Current::Avatar::Delete < BaseMutation
  4. description 'Delete an existing avatar for the currently logged in user.'
  5. argument :id, GraphQL::Types::ID, description: 'The unique identifier of the avatar which should be deleted.'
  6. field :success, Boolean, null: false, description: 'Was the avatar deletion successful?'
  7. def self.authorize(_obj, ctx)
  8. ctx.current_user.permissions?('user_preferences.avatar')
  9. end
  10. def resolve(id:)
  11. avatar_id = Gql::ZammadSchema.verified_object_from_id(id, type: Avatar).id
  12. if avatar_id.blank? || Avatar.find_by(id: avatar_id, o_id: context.current_user.id).blank?
  13. raise ActiveRecord::RecordNotFound, __('Avatar could not be found.')
  14. end
  15. Avatar.remove_one('User', context.current_user.id, avatar_id)
  16. set_default_avatar
  17. { success: true }
  18. end
  19. private
  20. def set_default_avatar
  21. return if Avatar.find_by(
  22. object_lookup_id: ObjectLookup.by_name('User'),
  23. o_id: context.current_user.id,
  24. default: true,
  25. ).present?
  26. Avatar.find_by(
  27. object_lookup_id: ObjectLookup.by_name('User'),
  28. o_id: context.current_user.id,
  29. initial: true,
  30. )&.update!(default: true)
  31. context.current_user.update!(image: nil)
  32. end
  33. end
  34. end