SMS service

This commit is contained in:
Simon Pocrnjič
2025-10-24 21:39:10 +02:00
parent 3a2eed7dda
commit 930ac83604
52 changed files with 3830 additions and 36 deletions
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Database\Factories;
use App\Models\SmsProfile;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends Factory<SmsProfile>
*/
class SmsProfileFactory extends Factory
{
protected $model = SmsProfile::class;
public function definition(): array
{
return [
'uuid' => (string) Str::uuid(),
'name' => $this->faker->unique()->words(2, true),
'active' => true,
'api_username' => $this->faker->userName(),
// use attribute mutator to encrypt
'api_password' => 'secret-'.Str::random(8),
'settings' => [],
];
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace Database\Factories;
use App\Models\SmsProfile;
use App\Models\SmsSender;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<SmsSender>
*/
class SmsSenderFactory extends Factory
{
protected $model = SmsSender::class;
public function definition(): array
{
return [
'profile_id' => SmsProfile::factory(),
'sname' => strtoupper($this->faker->lexify('SND??')),
'description' => $this->faker->sentence(4),
'active' => true,
];
}
}
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('sms_profiles', function (Blueprint $table): void {
$table->id();
$table->uuid('uuid')->unique();
$table->string('name');
$table->boolean('active')->default(true);
// Credentials
$table->string('api_username');
$table->text('encrypted_api_password')->nullable();
// Defaults and settings
$table->unsignedBigInteger('default_sender_id')->nullable(); // no FK to avoid circular dep with sms_senders
$table->json('settings')->nullable();
$table->timestamps();
$table->index(['active']);
$table->index(['name']);
});
}
public function down(): void
{
Schema::dropIfExists('sms_profiles');
}
};
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('sms_senders', function (Blueprint $table): void {
$table->id();
$table->foreignId('profile_id')->constrained('sms_profiles')->cascadeOnDelete();
$table->string('sname'); // provider sender ID
$table->string('description')->nullable();
$table->boolean('active')->default(true);
$table->timestamps();
$table->unique(['profile_id', 'sname']);
$table->index(['active']);
});
}
public function down(): void
{
Schema::dropIfExists('sms_senders');
}
};
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('sms_templates', function (Blueprint $table): void {
$table->id();
$table->uuid('uuid')->unique();
$table->string('name');
$table->string('slug')->unique();
$table->text('content');
$table->json('variables_json')->nullable();
$table->boolean('is_active')->default(true);
$table->foreignId('default_profile_id')->nullable()->constrained('sms_profiles')->nullOnDelete();
$table->foreignId('default_sender_id')->nullable()->constrained('sms_senders')->nullOnDelete();
$table->timestamps();
$table->index(['is_active']);
});
}
public function down(): void
{
Schema::dropIfExists('sms_templates');
}
};
@@ -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
{
public function up(): void
{
Schema::create('sms_logs', function (Blueprint $table): void {
$table->id();
$table->uuid('uuid')->unique();
$table->foreignId('profile_id')->constrained('sms_profiles')->cascadeOnDelete();
$table->foreignId('template_id')->nullable()->constrained('sms_templates')->nullOnDelete();
$table->string('to_number');
$table->string('sender')->nullable(); // sname used when available
$table->text('message');
$table->enum('status', ['queued', 'sent', 'failed', 'delivered'])->default('queued');
$table->string('provider_message_id')->nullable();
$table->string('error_code')->nullable();
$table->text('error_message')->nullable();
$table->decimal('cost', 10, 2)->nullable();
$table->char('currency', 3)->nullable();
$table->json('meta')->nullable();
$table->timestamp('queued_at')->nullable();
$table->timestamp('sent_at')->nullable();
$table->timestamp('delivered_at')->nullable();
$table->timestamp('failed_at')->nullable();
$table->timestamps();
$table->index(['status']);
$table->index(['provider_message_id']);
$table->index(['created_at']);
});
}
public function down(): void
{
Schema::dropIfExists('sms_logs');
}
};
@@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('sms_senders', function (Blueprint $table): void {
$table->string('phone_number', 30)->nullable()->after('sname');
});
}
public function down(): void
{
Schema::table('sms_senders', function (Blueprint $table): void {
$table->dropColumn('phone_number');
});
}
};
@@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('sms_senders', function (Blueprint $table): void {
// Make Sender ID nullable
$table->string('sname', 255)->nullable()->change();
});
}
public function down(): void
{
Schema::table('sms_senders', function (Blueprint $table): void {
// Revert to not nullable
$table->string('sname', 255)->nullable(false)->change();
});
}
};
@@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('sms_templates', function (Blueprint $table) {
$table->boolean('allow_custom_body')->default(false)->after('content');
$table->foreignId('action_id')->nullable()->after('default_sender_id')->constrained('actions')->nullOnDelete();
$table->foreignId('decision_id')->nullable()->after('action_id')->constrained('decisions')->nullOnDelete();
});
}
public function down(): void
{
Schema::table('sms_templates', function (Blueprint $table) {
$table->dropConstrainedForeignId('decision_id');
$table->dropConstrainedForeignId('action_id');
$table->dropColumn('allow_custom_body');
});
}
};