Model Helpdesk

B.M. Restu Pandhu P.

/ 8

Application.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Table(name: 'application')]
#[Fillable(['client_id', 'dedicated_technician_id', 'name', 'url', 'is_active'])]
class Application extends Model
{
    use HasUuids;

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
        ];
    }

    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'client_id');
    }

    public function dedicatedTechnician(): BelongsTo
    {
        return $this->belongsTo(User::class, 'dedicated_technician_id');
    }

    public function isClaimableBy(User $technician): bool
    {
        return $this->dedicated_technician_id === null || $this->dedicated_technician_id === $technician->id;
    }

    public function complaints(): HasMany
    {
        return $this->hasMany(Complaint::class, 'application_id');
    }
}

Client.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Table(name: 'client')]
#[Fillable([
    'name',
    'logo',
    'email',
    'phone',
    'address',
    'website',
    'person_responsible',
    'is_active',
])]
class Client extends Model
{
    use HasUuids;

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
        ];
    }

    /**
     * Get the complaints reported by this client.
     */
    public function complaints(): HasMany
    {
        return $this->hasMany(Complaint::class, 'client_id');
    }

    /**
     * Get the web applications/sites owned by this client (§ Aplikasi).
     */
    public function applications(): HasMany
    {
        return $this->hasMany(Application::class, 'client_id');
    }
}

Complaint.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Table(name: 'complaint')]
#[Fillable([
    'ticket_number',
    'subject',
    'description',
    'status',
    'priority',
    'price',
    'report_by',
    'client_id',
    'application_id',
    'assigned_technician_id',
    'photo',
    'report_date',
])]
class Complaint extends Model
{
    use HasUuids;

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'photo' => 'array',
            'report_date' => 'datetime',
            'price' => 'decimal:2',
        ];
    }

    /**
     * Get the client that owns the complaint.
     */
    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'client_id');
    }

    /**
     * Get the specific web application/site this complaint is about.
     */
    public function application(): BelongsTo
    {
        return $this->belongsTo(Application::class, 'application_id');
    }

    /**
     * Get the technician currently assigned to this complaint.
     */
    public function assignedTechnician(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_technician_id');
    }

    /**
     * Get the responses given to the complaint.
     */
    public function responses(): HasMany
    {
        return $this->hasMany(Respons::class, 'complaint_id');
    }

    /**
     * Get the activity log entries for this complaint.
     */
    public function activities(): HasMany
    {
        return $this->hasMany(ComplaintActivity::class, 'complaint_id');
    }
}

ComplaintActivity.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Table(name: 'complaint_activity')]
#[Fillable([
    'complaint_id',
    'causer_id',
    'action',
    'note',
])]
class ComplaintActivity extends Model
{
    use HasUuids;

    /**
     * Activity log entries are append-only; there is no updated_at column.
     */
    const UPDATED_AT = null;

    /**
     * Get the complaint this activity belongs to.
     */
    public function complaint(): BelongsTo
    {
        return $this->belongsTo(Complaint::class, 'complaint_id');
    }

    /**
     * Get the user who caused this activity. Null for guest-triggered actions.
     */
    public function causer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'causer_id');
    }
}

Config.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;

#[Table(name: 'config')]
#[Fillable([
    'company_name',
    'company_logo',
    'company_email',
    'company_phone',
    'company_address',
    'company_website',
    'signer_name',
    'signer_title',
])]
class Config extends Model
{
    use HasUuids;
}

RecapReport.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Table(name: 'recap_report')]
#[Fillable([
    'client_id',
    'month',
    'year',
    'token',
    'reference_number',
    'client_name',
    'company_name',
    'signer_name',
    'signer_title',
    'complaint_count',
    'billable_count',
    'billable_total',
])]
class RecapReport extends Model
{
    use HasUuids;

    /**
     * Append-only snapshot; there is no updated_at column.
     */
    const UPDATED_AT = null;

    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }
}

Respons.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Table(name: 'respons')]
#[Fillable([
    'respons',
    'complaint_id',
    'technician_id',
    'respons_date',
    'photo',
])]
class Respons extends Model
{
    use HasUuids;

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'photo' => 'array',
            'respons_date' => 'datetime',
        ];
    }

    /**
     * Get the complaint this response belongs to.
     */
    public function complaint(): BelongsTo
    {
        return $this->belongsTo(Complaint::class, 'complaint_id');
    }

    /**
     * Get the technician who wrote the response.
     */
    public function technician(): BelongsTo
    {
        return $this->belongsTo(User::class, 'technician_id');
    }
}

User.php

<?php

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\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

#[Fillable(['name', 'username', 'email', 'password', 'role', 'logo', 'is_active', 'password_must_change', 'password_reset_code', 'password_reset_expires_at'])]
#[Hidden(['password', 'remember_token', 'password_reset_code'])]
class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, HasUuids, Notifiable;

    /**
     * Minutes an OTP code stays valid for the "lupa password" flow.
     */
    public const PASSWORD_RESET_CODE_EXPIRES_MINUTES = 15;

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'is_active' => 'boolean',
            'password_must_change' => 'boolean',
            'password_reset_expires_at' => 'datetime',
        ];
    }

    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    public function isTechnician(): bool
    {
        return $this->role === 'technician';
    }

    /**
     * Generate a fresh 6-digit OTP for the "lupa password" flow and persist it.
     */
    public function generatePasswordResetCode(): string
    {
        $code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);

        $this->update([
            'password_reset_code' => $code,
            'password_reset_expires_at' => now()->addMinutes(self::PASSWORD_RESET_CODE_EXPIRES_MINUTES),
        ]);

        return $code;
    }

    public function isPasswordResetCodeExpired(): bool
    {
        return $this->password_reset_expires_at === null || now()->greaterThan($this->password_reset_expires_at);
    }

    public function clearPasswordResetCode(): void
    {
        $this->update(['password_reset_code' => null, 'password_reset_expires_at' => null]);
    }

    /**
     * Get the responses written by this technician.
     */
    public function responses(): HasMany
    {
        return $this->hasMany(Respons::class, 'technician_id');
    }

    /**
     * Get the complaints currently assigned to this technician.
     */
    public function assignedComplaints(): HasMany
    {
        return $this->hasMany(Complaint::class, 'assigned_technician_id');
    }
}

Komentar

Belum ada komentar.