diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index a4820a002..827e3ac83 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -374,7 +374,7 @@ type Demarche { """ Date de la publication. """ - datePublication: ISO8601DateTime! + datePublication: ISO8601DateTime """ L’état de dossier pour une démarche déclarative @@ -1589,6 +1589,11 @@ type Revision { Date de la création. """ dateCreation: ISO8601DateTime! + + """ + Date de la publication. + """ + datePublication: ISO8601DateTime id: ID! } diff --git a/app/graphql/types/demarche_type.rb b/app/graphql/types/demarche_type.rb index 9cf7b8e98..42aae2d43 100644 --- a/app/graphql/types/demarche_type.rb +++ b/app/graphql/types/demarche_type.rb @@ -24,7 +24,7 @@ module Types field :declarative, DossierDeclarativeState, "L’état de dossier pour une démarche déclarative", null: true, method: :declarative_with_state field :date_creation, GraphQL::Types::ISO8601DateTime, "Date de la création.", null: false, method: :created_at - field :date_publication, GraphQL::Types::ISO8601DateTime, "Date de la publication.", null: false, method: :published_at + field :date_publication, GraphQL::Types::ISO8601DateTime, "Date de la publication.", null: true, method: :published_at field :date_derniere_modification, GraphQL::Types::ISO8601DateTime, "Date de la dernière modification.", null: false, method: :updated_at field :date_depublication, GraphQL::Types::ISO8601DateTime, "Date de la dépublication.", null: true, method: :unpublished_at field :date_fermeture, GraphQL::Types::ISO8601DateTime, "Date de la fermeture.", null: true, method: :closed_at diff --git a/app/graphql/types/revision_type.rb b/app/graphql/types/revision_type.rb index 3f1ef0847..372dbebb2 100644 --- a/app/graphql/types/revision_type.rb +++ b/app/graphql/types/revision_type.rb @@ -2,6 +2,7 @@ module Types class RevisionType < Types::BaseObject global_id_field :id field :date_creation, GraphQL::Types::ISO8601DateTime, "Date de la création.", null: false, method: :created_at + field :date_publication, GraphQL::Types::ISO8601DateTime, "Date de la publication.", null: true, method: :published_at field :champ_descriptors, [Types::ChampDescriptorType], null: false field :annotation_descriptors, [Types::ChampDescriptorType], null: false diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 27ceb25c5..0aa534ad3 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -248,10 +248,10 @@ class Procedure < ApplicationRecord state :close state :depubliee - event :publish, before: :before_publish, after: :after_publish do - transitions from: :brouillon, to: :publiee - transitions from: :close, to: :publiee - transitions from: :depubliee, to: :publiee + event :publish, before: :before_publish do + transitions from: :brouillon, to: :publiee, after: :after_publish + transitions from: :close, to: :publiee, after: :after_republish + transitions from: :depubliee, to: :publiee, after: :after_republish end event :close, after: :after_close do @@ -550,7 +550,7 @@ class Procedure < ApplicationRecord end def whitelist! - update_attribute('whitelisted_at', Time.zone.now) + touch(:whitelisted_at) end def closed_mail_template_attestation_inconsistency_state @@ -693,15 +693,21 @@ class Procedure < ApplicationRecord end def after_publish(canonical_procedure = nil) - update!(published_at: Time.zone.now, canonical_procedure: canonical_procedure, draft_revision: create_new_revision, published_revision: draft_revision) + update!(canonical_procedure: canonical_procedure, draft_revision: create_new_revision, published_revision: draft_revision) + touch(:published_at) + published_revision.touch(:published_at) + end + + def after_republish(canonical_procedure = nil) + touch(:published_at) end def after_close - update!(closed_at: Time.zone.now) + touch(:closed_at) end def after_unpublish - update!(unpublished_at: Time.zone.now) + touch(:unpublished_at) end def update_juridique_required diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index e4da229c8..42c646ee2 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -3,6 +3,7 @@ # Table name: procedure_revisions # # id :bigint not null, primary key +# published_at :datetime # created_at :datetime not null # updated_at :datetime not null # procedure_id :bigint not null diff --git a/db/migrate/20210120121240_add_published_at_to_procedure_revisions.rb b/db/migrate/20210120121240_add_published_at_to_procedure_revisions.rb new file mode 100644 index 000000000..5e1d8de0d --- /dev/null +++ b/db/migrate/20210120121240_add_published_at_to_procedure_revisions.rb @@ -0,0 +1,5 @@ +class AddPublishedAtToProcedureRevisions < ActiveRecord::Migration[6.0] + def change + add_column :procedure_revisions, :published_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 0955c73f6..1f272774e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_05_06_135603) do +ActiveRecord::Schema.define(version: 2021_05_07_135603) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -533,6 +533,7 @@ ActiveRecord::Schema.define(version: 2021_05_06_135603) do t.bigint "procedure_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "published_at" t.index ["procedure_id"], name: "index_procedure_revisions_on_procedure_id" end diff --git a/lib/tasks/deployment/20210525114448_fix_published_revisions.rake b/lib/tasks/deployment/20210525114448_fix_published_revisions.rake new file mode 100644 index 000000000..781c0d9f0 --- /dev/null +++ b/lib/tasks/deployment/20210525114448_fix_published_revisions.rake @@ -0,0 +1,56 @@ +namespace :after_party do + desc 'Deployment task: fix_published_revisions' + task fix_published_revisions: :environment do + puts "Running deploy task 'fix_published_revisions'" + + procedure_ids = Procedure + .with_discarded + .joins(:revisions) + .group('procedures.id') + .having('count(procedure_id) > 2') + .map(&:id) + + # Remove wrongfully created revisions. Every procedure should have only a draft revision + # and a published revision for published procedure + revisions = ProcedureRevision + .joins(:procedure) + .where(procedure_id: procedure_ids) + .where('procedure_revisions.id != procedures.draft_revision_id AND procedure_revisions.id != procedures.published_revision_id') + + dossiers = Dossier.with_discarded.joins(:procedure).where(revision_id: revisions) + progress = ProgressReport.new(dossiers.count) + dossiers.find_each do |dossier| + dossier.update_column(:revision_id, dossier.procedure.published_revision_id) + progress.inc + end + progress.finish + + types_de_champ = TypeDeChamp.joins(:procedure).where(revision_id: revisions) + progress = ProgressReport.new(types_de_champ.count) + types_de_champ.find_each do |type_de_champ| + type_de_champ.update_column(:revision_id, type_de_champ.procedure.published_revision_id) + progress.inc + end + progress.finish + + ProcedureRevisionTypeDeChamp.where(revision_id: revisions).delete_all + revisions.delete_all + + # Fill published_at column on all published revisions + published_revisions = ProcedureRevision + .joins(:procedure) + .where(published_at: nil) + .where('procedure_revisions.id = procedures.published_revision_id') + progress = ProgressReport.new(published_revisions.count) + published_revisions.find_each do |revision| + revision.update_column(:published_at, revision.procedure.published_at) + progress.inc + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index f4101ccc1..e81764862 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -625,6 +625,7 @@ describe Procedure do expect(procedure.draft_revision).not_to be_nil expect(procedure.revisions.count).to eq(2) expect(procedure.revisions).to eq([procedure.published_revision, procedure.draft_revision]) + expect(procedure.published_revision.published_at).to eq(now) end end @@ -649,12 +650,37 @@ describe Procedure do expect(procedure.canonical_procedure).to eq(canonical_procedure) expect(procedure.closed_at).to be_nil expect(procedure.published_at).to eq(now) + expect(procedure.published_revision.published_at).to eq(now) end it 'unpublishes parent procedure' do expect(parent_procedure.unpublished_at).to eq(now) end end + + context 'when republishing a previously closed procedure' do + let(:procedure) { create(:procedure, :published, administrateurs: [administrateur]) } + + before do + procedure.close! + Timecop.freeze(now) do + procedure.publish_or_reopen!(administrateur) + end + end + + it 'changes the procedure state to published' do + expect(procedure.closed_at).to be_nil + expect(procedure.published_at).to eq(now) + expect(procedure.published_revision.published_at).not_to eq(now) + end + + it "doesn't create a new revision" do + expect(procedure.published_revision).not_to be_nil + expect(procedure.draft_revision).not_to be_nil + expect(procedure.revisions.count).to eq(2) + expect(procedure.revisions).to eq([procedure.published_revision, procedure.draft_revision]) + end + end end describe "#unpublish!" do