Feature: Import users department. (#5987)
Maps to the "Department" header key by default. Bug: #5382
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+17
-17
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Vendored
+17
-17
File diff suppressed because one or more lines are too long
@@ -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"
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user