2
2
3
3
namespace App \Http \Controllers \Backend ;
4
4
5
- use App \Http \Controllers \Controller ;
6
- use App \Models \AppointmentTime ;
7
- use App \Models \Calendar ;
8
- use App \Models \UserAppointment ;
9
- use App \Notifications \AppointmentApprovedNotification ;
10
5
use Carbon \Carbon ;
11
- use Illuminate \ Database \ Eloquent \ Builder ;
6
+ use App \ Models \ Calendar ;
12
7
use Illuminate \Http \Request ;
8
+ use App \Models \ExpertProfile ;
9
+ use App \Models \AppointmentTime ;
10
+ use App \Models \UserAppointment ;
11
+ use App \Http \Controllers \Controller ;
13
12
use Illuminate \Support \Facades \Notification ;
13
+ use App \Notifications \AppointmentApprovedNotification ;
14
+ use Log ;
14
15
15
16
class UserAppointmentController extends Controller
16
17
{
17
- public function __construct () {
18
+ public function __construct ()
19
+ {
18
20
if (auth ()->user () == null ) {
19
21
return redirect (route ('login ' ));
20
22
}
21
- if (request ('type ' ) != 'consultation ' ){
22
- if (!auth ()->user ()->hasRole ('super admin ' )){
23
+ if (request ('type ' ) != 'consultation ' ) {
24
+ if (!auth ()->user ()->hasRole ('super admin ' )) {
23
25
abort (403 , 'You don \'t have access to this page ' );
24
26
}
25
27
}
26
28
}
27
29
public function index ()
28
30
{
31
+ /**
32
+ * @var ?ExpertProfile $expert
33
+ */
29
34
$ expert = auth ()->user ()->expertProfile ;
30
- $ appointments = UserAppointment:: where ( ' is_approved ' , false )
31
- -> where ( function ( Builder $ q ) use ( $ expert) {
32
- if ( request ( ' type ' ) == ' consultation ' ) {
33
- $ q -> whereNotNull ( ' expert_profile_id ' );
34
- if ( $ expert != null ) {
35
- $ q -> where ( ' expert_profile_id ' , $ expert -> id );
36
- }
37
- }{
38
- $ q ->whereNull ('expert_profile_id ' );
39
- }
40
- } )
41
- -> with ( ' map ' , ' user ' )-> latest () ->paginate (paginateCount ());
42
- $ apt = $ appointments -> first ();
35
+ if ( $ expert ) {
36
+ $ appointments = $ expert-> appointments ()
37
+ -> unapproved ()
38
+ -> with ( ' map ' , ' user ' )
39
+ -> latest ()
40
+ -> paginate ( paginateCount () );
41
+ } else {
42
+ $ appointments = UserAppointment:: unapproved ()
43
+ ->whereNull ('expert_profile_id ' )
44
+ -> with ( ' map ' , ' user ' )
45
+ -> latest ( )
46
+ ->paginate (paginateCount ());
47
+ }
43
48
44
49
return view ('backend.user.appointments ' , compact ('appointments ' ));
45
50
}
@@ -60,7 +65,7 @@ public function timesUpdate(Request $request)
60
65
foreach ($ request ->times as $ day => $ times ) {
61
66
AppointmentTime::create ([
62
67
'user_id ' => auth ()->id (),
63
- 'day ' => $ day ,
68
+ 'day ' => $ day ,
64
69
'times ' => $ times
65
70
]);
66
71
}
@@ -82,39 +87,46 @@ public function timeDelete(AppointmentTime $time)
82
87
83
88
public function approvedList ()
84
89
{
90
+ /**
91
+ * @var ?ExpertProfile $expert
92
+ */
85
93
$ expert = auth ()->user ()->expertProfile ;
86
- $ appointments = UserAppointment::where (['is_approved ' => true , 'is_completed ' => false ])
87
- ->where (function (Builder $ q ) use ($ expert ) {
88
- if (request ('type ' ) == 'consultation ' ) {
89
- $ q ->whereNotNull ('expert_profile_id ' );
90
- if ($ expert != null ) {
91
- $ q ->where ('expert_profile_id ' , $ expert ->id );
92
- }
93
- }{
94
- $ q ->whereNull ('expert_profile_id ' );
95
- }
96
- })
97
- ->with ('map ' , 'user ' )->latest ('approved_at ' )->get ();
94
+ if ($ expert ) {
95
+ $ appointments = $ expert ->appointments ()
96
+ ->approvedOnly ()
97
+ ->with ('map ' , 'user ' )
98
+ ->latest ()
99
+ ->paginate (paginateCount ());
100
+ } else {
101
+ $ appointments = UserAppointment::approvedOnly ()
102
+ ->whereNull ('expert_profile_id ' )
103
+ ->with ('map ' , 'user ' )
104
+ ->latest ()
105
+ ->paginate (paginateCount ());
106
+ }
98
107
99
108
return view ('backend.user.appointmentsApproved ' , compact ('appointments ' ));
100
109
}
101
110
102
111
public function completedList ()
103
112
{
113
+ /**
114
+ * @var ?ExpertProfile $expert
115
+ */
104
116
$ expert = auth ()->user ()->expertProfile ;
105
- $ appointments = UserAppointment:: where ([ ' is_completed ' => true ])
106
- -> where ( function ( Builder $ q ) use ( $ expert) {
107
- if ( request ( ' type ' ) == ' consultation ' ) {
108
- $ q -> whereNotNull ( ' expert_profile_id ' );
109
- if ( $ expert != null ) {
110
- $ q -> where ( ' expert_profile_id ' , $ expert -> id );
111
- }
112
- }{
113
- $ q ->whereNull ('expert_profile_id ' );
114
- }
115
- } )
116
- -> with ( ' map ' , ' user ' )-> latest ( ' completed_at ' )-> get ( );
117
-
117
+ if ( $ expert ) {
118
+ $ appointments = $ expert-> appointments ()
119
+ -> completedOnly ()
120
+ -> with ( ' map ' , ' user ' )
121
+ -> latest ()
122
+ -> paginate ( paginateCount () );
123
+ } else {
124
+ $ appointments = UserAppointment:: completedOnly ()
125
+ ->whereNull ('expert_profile_id ' )
126
+ -> with ( ' map ' , ' user ' )
127
+ -> latest ( )
128
+ -> paginate ( paginateCount () );
129
+ }
118
130
return view ('backend.user.appointmentsCompleted ' , compact ('appointments ' ));
119
131
}
120
132
@@ -141,7 +153,11 @@ public function approve(int $id)
141
153
'start ' => Carbon::parse ($ appointment ->date .', ' .$ appointment ->time , 'Asia/Dhaka ' )->format ('Y-m-d H:m:s ' ),
142
154
'description ' => null ,
143
155
]);
144
- Notification::route ('mail ' , $ appointment ->email )->notify (new AppointmentApprovedNotification ($ appointment ));
156
+ try {
157
+ Notification::route ('mail ' , $ appointment ->email )->notify (new AppointmentApprovedNotification ($ appointment ));
158
+ } catch (\Throwable $ th ) {
159
+ Log::error ($ th ->getMessage ());
160
+ }
145
161
$ alert = [
146
162
'message ' => 'Appointment Approved ' ,
147
163
'alert-type ' => 'success ' ,
@@ -165,6 +181,7 @@ public function complete(int $id)
165
181
$ appointment ->is_completed = true ;
166
182
$ appointment ->completed_at = now ();
167
183
$ appointment ->update ();
184
+ dd ($ appointment ->refresh ());
168
185
Calendar::create ([
169
186
'title ' => 'Appointment Completed ' ,
170
187
'user_appointment_id ' => $ appointment ->id ,
0 commit comments