Add: Sync courses
This commit is contained in:
parent
549fe05d60
commit
d42cee55af
app
database
tests/Feature/Initialize
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Course extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
public $guarded = [];
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Initialize;
|
||||
|
||||
use App\Course;
|
||||
use Aweos\Agnoster\Progress\Progress;
|
||||
use Zoomyboy\LaravelNami\Api;
|
||||
use Zoomyboy\LaravelNami\NamiUser;
|
||||
|
||||
class InitializeCourses {
|
||||
|
||||
private Progress $bar;
|
||||
private Api $api;
|
||||
|
||||
public function __construct(Progress $bar, Api $api) {
|
||||
$this->bar = $bar;
|
||||
$this->api = $api;
|
||||
}
|
||||
|
||||
public function handle(NamiUser $user): void
|
||||
{
|
||||
$this->api->courses()->each(function($course) {
|
||||
Course::create(['nami_id' => $course->id, 'name' => $course->name]);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ class InitializeJob implements ShouldQueue
|
|||
InitializeGenders::class,
|
||||
InitializeRegions::class,
|
||||
InitializeActivities::class,
|
||||
InitializeCourses::class,
|
||||
InitializeMembers::class,
|
||||
];
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace App\Initialize;
|
|||
use App\Activity;
|
||||
use App\Confession;
|
||||
use App\Country;
|
||||
use App\Course;
|
||||
use App\Fee;
|
||||
use App\Gender;
|
||||
use App\Group;
|
||||
|
@ -72,6 +73,15 @@ class InitializeMembers {
|
|||
'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id,
|
||||
'version' => $member->version,
|
||||
]);
|
||||
|
||||
foreach ($this->api->coursesFor($member->id) as $course) {
|
||||
$m->courses()->attach(Course::where('nami_id', $course->course_id)->firstOrFail(), [
|
||||
'organizer' => $course->organizer,
|
||||
'event_name' => $course->event_name,
|
||||
'completed_at' => $course->completed_at,
|
||||
'nami_id' => $course->id,
|
||||
]);
|
||||
}
|
||||
} catch (ModelNotFoundException $e) {
|
||||
dd($e->getMessage(), $member);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ use App\Activity;
|
|||
use App\Bill\BillKind;
|
||||
use App\Confession;
|
||||
use App\Country;
|
||||
use App\Course;
|
||||
use App\Group;
|
||||
use App\Nationality;
|
||||
use App\Payment\Payment;
|
||||
|
@ -16,6 +17,7 @@ use Illuminate\Database\Eloquent\Builder;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Zoomyboy\LaravelNami\Api;
|
||||
|
@ -147,6 +149,11 @@ class Member extends Model
|
|||
return $this->belongsTo(Subactivity::class, 'first_subactivity_id');
|
||||
}
|
||||
|
||||
public function courses(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Course::class)->withPivot(['organizer', 'completed_at', 'event_name']);
|
||||
}
|
||||
|
||||
public static function booted()
|
||||
{
|
||||
static::deleting(function(self $model): void {
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class CourseFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateCoursesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('courses', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedInteger('nami_id');
|
||||
$table->string('name');
|
||||
});
|
||||
Schema::create('course_member', function($table) {
|
||||
$table->foreignId('member_id')->constrained();
|
||||
$table->foreignId('course_id')->constrained();
|
||||
$table->string('organizer');
|
||||
$table->string('event_name');
|
||||
$table->unsignedInteger('nami_id');
|
||||
$table->date('completed_at');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('courses');
|
||||
}
|
||||
}
|
|
@ -4,7 +4,9 @@ namespace Tests\Feature\Initialize;
|
|||
|
||||
use App\Activity;
|
||||
use App\Country;
|
||||
use App\Course;
|
||||
use App\Gender;
|
||||
use App\Member\Member;
|
||||
use App\Nationality;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
|
@ -28,6 +30,7 @@ class InitializeTest extends TestCase
|
|||
->fakeCountries([['name' => 'Germany', 'id' => 302]])
|
||||
->fakeGenders([['name' => 'Male', 'id' => 303]])
|
||||
->fakeRegions([['name' => 'nrw', 'id' => 304]])
|
||||
->fakeCourses([['name' => '1a', 'id' => 506]])
|
||||
->fakeActivities(1000, [['name' => 'leiter', 'id' => 305]]);
|
||||
|
||||
if (!$callback) {
|
||||
|
@ -59,7 +62,7 @@ class InitializeTest extends TestCase
|
|||
]);
|
||||
}
|
||||
|
||||
public function testItInitializesGenders(): void
|
||||
public function testItInitializesAll(): void
|
||||
{
|
||||
$this->withoutExceptionHandling();
|
||||
$this->initializeProvider();
|
||||
|
@ -98,6 +101,10 @@ class InitializeTest extends TestCase
|
|||
'name' => 'Leiter',
|
||||
'nami_id' => 305
|
||||
]);
|
||||
$this->assertDatabaseHas('courses', [
|
||||
'name' => '1a',
|
||||
'nami_id' => 506
|
||||
]);
|
||||
$this->assertDatabaseHas('groups', ['nami_id' => 1000, 'name' => '::group::']);
|
||||
$this->assertDatabaseHas('members', [
|
||||
'nami_id' => 411,
|
||||
|
@ -112,7 +119,32 @@ class InitializeTest extends TestCase
|
|||
]);
|
||||
$this->assertEquals([306], Activity::where('nami_id', 305)->firstOrFail()->subactivities()->pluck('nami_id')->toArray());
|
||||
|
||||
Http::assertSentCount(13);
|
||||
Http::assertSentCount(15);
|
||||
}
|
||||
|
||||
public function testSyncCoursesOfMember(): void
|
||||
{
|
||||
$this->withoutExceptionHandling();
|
||||
$this->initializeProvider(function($backend) {
|
||||
$backend->fakeMembers([
|
||||
$this->member(['courses' => [ ['bausteinId' => 506, 'id' => 788, 'veranstalter' => 'KJA', 'vstgName' => 'eventname', 'vstgTag' => '2021-11-12 00:00:00'] ]])
|
||||
]);
|
||||
});
|
||||
$this->post('/login', [
|
||||
'mglnr' => 123,
|
||||
'password' => 'secret',
|
||||
]);
|
||||
|
||||
$this->post('/initialize');
|
||||
|
||||
$this->assertDatabaseHas('course_member', [
|
||||
'member_id' => Member::where('firstname', '::firstname::')->firstOrFail()->id,
|
||||
'course_id' => Course::where('name', '1a')->firstOrFail()->id,
|
||||
'event_name' => 'eventname',
|
||||
'completed_at' => '2021-11-12',
|
||||
'organizer' => 'KJA',
|
||||
'nami_id' => 788,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItDoesntGetMembersWithNoJoinedAtDate(): void
|
||||
|
|
Loading…
Reference in New Issue