From 8bff2fb34cfc5c5c1ec181041356678cbac8fa35 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Wed, 30 Jul 2014 18:09:55 +0300 Subject: [PATCH] Domain has many admin and technical contacts now --- app/helpers/epp/domains_helper.rb | 17 +++++++------ app/models/contact.rb | 18 ++++++++----- app/models/domain.rb | 25 ++++++++++++++++--- app/models/domain_contact.rb | 4 +++ .../20140730141443_add_contacts_to_domain.rb | 11 ++++++++ db/schema.rb | 10 +++++++- spec/epp/domain_spec.rb | 13 ++++++++++ spec/models/domain_spec.rb | 4 +-- 8 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 app/models/domain_contact.rb create mode 100644 db/migrate/20140730141443_add_contacts_to_domain.rb diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index fac6986bb..f5fa25fc0 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -1,13 +1,14 @@ module Epp::DomainsHelper def create_domain @domain = Domain.new(domain_create_params) - @domain.create_contacts(domain_contacts) - if @domain.save - render '/epp/domains/create' - else - handle_domain_name_errors - render '/epp/error' + Domain.transaction do + if @domain.save && @domain.attach_contacts(domain_contacts) + render '/epp/domains/create' + else + handle_domain_name_errors + render '/epp/error' + end end end @@ -37,11 +38,11 @@ module Epp::DomainsHelper res = {tech: [], admin: []} parsed_frame.css('contact[type="tech"]').each do |x| - res[:tech] << Hash.from_xml(x.to_s) + res[:tech] << Hash.from_xml(x.to_s).with_indifferent_access end parsed_frame.css('contact[type="admin"]').each do |x| - res[:admin] << Hash.from_xml(x.to_s) + res[:admin] << Hash.from_xml(x.to_s).with_indifferent_access end res diff --git a/app/models/contact.rb b/app/models/contact.rb index 77d118312..141df9261 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,23 +1,29 @@ class Contact < ActiveRecord::Base #TODO Foreign contact will get email with activation link/username/temp password #TODO Phone number validation, in first phase very minimam in order to support current registries - + has_many :addresses + has_many :domain_contacts + has_many :domains, through: :domain_contacts validates_presence_of :code, :name, :phone, :email, :ident - validate :ident_must_be_valid + validate :ident_must_be_valid validates :phone, format: { with: /\+\d{3}\.\d+/, message: "bad format" } IDENT_TYPES = [ "ico", #Company registry code (or similar) - "op", #Estonian ID + "op", #Estonian ID "passport", #Passport number "birthday" #Birthday date ] - + + CONTACT_TYPE_TECH = 'tech' + CONTACT_TYPE_ADMIN = 'admin' + CONTACT_TYPES = [CONTACT_TYPE_TECH, CONTACT_TYPE_ADMIN] + def ident_must_be_valid - #TODO Ident can also be passport number or company registry code. + #TODO Ident can also be passport number or company registry code. #so have to make changes to validations (and doc/schema) accordingly return true unless ident.present? && ident_type.present? && ident_type == "op" code = Isikukood.new(ident) @@ -31,7 +37,7 @@ class Contact < ActiveRecord::Base res = [] codes.each do |x| if Contact.find_by(code: x) - res << {code: x, avail: 0, reason: 'in use'} + res << {code: x, avail: 0, reason: 'in use'} else res << {code: x, avail: 1} end diff --git a/app/models/domain.rb b/app/models/domain.rb index 390ca525c..6bc1a3b42 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -5,8 +5,17 @@ class Domain < ActiveRecord::Base belongs_to :registrar belongs_to :ns_set belongs_to :owner_contact, class_name: 'Contact' - belongs_to :technical_contact, class_name: 'Contact' - belongs_to :admin_contact, class_name: 'Contact' + + has_many :domain_contacts + + has_many :tech_contacts, -> { + where(domain_contacts: {contact_type: Contact::CONTACT_TYPE_TECH}) + }, through: :domain_contacts, source: :contact + + has_many :admin_contacts, -> { + where(domain_contacts: {contact_type: Contact::CONTACT_TYPE_ADMIN}) + }, through: :domain_contacts, source: :contact + validates_presence_of :name @@ -21,9 +30,19 @@ class Domain < ActiveRecord::Base write_attribute(:name_dirty, value) end - def create_contacts(contacts) + def attach_contacts(contacts) contacts[:tech].each do |x| + domain_contacts.create( + contact: Contact.find_by(code: x[:contact]), + contact_type: Contact::CONTACT_TYPE_TECH + ) + end + contacts[:admin].each do |x| + domain_contacts.create( + contact: Contact.find_by(code: x[:contact]), + contact_type: Contact::CONTACT_TYPE_ADMIN + ) end end diff --git a/app/models/domain_contact.rb b/app/models/domain_contact.rb new file mode 100644 index 000000000..6d21a6843 --- /dev/null +++ b/app/models/domain_contact.rb @@ -0,0 +1,4 @@ +class DomainContact < ActiveRecord::Base + belongs_to :contact + belongs_to :domain +end diff --git a/db/migrate/20140730141443_add_contacts_to_domain.rb b/db/migrate/20140730141443_add_contacts_to_domain.rb new file mode 100644 index 000000000..80ad2a376 --- /dev/null +++ b/db/migrate/20140730141443_add_contacts_to_domain.rb @@ -0,0 +1,11 @@ +class AddContactsToDomain < ActiveRecord::Migration + def change + create_table :domain_contacts do |t| + t.integer :contact_id + t.integer :domain_id + t.string :contact_type + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e35a09e1d..97e821f92 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140730104916) do +ActiveRecord::Schema.define(version: 20140730141443) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -47,6 +47,14 @@ ActiveRecord::Schema.define(version: 20140730104916) do t.datetime "updated_at" end + create_table "domain_contacts", force: true do |t| + t.integer "contact_id" + t.integer "domain_id" + t.string "contact_type" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "domains", force: true do |t| t.string "name" t.integer "registrar_id" diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 9cc797e25..2255eca9b 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -31,6 +31,19 @@ describe 'EPP Domain', epp: true do expect(response[:clTRID]).to eq('ABC-12345') end + it 'creates a domain with contacts' do + Fabricate(:contact, code: 'sh8013') + Fabricate(:contact, code: 'sh801333') + + response = epp_request('domains/create.xml') + expect(response[:result_code]).to eq('1000') + expect(response[:msg]).to eq('Command completed successfully') + expect(response[:clTRID]).to eq('ABC-12345') + + expect(Domain.first.tech_contacts.count).to eq 2 + expect(Domain.first.admin_contacts.count).to eq 1 + end + it 'checks a domain' do response = epp_request('domains/check.xml') expect(response[:result_code]).to eq('1000') diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 1fd0b1891..b5340b3ba 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -3,9 +3,9 @@ require "rails_helper" describe Domain do it { should belong_to(:registrar) } it { should belong_to(:ns_set) } - it { should belong_to(:admin_contact) } it { should belong_to(:owner_contact) } - it { should belong_to(:technical_contact) } + it { should have_many(:tech_contacts) } + it { should have_many(:admin_contacts) } it 'validates domain name' do d = Fabricate(:domain)