diff --git a/app/Activity.php b/app/Activity.php index 49919d7f..f37853c1 100644 --- a/app/Activity.php +++ b/app/Activity.php @@ -18,4 +18,8 @@ class Activity extends Model public function subactivities() { return $this->belongsToMany(Subactivity::class); } + + public static function nami($id) { + return static::firstWhere('nami_id', $id); + } } diff --git a/app/Group.php b/app/Group.php index 3b1ccd99..58322289 100644 --- a/app/Group.php +++ b/app/Group.php @@ -11,4 +11,8 @@ class Group extends Model public $fillable = ['nami_id', 'name']; public $timestamps = false; + + public static function nami($id) { + return static::firstWhere('nami_id', $id); + } } diff --git a/app/Member/CreateJob.php b/app/Member/CreateJob.php index 79020e1d..55f03dde 100644 --- a/app/Member/CreateJob.php +++ b/app/Member/CreateJob.php @@ -9,6 +9,8 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Zoomyboy\LaravelNami\Nami; use App\Confession; +use App\Group; +use App\Activity; class CreateJob implements ShouldQueue { @@ -37,7 +39,7 @@ class CreateJob implements ShouldQueue return false; } - $response = Nami::login($this->user->mglnr)->putMember([ + $response = $this->user->api()->putMember([ 'firstname' => $this->member->firstname, 'lastname' => $this->member->lastname, 'nickname' => $this->member->nickname, @@ -70,5 +72,16 @@ class CreateJob implements ShouldQueue $version = Nami::login($this->user->mglnr)->member($this->member->group->nami_id, $response['id'])['version']; $this->member->update(['version' => $version, 'nami_id' => $response['id']]); }); + + $memberships = $this->member->getNamiMemberships($this->user->api()); + foreach ($memberships as $membership) { + $this->member->memberships()->create([ + 'activity_id' => Activity::nami($membership['activity_id'])->id, + 'group_id' => Group::nami($membership['group_id'])->id, + 'nami_id' => $membership['id'], + 'created_at' => $membership['starts_at'], + ]); + } + } } diff --git a/app/Member/Member.php b/app/Member/Member.php index e6c3ac18..20ddc498 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -12,6 +12,7 @@ use App\Fee; use App\Group; use App\Activity; use App\Subactivity; +use Zoomyboy\LaravelNami\NamiUser; class Member extends Model { @@ -88,7 +89,7 @@ class Member extends Model public function memberships() { - return $this->hasMany(App\Membership::class); + return $this->hasMany(Membership::class); } public function fee() @@ -119,4 +120,9 @@ class Member extends Model } }); } + + public function getNamiMemberships($api) { + return $api->group($this->group->nami_id)->member($this->nami_id)->memberships()->toArray(); + } + } diff --git a/app/Member/Membership.php b/app/Member/Membership.php new file mode 100644 index 00000000..7ffb598e --- /dev/null +++ b/app/Member/Membership.php @@ -0,0 +1,13 @@ +