Created
November 30, 2017 12:34
-
-
Save myf9000/29010b1ad9d12f2db6bef4c966a90bb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Slack | |
module UseCases | |
module Users | |
class Update | |
include Import[user_repository: "repositories.user_repository"] | |
def initialize( | |
slack_api: ::Slack::Services::SlackApi, | |
create_command: ::Slack::Commands::Create, | |
archive_command: ::Slack::Commands::Archive | |
) | |
@slack_api = slack_api | |
@create_command = create_command | |
@archive_command = archive_command | |
end | |
def call | |
@db_users = user_repository.scope_active.to_a | |
@slack_users = slack_api.new.fetch_users | |
manage_users | |
rescue slack_api::SlackCustomError | |
nil | |
end | |
private | |
attr_reader :slack_users, :db_users, :slack_api, :create_command, :archive_command | |
def manage_users | |
db_users_emails = db_users.map(&:email) | |
slack_users_emails = slack_users.map { |user| user[:email] } | |
intersection = db_users_emails & slack_users_emails | |
new_users_emails = slack_users_emails - intersection | |
old_users_emails = db_users_emails - intersection | |
add_users(new_users_emails) | |
delete_users(old_users_emails) | |
end | |
def add_users(users_emails) | |
users_emails.each do |email| | |
params = slack_users.find { |user| user[:email] == email } | |
create_command.new.call(params) | |
end | |
end | |
def delete_users(users_emails) | |
users_emails.each { |email| archive_command.new.call(email) } | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment