Skip to content

Instantly share code, notes, and snippets.

@fmundaca
Created May 13, 2021 19:53
Show Gist options
  • Save fmundaca/cbd445d722c36f239a07b30233be8a8b to your computer and use it in GitHub Desktop.
Save fmundaca/cbd445d722c36f239a07b30233be8a8b to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Sns
module Subscriptions
class Users
include Sidekiq::Worker
include Helpers
sidekiq_options queue: 'critical'
attr_accessor :params, :message
def perform(params, message)
@params = params
@message = message
return sns if token.nil?
current_tenant!
return if sns.processed
create_or_update_user!
sns.toggle! :processed
end
private
def create_or_update_user!
user.use_service_context(Sns::ServiceName::MONOLITH) do |user|
user.update(build_params_users)
end
end
def sns
@sns = Sns::User.find_or_create_by!(build_params)
end
def user
@user ||= ::User.get_by_cache(message['message']['token']) || ::User.new(token: message['message']['token'])
end
def build_params_users
msg = message['message']
{
name: msg['name'],
lastname: msg['lastname'],
middle_name: msg['middle_name'],
mothers_name: msg['mothers_name'],
email: msg['email'],
identifier: msg['identifier'],
rut: msg['rut'],
token: msg['token'],
birthdate: msg['birthdate'],
joining_date: msg['joining_date'],
gender: gender(msg['gender']),
lang: msg['lang'],
exclude_search: msg['exclude_search'],
phone: msg['phone'],
address: msg['address'],
biography: msg['biography'],
linkedin: msg['linkedin'],
facebook: msg['facebook'],
twitter: msg['twitter'],
instagram: msg['instagram'],
skip_email: msg['skip_email'],
active: msg['active'],
position_id: position(msg['enterprise_position_token']),
subdomain_id: subdomain(msg['subdomain_token']),
avatar: avatar(msg['avatar_url']),
encrypted_password: password(msg['encrypted_password']),
username: msg['username']
}
end
def position(token)
return if token.nil?
::Position.find_by(token: token)&.id
end
def subdomain(token)
return if token.nil?
::Subdomain.find_by(token: token)&.id
end
def gender(gender)
return gender if gender.nil?
::Users::Gender.value_for(gender.strip.upcase) || ::Users::Gender::O
end
def avatar(avatar_url)
return if avatar_url.nil?
download_avatar(avatar_url)
end
def password(password)
return user.encrypted_password if password.nil?
password
end
def download_avatar(avatar_url)
extension = File.extname(avatar_url).split('?')[0]
file_path = "/tmp/#{SecureRandom.uuid}#{extension}"
File.open(file_path, "wb") do |file|
file.write URI.open(avatar_url).read
end
File.open(file_path)
rescue
nil
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment