Add merge log

This commit is contained in:
simon lehericey 2021-10-26 14:41:30 +02:00
parent 3328c9a132
commit 7935e42109
5 changed files with 45 additions and 1 deletions

14
app/models/merge_log.rb Normal file
View file

@ -0,0 +1,14 @@
# == Schema Information
#
# Table name: merge_logs
#
# id :bigint not null, primary key
# from_user_email :string not null
# created_at :datetime not null
# updated_at :datetime not null
# from_user_id :bigint not null
# user_id :bigint not null
#
class MergeLog < ApplicationRecord
belongs_to :user
end

View file

@ -47,6 +47,7 @@ class User < ApplicationRecord
has_many :invites, dependent: :destroy has_many :invites, dependent: :destroy
has_many :dossiers_invites, through: :invites, source: :dossier has_many :dossiers_invites, through: :invites, source: :dossier
has_many :deleted_dossiers has_many :deleted_dossiers
has_many :merge_logs, dependent: :destroy
has_one :france_connect_information, dependent: :destroy has_one :france_connect_information, dependent: :destroy
belongs_to :instructeur, optional: true, dependent: :destroy belongs_to :instructeur, optional: true, dependent: :destroy
belongs_to :administrateur, optional: true, dependent: :destroy belongs_to :administrateur, optional: true, dependent: :destroy
@ -197,6 +198,7 @@ class User < ApplicationRecord
def merge(old_user) def merge(old_user)
old_user.dossiers.update_all(user_id: id) old_user.dossiers.update_all(user_id: id)
old_user.invites.update_all(user_id: id) old_user.invites.update_all(user_id: id)
old_user.merge_logs.update_all(user_id: id)
[ [
[old_user.instructeur, instructeur], [old_user.instructeur, instructeur],
@ -210,6 +212,7 @@ class User < ApplicationRecord
end end
end end
merge_logs.create(from_user_id: old_user.id, from_user_email: old_user.email)
old_user.destroy old_user.destroy
end end

View file

@ -0,0 +1,11 @@
class CreateMergeLogs < ActiveRecord::Migration[6.1]
def change
create_table :merge_logs do |t|
t.bigint :from_user_id, null: false
t.string :from_user_email, null: false
t.references :user, null: false, foreign_key: true
t.timestamps
end
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_10_20_114237) do ActiveRecord::Schema.define(version: 2021_10_26_131800) 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"
@ -546,6 +546,15 @@ ActiveRecord::Schema.define(version: 2021_10_20_114237) do
t.index ["email", "dossier_id"], name: "index_invites_on_email_and_dossier_id", unique: true t.index ["email", "dossier_id"], name: "index_invites_on_email_and_dossier_id", unique: true
end end
create_table "merge_logs", force: :cascade do |t|
t.bigint "from_user_id", null: false
t.string "from_user_email", null: false
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_merge_logs_on_user_id"
end
create_table "module_api_cartos", id: :serial, force: :cascade do |t| create_table "module_api_cartos", id: :serial, force: :cascade do |t|
t.integer "procedure_id" t.integer "procedure_id"
t.boolean "use_api_carto", default: false t.boolean "use_api_carto", default: false
@ -841,6 +850,7 @@ ActiveRecord::Schema.define(version: 2021_10_20_114237) do
add_foreign_key "geo_areas", "champs" add_foreign_key "geo_areas", "champs"
add_foreign_key "groupe_instructeurs", "procedures" add_foreign_key "groupe_instructeurs", "procedures"
add_foreign_key "initiated_mails", "procedures" add_foreign_key "initiated_mails", "procedures"
add_foreign_key "merge_logs", "users"
add_foreign_key "procedure_presentations", "assign_tos" add_foreign_key "procedure_presentations", "assign_tos"
add_foreign_key "procedure_revision_types_de_champ", "procedure_revisions", column: "revision_id" add_foreign_key "procedure_revision_types_de_champ", "procedure_revisions", column: "revision_id"
add_foreign_key "procedure_revision_types_de_champ", "types_de_champ" add_foreign_key "procedure_revision_types_de_champ", "types_de_champ"

View file

@ -427,12 +427,18 @@ describe User, type: :model do
context 'and the old account has some stuff' do context 'and the old account has some stuff' do
let!(:dossier) { create(:dossier, user: old_user) } let!(:dossier) { create(:dossier, user: old_user) }
let!(:invite) { create(:invite, user: old_user) } let!(:invite) { create(:invite, user: old_user) }
let!(:merge_log) { MergeLog.create(user: old_user, from_user_id: 1, from_user_email: 'a') }
it 'transfers the dossier' do it 'transfers the dossier' do
subject subject
expect(targeted_user.dossiers).to match([dossier]) expect(targeted_user.dossiers).to match([dossier])
expect(targeted_user.invites).to match([invite]) expect(targeted_user.invites).to match([invite])
expect(targeted_user.merge_logs.first).to eq(merge_log)
added_merge_log = targeted_user.merge_logs.last
expect(added_merge_log.from_user_id).to eq(old_user.id)
expect(added_merge_log.from_user_email).to eq(old_user.email)
end end
end end