reconstruction of the project
This commit is contained in:
@@ -2,14 +2,19 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class WhatsappInviteLog extends BaseModel
|
||||
{
|
||||
protected $table = 'whatsapp_invites_log';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $useSoftDeletes = false;
|
||||
|
||||
/**
|
||||
* CI: timestamps disabled (we manage sent_at manually)
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'parent_id',
|
||||
'email',
|
||||
@@ -20,68 +25,155 @@ class WhatsappInviteLog extends BaseModel
|
||||
'sent_at',
|
||||
];
|
||||
|
||||
public $timestamps = false; // we handle sent_at manually
|
||||
const CREATED_AT = '';
|
||||
const UPDATED_AT = '';
|
||||
|
||||
// Validation (optional)
|
||||
protected $validationRules = [
|
||||
'parent_id' => 'required|integer',
|
||||
'email' => 'required|valid_email|max_length[255]',
|
||||
'status' => 'required|max_length[20]',
|
||||
'class_section_id' => 'permit_empty|integer',
|
||||
'link_id' => 'permit_empty|integer',
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'class_section_id' => 'integer',
|
||||
'link_id' => 'integer',
|
||||
'sent_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $validationMessages = [];
|
||||
protected $skipValidation = false;
|
||||
/* ============================================================
|
||||
* Status constants (optional but helpful)
|
||||
* ============================================================
|
||||
*/
|
||||
public const STATUS_SENT = 'sent';
|
||||
public const STATUS_FAILED = 'failed';
|
||||
|
||||
/* ============================================================
|
||||
* Relationships (optional)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
// If "parents" are users in your DB, keep User::class.
|
||||
// If you have a Parent model/table, change it accordingly.
|
||||
return $this->belongsTo(User::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function link(): BelongsTo
|
||||
{
|
||||
// Link row (whatsapp_group_links)
|
||||
return $this->belongsTo(WhatsappGroupLink::class, 'link_id');
|
||||
}
|
||||
|
||||
public function classSection(): BelongsTo
|
||||
{
|
||||
// Adjust keys if your ClassSection model uses a different PK
|
||||
return $this->belongsTo(ClassSection::class, 'class_section_id', 'class_section_id');
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Scopes
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function scopeForParent(Builder $q, int $parentId): Builder
|
||||
{
|
||||
return $q->where('parent_id', $parentId);
|
||||
}
|
||||
|
||||
public function scopeForSection(Builder $q, int $classSectionId): Builder
|
||||
{
|
||||
return $q->where('class_section_id', $classSectionId);
|
||||
}
|
||||
|
||||
public function scopeStatus(Builder $q, string $status): Builder
|
||||
{
|
||||
return $q->where('status', $status);
|
||||
}
|
||||
|
||||
public function scopeLatestFirst(Builder $q): Builder
|
||||
{
|
||||
return $q->orderByDesc('sent_at')->orderByDesc('id');
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* CI-compatible helpers
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Log a successful send.
|
||||
*/
|
||||
public function logSuccess(int $parentId, string $email, int $classSectionId = null, int $linkId = null): bool
|
||||
{
|
||||
return (bool) $this->insert([
|
||||
public static function logSuccess(
|
||||
int $parentId,
|
||||
string $email,
|
||||
?int $classSectionId = null,
|
||||
?int $linkId = null
|
||||
): bool {
|
||||
static::query()->create([
|
||||
'parent_id' => $parentId,
|
||||
'email' => $email,
|
||||
'class_section_id' => $classSectionId,
|
||||
'link_id' => $linkId,
|
||||
'status' => 'sent',
|
||||
'status' => self::STATUS_SENT,
|
||||
'error_message' => null,
|
||||
'sent_at' => utc_now(),
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a failure with error message.
|
||||
*/
|
||||
public function logFailure(int $parentId, string $email, string $error, int $classSectionId = null, int $linkId = null): bool
|
||||
{
|
||||
return (bool) $this->insert([
|
||||
public static function logFailure(
|
||||
int $parentId,
|
||||
string $email,
|
||||
string $error,
|
||||
?int $classSectionId = null,
|
||||
?int $linkId = null
|
||||
): bool {
|
||||
static::query()->create([
|
||||
'parent_id' => $parentId,
|
||||
'email' => $email,
|
||||
'class_section_id' => $classSectionId,
|
||||
'link_id' => $linkId,
|
||||
'status' => 'failed',
|
||||
'status' => self::STATUS_FAILED,
|
||||
'error_message' => $error,
|
||||
'sent_at' => utc_now(),
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get logs for a specific term, parent, or section (optional helper).
|
||||
* Get logs for a parent and/or section.
|
||||
* CI: getLogs($parentId = null, $classSectionId = null)
|
||||
*/
|
||||
public function getLogs(?int $parentId = null, ?int $classSectionId = null): array
|
||||
public static function getLogs(?int $parentId = null, ?int $classSectionId = null): array
|
||||
{
|
||||
$builder = $this->builder();
|
||||
$q = static::query();
|
||||
|
||||
if ($parentId) {
|
||||
$builder->where('parent_id', $parentId);
|
||||
$q->forParent($parentId);
|
||||
}
|
||||
if ($classSectionId) {
|
||||
$builder->where('class_section_id', $classSectionId);
|
||||
$q->forSection($classSectionId);
|
||||
}
|
||||
|
||||
return $builder->orderBy('sent_at', 'DESC')->get()->getResultArray();
|
||||
return $q->latestFirst()->get()->all();
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Optional validation helper (FormRequest/controller)
|
||||
* Mirrors CI validation intent
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public static function rules(bool $updating = false): array
|
||||
{
|
||||
$req = $updating ? 'sometimes' : 'required';
|
||||
|
||||
return [
|
||||
'parent_id' => [$req, 'integer', 'min:1'],
|
||||
'email' => [$req, 'email', 'max:255'],
|
||||
'status' => [$req, 'string', 'max:20'],
|
||||
'class_section_id' => ['nullable', 'integer'],
|
||||
'link_id' => ['nullable', 'integer'],
|
||||
'error_message' => ['nullable', 'string', 'max:5000'],
|
||||
'sent_at' => ['nullable', 'date'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user