diff options
| -rw-r--r-- | app/controllers/api/v1/internals/application_controller.rb | 19 | ||||
| -rw-r--r-- | app/controllers/api/v1/internals/compliance_check_sets_controller.rb | 46 | ||||
| -rw-r--r-- | app/controllers/api/v1/internals/netex_imports_controller.rb | 104 | ||||
| -rw-r--r-- | app/models/compliance_check_set.rb | 7 | ||||
| -rw-r--r-- | config/routes.rb | 7 | ||||
| -rw-r--r-- | db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 1 | 
7 files changed, 186 insertions, 3 deletions
| diff --git a/app/controllers/api/v1/internals/application_controller.rb b/app/controllers/api/v1/internals/application_controller.rb new file mode 100644 index 000000000..2a9f2558a --- /dev/null +++ b/app/controllers/api/v1/internals/application_controller.rb @@ -0,0 +1,19 @@ +module Api +  module V1 +    module Internals +      class ApplicationController < ActionController::Base +        inherit_resources +        respond_to :json +        layout false +        before_action :authenticate + +        private +        def authenticate +          authenticate_with_http_token do |token, options| +            @current_organisation = Api::V1::ApiKey.find_by_token(token).try(:organisation) +          end +        end +      end +    end +  end +end diff --git a/app/controllers/api/v1/internals/compliance_check_sets_controller.rb b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb new file mode 100644 index 000000000..5e9137cb5 --- /dev/null +++ b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb @@ -0,0 +1,46 @@ +module Api +  module V1 +    module Internals +      class ComplianceCheckSetsController < Api::V1::Internals::ApplicationController +        include ControlFlow + +        def validated +          find_compliance_check_set + +          if @compliance_check_set.update_status +            render :validated +          else +            render json: { +              status: "error", +              messages: @compliance_check_set.errors.full_messages +            } +          end +        end + +        def notify_parent +          find_compliance_check_set +          if  @compliance_check_set.notify_parent +            render json: { +              status: "ok", +              message:"#{@compliance_check_set.parent_type} (id: #{@compliance_check_set.parent_id}) successfully notified at #{l(@compliance_check_set.notified_parent_at)}" +            } +          else +            render json: {status: "error", message: @compliance_check_set.errors.full_messages } +          end          +        end + +        private + +        def find_compliance_check_set +          @compliance_check_set = ComplianceCheckSet.find(params[:id]) +        rescue ActiveRecord::RecordNotFound +          render json: { +            status: "error",  +            message: "Record not found" +          } +          finish_action!    +        end +      end +    end +  end +end   diff --git a/app/controllers/api/v1/internals/netex_imports_controller.rb b/app/controllers/api/v1/internals/netex_imports_controller.rb new file mode 100644 index 000000000..c8e33f7b8 --- /dev/null +++ b/app/controllers/api/v1/internals/netex_imports_controller.rb @@ -0,0 +1,104 @@ +module Api +  module V1 +    module Internals +      class NetexImportsController < Api::V1::Internals::ApplicationController +        include ControlFlow + +        def create +          respond_to do | format | +            format.json(&method(:create_models)) +          end +        end + +        def notify_parent +          find_netex_import +          if @netex_import.notify_parent +            render json: { +              status: "ok", +              message:"#{@netex_import.parent_type} (id: #{@netex_import.parent_id}) successfully notified at #{l(@netex_import.notified_parent_at)}" +            } +          else +            render json: {status: "error", message: @netex_import.errors.full_messages } +          end +        end + +        private + +        def find_netex_import +          @netex_import = NetexImport.find(params[:id]) +        rescue ActiveRecord::RecordNotFound +          render json: { +            status: "error",  +            message: "Record not found" +          } +          finish_action!    +        end + +        def find_workbench +          @workbench = Workbench.find(netex_import_params['workbench_id']) +        rescue ActiveRecord::RecordNotFound +          render json: {errors: {'workbench_id' => 'missing'}}, status: 406 +          finish_action! +        end + +        def create_models +          find_workbench +          create_referential +          create_netex_import +        end + +        def create_netex_import +          attributes = netex_import_params.merge creator: "Webservice" + +          attributes = attributes.merge referential_id: @new_referential.id + +          @netex_import = NetexImport.new attributes +          @netex_import.save! + +          unless @netex_import.referential +            Rails.logger.info "Can't create referential for import #{@netex_import.id}: #{@new_referential.inspect} #{@new_referential.metadatas.inspect} #{@new_referential.errors.full_messages}" +            @netex_import.messages.create criticity: :error, message_key: "referential_creation" +          end +        rescue ActiveRecord::RecordInvalid +          render json: {errors: @netex_import.errors}, status: 406 +          finish_action! +        end + +        def create_referential +          @new_referential = +            Referential.new( +              name: netex_import_params['name'], +              organisation_id: @workbench.organisation_id, +              workbench_id: @workbench.id, +              metadatas: [metadata] +            ) +          @new_referential.save +        end + +        def metadata +          metadata = ReferentialMetadata.new + +          if netex_import_params['file'] +            netex_file = STIF::NetexFile.new(netex_import_params['file'].to_io) +            frame = netex_file.frames.first + +            if frame +              metadata.periodes = frame.periods + +              line_objectids = frame.line_refs.map { |ref| "STIF:CODIFLIGNE:Line:#{ref}" } +              metadata.line_ids = @workbench.lines.where(objectid: line_objectids).pluck(:id) +            end +          end + +          metadata +        end + +        def netex_import_params +          params +            .require('netex_import') +            .permit(:file, :name, :workbench_id, :parent_id, :parent_type) +        end +      end +    end +  end +end diff --git a/app/models/compliance_check_set.rb b/app/models/compliance_check_set.rb index d734e5f5a..f440f9b6e 100644 --- a/app/models/compliance_check_set.rb +++ b/app/models/compliance_check_set.rb @@ -19,6 +19,13 @@ class ComplianceCheckSet < ActiveRecord::Base      where('created_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end)    end +  def notify_parent +    if parent +      parent.child_change +      update(notified_parent_at: DateTime.now) +    end +  end +    def update_status      statuses = compliance_check_resources.map do |resource|        case resource.status diff --git a/config/routes.rb b/config/routes.rb index 8ea8168bf..92ff134b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,9 +64,10 @@ ChouetteIhm::Application.routes.draw do        resources :stop_areas, only: [:index, :show]        resources :time_tables, only: [:index, :show]        resources :vehicle_journeys, only: :show -      patch 'compliance_check_sets/:id', -        to: 'compliance_check_sets#validated', -        as: 'compliance_check_set' +      namespace :internals do +        get 'compliance_check_sets/:id/notify_parent', to: 'compliance_check_sets#notify_parent', as: 'compliance_check_set' +        get 'netex_imports/:id/notify_parent', to: 'netex_imports#notify_parent', as: 'netex_imports' +      end      end    end diff --git a/db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb b/db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb new file mode 100644 index 000000000..3bf6b8aa1 --- /dev/null +++ b/db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb @@ -0,0 +1,5 @@ +class AddNotifyParentAtToComplianceCheckSets < ActiveRecord::Migration +  def change +    add_column :compliance_check_sets, :notified_parent_at, :datetime +  end +end diff --git a/db/schema.rb b/db/schema.rb index 4650d8812..c94b43da1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -195,6 +195,7 @@ ActiveRecord::Schema.define(version: 20171128112629) do      t.string   "name"      t.datetime "started_at"      t.datetime "ended_at" +    t.datetime "notified_parent_at"    end    add_index "compliance_check_sets", ["compliance_control_set_id"], name: "index_compliance_check_sets_on_compliance_control_set_id", using: :btree | 
