2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class CustomerUser
|
|
|
|
include Import::Helper
|
|
|
|
include Import::OTRS::Helper
|
|
|
|
|
|
|
|
MAPPING = {
|
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
UserComment: :note,
|
|
|
|
UserEmail: :email,
|
|
|
|
UserFirstname: :firstname,
|
|
|
|
UserLastname: :lastname,
|
|
|
|
UserLogin: :login,
|
|
|
|
UserPassword: :password,
|
|
|
|
UserPhone: :phone,
|
|
|
|
UserFax: :fax,
|
|
|
|
UserMobile: :mobile,
|
|
|
|
UserStreet: :street,
|
|
|
|
UserZip: :zip,
|
|
|
|
UserCity: :city,
|
|
|
|
UserCountry: :country,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def initialize(customer)
|
|
|
|
import(customer)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import(customer)
|
|
|
|
create_or_update(map(customer))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update(customer)
|
|
|
|
return if updated?(customer)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
create(customer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated?(customer)
|
|
|
|
@local_customer = ::User.find_by(login: customer[:login])
|
|
|
|
return false if !@local_customer
|
|
|
|
|
|
|
|
# do not update user if it is already agent
|
|
|
|
return true if @local_customer.role_ids.include?(Role.find_by(name: 'Agent').id)
|
|
|
|
|
|
|
|
# only update roles if different (reduce sql statements)
|
|
|
|
if @local_customer.role_ids == customer[:role_ids]
|
2016-12-12 10:46:48 +00:00
|
|
|
customer.delete(:role_ids)
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
log "update User.find_by(login: #{customer[:login]})"
|
2017-09-11 11:16:08 +00:00
|
|
|
@local_customer.update!(customer)
|
2016-11-25 16:10:37 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(customer)
|
|
|
|
log "add User.find_by(login: #{customer[:login]})"
|
|
|
|
@local_customer = ::User.new(customer)
|
|
|
|
@local_customer.save
|
|
|
|
reset_primary_key_sequence('users')
|
|
|
|
end
|
|
|
|
|
|
|
|
def map(customer)
|
2016-12-16 10:12:15 +00:00
|
|
|
mapped = map_default(customer)
|
|
|
|
mapped[:created_at] ||= DateTime.current
|
|
|
|
mapped[:updated_at] ||= DateTime.current
|
2017-05-14 16:33:30 +00:00
|
|
|
mapped[:email].downcase!
|
2017-11-24 16:49:40 +00:00
|
|
|
mapped[:login].downcase!
|
2016-12-16 10:12:15 +00:00
|
|
|
mapped
|
|
|
|
end
|
|
|
|
|
|
|
|
def map_default(customer)
|
2016-11-25 16:10:37 +00:00
|
|
|
{
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
active: active?(customer),
|
|
|
|
source: 'OTRS Import',
|
|
|
|
organization_id: organization_id(customer),
|
|
|
|
role_ids: role_ids,
|
|
|
|
}
|
|
|
|
.merge(from_mapping(customer))
|
|
|
|
end
|
|
|
|
|
|
|
|
def role_ids
|
|
|
|
[
|
|
|
|
Role.find_by(name: 'Customer').id
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def organization_id(customer)
|
|
|
|
return if !customer['UserCustomerID']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
organization = Import::OTRS::Customer.by_customer_id(customer['UserCustomerID'])
|
|
|
|
return if !organization
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
organization.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|