sync members

This commit is contained in:
philipp lang 2021-04-10 01:39:39 +02:00
parent 0800dc4a3d
commit 9568c630f0
3 changed files with 42 additions and 62 deletions

View File

@ -2,6 +2,13 @@
namespace App\Initialize; namespace App\Initialize;
use App\Gender;
use App\Confession;
use App\Country;
use App\Member\Member;
use App\Region;
use App\Nationality;
class InitializeMembers { class InitializeMembers {
private $bar; private $bar;
@ -15,60 +22,35 @@ class InitializeMembers {
public function handle() { public function handle() {
$allMembers = collect([]); $allMembers = collect([]);
$this->bar->task('Finde Mitglieder', function() use (&$allMembers) { $this->bar->task('Synchronisiere Mitglieder', function() {
$allMembers = collect($this->api->allMembers()->data); $this->api->group(auth()->user()->getNamiGroupId())->members()->each(function($member) {
}); $m = Member::create([
'firstname' => $member->firstname,
$this->bar->tasks($allMembers, function($member) { 'lastname' => $member->lastname,
return "Synchronisiere {$member->entries_vorname} {$member->entries_nachname}"; 'nickname' => $member->nickname,
}, function($member) { 'joined_at' => $member->joined_at,
$data = $this->api->getMember($member->id)->data; 'birthday' => $member->birthday,
$gender = \App\Gender::where('nami_id', $data->geschlechtId)->where('is_null', false)->first(); 'sendnewspaper' => true, // @todo implement in nami api
'address' => $member->address,
$confession = $data->konfessionId 'zip' => $member->zip,
? \App\Confession::where('nami_id', $data->konfessionId)->first() 'location' => $member->location,
: null 'nickname' => $member->nickname,
; 'other_country' => $member->other_country,
$region = \App\Region::where('nami_id', $data->regionId)->where('is_null', false)->first(); 'further_address' => $member->further_address,
$country = \App\Country::where('nami_id', $data->landId)->first(); 'main_phone' => $member->main_phone,
$nationality = \App\Nationality::where('nami_id', $data->staatsangehoerigkeitId)->first(); 'mobile_phone' => $member->mobile_phone,
'work_phone' => $member->work_phone,
$sub = null; 'fax' => $member->fax,
'email' => $member->email,
$attributes = [ 'email_parents' => $member->email_parents,
'firstname' => $data->vorname, 'nami_id' => $member->id,
'lastname' => $data->nachname, 'gender_id' => Gender::firstOrFail('nami_id', $member->gender_id)->id,
'nickname' => $data->spitzname, 'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id))->id,
'joined_at' => $data->eintrittsdatum, 'region_id' => 1, // @todo implement in nami api
'birthday' => $data->geburtsDatum, 'country_id' => 1, // @todo implement in nami api
'keepdata' => $data->wiederverwendenFlag, 'nationality_id' => 1, // @todo implement in nami api
'sendnewspaper' => $data->zeitschriftenversand, ]);
'address' => $data->strasse, });
'zip' => $data->plz,
'city' => $data->ort,
'nickname' => $data->spitzname,
'other_country' => $data->staatsangehoerigkeitText,
'further_address' => $data->nameZusatz,
'phone' => $data->telefon1,
'mobile' => $data->telefon2,
'business_phone' => $data->telefon3,
'fax' => $data->telefax,
'email' => $data->email,
'email_parents' => $data->emailVertretungsberechtigter,
'nami_id' => $data->id,
'active' => $data->status == 'Aktiv'
];
$m = new \App\Member\Member($attributes);
$m->gender()->associate($gender);
$m->country()->associate($country);
$m->region()->associate($region);
// $m->way()->associate(\Setting::get('defaultWay'));
$m->confession()->associate($confession);
$m->nationality()->associate($nationality);
$m->save();
}); });
} }
} }

View File

@ -10,12 +10,11 @@ class Member extends Model
{ {
use Notifiable; use Notifiable;
public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'sendnewspaper', 'address', 'further_address', 'zip', 'city', 'phone', 'mobile', 'business_phone', 'fax', 'email', 'email_parents', 'nami_id', 'active', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'subscription_id', 'region_id', 'gender_id', 'confession_id']; public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'sendnewspaper', 'address', 'further_address', 'zip', 'location', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'nami_id', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'subscription_id', 'region_id', 'gender_id', 'confession_id'];
public $dates = ['joined_at', 'birthday']; public $dates = ['joined_at', 'birthday'];
public $casts = [ public $casts = [
'active' => 'boolean',
'sendnewspaper' => 'boolean', 'sendnewspaper' => 'boolean',
'gender_id' => 'integer', 'gender_id' => 'integer',
'way_id' => 'integer', 'way_id' => 'integer',
@ -30,7 +29,7 @@ class Member extends Model
->orWhere('lastname', 'LIKE', '%'.$text.'%') ->orWhere('lastname', 'LIKE', '%'.$text.'%')
->orWhere('address', 'LIKE', '%'.$text.'%') ->orWhere('address', 'LIKE', '%'.$text.'%')
->orWhere('zip', 'LIKE', '%'.$text.'%') ->orWhere('zip', 'LIKE', '%'.$text.'%')
->orWhere('city', 'LIKE', '%'.$text.'%'); ->orWhere('location', 'LIKE', '%'.$text.'%');
} }

View File

@ -28,15 +28,14 @@ class CreateMembersTable extends Migration
$table->string('address'); $table->string('address');
$table->string('further_address')->nullable(); $table->string('further_address')->nullable();
$table->string('zip'); $table->string('zip');
$table->string('city'); $table->string('location');
$table->string('region_id')->nullable(); $table->string('region_id')->nullable();
$table->string('phone')->nullable(); $table->string('main_phone')->nullable();
$table->string('mobile')->nullable(); $table->string('mobile_phone')->nullable();
$table->string('business_phone')->nullable(); $table->string('work_phone')->nullable();
$table->string('fax')->nullable(); $table->string('fax')->nullable();
$table->string('email')->nullable(); $table->string('email')->nullable();
$table->string('email_parents')->nullable(); $table->string('email_parents')->nullable();
$table->boolean('active')->default(1);
$table->integer('nami_id')->nullable(); $table->integer('nami_id')->nullable();
$table->integer('nationality_id')->unsigned(); $table->integer('nationality_id')->unsigned();
$table->integer('subscription_id')->nullable(); $table->integer('subscription_id')->nullable();