diff --git a/app/models/account.rb b/app/models/account.rb index b0c4e2b72..57d1b88c4 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -37,6 +37,7 @@ class Account < ApplicationRecord has_many :data_imports, dependent: :destroy has_many :users, through: :account_users has_many :inboxes, dependent: :destroy + has_many :notes, dependent: :destroy has_many :campaigns, dependent: :destroy has_many :conversations, dependent: :destroy has_many :messages, dependent: :destroy diff --git a/app/models/contact.rb b/app/models/contact.rb index 34d54bee6..62d4970ea 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -41,6 +41,7 @@ class Contact < ApplicationRecord has_many :contact_inboxes, dependent: :destroy has_many :inboxes, through: :contact_inboxes has_many :messages, as: :sender, dependent: :destroy + has_many :notes, dependent: :destroy before_validation :prepare_email_attribute after_create_commit :dispatch_create_event, :ip_lookup diff --git a/app/models/note.rb b/app/models/note.rb new file mode 100644 index 000000000..81613558d --- /dev/null +++ b/app/models/note.rb @@ -0,0 +1,34 @@ +# == Schema Information +# +# Table name: notes +# +# id :bigint not null, primary key +# content :text not null +# created_at :datetime not null +# updated_at :datetime not null +# account_id :bigint not null +# contact_id :bigint not null +# user_id :bigint +# +# Indexes +# +# index_notes_on_account_id (account_id) +# index_notes_on_contact_id (contact_id) +# index_notes_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (account_id => accounts.id) +# fk_rails_... (contact_id => contacts.id) +# fk_rails_... (user_id => users.id) +# +class Note < ApplicationRecord + validates :content, presence: true + validates :account_id, presence: true + validates :contact_id, presence: true + validates :user_id, presence: true + + belongs_to :account + belongs_to :contact + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 460608178..3fc8d7668 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -82,6 +82,7 @@ class User < ApplicationRecord has_many :notification_subscriptions, dependent: :destroy has_many :team_members, dependent: :destroy has_many :teams, through: :team_members + has_many :notes, dependent: :nullify before_validation :set_password_and_uid, on: :create diff --git a/db/migrate/20210618073042_create_notes.rb b/db/migrate/20210618073042_create_notes.rb new file mode 100644 index 000000000..1e7b091a7 --- /dev/null +++ b/db/migrate/20210618073042_create_notes.rb @@ -0,0 +1,11 @@ +class CreateNotes < ActiveRecord::Migration[6.0] + def change + create_table :notes do |t| + t.text :content, null: false + t.references :account, foreign_key: true, null: false + t.references :contact, foreign_key: true, null: false + t.references :user, foreign_key: true + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e22906be..2e93206de 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -438,6 +438,18 @@ ActiveRecord::Schema.define(version: 2021_06_18_095823) do t.index ["source_id"], name: "index_messages_on_source_id" end + create_table "notes", force: :cascade do |t| + t.text "content", null: false + t.bigint "account_id", null: false + t.bigint "contact_id", null: false + t.bigint "user_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["account_id"], name: "index_notes_on_account_id" + t.index ["contact_id"], name: "index_notes_on_contact_id" + t.index ["user_id"], name: "index_notes_on_user_id" + end + create_table "notification_settings", force: :cascade do |t| t.integer "account_id" t.integer "user_id" @@ -629,6 +641,9 @@ ActiveRecord::Schema.define(version: 2021_06_18_095823) do add_foreign_key "conversations", "contact_inboxes" add_foreign_key "conversations", "teams" add_foreign_key "data_imports", "accounts" + add_foreign_key "notes", "accounts" + add_foreign_key "notes", "contacts" + add_foreign_key "notes", "users" add_foreign_key "team_members", "teams" add_foreign_key "team_members", "users" add_foreign_key "teams", "accounts" diff --git a/spec/factories/notes.rb b/spec/factories/notes.rb new file mode 100644 index 000000000..be650010f --- /dev/null +++ b/spec/factories/notes.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :note do + content { 'Hey welcome to chatwoot' } + account + user + contact + end +end diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb new file mode 100644 index 000000000..3d36c56ae --- /dev/null +++ b/spec/models/note_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe Note, type: :model do + describe 'validations' do + it { is_expected.to validate_presence_of(:content) } + it { is_expected.to validate_presence_of(:account_id) } + it { is_expected.to validate_presence_of(:user_id) } + it { is_expected.to validate_presence_of(:contact_id) } + end + + describe 'associations' do + it { is_expected.to belong_to(:account) } + it { is_expected.to belong_to(:user) } + it { is_expected.to belong_to(:contact) } + end + + describe 'validates_factory' do + it 'creates valid note object' do + note = create(:note) + expect(note.content).to eq 'Hey welcome to chatwoot' + end + end +end