Skip to content

Instantly share code, notes, and snippets.

@myf9000
Created January 9, 2018 13:58
Show Gist options
  • Save myf9000/dd31a9b43c5ec15352af78957ffeec31 to your computer and use it in GitHub Desktop.
Save myf9000/dd31a9b43c5ec15352af78957ffeec31 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module ProjectApp
module Commands
module UsersProject
class Upsert
include Import["interactors.users_project_repository"]
def call(params)
@params = params
users_project_repository
.users_projects
.dataset
.insert_conflict(opts)
.insert(params)
end
private
attr_reader :params
def opts
{
target: [:project_id, :user_id],
update: {
teams: Sequel.pg_array(processed_teams),
}
}
end
def processed_teams
existed_teams = users_project_repository
.find_by(project_id: params[:project_id], user_id: params[:user_id])
.teams
existed_teams.push(*params[:teams]).uniq
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment