|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Http\RedirectResponse; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Jaxon\Laravel\App\Jaxon; |
|
10
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagContext; |
|
11
|
|
|
use Siak\Tontine\Model\Round; |
|
12
|
|
|
use Siak\Tontine\Model\Tontine; |
|
13
|
|
|
use Siak\Tontine\Model\User; |
|
14
|
|
|
use Siak\Tontine\Service\TenantService; |
|
15
|
|
|
use Siak\Tontine\Service\Tontine\TontineService; |
|
16
|
|
|
|
|
17
|
|
|
use function auth; |
|
18
|
|
|
use function Jaxon\jaxon; |
|
19
|
|
|
use function view; |
|
20
|
|
|
|
|
21
|
|
|
class TontineTenant |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param Jaxon $jaxon |
|
25
|
|
|
* @param TenantService $tenantService |
|
26
|
|
|
* @param TontineService $tontineService |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(private Jaxon $jaxon, private TenantService $tenantService, |
|
29
|
|
|
private TontineService $tontineService) |
|
30
|
|
|
{} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the latest user tontine, from the session or the database. |
|
34
|
|
|
* |
|
35
|
|
|
* @param User $user |
|
36
|
|
|
* |
|
37
|
|
|
* @return Tontine|null |
|
38
|
|
|
*/ |
|
39
|
|
|
private function setLatestTontine(User $user): ?Tontine |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var DataBagContext */ |
|
42
|
|
|
$tenantDatabag = jaxon()->getResponse()->bag('tenant'); |
|
43
|
|
|
|
|
44
|
|
|
// First try to get the current tontine id from the databag. |
|
45
|
|
|
$tontine = null; |
|
46
|
|
|
$tontineId = $tenantDatabag->get('tontine.id', 0); |
|
47
|
|
|
if($tontineId > 0 && |
|
48
|
|
|
($tontine = $this->tontineService->getUserOrGuestTontine($tontineId)) !== null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->tenantService->setTontine($tontine); |
|
51
|
|
|
return $tontine; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Try to get the latest tontine the user worked on. |
|
55
|
|
|
if(($tontineId = $user->properties['latest']['tontine'] ?? 0) > 0) |
|
56
|
|
|
{ |
|
57
|
|
|
$tontine = $this->tontineService->getUserOrGuestTontine($tontineId); |
|
58
|
|
|
} |
|
59
|
|
|
if(!$tontine) |
|
60
|
|
|
{ |
|
61
|
|
|
$tontine = $this->tontineService->getTontines()->first() ?? |
|
62
|
|
|
$this->tontineService->getGuestTontines()->first(); |
|
63
|
|
|
} |
|
64
|
|
|
if(($tontine)) |
|
65
|
|
|
{ |
|
66
|
|
|
$tenantDatabag->set('tontine.id', $tontine->id); |
|
67
|
|
|
$tenantDatabag->set('round.id', 0); |
|
68
|
|
|
$this->tenantService->setTontine($tontine); |
|
69
|
|
|
} |
|
70
|
|
|
return $tontine; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the latest tontine round, from the session or the database. |
|
75
|
|
|
* |
|
76
|
|
|
* @param Tontine $tontine |
|
77
|
|
|
* |
|
78
|
|
|
* @return Round|null |
|
79
|
|
|
*/ |
|
80
|
|
|
private function setLatestRound(Tontine $tontine): ?Round |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var DataBagContext */ |
|
83
|
|
|
$tenantDatabag = jaxon()->getResponse()->bag('tenant'); |
|
84
|
|
|
|
|
85
|
|
|
// First try to get the current round id from the databag. |
|
86
|
|
|
$round = null; |
|
87
|
|
|
$roundId = $tenantDatabag->get('round.id', 0); |
|
88
|
|
|
if($roundId > 0 && |
|
89
|
|
|
($round = $tontine->rounds()->find($roundId)) !== null) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->tenantService->setRound($round); |
|
92
|
|
|
return $round; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Try to get the latest round the user worked on. |
|
96
|
|
|
if(($roundId = $tontine->user->properties['latest']['round'] ?? 0) > 0) |
|
97
|
|
|
{ |
|
98
|
|
|
$round = $tontine->rounds()->find($roundId); |
|
99
|
|
|
} |
|
100
|
|
|
if(!$round) |
|
101
|
|
|
{ |
|
102
|
|
|
$round = $tontine->rounds()->first(); |
|
103
|
|
|
} |
|
104
|
|
|
if(($round)) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->tenantService->setRound($round); |
|
107
|
|
|
$tenantDatabag->set('tontine.id', $tontine->id); |
|
108
|
|
|
$tenantDatabag->set('round.id', $round->id); |
|
109
|
|
|
} |
|
110
|
|
|
return $round; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Handle an incoming request. |
|
115
|
|
|
* |
|
116
|
|
|
* @param Request $request |
|
117
|
|
|
* @param Closure(Request): (Response|RedirectResponse) $next |
|
118
|
|
|
* |
|
119
|
|
|
* @return Response|RedirectResponse |
|
120
|
|
|
*/ |
|
121
|
|
|
public function handle(Request $request, Closure $next) |
|
122
|
|
|
{ |
|
123
|
|
|
/** @var User */ |
|
124
|
|
|
$user = auth()->user(); |
|
125
|
|
|
$this->tenantService->setUser($user); |
|
126
|
|
|
|
|
127
|
|
|
if(($tontine = $this->setLatestTontine($user)) !== null) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->setLatestRound($tontine); |
|
130
|
|
|
} |
|
131
|
|
|
view()->share('tontine', $tontine); |
|
132
|
|
|
|
|
133
|
|
|
return $next($request); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|