From f78eb10c75811c86b49e44a5950242469e5b3948 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 8 Jun 2020 15:17:56 +0200 Subject: [PATCH] use openlab_app_secret from db w/ openlab_ruby gem --- .../api/open_api_clients_controller.rb | 28 +++++++++---------- .../api/openlab_projects_controller.rb | 11 ++++++-- app/models/open_api/calls_count_tracing.rb | 4 +-- app/services/accounting_export_service.rb | 6 ++-- .../api/open_api_clients/create.json.jbuilder | 2 +- .../api/open_api_clients/index.json.jbuilder | 2 +- .../reset_token.json.jbuilder | 2 +- .../api/open_api_clients/update.json.jbuilder | 2 +- .../open_api_trace_calls_count_worker.rb | 2 +- app/workers/openlab_worker.rb | 13 ++++++--- 10 files changed, 42 insertions(+), 30 deletions(-) diff --git a/app/controllers/api/open_api_clients_controller.rb b/app/controllers/api/open_api_clients_controller.rb index 27edc269f..96f41442e 100644 --- a/app/controllers/api/open_api_clients_controller.rb +++ b/app/controllers/api/open_api_clients_controller.rb @@ -11,35 +11,35 @@ class API::OpenAPIClientsController < API::ApiController end def create - @client = OpenAPI::Client.new(client_params) - authorize @client - if @client.save + @projets = OpenAPI::Client.new(client_params) + authorize @projets + if @projets.save render status: :created else - render json: @client.errors, status: :unprocessable_entity + render json: @projets.errors, status: :unprocessable_entity end end def update - @client = OpenAPI::Client.find(params[:id]) - authorize @client - if @client.update(client_params) + @projets = OpenAPI::Client.find(params[:id]) + authorize @projets + if @projets.update(client_params) render status: :ok else - render json: @client.errors, status: :unprocessable_entity + render json: @projets.errors, status: :unprocessable_entity end end def reset_token - @client = OpenAPI::Client.find(params[:id]) - authorize @client - @client.regenerate_token + @projets = OpenAPI::Client.find(params[:id]) + authorize @projets + @projets.regenerate_token end def destroy - @client = OpenAPI::Client.find(params[:id]) - authorize @client - @client.destroy + @projets = OpenAPI::Client.find(params[:id]) + authorize @projets + @projets.destroy head 204 end diff --git a/app/controllers/api/openlab_projects_controller.rb b/app/controllers/api/openlab_projects_controller.rb index 7d36f0fea..d117725c6 100644 --- a/app/controllers/api/openlab_projects_controller.rb +++ b/app/controllers/api/openlab_projects_controller.rb @@ -3,11 +3,18 @@ # API Controller for resources of type Openlab::Projects # Openlab::Projects are Projects shared between different instances class API::OpenlabProjectsController < API::ApiController - PROJECTS = Openlab::Projects.new + before_action :init_openlab def index - render json: PROJECTS.search(params[:q], page: params[:page], per_page: params[:per_page]).response.body + render json: @projets.search(params[:q], page: params[:page], per_page: params[:per_page]).response.body rescue StandardError render json: { errors: ['service unavailable'] } end + + private + + def init_openlab + client = Openlab::Client.new(app_secret: Setting.get('openlab_app_secret')) + @projets = Openlab::Projects.new(client) + end end diff --git a/app/models/open_api/calls_count_tracing.rb b/app/models/open_api/calls_count_tracing.rb index 4b6a3a79b..c54338a12 100644 --- a/app/models/open_api/calls_count_tracing.rb +++ b/app/models/open_api/calls_count_tracing.rb @@ -1,4 +1,4 @@ class OpenAPI::CallsCountTracing < ApplicationRecord - belongs_to :client, foreign_key: :open_api_client_id - validates :client, :at, presence: true + belongs_to :projets, foreign_key: :open_api_client_id + validates :projets, :at, presence: true end diff --git a/app/services/accounting_export_service.rb b/app/services/accounting_export_service.rb index 1e7e502bf..f3986f75a 100644 --- a/app/services/accounting_export_service.rb +++ b/app/services/accounting_export_service.rb @@ -79,8 +79,8 @@ class AccountingExportService invoice.payment_means.each do |details| rows << row( invoice, - account(invoice, :client, means: details[:means]), - account(invoice, :client, means: details[:means], type: :label), + account(invoice, :projets, means: details[:means]), + account(invoice, :projets, means: details[:means], type: :label), details[:amount] / 100.00, line_label: label(invoice), debit_method: :debit_client, @@ -179,7 +179,7 @@ class AccountingExportService # Get the account code (or label) for the given invoice and the specified line type (client, vat, subscription or reservation) def account(invoice, account, type: :code, means: :other) case account - when :client + when :projets Setting.find_by(name: "accounting_#{means}_client_#{type}")&.value when :vat Setting.find_by(name: "accounting_VAT_#{type}")&.value diff --git a/app/views/api/open_api_clients/create.json.jbuilder b/app/views/api/open_api_clients/create.json.jbuilder index 467fb8a7a..2fb8a1927 100644 --- a/app/views/api/open_api_clients/create.json.jbuilder +++ b/app/views/api/open_api_clients/create.json.jbuilder @@ -1 +1 @@ -json.partial! 'api/open_api_clients/client', client: @client +json.partial! 'api/open_api_clients/client', projets: @projets diff --git a/app/views/api/open_api_clients/index.json.jbuilder b/app/views/api/open_api_clients/index.json.jbuilder index c1b98a76b..5007e434b 100644 --- a/app/views/api/open_api_clients/index.json.jbuilder +++ b/app/views/api/open_api_clients/index.json.jbuilder @@ -1,3 +1,3 @@ json.array! @clients do |client| - json.partial! 'api/open_api_clients/client', client: client + json.partial! 'api/open_api_clients/client', projets: client end diff --git a/app/views/api/open_api_clients/reset_token.json.jbuilder b/app/views/api/open_api_clients/reset_token.json.jbuilder index 467fb8a7a..2fb8a1927 100644 --- a/app/views/api/open_api_clients/reset_token.json.jbuilder +++ b/app/views/api/open_api_clients/reset_token.json.jbuilder @@ -1 +1 @@ -json.partial! 'api/open_api_clients/client', client: @client +json.partial! 'api/open_api_clients/client', projets: @projets diff --git a/app/views/api/open_api_clients/update.json.jbuilder b/app/views/api/open_api_clients/update.json.jbuilder index 467fb8a7a..2fb8a1927 100644 --- a/app/views/api/open_api_clients/update.json.jbuilder +++ b/app/views/api/open_api_clients/update.json.jbuilder @@ -1 +1 @@ -json.partial! 'api/open_api_clients/client', client: @client +json.partial! 'api/open_api_clients/client', projets: @projets diff --git a/app/workers/open_api_trace_calls_count_worker.rb b/app/workers/open_api_trace_calls_count_worker.rb index e4bfd52f5..d54569567 100644 --- a/app/workers/open_api_trace_calls_count_worker.rb +++ b/app/workers/open_api_trace_calls_count_worker.rb @@ -4,7 +4,7 @@ class OpenAPITraceCallsCountWorker < Sidekiq::Workers def perform OpenAPI::Client.find_each do |client| - OpenAPI::CallsCountTracing.create!(client: client, calls_count: client.calls_count, at: DateTime.current) + OpenAPI::CallsCountTracing.create!(projets: client, calls_count: client.calls_count, at: DateTime.current) end end end diff --git a/app/workers/openlab_worker.rb b/app/workers/openlab_worker.rb index d19534ef9..173287221 100644 --- a/app/workers/openlab_worker.rb +++ b/app/workers/openlab_worker.rb @@ -6,7 +6,12 @@ class OpenlabWorker sidekiq_options queue: 'default', retry: true LOGGER = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil - OPENLAB_CLIENT = Openlab::Projects.new + + def initialize + client = Openlab::Client.new(app_secret: Setting.get('openlab_app_secret')) + @projets = Openlab::Projects.new(client) + super + end def perform(action, project_id) LOGGER&.debug ['Openlab sync', action, "project ID: #{project_id}"] @@ -14,12 +19,12 @@ class OpenlabWorker case action.to_s when /create/ project = Project.find(project_id) - response = OPENLAB_CLIENT.create(project.openlab_attributes) + response = @projets.create(project.openlab_attributes) when /update/ project = Project.find(project_id) - response = OPENLAB_CLIENT.update(project_id, project.openlab_attributes) + response = @projets.update(project_id, project.openlab_attributes) when /destroy/ - response = OPENLAB_CLIENT.destroy(project_id) + response = @projets.destroy(project_id) else raise NotImplementedError end