app/controllers/maestrano/synchronizations_controller.rb in maestrano-connector-rails-1.4.0 vs app/controllers/maestrano/synchronizations_controller.rb in maestrano-connector-rails-1.5.0

- old
+ new

@@ -1,47 +1,69 @@ class Maestrano::SynchronizationsController < Maestrano::Rails::WebHookController def show tenant = params[:tenant] uid = params[:id] organization = Maestrano::Connector::Rails::Organization.find_by_uid_and_tenant(uid, tenant) - return render json: {errors: [{message: "Organization not found", code: 404}]}, status: :not_found unless organization + return render json: {errors: [{message: 'Organization not found', code: 404}]}, status: :not_found unless organization - h = { - group_id: organization.uid, - sync_enabled: organization.sync_enabled - } + status = organization_status organization - last_sync = organization.synchronizations.last - if last_sync - h.merge!( - status: last_sync.status, - message: last_sync.message, - updated_at: last_sync.updated_at - ) - end - - render json: h + render_organization_sync(organization, status, 200) end def create tenant = params[:tenant] uid = params[:group_id] opts = params[:opts] || {} organization = Maestrano::Connector::Rails::Organization.find_by_uid_and_tenant(uid, tenant) - return render json: {errors: [{message: "Organization not found", code: 404}]}, status: :not_found unless organization + return render json: {errors: [{message: 'Organization not found', code: 404}]}, status: :not_found unless organization - Maestrano::Connector::Rails::SynchronizationJob.perform_later(organization, opts.with_indifferent_access) - head :created + status = organization_status(organization) + + unless status == 'RUNNING' || status == 'ENQUEUED' + Maestrano::Connector::Rails::SynchronizationJob.perform_later(organization.id, opts.with_indifferent_access) + status = 'ENQUEUED' + end + + render_organization_sync(organization, status, 201) end def toggle_sync tenant = params[:tenant] uid = params[:group_id] organization = Maestrano::Connector::Rails::Organization.find_by_uid_and_tenant(uid, tenant) - return render json: {errors: [{message: "Organization not found", code: 404}]}, status: :not_found unless organization + return render json: {errors: [{message: 'Organization not found', code: 404}]}, status: :not_found unless organization organization.toggle(:sync_enabled) organization.save - - render json: {sync_enabled: organization.sync_enabled} + status = organization_status organization + render_organization_sync(organization, status, 200) end + + private + + def render_organization_sync(organization, status, code) + h = { + group_id: organization.uid, + sync_enabled: organization.sync_enabled, + status: status + } + last_sync = organization.synchronizations.last + if last_sync + h[:message] = last_sync.message + h[:updated_at] = last_sync.updated_at + end + + render json: h, status: code + end + + def organization_status(organization) + last_sync = organization.synchronizations.last + if Maestrano::Connector::Rails::SynchronizationJob.find_running_job(organization.id) + 'RUNNING' + elsif Maestrano::Connector::Rails::SynchronizationJob.find_job(organization.id) + 'ENQUEUED' + else + last_sync&.status || 'DISABLED' + end + end end