Add service for conversations

This commit is contained in:
Frank Rousseau 2017-05-21 00:31:54 +02:00
parent c45b785370
commit b37c14ce0e
2 changed files with 152 additions and 0 deletions

View file

@ -0,0 +1,45 @@
class ConversationService
def initialize(user=nil)
@user = user
end
def all_for_user
Conversation.where(author_id: @user.person.id).all
end
def build(subject, text, recipients)
person_ids = @user.contacts
.mutual
.where(person_id: recipients)
.pluck(:person_id)
opts = {
subject: subject,
message: {text: text},
participant_ids: person_ids
}
@user.build_conversation(opts)
end
def find!(conversation_id)
@user.conversations
.joins(:conversation_visibilities)
.where(conversation_visibilities: {
person_id: @user.person_id,
conversation_id: conversation_id
}).first!
end
def destroy!(conversation_id)
conversation = find!(conversation_id)
conversation.destroy!
end
def get_visibility(conversation_id)
ConversationVisibility.where(
person_id: @user.person.id,
conversation_id: conversation_id
).first!
end
end

View file

@ -0,0 +1,107 @@
describe ConversationService do
opts = {
subject: "conversation subject",
message: {text: "conversation text"},
participant_ids: [bob.person.id]
}
conversation = alice.build_conversation(opts)
conversation.save
describe "#find!" do
it "returns the conversation, if it is the user's conversation" do
expect(alice_conversation_service.find!(conversation.id)).to eq(
conversation
)
end
it "returns the conversation, if the user is recipient" do
expect(bob_conversation_service.find!(conversation.id)).to eq(
conversation
)
end
it "raises RecordNotFound if the conversation cannot be found" do
expect {
alice_conversation_service.find!("unknown")
}.to raise_error ActiveRecord::RecordNotFound
end
it "raises RecordNotFound if the user is not recipient" do
expect {
eve_conversation_service.find!(conversation.id)
}.to raise_error ActiveRecord::RecordNotFound
end
end
describe "#build" do
it "creates the conversation for given user and recipients" do
new_conversation = alice_conversation_service.build(
"subject test",
"message test",
[bob.person.id]
)
expect(new_conversation.subject).to eq("subject test")
expect(new_conversation.author_id).to eq(alice.person.id)
expect(new_conversation.messages[0].text).to eq("message test")
expect(new_conversation.messages[0].author_id).to eq(alice.person.id)
expect(new_conversation.participants.length).to eq(2)
end
it "doesn't add recipients if they are not user contacts" do
new_conversation = alice_conversation_service.build(
"subject test",
"message test",
[bob.person.id, eve.person.id]
)
expect(new_conversation.participants.length).to eq(2)
expect(new_conversation.messages[0].text).to eq("message test")
expect(new_conversation.messages[0].author_id).to eq(alice.person.id)
end
end
describe "#get_visibility" do
it "returns visibility for current user" do
visibility = alice_conversation_service.get_visibility(conversation.id)
expect(visibility).to_not be_nil
end
it "raises RecordNotFound if the user has no visibility" do
expect {
eve_conversation_service.get_visibility(conversation.id)
}.to raise_error ActiveRecord::RecordNotFound
end
end
describe "#destroy!" do
it "deletes the conversation, when it is the user conversation" do
alice_conversation_service.destroy!(conversation.id)
expect {
alice_conversation_service.find!(conversation.id)
}.to raise_error ActiveRecord::RecordNotFound
end
it "raises RecordNotFound if the conversation cannot be found" do
expect {
alice_conversation_service.destroy!("unknown")
}.to raise_error ActiveRecord::RecordNotFound
end
it "raises RecordNotFound if the user is not part of the conversation" do
expect {
eve_conversation_service.destroy!(conversation.id)
}.to raise_error ActiveRecord::RecordNotFound
end
end
def alice_conversation_service
ConversationService.new(alice)
end
def bob_conversation_service
ConversationService.new(bob)
end
def eve_conversation_service
ConversationService.new(eve)
end
end