feat: enums, schema and models for hado core
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
9
core/app/Enums/AfterDue.php
Normal file
9
core/app/Enums/AfterDue.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum AfterDue: string
|
||||
{
|
||||
case Keep = 'keep';
|
||||
case Expire = 'expire';
|
||||
}
|
||||
10
core/app/Enums/ChannelType.php
Normal file
10
core/app/Enums/ChannelType.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum ChannelType: string
|
||||
{
|
||||
case Web = 'web';
|
||||
case Telegram = 'telegram';
|
||||
case Webhook = 'webhook';
|
||||
}
|
||||
10
core/app/Enums/DeliveryResult.php
Normal file
10
core/app/Enums/DeliveryResult.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum DeliveryResult: string
|
||||
{
|
||||
case Pending = 'pending';
|
||||
case Ok = 'ok';
|
||||
case Failed = 'failed';
|
||||
}
|
||||
9
core/app/Enums/DueMode.php
Normal file
9
core/app/Enums/DueMode.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum DueMode: string
|
||||
{
|
||||
case Local = 'local';
|
||||
case Fixed = 'fixed';
|
||||
}
|
||||
39
core/app/Enums/EventState.php
Normal file
39
core/app/Enums/EventState.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum EventState: string
|
||||
{
|
||||
case Scheduled = 'scheduled';
|
||||
case Preparing = 'preparing';
|
||||
case Today = 'today';
|
||||
case Overdue = 'overdue';
|
||||
case Done = 'done';
|
||||
case Expired = 'expired';
|
||||
case Withdrawn = 'withdrawn';
|
||||
|
||||
public function isTerminal(): bool
|
||||
{
|
||||
return in_array($this, [self::Done, self::Expired, self::Withdrawn], true);
|
||||
}
|
||||
|
||||
/** Состояния, в которых ядро напоминает. */
|
||||
public function isActive(): bool
|
||||
{
|
||||
return in_array($this, [self::Preparing, self::Today, self::Overdue], true);
|
||||
}
|
||||
|
||||
public function countsInBadge(): bool
|
||||
{
|
||||
return in_array($this, [self::Today, self::Overdue], true);
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public static function nonTerminalValues(): array
|
||||
{
|
||||
return array_values(array_map(
|
||||
fn (self $s) => $s->value,
|
||||
array_filter(self::cases(), fn (self $s) => ! $s->isTerminal()),
|
||||
));
|
||||
}
|
||||
}
|
||||
10
core/app/Enums/Presence.php
Normal file
10
core/app/Enums/Presence.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum Presence: string
|
||||
{
|
||||
case Present = 'present';
|
||||
case Absent = 'absent';
|
||||
case Unknown = 'unknown';
|
||||
}
|
||||
26
core/app/Models/Channel.php
Normal file
26
core/app/Models/Channel.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\ChannelType;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Channel extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => ChannelType::class,
|
||||
'config' => 'array',
|
||||
'enabled' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
34
core/app/Models/Delivery.php
Normal file
34
core/app/Models/Delivery.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\DeliveryResult;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Delivery extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
protected $attributes = ['meta' => '{}'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'result' => DeliveryResult::class,
|
||||
'window_start' => 'immutable_datetime',
|
||||
'sent_at' => 'immutable_datetime',
|
||||
'meta' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function event(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Event::class);
|
||||
}
|
||||
|
||||
public function channel(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Channel::class);
|
||||
}
|
||||
}
|
||||
50
core/app/Models/Event.php
Normal file
50
core/app/Models/Event.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\AfterDue;
|
||||
use App\Enums\DueMode;
|
||||
use App\Enums\EventState;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Event extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'due_mode' => DueMode::class,
|
||||
'after_due' => AfterDue::class,
|
||||
'state' => EventState::class,
|
||||
'due_date' => 'immutable_date',
|
||||
'fire_on' => 'immutable_date',
|
||||
'due_instant' => 'immutable_datetime',
|
||||
'quiet_until' => 'immutable_datetime',
|
||||
'done_at' => 'immutable_datetime',
|
||||
'payload' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function source(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Source::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function deliveries(): HasMany
|
||||
{
|
||||
return $this->hasMany(Delivery::class);
|
||||
}
|
||||
|
||||
public function isTerminal(): bool
|
||||
{
|
||||
return $this->state->isTerminal();
|
||||
}
|
||||
}
|
||||
16
core/app/Models/Source.php
Normal file
16
core/app/Models/Source.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Source extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function events(): HasMany
|
||||
{
|
||||
return $this->hasMany(Event::class);
|
||||
}
|
||||
}
|
||||
@@ -2,31 +2,39 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
class User extends Model
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
public function channels(): HasMany
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
return $this->hasMany(Channel::class);
|
||||
}
|
||||
|
||||
public function events(): HasMany
|
||||
{
|
||||
return $this->hasMany(Event::class);
|
||||
}
|
||||
|
||||
/** «Сейчас» в поясе пользователя. */
|
||||
public function now(): CarbonImmutable
|
||||
{
|
||||
return CarbonImmutable::now($this->tz);
|
||||
}
|
||||
|
||||
/** Начало активных часов в данный календарный день. */
|
||||
public function activeStart(CarbonImmutable $day): CarbonImmutable
|
||||
{
|
||||
return $day->startOfDay()->setTimeFromTimeString($this->quiet_end);
|
||||
}
|
||||
|
||||
/** Конец активных часов (начало тихих) в данный календарный день. */
|
||||
public function activeEnd(CarbonImmutable $day): CarbonImmutable
|
||||
{
|
||||
return $day->startOfDay()->setTimeFromTimeString($this->quiet_start);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user