Delete users without PIREPS; pilot leave fix (#1171)

* Delete users without PIREPS; pilot leave fix

* Style fixes
This commit is contained in:
Nabeel S
2021-05-05 09:56:28 -04:00
committed by GitHub
parent fca04e6b0c
commit b6c0946795
4 changed files with 84 additions and 47 deletions

View File

@@ -139,13 +139,6 @@ class UserService extends Service
*/
public function removeUser(User $user)
{
$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();
// Detach all roles from this user
$user->detachRoles($user->roles);
@@ -154,6 +147,18 @@ class UserService extends Service
// 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->delete();
}
}
/**
@@ -299,49 +304,41 @@ class UserService extends Service
* 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(): array
public function findUsersOnLeave()
{
$leave_days = setting('pilots.auto_leave_days');
if ($leave_days === 0) {
return [];
}
$return_users = [];
$date = Carbon::now('UTC');
$users = User::with(['last_pirep'])->where('state', UserState::ACTIVE)->get();
$users = User::where('state', UserState::ACTIVE)->get();
/** @var User $user */
foreach ($users as $user) {
// If they haven't submitted a PIREP, use the date that the user was created
if (!$user->last_pirep) {
$diff_date = $user->created_at;
} else {
$diff_date = $user->last_pirep->submitted_at;
}
// See if the difference is larger than what the setting calls for
if ($date->diffInDays($diff_date) <= $leave_days) {
continue;
}
$skip = false;
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) {
$skip = true;
break;
return false;
}
}
if ($skip) {
continue;
// 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;
}
$return_users[] = $user;
}
return $return_users;
// See if the difference is larger than what the setting calls for
if ($date->diffInDays($diff_date) <= $leave_days) {
return false;
}
return true;
});
}
/**