From e63fd9d573a8f5ef6db21a484e37b7448bf37828 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 11:17:03 +0100 Subject: [PATCH 01/12] Add migrated flag to module_api_cartos --- .../20181031100150_add_migrated_to_module_api_cartos.rb | 5 +++++ db/schema.rb | 1 + 2 files changed, 6 insertions(+) create mode 100644 db/migrate/20181031100150_add_migrated_to_module_api_cartos.rb diff --git a/db/migrate/20181031100150_add_migrated_to_module_api_cartos.rb b/db/migrate/20181031100150_add_migrated_to_module_api_cartos.rb new file mode 100644 index 000000000..8735ab8e0 --- /dev/null +++ b/db/migrate/20181031100150_add_migrated_to_module_api_cartos.rb @@ -0,0 +1,5 @@ +class AddMigratedToModuleAPICartos < ActiveRecord::Migration[5.2] + def change + add_column :module_api_cartos, :migrated, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 8e642e1f6..b77ed7444 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -408,6 +408,7 @@ ActiveRecord::Schema.define(version: 2018_11_08_151929) do t.boolean "cadastre", default: false t.datetime "created_at" t.datetime "updated_at" + t.boolean "migrated" t.index ["procedure_id"], name: "index_module_api_cartos_on_procedure_id", unique: true end From 44ca6a2d026d60b4c692a39af2d8095ecfa460c0 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 30 Oct 2018 18:26:28 +0100 Subject: [PATCH 02/12] Task to migrate carto to champ carte --- ...20181030155307_migrate_carto_to_carte.rake | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lib/tasks/deployment/20181030155307_migrate_carto_to_carte.rake diff --git a/lib/tasks/deployment/20181030155307_migrate_carto_to_carte.rake b/lib/tasks/deployment/20181030155307_migrate_carto_to_carte.rake new file mode 100644 index 000000000..1b8a43a8f --- /dev/null +++ b/lib/tasks/deployment/20181030155307_migrate_carto_to_carte.rake @@ -0,0 +1,80 @@ +namespace :after_party do + desc 'Deployment task: migrate_carto_to_carte' + task migrate_carto_to_carte: :environment do + def add_champ_carte_if_needed(procedure) + champ_carte = procedure.types_de_champ_ordered.to_a.find do |type_de_champ| + type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:carte) + end + + if champ_carte + puts "Procedure##{procedure.id} already migrated to use champ carte" + else + add_champ_carte(procedure) + end + end + + def add_champ_carte(procedure) + qp = !!procedure.module_api_carto.quartiers_prioritaires + ca = !!procedure.module_api_carto.cadastre + + puts "Creating champ carte on Procedure##{procedure.id} with qp:#{qp} and ca:#{ca}..." + + procedure.types_de_champ.update_all('order_place = order_place + 1') + type_de_champ = procedure.types_de_champ.create( + order_place: 0, + libelle: 'Cartographie', + type_champ: TypeDeChamp.type_champs.fetch(:carte), + quartiers_prioritaires: qp, + cadastres: ca, + mandatory: true + ) + + procedure.dossiers.each do |dossier| + champ = type_de_champ.champ.create(dossier: dossier, value: dossier.json_latlngs) + + if ca && !dossier.cadastres.empty? + puts "Creating Cadastres on Dossier##{dossier.id}..." + dossier.cadastres.each do |cadastre| + champ.geo_areas.create( + source: GeoArea.sources.fetch(:cadastre), + geometry: cadastre.geometry, + surface_intersection: cadastre.surface_intersection, + surface_parcelle: cadastre.surface_parcelle, + numero: cadastre.numero, + feuille: cadastre.feuille, + section: cadastre.section, + code_dep: cadastre.code_dep, + nom_com: cadastre.nom_com, + code_com: cadastre.code_com, + code_arr: cadastre.code_arr + ) + end + end + + if qp && !dossier.quartier_prioritaires.empty? + puts "Creating Quartiers Prioritaires on Dossier##{dossier.id}..." + dossier.quartier_prioritaires.each do |qp| + champ.geo_areas.create( + source: GeoArea.sources.fetch(:quartier_prioritaire), + geometry: qp.geometry, + code: qp.code, + nom: qp.nom, + commune: qp.commune + ) + end + end + end + + procedure.module_api_carto.update(migrated: true) + end + + Procedure.includes(:types_de_champ, dossiers: [:cadastres, :quartier_prioritaires]) + .joins(:module_api_carto) + .where(module_api_cartos: { use_api_carto: true, migrated: nil }) + .find_each do |procedure| + add_champ_carte_if_needed(procedure) + end + + AfterParty::TaskRecord.create version: '20181030155307' + end +end From 2462e42b53a1abf7402d4e498a73c659eaa7304c Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 11:32:41 +0100 Subject: [PATCH 03/12] Stop using legacy api carto after migration --- app/controllers/new_user/dossiers_controller.rb | 2 +- app/models/dossier.rb | 4 ++++ app/models/procedure.rb | 6 ++++-- app/services/user_routes_authorization_service.rb | 2 +- app/views/new_gestionnaire/dossiers/print.html.haml | 2 +- app/views/new_user/dossiers/etablissement.html.haml | 2 +- app/views/shared/dossiers/_demande.html.haml | 2 +- spec/controllers/admin/procedures_controller_spec.rb | 2 +- 8 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index cfd0ee208..1788c4003 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -61,7 +61,7 @@ module NewUser @dossier.update!(autorisation_donnees: true) flash.notice = "Identité enregistrée" - if @dossier.procedure.module_api_carto.use_api_carto + if @dossier.use_legacy_carto? redirect_to users_dossier_carte_path(@dossier.id) else redirect_to brouillon_dossier_path(@dossier) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 69defd6aa..f6efdcdbf 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -245,6 +245,10 @@ class Dossier < ApplicationRecord end end + def use_legacy_carto? + procedure.use_legacy_carto? + end + def user_geometry if json_latlngs.present? UserGeometry.new(json_latlngs) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 8242e67c4..75f69af7b 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -30,8 +30,6 @@ class Procedure < ApplicationRecord has_one_attached :notice has_one_attached :deliberation - delegate :use_api_carto, to: :module_api_carto - accepts_nested_attributes_for :types_de_champ, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true accepts_nested_attributes_for :types_de_piece_justificative, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true accepts_nested_attributes_for :module_api_carto @@ -133,6 +131,10 @@ class Procedure < ApplicationRecord publiee? || archivee? end + def use_legacy_carto? + module_api_carto.use_api_carto? && !module_api_carto.migrated? + end + # Warning: dossier after_save build_default_champs must be removed # to save a dossier created from this method def new_dossier diff --git a/app/services/user_routes_authorization_service.rb b/app/services/user_routes_authorization_service.rb index 9fc85c97b..dd2cbce18 100644 --- a/app/services/user_routes_authorization_service.rb +++ b/app/services/user_routes_authorization_service.rb @@ -3,6 +3,6 @@ class UserRoutesAuthorizationService auth = controller.route_authorization auth[:states].include?(dossier.state) && - (auth[:api_carto].nil? ? true : auth[:api_carto] == dossier.procedure.use_api_carto) + (auth[:api_carto].nil? ? true : auth[:api_carto] == dossier.use_legacy_carto?) end end diff --git a/app/views/new_gestionnaire/dossiers/print.html.haml b/app/views/new_gestionnaire/dossiers/print.html.haml index 3bf327b69..0e9f98214 100644 --- a/app/views/new_gestionnaire/dossiers/print.html.haml +++ b/app/views/new_gestionnaire/dossiers/print.html.haml @@ -17,7 +17,7 @@ - if champs.any? = render partial: "shared/dossiers/champs", locals: { champs: champs, dossier: @dossier, demande_seen_at: nil, profile: 'instructeur' } -- if @dossier.procedure.use_api_carto +- if @dossier.use_legacy_carto? %h3 Cartographie - if @dossier.quartier_prioritaires.any? %h4 Quartiers prioritaires diff --git a/app/views/new_user/dossiers/etablissement.html.haml b/app/views/new_user/dossiers/etablissement.html.haml index 173f40b6e..822d54325 100644 --- a/app/views/new_user/dossiers/etablissement.html.haml +++ b/app/views/new_user/dossiers/etablissement.html.haml @@ -25,7 +25,7 @@ .actions = link_to 'Utiliser un autre numéro SIRET', siret_dossier_path(@dossier), class: 'button' - - if @dossier.procedure.use_api_carto + - if @dossier.use_legacy_carto? / Until the old layout is gone, we need to disable turbolinks / to avoid the map loading twice (once for the turbolinks preview, / once when turbolinks notices the layout are differents and reloads diff --git a/app/views/shared/dossiers/_demande.html.haml b/app/views/shared/dossiers/_demande.html.haml index 9e7e572af..915af81fd 100644 --- a/app/views/shared/dossiers/_demande.html.haml +++ b/app/views/shared/dossiers/_demande.html.haml @@ -15,7 +15,7 @@ .card = render partial: "shared/dossiers/champs", locals: { champs: champs, demande_seen_at: demande_seen_at, profile: profile } - - if dossier.procedure.use_api_carto + - if dossier.use_legacy_carto? .tab-title Cartographie .card = render partial: "shared/dossiers/map", locals: { dossier: dossier } diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index c1ff613e4..bd717ab09 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -346,7 +346,7 @@ describe Admin::ProceduresController, type: :controller do 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] } + it { expect(subject.use_legacy_carto?).not_to eq procedure_params[:module_api_carto_attributes][:use_api_carto] } end end end From 18815e88a3d84c4558ab683439b06f9d11201c10 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 13:27:56 +0100 Subject: [PATCH 04/12] Rename champ.zones to champ.geo_json --- app/models/champs/carte_champ.rb | 4 ++-- app/views/shared/champs/carte/_geo_areas.html.haml | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index 26658adeb..e79241e56 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -43,7 +43,7 @@ class Champs::CarteChamp < Champ end end - def zones - value.blank? ? [] : JSON.parse(value) + def geo_json + @geo_json ||= value.blank? ? [] : JSON.parse(value) end end diff --git a/app/views/shared/champs/carte/_geo_areas.html.haml b/app/views/shared/champs/carte/_geo_areas.html.haml index b2fabe249..03d02e6e8 100644 --- a/app/views/shared/champs/carte/_geo_areas.html.haml +++ b/app/views/shared/champs/carte/_geo_areas.html.haml @@ -6,7 +6,7 @@ - elsif champ.value.blank? Aucune zone tracée - elsif champ.quartiers_prioritaires.blank? - = t('errors.messages.quartiers_prioritaires_empty', count: champ.zones.size) + = t('errors.messages.quartiers_prioritaires_empty', count: champ.geo_json.size) - else %ul - champ.quartiers_prioritaires.each do |qp| @@ -20,7 +20,7 @@ - elsif champ.value.blank? Aucune zone tracée - elsif champ.cadastres.blank? - = t('errors.messages.cadastres_empty', count: champ.zones.size) + = t('errors.messages.cadastres_empty', count: champ.geo_json.size) - else %ul - champ.cadastres.each do |pc| @@ -34,7 +34,7 @@ - elsif champ.value.blank? Aucune zone tracée - elsif champ.parcelles_agricoles.blank? - = t('errors.messages.parcelles_agricoles_empty', count: champ.zones.size) + = t('errors.messages.parcelles_agricoles_empty', count: champ.geo_json.size) - else %ul - champ.parcelles_agricoles.each do |pa| From 971a08c92a3fdb7b5c38f33aa2e6d04a2e994645 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 13:28:39 +0100 Subject: [PATCH 05/12] Add expose legacy carto api flag --- app/models/dossier.rb | 4 ++++ app/models/procedure.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index f6efdcdbf..fa3d11d7d 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -249,6 +249,10 @@ class Dossier < ApplicationRecord procedure.use_legacy_carto? end + def expose_legacy_carto_api? + procedure.expose_legacy_carto_api? + end + def user_geometry if json_latlngs.present? UserGeometry.new(json_latlngs) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 75f69af7b..2ad171a4c 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -135,6 +135,10 @@ class Procedure < ApplicationRecord module_api_carto.use_api_carto? && !module_api_carto.migrated? end + def expose_legacy_carto_api? + module_api_carto.use_api_carto? && module_api_carto.migrated? + end + # Warning: dossier after_save build_default_champs must be removed # to save a dossier created from this method def new_dossier From d92364b90b87eccc9ab986f6bfcb13322a9ae6f1 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 13:29:25 +0100 Subject: [PATCH 06/12] Add user_geo_area do champ carte --- app/models/champs/carte_champ.rb | 20 ++++++++++++++++++++ app/models/geo_area.rb | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index e79241e56..db922d24e 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -46,4 +46,24 @@ class Champs::CarteChamp < Champ def geo_json @geo_json ||= value.blank? ? [] : JSON.parse(value) end + + def user_geometry + { + type: 'Polygon', + coordinates: [ + geo_json[0].map do |polygon| + [polygon['lat'], polygon['lng']] + end + ] + } + end + + def user_geo_area + if geo_json.present? + GeoArea.new( + geometry: user_geometry, + source: GeoArea.sources.fetch(:selection_utilisateur) + ) + end + end end diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index 6d7613d4e..05ad92ebd 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -23,7 +23,8 @@ class GeoArea < ApplicationRecord enum source: { quartier_prioritaire: 'quartier_prioritaire', cadastre: 'cadastre', - parcelle_agricole: 'parcelle_agricole' + parcelle_agricole: 'parcelle_agricole', + selection_utilisateur: 'selection_utilisateur' } scope :quartiers_prioritaires, -> { where(source: sources.fetch(:quartier_prioritaire)) } From 31099b782fc9cba2c1d2cfdef678d0f463f230b5 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 13:30:28 +0100 Subject: [PATCH 07/12] Remove dead code --- app/serializers/dossier_serializer.rb | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index 1e94ac318..8094badd1 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -74,19 +74,4 @@ class DossierSerializer < ActiveModel::Serializer def processed_at object.processed_at&.in_time_zone('UTC') end - - private - - def user_geometry(dossier) - { - value: dossier.geometry, - type_de_champ: { - id: -1, - libelle: 'user_geometry', - type_champ: 'user_geometry', - order_place: -1, - descripton: '' - } - } - end end From fe630847e980d8f598cf0c9c4ca12c811ca3cc08 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 31 Oct 2018 13:32:17 +0100 Subject: [PATCH 08/12] Refactor legacy carto serializer --- app/serializers/dossier_serializer.rb | 17 +++++++++++++---- .../api/v1/dossiers_controller_spec.rb | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index 8094badd1..75f40effb 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -23,11 +23,20 @@ class DossierSerializer < ActiveModel::Serializer has_many :pieces_justificatives has_many :types_de_piece_justificative - has_many :champs do - champs = object.champs + object.quartier_prioritaires + object.cadastres - if object.user_geometry.present? - champs << object.user_geometry + has_many :champs + + def champs + champs = object.champs.to_a + + if object.use_legacy_carto? + champs += object.quartier_prioritaires + champs += object.cadastres + + if object.user_geometry.present? + champs << object.user_geometry + end end + champs end diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index ba2018551..2cd99a95f 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe API::V1::DossiersController do let(:admin) { create(:administrateur) } let(:token) { admin.renew_api_token } - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin) } + let(:procedure) { create(:procedure, :with_api_carto, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin) } let(:wrong_procedure) { create(:procedure) } it { expect(described_class).to be < APIController } From 0b017580c551bf09bc3ef53f23ee5957b4ac9be9 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 6 Nov 2018 14:14:22 +0100 Subject: [PATCH 09/12] Expose migrated legacy carto champs on api --- app/models/user_geometry.rb | 2 +- app/serializers/cadastre_serializer.rb | 17 ------ app/serializers/champ_serializer.rb | 58 ++++++++++++++++++- app/serializers/dossier_serializer.rb | 12 +++- .../quartier_prioritaire_serializer.rb | 17 ------ app/serializers/user_geometry_serializer.rb | 3 - .../api/v1/dossiers_controller_spec.rb | 4 +- spec/models/dossier_spec.rb | 2 +- 8 files changed, 70 insertions(+), 45 deletions(-) delete mode 100644 app/serializers/cadastre_serializer.rb delete mode 100644 app/serializers/quartier_prioritaire_serializer.rb delete mode 100644 app/serializers/user_geometry_serializer.rb diff --git a/app/models/user_geometry.rb b/app/models/user_geometry.rb index 8f5e398aa..74633bc8d 100644 --- a/app/models/user_geometry.rb +++ b/app/models/user_geometry.rb @@ -5,7 +5,7 @@ class UserGeometry @json_latlngs = json_latlngs end - def value + def geometry to_geo_json(@json_latlngs) end diff --git a/app/serializers/cadastre_serializer.rb b/app/serializers/cadastre_serializer.rb deleted file mode 100644 index 193a292a5..000000000 --- a/app/serializers/cadastre_serializer.rb +++ /dev/null @@ -1,17 +0,0 @@ -class CadastreSerializer < ActiveModel::Serializer - attributes :value, :type_de_champ - - def value - object.geometry - end - - def type_de_champ - { - id: -1, - libelle: 'cadastre', - type_champ: 'cadastre', - order_place: -1, - descripton: '' - } - end -end diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb index 454d3b944..c57c185f9 100644 --- a/app/serializers/champ_serializer.rb +++ b/app/serializers/champ_serializer.rb @@ -6,10 +6,62 @@ class ChampSerializer < ActiveModel::Serializer has_one :type_de_champ def value - if object.piece_justificative_file.attached? - url_for(object.piece_justificative_file) + case object + when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire + object.geometry else - object.value + if object.piece_justificative_file.attached? + url_for(object.piece_justificative_file) + else + object.value + end + end + end + + def type_de_champ + case object + when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire + legacy_type_de_champ + else + object.type_de_champ + end + end + + private + + def legacy_type_de_champ + { + id: -1, + libelle: legacy_carto_libelle, + type_champ: legacy_carto_type_champ, + order_place: -1, + descripton: '' + } + end + + def legacy_carto_libelle + case object + when UserGeometry, Cadastre, QuartierPrioritaire + object.class.name.underscore.tr('_', ' ') + else + if object.source == GeoArea.sources.fetch(:selection_utilisateur) + 'user geometry' + else + object.source.to_s.tr('_', ' ') + end + end + end + + def legacy_carto_type_champ + case object + when UserGeometry, Cadastre, QuartierPrioritaire + object.class.name.underscore + else + if object.source == GeoArea.sources.fetch(:selection_utilisateur) + 'user_geometry' + else + object.source.to_s + end end end end diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index 75f40effb..851cf7f62 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -23,7 +23,7 @@ class DossierSerializer < ActiveModel::Serializer has_many :pieces_justificatives has_many :types_de_piece_justificative - has_many :champs + has_many :champs, serializer: ChampSerializer def champs champs = object.champs.to_a @@ -35,6 +35,16 @@ class DossierSerializer < ActiveModel::Serializer if object.user_geometry.present? champs << object.user_geometry end + elsif object.expose_legacy_carto_api? + champ_carte = champs.find do |champ| + champ.type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:carte) + end + + if champ_carte.present? + carto_champs = champ_carte.geo_areas.to_a + carto_champs << champ_carte.user_geo_area + champs += carto_champs.compact + end end champs diff --git a/app/serializers/quartier_prioritaire_serializer.rb b/app/serializers/quartier_prioritaire_serializer.rb deleted file mode 100644 index 0c5c73259..000000000 --- a/app/serializers/quartier_prioritaire_serializer.rb +++ /dev/null @@ -1,17 +0,0 @@ -class QuartierPrioritaireSerializer < ActiveModel::Serializer - attributes :value, :type_de_champ - - def value - object.geometry - end - - def type_de_champ - { - id: -1, - libelle: 'quartier prioritaire', - type_champ: 'quartier_prioritaire', - order_place: -1, - descripton: '' - } - end -end diff --git a/app/serializers/user_geometry_serializer.rb b/app/serializers/user_geometry_serializer.rb deleted file mode 100644 index 964bda894..000000000 --- a/app/serializers/user_geometry_serializer.rb +++ /dev/null @@ -1,3 +0,0 @@ -class UserGeometrySerializer < ActiveModel::Serializer - attributes :value, :type_de_champ -end diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index 2cd99a95f..d5566f0f2 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -290,8 +290,8 @@ describe API::V1::DossiersController do super().find { |champ| champ[:type_de_champ][:type_champ] == 'user_geometry' } end - it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'user_geometry', type_champ: 'user_geometry', order_place: -1, descripton: '' }) } - it { expect(subject[:value]).to match(UserGeometry.new(dossier.json_latlngs).value) } + it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'user geometry', type_champ: 'user_geometry', order_place: -1, descripton: '' }) } + it { expect(subject[:value]).to match(UserGeometry.new(dossier.json_latlngs).geometry) } end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 0562d528f..e923aca40 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -841,7 +841,7 @@ describe Dossier do } end - subject{ dossier.user_geometry.value } + subject{ dossier.user_geometry.geometry } it { is_expected.to eq(expected) } end From e374a5c7262ac8aa83b1cc65f838f3b54321c097 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 13 Nov 2018 18:48:04 +0100 Subject: [PATCH 10/12] Refactor ChampSerializer --- app/serializers/champ_serializer.rb | 42 +++++++++++++++++-- .../champs/carte_champ_serializer.rb | 11 ----- .../champs/decimal_number_champ_serializer.rb | 7 ---- .../champs/integer_number_champ_serializer.rb | 7 ---- .../linked_drop_down_list_champ_serializer.rb | 5 --- .../champs/siret_champ_serializer.rb | 12 ------ spec/factories/champ.rb | 37 ++++++++++++++++ spec/serializers/champ_serializer_spec.rb | 42 ++++++++++++++++++- .../champs/carte_champ_serializer_spec.rb | 34 --------------- .../champs/siret_champ_serializer_spec.rb | 16 ------- spec/serializers/dossier_serializer_spec.rb | 31 ++++++++++++++ 11 files changed, 148 insertions(+), 96 deletions(-) delete mode 100644 app/serializers/champs/carte_champ_serializer.rb delete mode 100644 app/serializers/champs/decimal_number_champ_serializer.rb delete mode 100644 app/serializers/champs/integer_number_champ_serializer.rb delete mode 100644 app/serializers/champs/linked_drop_down_list_champ_serializer.rb delete mode 100644 app/serializers/champs/siret_champ_serializer.rb delete mode 100644 spec/serializers/champs/carte_champ_serializer_spec.rb delete mode 100644 spec/serializers/champs/siret_champ_serializer_spec.rb diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb index c57c185f9..2638ce622 100644 --- a/app/serializers/champ_serializer.rb +++ b/app/serializers/champ_serializer.rb @@ -5,16 +5,36 @@ class ChampSerializer < ActiveModel::Serializer has_one :type_de_champ + has_many :geo_areas, if: :include_geo_areas? + has_one :etablissement, if: :include_etablissement? + has_one :entreprise, if: :include_etablissement? + def value case object when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire object.geometry - else + when Champs::CarteChamp + if object.value.present? + JSON.parse(object.value) + end + when Champs::DecimalNumberChamp + if object.value.present? + object.value.to_f + end + when Champs::IntegerNumberChamp + if object.value.present? + object.value.to_i + end + when Champs::LinkedDropDownListChamp + if object.value.present? + { primary: object.primary_value, secondary: object.secondary_value } + end + when Champs::PieceJustificativeChamp if object.piece_justificative_file.attached? url_for(object.piece_justificative_file) - else - object.value end + else + object.value end end @@ -27,6 +47,22 @@ class ChampSerializer < ActiveModel::Serializer end end + def etablissement + object.etablissement + end + + def entreprise + object.etablissement&.entreprise + end + + def include_etablissement? + object.is_a?(Champs::SiretChamp) + end + + def include_geo_areas? + object.is_a?(Champs::CarteChamp) + end + private def legacy_type_de_champ diff --git a/app/serializers/champs/carte_champ_serializer.rb b/app/serializers/champs/carte_champ_serializer.rb deleted file mode 100644 index da41b34f9..000000000 --- a/app/serializers/champs/carte_champ_serializer.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Champs::CarteChampSerializer < ChampSerializer - has_many :geo_areas - - def value - if object.value.present? - JSON.parse(object.value) - else - nil - end - end -end diff --git a/app/serializers/champs/decimal_number_champ_serializer.rb b/app/serializers/champs/decimal_number_champ_serializer.rb deleted file mode 100644 index 987732ec5..000000000 --- a/app/serializers/champs/decimal_number_champ_serializer.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Champs::DecimalNumberChampSerializer < ChampSerializer - def value - if object.value.present? - object.value.to_f - end - end -end diff --git a/app/serializers/champs/integer_number_champ_serializer.rb b/app/serializers/champs/integer_number_champ_serializer.rb deleted file mode 100644 index ac1e03494..000000000 --- a/app/serializers/champs/integer_number_champ_serializer.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Champs::IntegerNumberChampSerializer < ChampSerializer - def value - if object.value.present? - object.value.to_i - end - end -end diff --git a/app/serializers/champs/linked_drop_down_list_champ_serializer.rb b/app/serializers/champs/linked_drop_down_list_champ_serializer.rb deleted file mode 100644 index 01d91cb84..000000000 --- a/app/serializers/champs/linked_drop_down_list_champ_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Champs::LinkedDropDownListChampSerializer < ChampSerializer - def value - { primary: object.primary_value, secondary: object.secondary_value } - end -end diff --git a/app/serializers/champs/siret_champ_serializer.rb b/app/serializers/champs/siret_champ_serializer.rb deleted file mode 100644 index 9bb54ce4d..000000000 --- a/app/serializers/champs/siret_champ_serializer.rb +++ /dev/null @@ -1,12 +0,0 @@ -class Champs::SiretChampSerializer < ChampSerializer - has_one :etablissement - has_one :entreprise - - def etablissement - object.etablissement - end - - def entreprise - object.etablissement&.entreprise - end -end diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 41553b5b6..f83c2aff7 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -28,4 +28,41 @@ FactoryBot.define do end end end + + factory :champ_integer_number, class: 'Champs::IntegerNumberChamp' do + type_de_champ { create(:type_de_champ_integer_number) } + value { '42' } + end + + factory :champ_decimal_number, class: 'Champs::DecimalNumberChamp' do + type_de_champ { create(:type_de_champ_decimal_number) } + value { '42.1' } + end + + factory :champ_linked_drop_down_list, class: 'Champs::LinkedDropDownListChamp' do + type_de_champ { create(:type_de_champ_linked_drop_down_list) } + value { '{}' } + end + + factory :champ_carte, class: 'Champs::CarteChamp' do + type_de_champ { create(:type_de_champ_carte) } + end + + factory :champ_siret, class: 'Champs::SiretChamp' do + type_de_champ { create(:type_de_champ_siret) } + value { '44011762001530' } + etablissement { create(:etablissement) } + + before(:create) do |champ, evaluator| + champ.etablissement.signature = champ.etablissement.sign + end + end + + factory :champ_piece_justificative, class: 'Champs::PieceJustificativeChamp' do + type_de_champ { create(:type_de_champ_piece_justificative) } + + after(:create) do |champ, evaluator| + champ.piece_justificative_file.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain") + end + end end diff --git a/spec/serializers/champ_serializer_spec.rb b/spec/serializers/champ_serializer_spec.rb index 96b8ce784..840f79516 100644 --- a/spec/serializers/champ_serializer_spec.rb +++ b/spec/serializers/champ_serializer_spec.rb @@ -5,7 +5,7 @@ describe ChampSerializer do context 'when type champ is piece justificative' do include Rails.application.routes.url_helpers - let(:champ) { create(:champ, type_de_champ: create(:type_de_champ_piece_justificative)) } + let(:champ) { create(:champ_piece_justificative) } before { champ.piece_justificative_file.attach({ filename: __FILE__, io: File.open(__FILE__) }) } after { champ.piece_justificative_file.purge } @@ -18,5 +18,45 @@ describe ChampSerializer do it { is_expected.to include(value: "blah") } end + + 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]) } + + context 'and geo_area is cadastre' do + it { + expect(subject[:geo_areas].first).to include( + source: GeoArea.sources.fetch(:cadastre), + numero: '42', + feuille: 'A11' + ) + expect(subject[:geo_areas].first.key?(:nom)).to be_falsey + } + end + + context 'and geo_area is quartier_prioritaire' do + let(:geo_area) { create(:geo_area, :quartier_prioritaire) } + + it { + expect(subject[:geo_areas].first).to include( + source: GeoArea.sources.fetch(:quartier_prioritaire), + nom: 'XYZ', + commune: 'Paris' + ) + expect(subject[:geo_areas].first.key?(:numero)).to be_falsey + } + end + end + + 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 diff --git a/spec/serializers/champs/carte_champ_serializer_spec.rb b/spec/serializers/champs/carte_champ_serializer_spec.rb deleted file mode 100644 index 043fa3bc6..000000000 --- a/spec/serializers/champs/carte_champ_serializer_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -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]) } - - context 'and geo_area is cadastre' do - it { - expect(subject[:geo_areas].first).to include( - source: GeoArea.sources.fetch(:cadastre), - numero: '42', - feuille: 'A11' - ) - expect(subject[:geo_areas].first.key?(:nom)).to be_falsey - } - end - - context 'and geo_area is quartier_prioritaire' do - let(:geo_area) { create(:geo_area, :quartier_prioritaire) } - - it { - expect(subject[:geo_areas].first).to include( - source: GeoArea.sources.fetch(:quartier_prioritaire), - nom: 'XYZ', - commune: 'Paris' - ) - expect(subject[:geo_areas].first.key?(:numero)).to be_falsey - } - end - end - end -end diff --git a/spec/serializers/champs/siret_champ_serializer_spec.rb b/spec/serializers/champs/siret_champ_serializer_spec.rb deleted file mode 100644 index 1614dc7bd..000000000 --- a/spec/serializers/champs/siret_champ_serializer_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -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 diff --git a/spec/serializers/dossier_serializer_spec.rb b/spec/serializers/dossier_serializer_spec.rb index 4cf4b398d..e16e6f84e 100644 --- a/spec/serializers/dossier_serializer_spec.rb +++ b/spec/serializers/dossier_serializer_spec.rb @@ -14,5 +14,36 @@ describe DossierSerializer do it { is_expected.to include(received_at: dossier.en_instruction_at) } end + + context 'champs' do + subject { super()[:champs] } + + let(:dossier) { create(:dossier, :en_construction, :with_two_quartier_prioritaires, procedure: create(:procedure, :published, :with_api_carto, :with_type_de_champ)) } + + before do + dossier.champs << create(:champ_carte) + dossier.champs << create(:champ_siret) + dossier.champs << create(:champ_integer_number) + dossier.champs << create(:champ_decimal_number) + dossier.champs << create(:champ_linked_drop_down_list) + end + + it { + expect(subject.size).to eq(8) + + expect(subject[0][:type_de_champ][:type_champ]).to eq(TypeDeChamp.type_champs.fetch(:text)) + expect(subject[1][:type_de_champ][:type_champ]).to eq(TypeDeChamp.type_champs.fetch(:carte)) + expect(subject[2][:type_de_champ][:type_champ]).to eq(TypeDeChamp.type_champs.fetch(:siret)) + expect(subject[7][:type_de_champ][:type_champ]).to eq('quartier_prioritaire') + + expect(subject[1][:geo_areas].size).to eq(0) + expect(subject[2][:etablissement]).to be_present + expect(subject[2][:entreprise]).to be_present + + expect(subject[3][:value]).to eq(42) + expect(subject[4][:value]).to eq(42.1) + expect(subject[5][:value]).to eq({ primary: nil, secondary: nil }) + } + end end end From 864d1df413e64754cc679775392b975ad2281a10 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 15 Nov 2018 18:17:17 +0100 Subject: [PATCH 11/12] Test carto migration task --- .../2018_10_30_migrate_carto_to_carte_spec.rb | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spec/lib/tasks/2018_10_30_migrate_carto_to_carte_spec.rb diff --git a/spec/lib/tasks/2018_10_30_migrate_carto_to_carte_spec.rb b/spec/lib/tasks/2018_10_30_migrate_carto_to_carte_spec.rb new file mode 100644 index 000000000..8b7702eb5 --- /dev/null +++ b/spec/lib/tasks/2018_10_30_migrate_carto_to_carte_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe '2018_10_30_migrate_carto_to_carte' do + let(:rake_task) { Rake::Task['after_party:migrate_carto_to_carte'] } + let(:procedure) { create(:procedure, :published, :with_api_carto) } + let(:dossier) { create(:dossier, :with_two_quartier_prioritaires, :with_two_cadastres) } + + def run_task + procedure.module_api_carto.quartiers_prioritaires = true + procedure.module_api_carto.cadastre = true + procedure.module_api_carto.save + procedure.dossiers << dossier + + rake_task.invoke + procedure.reload + dossier.reload + end + + after { rake_task.reenable } + + context 'on happy path' do + before do + run_task + end + + it { + expect(procedure.module_api_carto.migrated?).to be_truthy + expect(dossier.cadastres.count).to eq(2) + expect(dossier.quartier_prioritaires.count).to eq(2) + expect(dossier.champs.first.type_champ).to eq('carte') + expect(dossier.champs.first.order_place).to eq(0) + expect(dossier.champs.first.libelle).to eq('Cartographie') + expect(dossier.champs.first.geo_areas.count).to eq(4) + expect(dossier.champs.first.mandatory?).to be_truthy + expect(dossier.champs.first.cadastres?).to be_truthy + expect(dossier.champs.first.quartiers_prioritaires?).to be_truthy + } + end +end From d43068ace5644e6679b51e19598d13d0883fec79 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 20 Nov 2018 11:20:10 +0100 Subject: [PATCH 12/12] Disable carto for new procedures --- .../admin/procedures_controller.rb | 6 +---- .../admin/procedures/_informations.html.haml | 24 ------------------- .../admin/procedures_controller_spec.rb | 12 ---------- 3 files changed, 1 insertion(+), 41 deletions(-) diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 5002e4cc2..973776036 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -66,7 +66,7 @@ class Admin::ProceduresController < AdminController @procedure = Procedure.new(procedure_params) if @procedure.valid? - @procedure.module_api_carto = ModuleAPICarto.new(create_module_api_carto_params) + @procedure.module_api_carto = ModuleAPICarto.new end @path = @procedure.path @@ -271,8 +271,4 @@ class Admin::ProceduresController < AdminController 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 - - def create_module_api_carto_params - params.require(:procedure).require(:module_api_carto_attributes).permit(:id, :use_api_carto, :quartiers_prioritaires, :cadastre) - end end diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 61fe6c136..751a0f5e5 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -119,30 +119,6 @@ Afficher le drapeau européen - if !@procedure.locked? - .row - .col-md-6 - %h4 Cartographie - - = f.fields_for :module_api_carto, @procedure.module_api_carto do |ff| - .checkbox - %label - = ff.check_box :use_api_carto, id: "procedure-module-api-carto-use-api-carto" - Utilisation de la cartographie - - %ul#modules-api-carto - %li - .checkbox - %label - = ff.check_box :quartiers_prioritaires - Quartiers prioritaires - %li - .checkbox - %label - = ff.check_box :cadastre - Cadastre - - .col-md-6 - .row .col-md-6 %h4 Particuliers diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index bd717ab09..b755f0a6f 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -201,18 +201,6 @@ describe Admin::ProceduresController, type: :controller do it { expect(subject.duree_conservation_dossiers_hors_ds).to eq(duree_conservation_dossiers_hors_ds) } end - describe 'procedure module api carto attributs in database' do - let(:procedure) { Procedure.last } - let(:use_api_carto) { '1' } - let(:quartiers_prioritaires) { '1' } - - subject { ModuleAPICarto.last } - - it { expect(subject.procedure).to eq(procedure) } - it { expect(subject.use_api_carto).to be_truthy } - it { expect(subject.quartiers_prioritaires).to be_truthy } - end - it { is_expected.to redirect_to(admin_procedure_types_de_champ_path(procedure_id: Procedure.last.id)) } it { expect(flash[:notice]).to be_present }