Files
astuto/app/workflows/add_custom_domain_workflow.rb
2024-03-24 12:54:02 +01:00

48 lines
1.2 KiB
Ruby

class AddCustomDomainWorkflow
include HTTParty
attr_accessor :new_custom_domain, :current_custom_domain
def initialize(new_custom_domain: "", current_custom_domain: "")
@new_custom_domain = new_custom_domain
@current_custom_domain = current_custom_domain
end
def make_request(method, domain)
return unless method == "POST" || method == "DELETE"
HTTParty.send(
method.downcase,
ENV["ASTUTO_CNAME_API_URL"],
headers: {
"api_key" => ENV["ASTUTO_CNAME_API_KEY"],
"Accept" => "application/json",
},
query: { "domain" => domain.downcase }
)
end
def run
return true unless Rails.application.multi_tenancy?
return true if @new_custom_domain == @current_custom_domain
return true if Tenant.exists?(custom_domain: @new_custom_domain)
begin
# Add new custom domain...
if @new_custom_domain.present?
response = make_request("POST", @new_custom_domain)
return false unless response.success?
end
# ... and remove the current one
if @current_custom_domain.present?
make_request("DELETE", @current_custom_domain)
end
return true
rescue => e
return false
end
end
end