diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php index cd7e81a5b1..0d864f175b 100644 --- a/app/Importer/Importer.php +++ b/app/Importer/Importer.php @@ -66,6 +66,7 @@ abstract class Importer 'phone_number' => 'phone number', 'first_name' => 'first name', 'last_name' => 'last name', + 'department' => 'department' ]; /** * Map of item fields->csv names @@ -150,7 +151,7 @@ abstract class Importer // This 'inverts' the fields such that we have a collection of fields indexed by name. $this->customFields = CustomField::All()->reduce(function ($nameLookup, $field) { $nameLookup[$field['name']] = $field; - return $nameLookup; + return $nameLookup; }); // Remove any custom fields that do not exist in the header row. This prevents nulling out values that shouldn't exist. // In detail, we compare the lower case name of custom fields (indexed by name) to the keys in the header row. This diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php index 9a47948668..bdcceb69ab 100644 --- a/app/Importer/ItemImporter.php +++ b/app/Importer/ItemImporter.php @@ -147,7 +147,7 @@ class ItemImporter extends Importer * @param $field string * @return boolean */ - private function shouldUpdateField($field) + protected function shouldUpdateField($field) { if (empty($field)) { return false; diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php index 0e3ff01b83..e83aadb042 100644 --- a/app/Importer/UserImporter.php +++ b/app/Importer/UserImporter.php @@ -3,6 +3,7 @@ namespace App\Importer; use App\Helpers\Helper; +use App\Models\Department; use App\Models\User; use App\Notifications\WelcomeNotification; @@ -39,6 +40,10 @@ class UserImporter extends ItemImporter $this->item['phone'] = $this->findCsvMatch($row, 'phone_number'); $this->item['jobtitle'] = $this->findCsvMatch($row, 'jobtitle'); $this->item['employee_num'] = $this->findCsvMatch($row, 'employee_num'); + $user_department = $this->findCsvMatch($row, 'department'); + if ($this->shouldUpdateField($user_department)) { + $this->item["department_id"] = $this->createOrFetchDepartment($user_department); + } $user = User::where('username', $this->item['username'])->first(); if ($user) { if (!$this->updating) { @@ -78,4 +83,31 @@ class UserImporter extends ItemImporter $this->logError($user, 'User'); return; } + + /** + * Fetch an existing department, or create new if it doesn't exist + * + * @author Daniel Melzter + * @since 5.0 + * @param $department_name string + * @return int id of department created/found + */ + public function createOrFetchDepartment($department_name) + { + $department = Department::where(['name' => $department_name])->first(); + if ($department) { + $this->log('A matching department ' . $department_name . ' already exists'); + return $department->id; + } + $department = new department(); + $department->name = $department_name; + $department->user_id = $this->user_id; + + if ($department->save()) { + $this->log('department ' . $department_name . ' was created'); + return $department->id; + } + $this->logError($department, 'Company'); + return null; + } } diff --git a/public/js/build/all.js b/public/js/build/all.js index 5f763a4ab4..20825b9ba6 100644 Binary files a/public/js/build/all.js and b/public/js/build/all.js differ diff --git a/public/js/build/vue.js b/public/js/build/vue.js index f27c653cc5..dca1c33b0b 100644 Binary files a/public/js/build/vue.js and b/public/js/build/vue.js differ diff --git a/public/js/build/vue.js.map b/public/js/build/vue.js.map index 11085b1750..6091f4b50d 100644 Binary files a/public/js/build/vue.js.map and b/public/js/build/vue.js.map differ diff --git a/public/js/dist/all.js b/public/js/dist/all.js index 5f763a4ab4..20825b9ba6 100644 Binary files a/public/js/dist/all.js and b/public/js/dist/all.js differ diff --git a/public/mix-manifest.json b/public/mix-manifest.json index 462b97e51e..b9e9f646ac 100644 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -1,14 +1,14 @@ { - "/js/build/vue.js": "/js/build/vue.js?id=832c22cb5b66ac81ed06", + "/js/build/vue.js": "/js/build/vue.js?id=51d77ccb198b24fcc478", "/css/AdminLTE.css": "/css/AdminLTE.css?id=5e72463a66acbcc740d5", "/css/app.css": "/css/app.css?id=407edb63cc6b6dc62405", "/css/overrides.css": "/css/overrides.css?id=2d81c3704393bac77011", - "/js/build/vue.js.map": "/js/build/vue.js.map?id=0deaf852882fe2d65263", + "/js/build/vue.js.map": "/js/build/vue.js.map?id=6607c7d7d7f7ccfd4e55", "/css/AdminLTE.css.map": "/css/AdminLTE.css.map?id=0be7790b84909dca6a0a", "/css/app.css.map": "/css/app.css.map?id=96b5c985e860716e6a16", "/css/overrides.css.map": "/css/overrides.css.map?id=f7ce9ca49027594ac402", "/css/dist/all.css": "/css/dist/all.css?id=98db4e9b7650453c8b00", - "/js/dist/all.js": "/js/dist/all.js?id=c9fd7cd517933dd8f567", + "/js/dist/all.js": "/js/dist/all.js?id=eed88600d0a80f50f170", "/css/build/all.css": "/css/build/all.css?id=98db4e9b7650453c8b00", - "/js/build/all.js": "/js/build/all.js?id=c9fd7cd517933dd8f567" + "/js/build/all.js": "/js/build/all.js?id=eed88600d0a80f50f170" } \ No newline at end of file diff --git a/resources/assets/js/components/importer/importer-file.vue b/resources/assets/js/components/importer/importer-file.vue index 7c8dbae002..9e5ee927fb 100644 --- a/resources/assets/js/components/importer/importer-file.vue +++ b/resources/assets/js/components/importer/importer-file.vue @@ -143,6 +143,7 @@ tr { {id: 'jobtitle', text: 'Job Title' }, {id: 'last_name', text: 'Last Name' }, {id: 'phone_number', text: 'Phone Number' }, + {id: 'department', text: 'Department'} ], customFields: this.customFields, diff --git a/tests/unit/ImporterTest.php b/tests/unit/ImporterTest.php index 37bb137b79..666305f00e 100644 --- a/tests/unit/ImporterTest.php +++ b/tests/unit/ImporterTest.php @@ -689,8 +689,8 @@ EOT; Notification::fake(); $this->signIn(); $csv = <<<'EOT' -First Name,Last Name,email,Username,Location,Phone Number,Job Title,Employee Number,Company -Blanche,O'Collopy,bocollopy0@livejournal.com,bocollopy0,Hinapalanan,63-(199)661-2186,Clinical Specialist,7080919053,Morar-Ward +First Name,Last Name,email,Username,Location,Phone Number,Job Title,Employee Number,Company,Department +Blanche,O'Collopy,bocollopy0@livejournal.com,bocollopy0,Hinapalanan,63-(199)661-2186,Clinical Specialist,7080919053,Morar-Ward,Management Jessie,Primo,,jprimo1,Korenovsk,7-(885)578-0266,Paralegal,6284292031,Jast-Stiedemann EOT; @@ -710,6 +710,10 @@ EOT; 'name' => 'Morar-Ward' ]); + $this->tester->seeRecord('departments', [ + 'name' => 'Management' + ]); + Notification::assertSentTo(User::find(2), \App\Notifications\WelcomeNotification::class); Notification::assertNotSentTo(User::find(3), \App\Notifications\WelcomeNotification::class); }