summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/common/transformers/award_emoji_transformer.rb
blob: 260b47ab9175699b7c39ad804947c6108f1e4fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# frozen_string_literal: true

module BulkImports
  module Common
    module Transformers
      class AwardEmojiTransformer
        def initialize(*args); end

        def transform(context, data)
          user = find_user(context, data&.dig('user', 'public_email')) || context.current_user

          data
            .except('user')
            .merge('user_id' => user.id)
        end

        private

        def find_user(context, email)
          return if email.blank?

          context.group.users.find_by_any_email(email, confirmed: true) # rubocop: disable CodeReuse/ActiveRecord
        end
      end
    end
  end
end