adrema/app/Member/CreateJob.php

90 lines
3.3 KiB
PHP
Raw Normal View History

2021-06-23 01:05:17 +02:00
<?php
namespace App\Member;
2021-08-22 21:40:27 +02:00
use App\Activity;
use App\Confession;
use App\Group;
use App\Subactivity;
2021-06-23 01:05:17 +02:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Zoomyboy\LaravelNami\Nami;
class CreateJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2022-02-12 15:35:58 +01:00
public int $memberId;
public Member $member;
2021-06-23 01:05:17 +02:00
2022-02-19 18:06:07 +01:00
public function __construct(Member $member)
2021-06-23 01:05:17 +02:00
{
$this->memberId = $member->id;
$this->user = $user;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->member = Member::find($this->memberId);
if ($this->member->hasNami) {
2022-01-02 12:32:57 +01:00
return;
2021-06-23 01:05:17 +02:00
}
2021-06-24 23:48:08 +02:00
$response = $this->user->api()->putMember([
2021-06-23 01:05:17 +02:00
'firstname' => $this->member->firstname,
'lastname' => $this->member->lastname,
'joined_at' => $this->member->joined_at,
'birthday' => $this->member->birthday,
'send_newspaper' => $this->member->send_newspaper,
'address' => $this->member->address,
'zip' => $this->member->zip,
'location' => $this->member->location,
'nickname' => $this->member->nickname,
'other_country' => $this->member->other_country,
'further_address' => $this->member->further_address,
'main_phone' => $this->member->main_phone,
'mobile_phone' => $this->member->mobile_phone,
'work_phone' => $this->member->work_phone,
'fax' => $this->member->fax,
'email' => $this->member->email,
'email_parents' => $this->member->email_parents,
'gender_id' => optional($this->member->gender)->nami_id,
'confession_id' => $this->member->confession ? $this->member->confession->nami_id : Confession::firstWhere('is_null', true)->id,
'region_id' => optional($this->member->region)->nami_id,
'country_id' => $this->member->country->nami_id,
2021-07-04 12:09:30 +02:00
'fee_id' => $this->member->getNamiFeeId(),
2021-06-23 01:05:17 +02:00
'nationality_id' => $this->member->nationality->nami_id,
'group_id' => $this->member->group->nami_id,
'first_activity_id' => $this->member->firstActivity->nami_id,
'first_subactivity_id' => $this->member->firstSubactivity->nami_id,
]);
Member::withoutEvents(function() use ($response) {
2021-06-23 01:58:03 +02:00
$version = Nami::login($this->user->mglnr)->member($this->member->group->nami_id, $response['id'])['version'];
$this->member->update(['version' => $version, 'nami_id' => $response['id']]);
2021-06-23 01:05:17 +02:00
});
2021-06-24 23:48:08 +02:00
$memberships = $this->member->getNamiMemberships($this->user->api());
foreach ($memberships as $membership) {
$this->member->memberships()->create([
2022-01-02 12:32:57 +01:00
'activity_id' => Activity::nami($membership['activity_id'])->id,
2021-08-22 21:40:27 +02:00
'subactivity_id' => $membership['subactivity_id']
? Subactivity::nami($membership['subactivity_id'])->id
2022-01-02 12:32:57 +01:00
: null,
2021-06-24 23:48:08 +02:00
'group_id' => Group::nami($membership['group_id'])->id,
'nami_id' => $membership['id'],
'created_at' => $membership['starts_at'],
]);
}
2021-06-23 01:05:17 +02:00
}
}