-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sponsorship.php
104 lines (90 loc) · 2.73 KB
/
Sponsorship.php
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Sponsorship extends Model
{
use HasFactory;
/**
* Returns the icons/emojis associated with the type of sponsorship
* @return string[]
*/
public static function icons()
{
return [
1 => '🥇',
2 => '🥈',
3 => '🥉'
];
}
/**
* Establishes the relationship between a sponsorship level
* and all the companies that have this sponsorship tier
* @return HasMany
*/
public function companies(): HasMany
{
return $this->hasMany(Company::class);
}
/**
* Checks if there are any spots left for the specific sponsorship tier
* @return bool
*/
public function areMoreSponsorsAllowed()
{
return $this->companies()->where('is_sponsorship_approved', '=', 1)
->count() < $this->max_sponsors;
}
/**
* Calculates the number of left spots for the specific sponsorship tier
* @return mixed
*/
public function leftSpots()
{
return $this->max_sponsors - $this->companies()->where('is_sponsorship_approved', '=', 1)
->count();
}
/**
* Scope a query to only include companies that require approval
*
* @param $query
* @return mixed
*/
public function scopeAwaitingApproval($query): mixed
{
return $query->join('companies', 'companies.sponsorship_id', '=', 'sponsorships.id')
->where('companies.is_sponsorship_approved', '=', 0);
}
/**
* Checks if there are available sponsorship places
*
* @return bool
*/
public static function canAddSponsor()
{
$availableTierNumber = Sponsorship::all()->filter(function ($tier) {
return $tier->areMoreSponsorsAllowed();
})->count();
$availableTeam = Company::all()->filter(function ($team) {
return is_null($team->sponsorship_id);
})->count();
return $availableTeam > 0 && $availableTierNumber > 0;
}
/**
* Reject all companies that have requested this sponsorship but are not approved
* @return void
*/
public function rejectAllExceptApproved()
{
foreach ($this->teams as $team) {
if (!$team->is_sponsor_approved) {
//TODO: Once we reach the mail
/* Mail::to($team->owner->email)->send(new SponsorshipDisapprovedMailable($team));*/
$team->sponsor_tier_id = null;
$team->is_sponsor_approved = null;
$team->save();
}
}
}
}