-
Notifications
You must be signed in to change notification settings - Fork 135
/
UserService.php
635 lines (541 loc) · 17.7 KB
/
UserService.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
<?php
namespace App\Services;
use App\Contracts\Service;
use App\Events\UserStateChanged;
use App\Events\UserStatsChanged;
use App\Exceptions\PilotIdNotFound;
use App\Exceptions\UserPilotIdExists;
use App\Models\Airline;
use App\Models\Bid;
use App\Models\Enums\PirepState;
use App\Models\Enums\UserState;
use App\Models\Pirep;
use App\Models\Rank;
use App\Models\Role;
use App\Models\Typerating;
use App\Models\User;
use App\Models\UserFieldValue;
use App\Repositories\AircraftRepository;
use App\Repositories\AirlineRepository;
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Support\Units\Time;
use App\Support\Utils;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use function is_array;
class UserService extends Service
{
/**
* @param AircraftRepository $aircraftRepo
* @param AirlineRepository $airlineRepo
* @param FareService $fareSvc
* @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/
public function __construct(
private readonly AircraftRepository $aircraftRepo,
private readonly AirlineRepository $airlineRepo,
private readonly FareService $fareSvc,
private readonly SubfleetRepository $subfleetRepo,
private readonly UserRepository $userRepo
) {
}
/**
* Find the user and return them with all of the data properly attached
*
* @param $user_id
*
* @return User|null
*/
public function getUser($user_id): ?User
{
/** @var User $user */
$user = $this->userRepo
->with(['airline', 'bids', 'rank'])
->find($user_id);
if (empty($user)) {
return null;
}
if ($user->state === UserState::DELETED) {
return null;
}
// Load the proper subfleets to the rank
$user->rank->subfleets = $this->getAllowableSubfleets($user);
$user->subfleets = $user->rank->subfleets;
return $user;
}
/**
* Register a pilot. Also attaches the initial roles
* required, and then triggers the UserRegistered event
*
* @param array $attrs Array with the user data
* @param array $roles List of "display_name" of groups to assign
* @param int|null $state
*
* @return User
*/
public function createUser(array $attrs, array $roles = [], ?int $state = null): User
{
$user = User::create($attrs);
$user->api_key = Utils::generateApiKey();
$user->curr_airport_id = $user->home_airport_id;
// Determine if we want to auto accept
if ($state === null && setting('pilots.auto_accept') === true) {
$user->state = UserState::ACTIVE;
} elseif ($state === null) {
$user->state = UserState::PENDING;
}
$user->save();
// Attach any additional roles
if (!empty($roles) && is_array($roles)) {
foreach ($roles as $role) {
$this->addUserToRole($user, $role);
}
}
// Let's check their rank and where they should start
$this->calculatePilotRank($user);
$user->refresh();
event(new Registered($user));
return $user;
}
/**
* Remove the user. But don't actually delete them - set the name to deleted, email to
* something random
*
* @param User $user
*
* @throws \Exception
*/
public function removeUser(User $user)
{
// Detach all roles from this user
$user->removeRoles($user->roles->toArray());
// Delete any fields which might have personal information
UserFieldValue::where('user_id', $user->id)->delete();
// Remove any bids
Bid::where('user_id', $user->id)->delete();
// If this user has PIREPs, do a soft delete. Otherwise, just delete them outright
if ($user->pireps->count() > 0) {
$user->name = 'Deleted User';
$user->email = Utils::generateApiKey().'@deleted-user.com';
$user->api_key = Utils::generateApiKey();
$user->password = Hash::make(Utils::generateApiKey());
$user->state = UserState::DELETED;
$user->save();
} else {
$user->forceDelete();
}
}
/**
* Add a user to a given role
*
* @param User $user
* @param string $roleName
*
* @return User
*/
public function addUserToRole(User $user, string $roleName): User
{
$role = Role::where(['name' => $roleName])->first();
$user->addRole($role);
return $user;
}
/**
* Find and return the next available pilot ID (usually just the max+1)
*
* @return int
*/
public function getNextAvailablePilotId(): int
{
return (int) User::withTrashed()->max('pilot_id') + 1;
}
/**
* Find the next available pilot ID and set the current user's pilot_id to that +1
* Called from UserObserver right now after a record is created
*
* @param User $user
*
* @return User
*/
public function findAndSetPilotId(User $user): User
{
if ($user->pilot_id !== null && $user->pilot_id > 0) {
return $user;
}
$user->pilot_id = $this->getNextAvailablePilotId();
$user->save();
Log::info('Set pilot ID for user '.$user->id.' to '.$user->pilot_id);
return $user;
}
/**
* Return true or false if a pilot ID already exists
*
* @param int $pilot_id
*
* @return bool
*/
public function isPilotIdAlreadyUsed(int $pilot_id): bool
{
return User::where('pilot_id', '=', $pilot_id)->exists();
}
/**
* Change a user's pilot ID
*
* @param User $user
* @param int $pilot_id
*
* @throws UserPilotIdExists
*
* @return User
*/
public function changePilotId(User $user, int $pilot_id): User
{
if ($user->pilot_id === $pilot_id) {
return $user;
}
if ($this->isPilotIdAlreadyUsed($pilot_id)) {
Log::error('User with id '.$pilot_id.' already exists');
throw new UserPilotIdExists($user);
}
$old_id = $user->pilot_id;
$user->pilot_id = $pilot_id;
$user->save();
Log::info('Changed pilot ID for user '.$user->id.' from '.$old_id.' to '.$user->pilot_id);
return $user;
}
/**
* Split a given pilot ID into an airline and ID portions
*
* @param string $pilot_id
*
* @return User
*/
public function findUserByPilotId(string $pilot_id): User
{
$pilot_id = trim($pilot_id);
if (empty($pilot_id)) {
throw new PilotIdNotFound('');
}
$airlines = $this->airlineRepo->all(['id', 'icao', 'iata']);
$ident_str = null;
$pilot_id = strtoupper($pilot_id);
/** @var Airline $airline */
foreach ($airlines as $airline) {
if (strpos($pilot_id, $airline->icao) !== false) {
$ident_str = $airline->icao;
break;
}
if (!empty($airline->iata)) {
if (strpos($pilot_id, $airline->iata) !== false) {
$ident_str = $airline->iata;
break;
}
}
}
if (empty($ident_str)) {
throw new PilotIdNotFound($pilot_id);
}
$parsed_pilot_id = str_replace($ident_str, '', $pilot_id);
$user = User::where(['airline_id' => $airline->id, 'pilot_id' => $parsed_pilot_id])->first();
if (empty($user)) {
throw new PilotIdNotFound($pilot_id);
}
return $user;
}
/**
* Return all of the users that are determined to be on leave. Only goes through the
* currently active users. If the user doesn't have a PIREP, then the creation date
* of the user record is used to determine the difference
*/
public function findUsersOnLeave()
{
$leave_days = setting('pilots.auto_leave_days');
if ($leave_days === 0) {
return [];
}
$date = Carbon::now('UTC');
$users = User::where('state', UserState::ACTIVE)->get();
/** @var User $user */
return $users->filter(function ($user, $i) use ($date, $leave_days) {
// If any role for this user has the "disable_activity_check" feature activated, skip this user
foreach ($user->roles()->get() as $role) {
/** @var Role $role */
if ($role->disable_activity_checks) {
return false;
}
}
// If they haven't submitted a PIREP, use the date that the user was created
$last_pirep = Pirep::where(['user_id' => $user->id])->latest('submitted_at')->first();
if (!$last_pirep) {
$diff_date = $user->created_at;
} else {
$diff_date = $last_pirep->created_at;
}
// See if the difference is larger than what the setting calls for
if ($date->diffInDays($diff_date) <= $leave_days) {
return false;
}
return true;
});
}
/**
* Return the subfleets this user is allowed access to,
* based on their current Rank and/or by Type Rating
*
* @param $user
*
* @return Collection
*/
public function getAllowableSubfleets($user)
{
$restrict_rank = setting('pireps.restrict_aircraft_to_rank', true);
$restrict_type = setting('pireps.restrict_aircraft_to_typerating', false);
$restricted_to = [];
if ($user) {
$rank_sf_array = $restrict_rank ? $user->rank->subfleets()->pluck('id')->toArray() : [];
$type_sf_array = $restrict_type ? $user->rated_subfleets->pluck('id')->toArray() : [];
if ($restrict_rank && !$restrict_type) {
$restricted_to = $rank_sf_array;
} elseif (!$restrict_rank && $restrict_type) {
$restricted_to = $type_sf_array;
} elseif ($restrict_rank && $restrict_type) {
$restricted_to = array_intersect($rank_sf_array, $type_sf_array);
}
} else {
$restrict_rank = false;
$restrict_type = false;
}
// @var Collection $subfleets
$subfleets = $this->subfleetRepo->when($restrict_rank || $restrict_type, function ($query) use ($restricted_to) {
return $query->whereIn('id', $restricted_to);
})->with('aircraft')->get();
// Map the subfleets with the proper fare information
return $subfleets->transform(function ($sf, $key) {
$sf->fares = $this->fareSvc->getForSubfleet($sf);
return $sf;
});
}
/**
* Return a bool if a user is allowed to fly the current aircraft
*
* @param $user
* @param $aircraft_id
*
* @return bool
*/
public function aircraftAllowed($user, $aircraft_id)
{
$aircraft = $this->aircraftRepo->find($aircraft_id, ['subfleet_id']);
$subfleets = $this->getAllowableSubfleets($user);
$subfleet_ids = $subfleets->pluck('id')->toArray();
return \in_array($aircraft->subfleet_id, $subfleet_ids, true);
}
/**
* Change the user's state. PENDING to ACCEPTED, etc
* Send out an email
*
* @param User $user
* @param $old_state
*
* @return User
*/
public function changeUserState(User $user, $old_state): User
{
if ($user->state === $old_state) {
return $user;
}
Log::info('User '.$user->ident.' state changing from '.UserState::label($old_state).' to '.UserState::label($user->state));
event(new UserStateChanged($user, $old_state));
return $user;
}
/**
* Adjust the number of flights a user has. Triggers
* UserStatsChanged event
*
* @param User $user
* @param int $count
*
* @return User
*/
public function adjustFlightCount(User $user, int $count): User
{
$user->refresh();
$old_value = $user->flights;
$user->flights += $count;
$user->save();
event(new UserStatsChanged($user, 'flights', $old_value));
return $user;
}
/**
* Update a user's flight times
*
* @param User $user
* @param int $minutes
*
* @return User
*/
public function adjustFlightTime(User $user, int $minutes): User
{
$user->refresh();
$user->flight_time += $minutes;
$user->save();
return $user;
}
/**
* See if a pilot's rank has change. Triggers the UserStatsChanged event
*
* @param User $user
*
* @return User
*/
public function calculatePilotRank(User $user): User
{
$user->refresh();
// If their current rank is one they were assigned, then
// don't change away from it automatically.
if ($user->rank && $user->rank->auto_promote === false) {
return $user;
}
// If we should count their transfer hours?
if (setting('pilots.count_transfer_hours', false) === true) {
$pilot_hours = new Time($user->flight_time + $user->transfer_time);
} else {
$pilot_hours = new Time($user->flight_time);
}
// The current rank's hours are over the pilot's current hours,
// so assume that they were "placed" here by an admin so don't
// bother with updating it
if ($user->rank && $user->rank->hours > $pilot_hours->hours) {
return $user;
}
$old_rank = $user->rank;
$original_rank_id = $user->rank_id;
$ranks = Rank::where('auto_promote', true)
->orderBy('hours', 'asc')->get();
foreach ($ranks as $rank) {
if ($rank->hours > $pilot_hours->hours) {
break;
}
$user->rank_id = $rank->id;
}
// Only trigger the event/update if there's been a change
if ($user->rank_id !== $original_rank_id) {
$user->save();
$user->refresh();
event(new UserStatsChanged($user, 'rank', $old_rank));
}
return $user;
}
/**
* Set the user's status to being on leave
*
* @param User $user
*
* @return User
*/
public function setStatusOnLeave(User $user): User
{
$user->refresh();
$user->state = UserState::ON_LEAVE;
$user->save();
event(new UserStateChanged($user, UserState::ON_LEAVE));
$user->refresh();
return $user;
}
/**
* Recalculate the stats for all active users
*/
public function recalculateAllUserStats(): void
{
$w = [
['state', '!=', UserState::REJECTED],
];
$this->userRepo
->findWhere($w, ['id', 'name', 'airline_id'])
->each(function ($user, $_) {
return $this->recalculateStats($user);
});
}
/**
* Recount/update all of the stats for a user
*
* @param User $user
*
* @return User
*/
public function recalculateStats(User $user): User
{
// Recalc their hours
$w = [
'user_id' => $user->id,
'state' => PirepState::ACCEPTED,
];
$pirep_count = Pirep::where($w)->count();
$user->flights = $pirep_count;
$flight_time = Pirep::where($w)->sum('flight_time');
$user->flight_time = $flight_time;
$user->save();
// Recalc the rank
$this->calculatePilotRank($user);
Log::info('User '.$user->ident.' updated; pirep count='.$pirep_count.', rank='.$user->rank->name.', flight_time='.$user->flight_time.' minutes');
$user->save();
return $user;
}
/**
* Attach a type rating to the user
*
* @param User $user
* @param Typerating $typerating
*/
public function addUserToTypeRating(User $user, Typerating $typerating)
{
$user->typeratings()->syncWithoutDetaching([$typerating->id]);
$user->save();
$user->refresh();
return $user;
}
/**
* Detach a type rating from the user
*
* @param User $user
* @param Typerating $typerating
*/
public function removeUserFromTypeRating(User $user, Typerating $typerating)
{
$user->typeratings()->detach($typerating->id);
$user->save();
$user->refresh();
return $user;
}
public function retrieveDiscordPrivateChannelId(User $user): void
{
if (is_null(config('services.discord.bot_token'))) {
return;
}
try {
$httpClient = new Client();
$response = $httpClient->post('https://discord.com/api/users/@me/channels', [
'headers' => [
'Authorization' => 'Bot '.config('services.discord.bot_token'),
],
'json' => [
'recipient_id' => $user->discord_id,
],
]);
$privateChannel = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR)['id'];
$user->update([
'discord_private_channel_id' => $privateChannel,
]);
} catch (\Exception $e) {
Log::error('Discord OAuth Error: '.$e->getMessage());
} catch (GuzzleException $e) {
Log::error('Discord OAuth Error: '.$e->getMessage());
}
}
}