2017-12-13 06:58:27 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by IntelliJ IDEA.
|
|
|
|
* User: nshahzad
|
|
|
|
* Date: 12/12/17
|
|
|
|
* Time: 2:48 PM
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Log;
|
2017-12-13 07:09:26 +08:00
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\UserBid;
|
|
|
|
|
|
|
|
class FlightService extends BaseService
|
|
|
|
{
|
2018-02-10 04:36:36 +08:00
|
|
|
protected $userSvc;
|
|
|
|
|
|
|
|
public function __construct(UserService $userSvc)
|
|
|
|
{
|
|
|
|
$this->userSvc = $userSvc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter out subfleets to only include aircraft that a user has access to
|
|
|
|
* @param $user
|
|
|
|
* @param $flight
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function filterSubfleets($user, $flight)
|
|
|
|
{
|
|
|
|
if (setting('pireps.restrict_aircraft_to_rank', false)) {
|
|
|
|
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
|
|
|
|
$flight->subfleets = $flight->subfleets->filter(
|
|
|
|
function ($subfleet, $item) use ($allowed_subfleets) {
|
|
|
|
if ($allowed_subfleets->contains($subfleet->id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $flight;
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:32:04 +08:00
|
|
|
/**
|
|
|
|
* Delete a flight, and all the user bids, etc associated with it
|
|
|
|
* @param Flight $flight
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function deleteFlight(Flight $flight)
|
|
|
|
{
|
|
|
|
$where = ['flight_id' => $flight->id];
|
|
|
|
UserBid::where($where)->delete();
|
|
|
|
$flight->delete();
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* Allow a user to bid on a flight. Check settings and all that good stuff
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param User $user
|
2017-12-13 07:26:27 +08:00
|
|
|
* @return UserBid|null
|
2017-12-13 06:58:27 +08:00
|
|
|
*/
|
2018-01-08 01:16:44 +08:00
|
|
|
public function addBid(Flight $flight, User $user)
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
|
|
|
# If it's already been bid on, then it can't be bid on again
|
|
|
|
if($flight->has_bid && setting('bids.disable_flight_on_bid')) {
|
2017-12-13 07:09:26 +08:00
|
|
|
Log::info($flight->id . ' already has a bid, skipping');
|
2017-12-13 06:58:27 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
# See if we're allowed to have multiple bids or not
|
|
|
|
if(!setting('bids.allow_multiple_bids')) {
|
|
|
|
$user_bids = UserBid::where(['user_id' => $user->id])->first();
|
|
|
|
if($user_bids) {
|
2017-12-13 07:09:26 +08:00
|
|
|
Log::info('User "' . $user->id . '" already has bids, skipping');
|
2017-12-13 06:58:27 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# See if this user has this flight bid on already
|
|
|
|
$bid_data = [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'flight_id' => $flight->id
|
|
|
|
];
|
|
|
|
|
|
|
|
$user_bid = UserBid::where($bid_data)->first();
|
|
|
|
if($user_bid) {
|
|
|
|
return $user_bid;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_bid = UserBid::create($bid_data);
|
|
|
|
|
|
|
|
$flight->has_bid = true;
|
|
|
|
$flight->save();
|
|
|
|
|
|
|
|
return $user_bid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a bid from a given flight
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param User $user
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function removeBid(Flight $flight, User $user)
|
|
|
|
{
|
|
|
|
$user_bid = UserBid::where([
|
|
|
|
'flight_id' => $flight->id, 'user_id' => $user->id
|
|
|
|
])->first();
|
|
|
|
|
|
|
|
if($user_bid) {
|
|
|
|
$user_bid->forceDelete();
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:26:27 +08:00
|
|
|
# Only flip the flag if there are no bids left for this flight
|
|
|
|
if(!UserBid::where('flight_id', $flight->id)->exists()) {
|
|
|
|
$flight->has_bid = false;
|
|
|
|
$flight->save();
|
|
|
|
}
|
2017-12-13 06:58:27 +08:00
|
|
|
}
|
|
|
|
}
|