Merge pull request #6229 from tchak/fix-publishing-revisions

Fix publishing revisions
This commit is contained in:
Paul Chavard 2021-06-03 13:19:46 +02:00 committed by GitHub
commit b4b4e9f0d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 112 additions and 11 deletions

View file

@ -374,7 +374,7 @@ type Demarche {
""" """
Date de la publication. Date de la publication.
""" """
datePublication: ISO8601DateTime! datePublication: ISO8601DateTime
""" """
Létat de dossier pour une démarche déclarative Létat de dossier pour une démarche déclarative
@ -1589,6 +1589,11 @@ type Revision {
Date de la création. Date de la création.
""" """
dateCreation: ISO8601DateTime! dateCreation: ISO8601DateTime!
"""
Date de la publication.
"""
datePublication: ISO8601DateTime
id: ID! id: ID!
} }

View file

@ -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 :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_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_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_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 field :date_fermeture, GraphQL::Types::ISO8601DateTime, "Date de la fermeture.", null: true, method: :closed_at

View file

@ -2,6 +2,7 @@ module Types
class RevisionType < Types::BaseObject class RevisionType < Types::BaseObject
global_id_field :id global_id_field :id
field :date_creation, GraphQL::Types::ISO8601DateTime, "Date de la création.", null: false, method: :created_at 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 :champ_descriptors, [Types::ChampDescriptorType], null: false
field :annotation_descriptors, [Types::ChampDescriptorType], null: false field :annotation_descriptors, [Types::ChampDescriptorType], null: false

View file

@ -248,10 +248,10 @@ class Procedure < ApplicationRecord
state :close state :close
state :depubliee state :depubliee
event :publish, before: :before_publish, after: :after_publish do event :publish, before: :before_publish do
transitions from: :brouillon, to: :publiee transitions from: :brouillon, to: :publiee, after: :after_publish
transitions from: :close, to: :publiee transitions from: :close, to: :publiee, after: :after_republish
transitions from: :depubliee, to: :publiee transitions from: :depubliee, to: :publiee, after: :after_republish
end end
event :close, after: :after_close do event :close, after: :after_close do
@ -550,7 +550,7 @@ class Procedure < ApplicationRecord
end end
def whitelist! def whitelist!
update_attribute('whitelisted_at', Time.zone.now) touch(:whitelisted_at)
end end
def closed_mail_template_attestation_inconsistency_state def closed_mail_template_attestation_inconsistency_state
@ -693,15 +693,21 @@ class Procedure < ApplicationRecord
end end
def after_publish(canonical_procedure = nil) 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 end
def after_close def after_close
update!(closed_at: Time.zone.now) touch(:closed_at)
end end
def after_unpublish def after_unpublish
update!(unpublished_at: Time.zone.now) touch(:unpublished_at)
end end
def update_juridique_required def update_juridique_required

View file

@ -3,6 +3,7 @@
# Table name: procedure_revisions # Table name: procedure_revisions
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# published_at :datetime
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# procedure_id :bigint not null # procedure_id :bigint not null

View file

@ -0,0 +1,5 @@
class AddPublishedAtToProcedureRevisions < ActiveRecord::Migration[6.0]
def change
add_column :procedure_revisions, :published_at, :datetime
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -533,6 +533,7 @@ ActiveRecord::Schema.define(version: 2021_05_06_135603) do
t.bigint "procedure_id", null: false t.bigint "procedure_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.datetime "published_at"
t.index ["procedure_id"], name: "index_procedure_revisions_on_procedure_id" t.index ["procedure_id"], name: "index_procedure_revisions_on_procedure_id"
end end

View file

@ -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

View file

@ -625,6 +625,7 @@ describe Procedure do
expect(procedure.draft_revision).not_to be_nil expect(procedure.draft_revision).not_to be_nil
expect(procedure.revisions.count).to eq(2) expect(procedure.revisions.count).to eq(2)
expect(procedure.revisions).to eq([procedure.published_revision, procedure.draft_revision]) expect(procedure.revisions).to eq([procedure.published_revision, procedure.draft_revision])
expect(procedure.published_revision.published_at).to eq(now)
end end
end end
@ -649,12 +650,37 @@ describe Procedure do
expect(procedure.canonical_procedure).to eq(canonical_procedure) expect(procedure.canonical_procedure).to eq(canonical_procedure)
expect(procedure.closed_at).to be_nil expect(procedure.closed_at).to be_nil
expect(procedure.published_at).to eq(now) expect(procedure.published_at).to eq(now)
expect(procedure.published_revision.published_at).to eq(now)
end end
it 'unpublishes parent procedure' do it 'unpublishes parent procedure' do
expect(parent_procedure.unpublished_at).to eq(now) expect(parent_procedure.unpublished_at).to eq(now)
end end
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 end
describe "#unpublish!" do describe "#unpublish!" do