{"action":"created","comment":{"url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/comments/50241145","id":50241145,"diff_hunk":"@@ -0,0 +1,56 @@\n+class BusinessEntityService\n+\n+  def initialize(office, jurisdiction)\n+    @office = office\n+    @jurisdiction = jurisdiction\n+    @timestamp = Time.zone.now\n+    @business_entity = BusinessEntity.find_by(office: office,\n+                                              jurisdiction: jurisdiction,\n+                                              valid_to: nil)\n+  end\n+\n+  def build(params)\n+    build_business_entity(params)\n+  end\n+\n+  def check_update(params)\n+    build_business_entity(params)\n+  end\n+\n+  def persist_update!(business_entity)\n+    return false if business_entity.nil? || @business_entity.nil?\n+    if duplicate_needed?(business_entity)\n+      @business_entity.assign_attributes(valid_to: @timestamp)\n+      save_entities_in_transaction(business_entity)\n+    else\n+      @business_entity.assign_attributes(name: business_entity.name)\n+      @business_entity.save\n+    end\n+  end\n+\n+  private\n+\n+  def build_business_entity(params)\n+    BusinessEntity.new(office: @office,\n+                       jurisdiction: @jurisdiction,\n+                       name: params[:name],\n+                       code: params[:code],\n+                       valid_from: @timestamp)\n+  end\n+\n+  def duplicate_needed?(business_entity)\n+    business_entity != @business_entity &&\n+      (both_entities_present?(business_entity) && business_entity.code != @business_entity.code)\n+  end\n+\n+  def both_entities_present?(business_entity)\n+    @business_entity.present? && business_entity.present?\n+  end\n+\n+  def save_entities_in_transaction(business_entity)","path":"app/services/business_entity_service.rb","position":50,"original_position":50,"commit_id":"ed37fb1df1c7509e9dd3fa16b54bd79aba795f86","original_commit_id":"ed37fb1df1c7509e9dd3fa16b54bd79aba795f86","user":{"login":"dotemacs","id":152363,"avatar_url":"https://avatars.githubusercontent.com/u/152363?v=3","gravatar_id":"","url":"https://api.github.com/users/dotemacs","html_url":"https://github.com/dotemacs","followers_url":"https://api.github.com/users/dotemacs/followers","following_url":"https://api.github.com/users/dotemacs/following{/other_user}","gists_url":"https://api.github.com/users/dotemacs/gists{/gist_id}","starred_url":"https://api.github.com/users/dotemacs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotemacs/subscriptions","organizations_url":"https://api.github.com/users/dotemacs/orgs","repos_url":"https://api.github.com/users/dotemacs/repos","events_url":"https://api.github.com/users/dotemacs/events{/privacy}","received_events_url":"https://api.github.com/users/dotemacs/received_events","type":"User","site_admin":false},"body":"Maybe a name of **save_duplicate** instead of **save_entities_in_transaction**?","created_at":"2016-01-20T11:05:55Z","updated_at":"2016-01-20T11:05:55Z","html_url":"https://github.com/ministryofjustice/fr-staffapp/pull/536#discussion_r50241145","pull_request_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536","_links":{"self":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/comments/50241145"},"html":{"href":"https://github.com/ministryofjustice/fr-staffapp/pull/536#discussion_r50241145"},"pull_request":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536"}}},"pull_request":{"url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536","id":55891566,"html_url":"https://github.com/ministryofjustice/fr-staffapp/pull/536","diff_url":"https://github.com/ministryofjustice/fr-staffapp/pull/536.diff","patch_url":"https://github.com/ministryofjustice/fr-staffapp/pull/536.patch","issue_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/536","number":536,"state":"open","locked":false,"title":"Allow business entity editing","user":{"login":"colinbruce","id":6757677,"avatar_url":"https://avatars.githubusercontent.com/u/6757677?v=3","gravatar_id":"","url":"https://api.github.com/users/colinbruce","html_url":"https://github.com/colinbruce","followers_url":"https://api.github.com/users/colinbruce/followers","following_url":"https://api.github.com/users/colinbruce/following{/other_user}","gists_url":"https://api.github.com/users/colinbruce/gists{/gist_id}","starred_url":"https://api.github.com/users/colinbruce/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colinbruce/subscriptions","organizations_url":"https://api.github.com/users/colinbruce/orgs","repos_url":"https://api.github.com/users/colinbruce/repos","events_url":"https://api.github.com/users/colinbruce/events{/privacy}","received_events_url":"https://api.github.com/users/colinbruce/received_events","type":"User","site_admin":false},"body":"This adds methods for admins to see the current state of the business entities for\r\na given office, add a new business entity and update an existing business_entity.code and .name.\r\n\r\nWhen updating *only* a BE.name, no new item is created.  If the code is amended, the existing BE has it's valid to date set and a new BE is created with the same start date and the new code.","created_at":"2016-01-13T12:34:18Z","updated_at":"2016-01-20T11:05:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8d386cf86012772b355db44d4ce322e9413906be","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536/commits","review_comments_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536/comments","review_comment_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/536/comments","statuses_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/statuses/ed37fb1df1c7509e9dd3fa16b54bd79aba795f86","head":{"label":"ministryofjustice:allow-business-entity-editing","ref":"allow-business-entity-editing","sha":"ed37fb1df1c7509e9dd3fa16b54bd79aba795f86","user":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"repo":{"id":30420185,"name":"fr-staffapp","full_name":"ministryofjustice/fr-staffapp","owner":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ministryofjustice/fr-staffapp","description":"Help with Court Fees Staff Interface","fork":false,"url":"https://api.github.com/repos/ministryofjustice/fr-staffapp","forks_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/forks","keys_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/teams","hooks_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/hooks","issue_events_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/events{/number}","events_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/events","assignees_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/assignees{/user}","branches_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/branches{/branch}","tags_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/tags","blobs_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/refs{/sha}","trees_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/statuses/{sha}","languages_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/languages","stargazers_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/stargazers","contributors_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/contributors","subscribers_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/subscribers","subscription_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/subscription","commits_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/commits{/sha}","git_commits_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/commits{/sha}","comments_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/comments{/number}","issue_comment_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/comments{/number}","contents_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/contents/{+path}","compare_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/merges","archive_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/downloads","issues_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues{/number}","pulls_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls{/number}","milestones_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/milestones{/number}","notifications_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/labels{/name}","releases_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/releases{/id}","created_at":"2015-02-06T16:01:04Z","updated_at":"2016-01-11T17:17:54Z","pushed_at":"2016-01-20T10:12:03Z","git_url":"git://github.com/ministryofjustice/fr-staffapp.git","ssh_url":"git@github.com:ministryofjustice/fr-staffapp.git","clone_url":"https://github.com/ministryofjustice/fr-staffapp.git","svn_url":"https://github.com/ministryofjustice/fr-staffapp","homepage":"","size":3683,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":7,"forks":0,"open_issues":7,"watchers":0,"default_branch":"master"}},"base":{"label":"ministryofjustice:master","ref":"master","sha":"f86c825944ea0692cab85d339b1d43f08ea0ec3d","user":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"repo":{"id":30420185,"name":"fr-staffapp","full_name":"ministryofjustice/fr-staffapp","owner":{"login":"ministryofjustice","id":2203574,"avatar_url":"https://avatars.githubusercontent.com/u/2203574?v=3","gravatar_id":"","url":"https://api.github.com/users/ministryofjustice","html_url":"https://github.com/ministryofjustice","followers_url":"https://api.github.com/users/ministryofjustice/followers","following_url":"https://api.github.com/users/ministryofjustice/following{/other_user}","gists_url":"https://api.github.com/users/ministryofjustice/gists{/gist_id}","starred_url":"https://api.github.com/users/ministryofjustice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ministryofjustice/subscriptions","organizations_url":"https://api.github.com/users/ministryofjustice/orgs","repos_url":"https://api.github.com/users/ministryofjustice/repos","events_url":"https://api.github.com/users/ministryofjustice/events{/privacy}","received_events_url":"https://api.github.com/users/ministryofjustice/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ministryofjustice/fr-staffapp","description":"Help with Court Fees Staff Interface","fork":false,"url":"https://api.github.com/repos/ministryofjustice/fr-staffapp","forks_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/forks","keys_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/teams","hooks_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/hooks","issue_events_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/events{/number}","events_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/events","assignees_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/assignees{/user}","branches_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/branches{/branch}","tags_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/tags","blobs_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/refs{/sha}","trees_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/statuses/{sha}","languages_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/languages","stargazers_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/stargazers","contributors_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/contributors","subscribers_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/subscribers","subscription_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/subscription","commits_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/commits{/sha}","git_commits_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/git/commits{/sha}","comments_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/comments{/number}","issue_comment_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/comments{/number}","contents_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/contents/{+path}","compare_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/merges","archive_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/downloads","issues_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues{/number}","pulls_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls{/number}","milestones_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/milestones{/number}","notifications_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/labels{/name}","releases_url":"https://api.github.com/repos/ministryofjustice/fr-staffapp/releases{/id}","created_at":"2015-02-06T16:01:04Z","updated_at":"2016-01-11T17:17:54Z","pushed_at":"2016-01-20T10:12:03Z","git_url":"git://github.com/ministryofjustice/fr-staffapp.git","ssh_url":"git@github.com:ministryofjustice/fr-staffapp.git","clone_url":"https://github.com/ministryofjustice/fr-staffapp.git","svn_url":"https://github.com/ministryofjustice/fr-staffapp","homepage":"","size":3683,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":7,"forks":0,"open_issues":7,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536"},"html":{"href":"https://github.com/ministryofjustice/fr-staffapp/pull/536"},"issue":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/536"},"comments":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/issues/536/comments"},"review_comments":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536/comments"},"review_comment":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/pulls/536/commits"},"statuses":{"href":"https://api.github.com/repos/ministryofjustice/fr-staffapp/statuses/ed37fb1df1c7509e9dd3fa16b54bd79aba795f86"}}}}