Sha256: 18f24e1c108f0caaa9239781a5063775f8e7ce99b43aab17f78767c9a00bc72d

Contents?: true

Size: 1.88 KB

Versions: 28

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require_relative '../utils/query_builder'

module FinApps
  module REST
    class Operators < FinAppsCore::REST::Resources
      include FinApps::Utils::QueryBuilder

      def list(params = nil)
        return super if params.nil?
        raise FinAppsCore::InvalidArgumentsError, 'Invalid argument: params' unless params.is_a? Hash

        super build_query_path(end_point, params)
      end

      def show(id)
        not_blank(id, :operator_id)

        super id
      end

      def create(params, path = nil)
        not_blank(params, :params)
        super params, path
      end

      def update(id, params)
        not_blank(id, :operator_id)
        not_blank(params, :params)

        path = "#{end_point}/#{ERB::Util.url_encode(id)}"
        super params, path
      end

      def update_password(params)
        # update password for current operator, need authorization session in header
        not_blank(params, :params)
        raise FinAppsCore::InvalidArgumentsError, 'Invalid argument: params.' unless validates params

        path = "#{end_point}/password/change"

        create params, path
      end

      def destroy(id)
        not_blank(id, :operator_id)
        super id
      end

      private

      def validates(params)
        params.key?(:password) && params[:password] &&
          params.key?(:password_confirm) &&
          params[:password_confirm]
      end

      def build_filter(params)
        filter = {}
        filter.merge!(search_query(params[:searchTerm])) if params[:searchTerm]
        filter.merge!(role_query(params[:role])) if params[:role]
        filter
      end

      def search_query(term)
        { "last_name": term }
      end

      def role_query(role)
        if role.is_a?(Array)
          { "role": { "$in": role.map(&:to_i) } }
        else
          { "role": role.to_i }
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
finapps-5.0.32 lib/finapps/rest/operators.rb
finapps-5.0.31 lib/finapps/rest/operators.rb
finapps-5.0.30 lib/finapps/rest/operators.rb
finapps-5.0.29 lib/finapps/rest/operators.rb
finapps-5.0.28 lib/finapps/rest/operators.rb
finapps-5.0.27 lib/finapps/rest/operators.rb
finapps-5.0.26 lib/finapps/rest/operators.rb
finapps-5.0.25 lib/finapps/rest/operators.rb
finapps-5.0.24 lib/finapps/rest/operators.rb
finapps-5.0.23 lib/finapps/rest/operators.rb
finapps-5.0.22 lib/finapps/rest/operators.rb
finapps-5.0.21 lib/finapps/rest/operators.rb
finapps-5.0.20 lib/finapps/rest/operators.rb
finapps-5.0.19 lib/finapps/rest/operators.rb
finapps-5.0.17 lib/finapps/rest/operators.rb
finapps-5.0.16 lib/finapps/rest/operators.rb
finapps-5.0.15 lib/finapps/rest/operators.rb
finapps-5.0.14 lib/finapps/rest/operators.rb
finapps-5.0.13 lib/finapps/rest/operators.rb
finapps-5.0.12 lib/finapps/rest/operators.rb