# frozen_string_literal: true module Alula class User < Alula::RestResource extend Alula::ResourceAttributes extend Alula::RelationshipAttributes extend Alula::ApiOperations::Request extend Alula::ApiOperations::List extend Alula::ApiOperations::Save resource_path 'users' type 'users' relationship :devices, type: 'devices', cardinality: 'To-many' relationship :device_notifications, type: 'devices-notifications', cardinality: 'To-many' relationship :dealer, type: 'dealers', cardinality: 'To-one' relationship :phone, type: 'users-phones', cardinality: 'To-one' relationship :address, type: 'users-addresses', cardinality: 'To-one' relationship :pushtokens, type: 'users-pushtokens', cardinality: 'To-many' relationship :preferences, type: 'users-preferences', cardinality: 'To-one' relationship :users_video_profile, type: 'users-videoprofiles', cardinality: 'To-many' # Resource Fields # Not all params are used at the moment. See Alula::ResourceAttributes for details # on how params are parsed, field :id, type: :string, sortable: true, filterable: true, creatable_by: [], patchable_by: [] field :username, type: :string, sortable: true, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: %i[system station dealer technician user sub_user] field :password, type: :string, sortable: false, filterable: false, creatable_by: %i[system station dealer technician user], patchable_by: [] field :u_type, type: :string, sortable: false, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: [] field :user_type, type: :string, sortable: false, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: [], symbolize: true field :u_level, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :parent_id, type: :string, sortable: false, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: [] field :dealer_id, type: :string, sortable: false, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: [] field :station_id, type: :string, sortable: false, filterable: true, creatable_by: [:system], patchable_by: [] field :last_login, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :date_entered, type: :date, sortable: true, filterable: true, creatable_by: [], patchable_by: [] field :date_modified, type: :date, sortable: true, filterable: true, creatable_by: [], patchable_by: [] field :entered_by, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :modified_by, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :pwch, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :features_selected, type: :object, sortable: false, filterable: false, creatable_by: %i[system station dealer technician], patchable_by: %i[system station dealer technician] field :eula, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: %i[system station dealer technician user sub_user] field :language, type: :string, sortable: false, filterable: false, creatable_by: %i[system station dealer technician user sub_user], patchable_by: %i[system station dealer technician user sub_user] field :def_mod, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :rg_activated, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :company_name, type: :string, sortable: true, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: %i[system station dealer technician user sub_user] field :name_first, type: :string, sortable: true, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: %i[system station dealer technician user sub_user] field :name_last, type: :string, sortable: true, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: %i[system station dealer technician user sub_user] field :email, type: :string, sortable: true, filterable: true, creatable_by: %i[system station dealer technician user], patchable_by: %i[system station dealer technician user sub_user] field :time_offset, type: :string, sortable: false, filterable: false, creatable_by: [], patchable_by: [] field :timezone, type: :string, sortable: false, filterable: false, creatable_by: %i[system station dealer technician user], patchable_by: %i[system station dealer technician user sub_user] field :hidden, type: :string, sortable: false, filterable: true, creatable_by: [], patchable_by: %i[system] def hidden? hidden == '1' end def user_role # pre-alula api users will be u_type 32, newer ones are 96. need to differentiate # between account users and account owners by checking if they own themselves { 2 => :system, 4 => :station, 8 => :dealer, 16 => :technician, 32 => :user, 64 => :sub_user, 96 => :account_user, 320 => :local_access_user }[(u_type == 96 && parent_id == id ? 32 : u_type)] end def user_role_name { system: 'System', station: 'Station', dealer: 'Dealer', technician: 'Technician', user: 'Account Owner', sub_user: 'Sub-User', account_user: 'Account User', local_access_user: 'Local Access User' }[user_role] end end end