Merge pull request #2888 from tchak/api-improuvements

API improvements (SIRET, Carte)
This commit is contained in:
Paul Chavard 2018-10-23 10:35:04 +03:00 committed by GitHub
commit 902184fab2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 117 additions and 30 deletions

View file

@ -276,7 +276,7 @@ class Admin::ProceduresController < AdminController
if @procedure&.locked?
params.require(:procedure).permit(*editable_params)
else
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :lien_demarche, :for_individual, :individual_with_siret, :ask_birthday, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id)
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :individual_with_siret, :ask_birthday, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id)
end
end

View file

@ -2,7 +2,7 @@ class API::V1::ProceduresController < APIController
before_action :fetch_procedure_and_check_token
def show
render json: { procedure: ProcedureSerializer.new(@procedure.decorate).as_json }
render json: { procedure: ProcedureSerializer.new(@procedure).as_json }
end
private

View file

@ -22,8 +22,4 @@ class ProcedureDecorator < Draper::Decorator
end
end
end
def geographic_information
module_api_carto
end
end

View file

@ -1,5 +1,5 @@
class Champs::CarteChamp < Champ
has_many :geo_areas, dependent: :destroy
has_many :geo_areas, foreign_key: :champ_id, dependent: :destroy
# We are not using scopes here as we want to access
# the following collections on unsaved records.

View file

@ -0,0 +1,11 @@
class Champs::CarteChampSerializer < ChampSerializer
has_many :geo_areas
def value
if object.value.present?
JSON.parse(object.value)
else
nil
end
end
end

View file

@ -0,0 +1,12 @@
class Champs::SiretChampSerializer < ChampSerializer
has_one :etablissement
has_one :entreprise
def etablissement
object.etablissement
end
def entreprise
object.etablissement&.entreprise
end
end

View file

@ -0,0 +1,16 @@
class GeoAreaSerializer < ActiveModel::Serializer
attributes :geometry,
:source,
:surface_intersection,
:surface_parcelle,
:numero,
:feuille,
:section,
:code_dep,
:nom_com,
:code_com,
:code_arr,
:code,
:nom,
:commune
end

View file

@ -1,6 +1,7 @@
class ProcedureSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attribute :libelle, key: :label
attribute :lien_demarche, key: :link
attributes :id,
:description,
@ -8,10 +9,30 @@ class ProcedureSerializer < ActiveModel::Serializer
:direction,
:archived_at,
:geographic_information,
:total_dossier
:total_dossier,
:link,
:state
has_one :geographic_information, serializer: ModuleApiCartoSerializer
has_many :types_de_champ, serializer: TypeDeChampSerializer
has_many :types_de_champ_private, serializer: TypeDeChampSerializer
has_many :types_de_piece_justificative, serializer: TypeDePieceJustificativeSerializer
def link
if object.path.present?
if object.brouillon_avec_lien?
commencer_test_url(procedure_path: object.path)
else
commencer_url(procedure_path: object.path)
end
end
end
def state
object.aasm_state
end
def geographic_information
object.module_api_carto
end
end

View file

@ -11,7 +11,6 @@ describe Admin::ProceduresController, type: :controller do
let(:description) { 'Description de test' }
let(:organisation) { 'Organisation de test' }
let(:direction) { 'Direction de test' }
let(:lien_demarche) { 'http://localhost.com' }
let(:cadre_juridique) { 'cadre juridique' }
let(:use_api_carto) { '0' }
let(:quartiers_prioritaires) { '0' }
@ -26,7 +25,6 @@ describe Admin::ProceduresController, type: :controller do
description: description,
organisation: organisation,
direction: direction,
lien_demarche: lien_demarche,
cadre_juridique: cadre_juridique,
duree_conservation_dossiers_dans_ds: duree_conservation_dossiers_dans_ds,
duree_conservation_dossiers_hors_ds: duree_conservation_dossiers_hors_ds,
@ -198,7 +196,6 @@ describe Admin::ProceduresController, type: :controller do
it { expect(subject.description).to eq(description) }
it { expect(subject.organisation).to eq(organisation) }
it { expect(subject.direction).to eq(direction) }
it { expect(subject.lien_demarche).to eq(lien_demarche) }
it { expect(subject.administrateur_id).to eq(admin.id) }
it { expect(subject.duree_conservation_dossiers_dans_ds).to eq(duree_conservation_dossiers_dans_ds) }
it { expect(subject.duree_conservation_dossiers_hors_ds).to eq(duree_conservation_dossiers_hors_ds) }
@ -270,7 +267,6 @@ describe Admin::ProceduresController, type: :controller do
let(:description) { 'blabla' }
let(:organisation) { 'plop' }
let(:direction) { 'plap' }
let(:lien_demarche) { 'http://plip.com' }
let(:use_api_carto) { '1' }
let(:cadastre) { '1' }
let(:duree_conservation_dossiers_dans_ds) { 7 }
@ -285,7 +281,6 @@ describe Admin::ProceduresController, type: :controller do
it { expect(subject.description).to eq(description) }
it { expect(subject.organisation).to eq(organisation) }
it { expect(subject.direction).to eq(direction) }
it { expect(subject.lien_demarche).to eq(lien_demarche) }
it { expect(subject.duree_conservation_dossiers_dans_ds).to eq(duree_conservation_dossiers_dans_ds) }
it { expect(subject.duree_conservation_dossiers_hors_ds).to eq(duree_conservation_dossiers_hors_ds) }
end
@ -349,7 +344,6 @@ describe Admin::ProceduresController, type: :controller do
it { expect(subject.organisation).to eq procedure_params[:organisation] }
it { expect(subject.direction).to eq procedure_params[:direction] }
it { expect(subject.lien_demarche).not_to eq procedure_params[:lien_demarche] }
it { expect(subject.for_individual).not_to eq procedure_params[:for_individual] }
it { expect(subject.individual_with_siret).not_to eq procedure_params[:individual_with_siret] }
it { expect(subject.use_api_carto).not_to eq procedure_params[:module_api_carto_attributes][:use_api_carto] }

View file

@ -36,7 +36,6 @@ describe API::V1::ProceduresController, type: :controller do
it { expect(subject[:description]).to eq(procedure.description) }
it { expect(subject[:organisation]).to eq(procedure.organisation) }
it { expect(subject[:direction]).to eq(procedure.direction) }
it { expect(subject[:link]).to eq(procedure.lien_demarche) }
it { expect(subject[:archived_at]).to eq(procedure.archived_at) }
it { expect(subject[:total_dossier]).to eq(procedure.total_dossier) }
it { is_expected.to have_key(:types_de_champ) }

View file

@ -26,11 +26,4 @@ describe ProcedureDecorator do
subject { super().logo_img }
it { is_expected.to match(/http.*#{ActionController::Base.helpers.image_url("marianne.svg")}/) }
end
describe 'geographic_information' do
subject { super().geographic_information }
it { expect(subject.use_api_carto).to be_falsey }
it { expect(subject.quartiers_prioritaires).to be_falsey }
it { expect(subject.cadastre).to be_falsey }
end
end

View file

@ -0,0 +1,7 @@
FactoryBot.define do
factory :geo_area do
source { GeoArea.sources.fetch(:cadastre) }
numero { '42' }
feuille { 'A11' }
end
end

View file

@ -1,7 +1,6 @@
FactoryBot.define do
sequence(:published_path) { |n| "fake_path#{n}" }
factory :procedure do
lien_demarche { 'http://localhost' }
sequence(:libelle) { |n| "Procedure #{n}" }
description { "Demande de subvention à l'intention des associations" }
organisation { "Orga DINSIC" }

View file

@ -164,12 +164,6 @@ describe Procedure do
it { is_expected.to allow_value('Description Demande de subvention').for(:description) }
end
context 'lien_demarche' do
it { is_expected.to allow_value(nil).for(:lien_demarche) }
it { is_expected.to allow_value('').for(:lien_demarche) }
it { is_expected.to allow_value('http://localhost').for(:lien_demarche) }
end
context 'organisation' do
it { is_expected.to allow_value('URRSAF').for(:organisation) }
end

View file

@ -0,0 +1,18 @@
describe Champs::CarteChampSerializer do
describe '#attributes' do
subject { Champs::CarteChampSerializer.new(champ).serializable_hash }
context 'when type champ is carte' do
let(:geo_area) { create(:geo_area) }
let(:champ) { create(:type_de_champ_carte).champ.create(geo_areas: [geo_area]) }
it {
expect(subject[:geo_areas].first).to include(
source: GeoArea.sources.fetch(:cadastre),
numero: '42',
feuille: 'A11'
)
}
end
end
end

View file

@ -0,0 +1,16 @@
describe Champs::SiretChampSerializer do
describe '#attributes' do
subject { Champs::SiretChampSerializer.new(champ).serializable_hash }
context 'when type champ is siret' do
let(:etablissement) { create(:etablissement) }
let(:champ) { create(:type_de_champ_siret).champ.create(etablissement: etablissement, value: etablissement.siret) }
it {
is_expected.to include(value: etablissement.siret)
expect(subject[:etablissement]).to include(siret: etablissement.siret)
expect(subject[:entreprise]).to include(capital_social: etablissement.entreprise_capital_social)
}
end
end
end

View file

@ -0,0 +1,11 @@
describe ProcedureSerializer do
describe '#attributes' do
subject { ProcedureSerializer.new(procedure).serializable_hash }
let(:procedure) { create(:procedure, :published) }
it {
is_expected.to include(link: "http://localhost:3000/commencer/#{procedure.path}")
is_expected.to include(state: "publiee")
}
end
end