feat(commentaire.soft_delete): wrap deletion behaviour in service
This commit is contained in:
parent
5b72bdec7e
commit
9a8ec1087c
4 changed files with 77 additions and 1 deletions
|
@ -4,6 +4,7 @@
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# body :string
|
# body :string
|
||||||
|
# deleted_at :datetime
|
||||||
# email :string
|
# email :string
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
|
|
|
@ -20,5 +20,20 @@ class CommentaireService
|
||||||
end
|
end
|
||||||
Commentaire.new(attributes)
|
Commentaire.new(attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def soft_delete(user, params)
|
||||||
|
commentaire = Dossier.find(params[:dossier_id])
|
||||||
|
.commentaires
|
||||||
|
.find(params[:commentaire_id])
|
||||||
|
if commentaire.sent_by?(user)
|
||||||
|
commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached?
|
||||||
|
commentaire.update!(body: "Message supprimé", deleted_at: Time.now.utc)
|
||||||
|
OpenStruct.new(status: true)
|
||||||
|
else
|
||||||
|
OpenStruct.new(status: false, error_message: "Impossible de supprimer le commentaire, celui ci ne vous appartient pas")
|
||||||
|
end
|
||||||
|
rescue ActiveRecord::RecordNotFound => e
|
||||||
|
return OpenStruct.new(status: false, error_message: "#{e.model.humanize} introuvable")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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_11_04_102349) do
|
ActiveRecord::Schema.define(version: 2021_11_15_112933) 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"
|
||||||
|
@ -219,6 +219,7 @@ ActiveRecord::Schema.define(version: 2021_11_04_102349) do
|
||||||
t.bigint "user_id"
|
t.bigint "user_id"
|
||||||
t.bigint "instructeur_id"
|
t.bigint "instructeur_id"
|
||||||
t.bigint "expert_id"
|
t.bigint "expert_id"
|
||||||
|
t.datetime "deleted_at"
|
||||||
t.index ["dossier_id"], name: "index_commentaires_on_dossier_id"
|
t.index ["dossier_id"], name: "index_commentaires_on_dossier_id"
|
||||||
t.index ["expert_id"], name: "index_commentaires_on_expert_id"
|
t.index ["expert_id"], name: "index_commentaires_on_expert_id"
|
||||||
t.index ["instructeur_id"], name: "index_commentaires_on_instructeur_id"
|
t.index ["instructeur_id"], name: "index_commentaires_on_instructeur_id"
|
||||||
|
|
|
@ -33,5 +33,64 @@ describe CommentaireService do
|
||||||
expect(commentaire.piece_jointe.attached?).to be_truthy
|
expect(commentaire.piece_jointe.attached?).to be_truthy
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.soft_delete' do
|
||||||
|
subject { CommentaireService.soft_delete(user, params) }
|
||||||
|
|
||||||
|
context 'when dossier not found' do
|
||||||
|
let(:user) { create(:instructeur) }
|
||||||
|
let(:params) { {} }
|
||||||
|
it 'returns error struct' do
|
||||||
|
expect(subject.status).to eq(false)
|
||||||
|
end
|
||||||
|
it 'returns error message' do
|
||||||
|
expect(subject.error_message).to eq("Dossier introuvable")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when commentaire not found' do
|
||||||
|
let(:user) { create(:instructeur) }
|
||||||
|
let(:params) { { dossier_id: create(:dossier).id } }
|
||||||
|
it 'returns error struct' do
|
||||||
|
expect(subject.status).to eq(false)
|
||||||
|
end
|
||||||
|
it 'returns error message' do
|
||||||
|
expect(subject.error_message).to eq("Commentaire introuvable")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when commentaire does not belongs to instructeur' do
|
||||||
|
let(:user) { create(:instructeur) }
|
||||||
|
let(:dossier) { create(:dossier) }
|
||||||
|
let(:params) { { dossier_id: dossier.id,
|
||||||
|
commentaire_id: create(:commentaire, dossier: dossier, instructeur: create(:instructeur)).id } }
|
||||||
|
it 'returns error struct' do
|
||||||
|
expect(subject.status).to eq(false)
|
||||||
|
end
|
||||||
|
it 'returns error message' do
|
||||||
|
expect(subject.error_message).to eq("Impossible de supprimer le commentaire, celui ci ne vous appartient pas")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when commentaire belongs to instructeur' do
|
||||||
|
let(:user) { create(:instructeur) }
|
||||||
|
let(:dossier) { create(:dossier) }
|
||||||
|
let(:commentaire) { create(:commentaire, dossier: dossier, instructeur: user) }
|
||||||
|
let(:params) { { dossier_id: dossier.id,
|
||||||
|
commentaire_id: commentaire.id } }
|
||||||
|
it 'returns error struct' do
|
||||||
|
expect(subject.status).to eq(true)
|
||||||
|
end
|
||||||
|
it 'sets commentaire.body to deleted message' do
|
||||||
|
expect{ subject}.to change { commentaire.reload.body}.from(an_instance_of(String)).to("Message supprimé")
|
||||||
|
end
|
||||||
|
it 'set deleted_at' do
|
||||||
|
Timecop.freeze do
|
||||||
|
expect{ subject}.to change { commentaire.reload.deleted_at}.from(nil).to(Time.now.utc)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue