|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* App\Models\Invitation. |
|
9
|
|
|
* |
|
10
|
|
|
* @property int $id |
|
11
|
|
|
* @property string $guid |
|
12
|
|
|
* @property int $users_id |
|
13
|
|
|
* @property string|null $created_at |
|
14
|
|
|
* @property string|null $updated_at |
|
15
|
|
|
* @property-read \App\Models\User $user |
|
16
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation whereCreatedAt($value) |
|
17
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation whereGuid($value) |
|
18
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation whereId($value) |
|
19
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation whereUpdatedAt($value) |
|
20
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation whereUsersId($value) |
|
21
|
|
|
* @mixin \Eloquent |
|
22
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation newModelQuery() |
|
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation newQuery() |
|
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Invitation query() |
|
25
|
|
|
*/ |
|
26
|
|
|
class Invitation extends Model |
|
27
|
|
|
{ |
|
28
|
|
|
public const DEFAULT_INVITES = 1; |
|
29
|
|
|
public const DEFAULT_INVITE_EXPIRY_DAYS = 7; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public $timestamps = true; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $dateFormat = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $guarded = ['id']; |
|
45
|
|
|
|
|
46
|
|
|
public function user() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->belongsTo(User::class, 'users_id'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param int $uid |
|
53
|
|
|
* @param string $inviteToken |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function addInvite(int $uid, string $inviteToken) |
|
56
|
|
|
{ |
|
57
|
|
|
self::create(['guid' => $inviteToken, 'users_id' => $uid]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $inviteToken |
|
62
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null|static |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getInvite($inviteToken) |
|
65
|
|
|
{ |
|
66
|
|
|
// |
|
67
|
|
|
// Tidy any old invites sent greater than DEFAULT_INVITE_EXPIRY_DAYS days ago. |
|
68
|
|
|
// |
|
69
|
|
|
self::query()->where('created_at', '<', now()->subDays(self::DEFAULT_INVITE_EXPIRY_DAYS)); |
|
70
|
|
|
|
|
71
|
|
|
return self::query()->where('guid', $inviteToken)->first(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $inviteToken |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function deleteInvite(string $inviteToken): void |
|
78
|
|
|
{ |
|
79
|
|
|
self::query()->where('guid', $inviteToken)->delete(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|