From c21c839dcaecf695441efa2478149d317807c0e0 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Sun, 27 Oct 2019 13:14:36 +0530 Subject: [PATCH] Contact model changes (#184) * move source id from contacts * Fix contactInbox model name * rubocop fix * Fix rspec --- app/builders/messages/message_builder.rb | 8 +++++--- .../widgets/conversation/ReplyBox.vue | 4 ++-- app/models/contact.rb | 10 ++++++---- app/models/contact_inbox.rb | 8 ++++++++ app/models/inbox.rb | 4 +++- app/services/facebook/send_reply_service.rb | 2 +- .../api/v1/conversations/index.json.jbuilder | 2 +- .../20191027054756_create_contact_inboxes.rb | 15 +++++++++++++++ db/schema.rb | 18 +++++++++++++++--- lib/integrations/facebook/delivery_status.rb | 2 +- spec/factories/contacts.rb | 2 -- spec/models/contact_spec.rb | 2 -- 12 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 app/models/contact_inbox.rb create mode 100644 db/migrate/20191027054756_create_contact_inboxes.rb diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index 77c894750..67d3272ba 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -30,11 +30,14 @@ module Messages private def contact - @contact ||= @inbox.contacts.find_by(source_id: @sender_id) + @contact ||= @inbox.contact_inboxes.find_by(source_id: @sender_id)&.contact end def build_contact - @contact = @inbox.contacts.create!(contact_params) if contact.nil? + return if contact.present? + + @contact = Contact.create!(contact_params) + ContactInbox.create(contact: contact, inbox: @inbox, source_id: @sender_id) end def build_message @@ -120,7 +123,6 @@ module Messages { name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}", account_id: @inbox.account_id, - source_id: @sender_id, remote_avatar_url: result['profile_pic'] || nil } end diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue index 38497daf1..24bcb818e 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue @@ -86,7 +86,7 @@ export default { data() { return { message: '', - private: false, + isPrivate: false, showEmojiPicker: false, showCannedModal: false, }; @@ -100,7 +100,7 @@ export default { }, watch: { message(val) { - if (this.private) { + if (this.isPrivate) { return; } const isSlashCommand = val[0] === '/'; diff --git a/app/models/contact.rb b/app/models/contact.rb index d05345e2c..e52645bca 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,20 +1,22 @@ class Contact < ApplicationRecord include Pubsubable - validates :account_id, presence: true - validates :inbox_id, presence: true belongs_to :account - belongs_to :inbox has_many :conversations, dependent: :destroy + has_many :contact_inboxes, dependent: :destroy + has_many :inboxes, through: :contact_inboxes mount_uploader :avatar, AvatarUploader + def get_source_id(inbox_id) + contact_inboxes.find_by!(inbox_id: inbox_id).source_id + end + def push_event_data { id: id, name: name, thumbnail: avatar.thumb.url, - channel: inbox.try(:channel).try(:name), pubsub_token: pubsub_token } end diff --git a/app/models/contact_inbox.rb b/app/models/contact_inbox.rb new file mode 100644 index 000000000..2877ef2d1 --- /dev/null +++ b/app/models/contact_inbox.rb @@ -0,0 +1,8 @@ +class ContactInbox < ApplicationRecord + validates :inbox_id, presence: true + validates :contact_id, presence: true + validates :source_id, presence: true + + belongs_to :contact + belongs_to :inbox +end diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 98c7f30e1..cdb12e3c3 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -6,11 +6,13 @@ class Inbox < ApplicationRecord belongs_to :account belongs_to :channel, polymorphic: true, dependent: :destroy + has_many :contact_inboxes, dependent: :destroy + has_many :contacts, through: :contact_inboxes + has_many :inbox_members, dependent: :destroy has_many :members, through: :inbox_members, source: :user has_many :conversations, dependent: :destroy has_many :messages, through: :conversations - has_many :contacts, dependent: :destroy after_create :subscribe_webhook, if: :facebook? after_destroy :delete_round_robin_agents diff --git a/app/services/facebook/send_reply_service.rb b/app/services/facebook/send_reply_service.rb index f20ebdd3f..37630f12e 100644 --- a/app/services/facebook/send_reply_service.rb +++ b/app/services/facebook/send_reply_service.rb @@ -35,7 +35,7 @@ module Facebook def fb_message_params { - recipient: { id: contact.source_id }, + recipient: { id: contact.get_source_id(inbox.id) }, message: { text: message.content } } end diff --git a/app/views/api/v1/conversations/index.json.jbuilder b/app/views/api/v1/conversations/index.json.jbuilder index 1021ce105..35dbf3988 100644 --- a/app/views/api/v1/conversations/index.json.jbuilder +++ b/app/views/api/v1/conversations/index.json.jbuilder @@ -9,7 +9,7 @@ json.data do json.array! @conversations do |conversation| json.meta do json.sender do - json.id conversation.contact.source_id + json.id conversation.contact.id json.name conversation.contact.name json.thumbnail conversation.contact.avatar.thumb.url json.channel conversation.inbox.try(:channel).try(:name) diff --git a/db/migrate/20191027054756_create_contact_inboxes.rb b/db/migrate/20191027054756_create_contact_inboxes.rb new file mode 100644 index 000000000..db5832571 --- /dev/null +++ b/db/migrate/20191027054756_create_contact_inboxes.rb @@ -0,0 +1,15 @@ +class CreateContactInboxes < ActiveRecord::Migration[6.1] + def change + create_table :contact_inboxes do |t| + t.references :contact, foreign_key: true, index: true + t.references :inbox, foreign_key: true, index: true + t.string :source_id, null: false + + t.timestamps + end + add_index :contact_inboxes, [:inbox_id, :source_id], :unique => true + add_index :contact_inboxes, [:source_id] + remove_column :contacts, :inbox_id, :integer + remove_column :contacts, :source_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 1a284be96..fe01b9e98 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: 2019_10_20_173522) do +ActiveRecord::Schema.define(version: 2019_10_27_054756) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -63,6 +63,18 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do t.datetime "updated_at", null: false end + create_table "contact_inboxes", force: :cascade do |t| + t.bigint "contact_id" + t.bigint "inbox_id" + t.string "source_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["contact_id"], name: "index_contact_inboxes_on_contact_id" + t.index ["inbox_id", "source_id"], name: "index_contact_inboxes_on_inbox_id_and_source_id", unique: true + t.index ["inbox_id"], name: "index_contact_inboxes_on_inbox_id" + t.index ["source_id"], name: "index_contact_inboxes_on_source_id" + end + create_table "contacts", id: :serial, force: :cascade do |t| t.string "name" t.string "email" @@ -70,8 +82,6 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do t.integer "account_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "inbox_id", null: false - t.bigserial "source_id", null: false t.string "avatar" t.string "pubsub_token" t.index ["account_id"], name: "index_contacts_on_account_id" @@ -208,5 +218,7 @@ ActiveRecord::Schema.define(version: 2019_10_20_173522) do t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true end + add_foreign_key "contact_inboxes", "contacts" + add_foreign_key "contact_inboxes", "inboxes" add_foreign_key "users", "users", column: "inviter_id" end diff --git a/lib/integrations/facebook/delivery_status.rb b/lib/integrations/facebook/delivery_status.rb index 3a419aa8d..ab0774dad 100644 --- a/lib/integrations/facebook/delivery_status.rb +++ b/lib/integrations/facebook/delivery_status.rb @@ -18,7 +18,7 @@ module Integrations end def contact - Contact.find_by(source_id: sender_id) + ::ContactInbox.find_by(source_id: sender_id).contact end def conversation diff --git a/spec/factories/contacts.rb b/spec/factories/contacts.rb index b926f841b..c3e416b8c 100644 --- a/spec/factories/contacts.rb +++ b/spec/factories/contacts.rb @@ -5,8 +5,6 @@ FactoryBot.define do sequence(:name) { |n| "Widget #{n}" } sequence(:email) { |n| "widget-#{n}@example.com" } phone_number { '+123456789011' } - source_id { rand(100) } account - inbox end end diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index a91294c4d..d296907cc 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -5,12 +5,10 @@ require 'rails_helper' RSpec.describe Contact do context 'validations' do it { is_expected.to validate_presence_of(:account_id) } - it { is_expected.to validate_presence_of(:inbox_id) } end context 'associations' do it { is_expected.to belong_to(:account) } - it { is_expected.to belong_to(:inbox) } it { is_expected.to have_many(:conversations).dependent(:destroy) } end