File size: 2,018 Bytes
89a1081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

namespace App\Models;

use App\Enums\CommunicationStatusEnum;
use App\Enums\ServiceType;
use App\Traits\Filterable;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;


class CommunicationLog extends Model
{
    use HasFactory, Notifiable, Filterable;

    protected $guarded = [];

    protected $casts = [

        'meta_data' => 'json',
        'message'   => 'json',
        'file_info' => 'json',
    ];
    protected static function booted() {

        static::creating(function (Model $model) {
            
            $model->uid = substr(Str::uuid(), 0, 32);
        });
    }

    public function scopeSms($query) {

        return $query->where('type', ServiceType::SMS->value);
    }
    public function scopeWhatsapp($query) {

        return $query->where('type', ServiceType::WHATSAPP->value);
    }
    public function scopeEmail($query) {

        return $query->where('type', ServiceType::EMAIL->value);
    }

    public function scopeRoutefilter(Builder $q) :Builder {

        return $q->when(request()->routeIs('*.communication.sms.index'),function($query) {
            
            return $query->sms();
        })->when(request()->routeIs('*.communication.whatsapp.index'),function($query) {
            
            return $query->whatsapp();
        })->when(request()->routeIs('*.communication.email.index'),function($query) {
            
            return $query->email();
        });
    }

    public function user() {

        return $this->belongsTo(User::class, 'user_id');
    }

    public function simInfo()
    {
    	return $this->belongsTo(AndroidApiSimInfo::class, 'android_gateway_sim_id');
    }

    public function whatsappGateway()
	{
		return $this->belongsTo(WhatsappDevice::class, 'gateway_id');
	}

    public function sender()
    {
    	return $this->belongsTo(Gateway::class, 'gateway_id');
    }

}