<commit_before>module MultiClient
  class CreateClientService < Itsf::Services::V2::Service::Base
    class Response < Itsf::Services::V2::Response::Base
      # attr_accessor :client, :client_setting, :users, :licensed_products, :roles, :permissions
      attr_accessor :client, :users, :roles, :permissions
    end

    # attr_accessor :subdomain, :identifier, :enabled, :brand_primary_color, :logo, :user_emails, :product_ids, :create_roles_and_permissions, :request_host
    attr_accessor :subdomain, :identifier, :enabled, :user_emails, :create_roles_and_permissions, :request_host

    # validates :subdomain, :identifier, :enabled, :brand_primary_color, :logo, :user_emails, :create_roles_and_permissions, :request_host, presence: true
    validates :subdomain, :identifier, :enabled, :user_emails, :create_roles_and_permissions, :request_host, presence: true
  
    def do_work
      say "Running on environment #{Rails.env}"
      say "Validating input"
      return response unless valid?
      say "Input is valid"
      ActiveRecord::Base.transaction do
        response.client = create_client
        Client.with_client(response.client) do
          # response.client_setting = create_client_setting
          response.roles, response.permissions = create_default_roles_and_permissions if create_roles_and_permissions == true
          response.users = create_users
          # response.licensed_products = create_licensed_products
          raise ActiveRecord::Rollback if errors.any?
          request_user_password_resets(response.users.map(&:email))
        end
      end
      say "Done"
      respond
    end

    private

    def create_client
      say "Creating client", indent: 1
      client = Client.new(client_attributes)
      if client.save
        say "Created #{client.class} #{client}", indent: 2
      else<commit_msg>Fix exception when trying to create a client with an existent identifier.<commit_after>module MultiClient
  class CreateClientService < Itsf::Services::V2::Service::Base
    class Response < Itsf::Services::V2::Response::Base
      # attr_accessor :client, :client_setting, :users, :licensed_products, :roles, :permissions
      attr_accessor :client, :users, :roles, :permissions
    end

    # attr_accessor :subdomain, :identifier, :enabled, :brand_primary_color, :logo, :user_emails, :product_ids, :create_roles_and_permissions, :request_host
    attr_accessor :subdomain, :identifier, :enabled, :user_emails, :create_roles_and_permissions, :request_host

    # validates :subdomain, :identifier, :enabled, :brand_primary_color, :logo, :user_emails, :create_roles_and_permissions, :request_host, presence: true
    validates :subdomain, :identifier, :enabled, :user_emails, :create_roles_and_permissions, :request_host, presence: true
  
    def do_work
      say "Running on environment #{Rails.env}"
      say "Validating input"
      return response unless valid?
      say "Input is valid"
      ActiveRecord::Base.transaction do
        response.client = create_client
        return response unless response.client.persisted?
        Client.with_client(response.client) do
          # response.client_setting = create_client_setting
          response.roles, response.permissions = create_default_roles_and_permissions if create_roles_and_permissions == true
          response.users = create_users
          # response.licensed_products = create_licensed_products
          raise ActiveRecord::Rollback if errors.any?
          request_user_password_resets(response.users.map(&:email))
        end
      end
      say "Done"
      respond
    end

    private

    def create_client
      say "Creating client", indent: 1
      client = Client.new(client_attributes)
      if client.save
        say "Created #{client.class} #{client}", indent: 2
      else
{'fraction_non_alphanumeric': 0.08905185961236249, 'fraction_numerical': 0.0020953378732320588, 'mean_word_length': 3.8773946360153255, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268393', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1090, 'n_words': 262}