Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Database\Factories;
|
||||
|
||||
use Functional\Users\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Technical\Integrations\Models\ExternalActivity;
|
||||
use Technical\Integrations\Models\Integration;
|
||||
|
||||
/**
|
||||
* @extends Factory<ExternalActivity>
|
||||
*/
|
||||
class ExternalActivityFactory extends Factory
|
||||
{
|
||||
protected $model = ExternalActivity::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$integration = Integration::factory()->create();
|
||||
|
||||
return [
|
||||
'integration_id' => $integration,
|
||||
'user_id' => $integration->user_id,
|
||||
'provider' => $integration->provider,
|
||||
'external_id' => (string) fake()->unique()->numberBetween(1000000, 9999999),
|
||||
'type' => fake()->randomElement(['run', 'ride', 'swim', 'walk', 'workout']),
|
||||
'started_at' => fake()->dateTimeBetween('-3 months', 'now'),
|
||||
'duration_seconds' => fake()->numberBetween(600, 7200),
|
||||
'distance_meters' => fake()->optional(0.8)->randomFloat(2, 500, 42000),
|
||||
'calories' => fake()->optional(0.8)->numberBetween(100, 2000),
|
||||
'raw' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Database\Factories;
|
||||
|
||||
use Functional\Users\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Technical\Integrations\Enums\IntegrationStatus;
|
||||
use Technical\Integrations\Models\Integration;
|
||||
|
||||
/**
|
||||
* @extends Factory<Integration>
|
||||
*/
|
||||
class IntegrationFactory extends Factory
|
||||
{
|
||||
protected $model = Integration::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$connectedAt = fake()->dateTimeBetween('-6 months', 'now');
|
||||
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'provider' => fake()->randomElement(['strava', 'garmin', 'polar', 'suunto']),
|
||||
'external_user_id' => (string) fake()->numberBetween(100000, 999999),
|
||||
'access_token' => fake()->sha256(),
|
||||
'refresh_token' => fake()->sha256(),
|
||||
'token_expires_at' => fake()->dateTimeBetween('now', '+1 month'),
|
||||
'scopes' => fake()->randomElements(['activity:read', 'activity:read_all', 'profile:read'], 2),
|
||||
'status' => IntegrationStatus::Connected,
|
||||
'connected_at' => $connectedAt,
|
||||
'last_synced_at' => fake()->optional(0.7)->dateTimeBetween($connectedAt, 'now'),
|
||||
'meta' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/** Intégration dont l'utilisateur a révoqué l'accès côté fournisseur. */
|
||||
public function revoked(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => ['status' => IntegrationStatus::Revoked]);
|
||||
}
|
||||
|
||||
/** Intégration en erreur (ex : refresh token invalide). */
|
||||
public function error(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => ['status' => IntegrationStatus::Error]);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// Compte connecté d'un utilisateur chez un fournisseur externe (Garmin/Strava/...).
|
||||
// Couche générique : le nom du fournisseur n'est qu'une string, jamais une référence en dur.
|
||||
Schema::create('integrations', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained();
|
||||
$table->string('provider'); // clé du fournisseur, ex "strava" (cf ActivityProvider::key())
|
||||
$table->string('external_user_id')->nullable(); // identifiant du compte côté fournisseur
|
||||
$table->text('access_token')->nullable(); // cast "encrypted"
|
||||
$table->text('refresh_token')->nullable(); // cast "encrypted"
|
||||
$table->timestamp('token_expires_at')->nullable();
|
||||
$table->json('scopes')->nullable(); // permissions OAuth accordées
|
||||
$table->string('status')->default('connected'); // cast PHP IntegrationStatus, pas d'enum SQL
|
||||
$table->timestamp('connected_at');
|
||||
$table->timestamp('last_synced_at')->nullable();
|
||||
$table->json('meta')->nullable(); // données libres propres au fournisseur
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'provider']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('integrations');
|
||||
}
|
||||
};
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// Activité importée depuis un fournisseur externe (course, sortie vélo, etc.), rattachée à l'intégration qui l'a récupérée.
|
||||
Schema::create('external_activities', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('integration_id')->constrained();
|
||||
$table->foreignId('user_id')->constrained();
|
||||
$table->string('provider'); // dupliqué depuis l'intégration pour requêter sans jointure
|
||||
$table->string('external_id'); // identifiant de l'activité côté fournisseur
|
||||
$table->string('type'); // ex "run", "ride", vocabulaire propre au fournisseur
|
||||
$table->timestamp('started_at');
|
||||
$table->unsignedInteger('duration_seconds')->nullable();
|
||||
$table->decimal('distance_meters', 10, 2)->nullable();
|
||||
$table->unsignedInteger('calories')->nullable();
|
||||
$table->json('raw')->nullable(); // payload brut du fournisseur, pour ré-exploitation future
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['provider', 'external_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('external_activities');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class IntegrationsSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user