-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from fleetbase/dev-v0.5.18
added unpurchased service quote purge command
- Loading branch information
Showing
5 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
server/src/Console/Commands/PurgeUnpurchasedServiceQuotes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Fleetbase\FleetOps\Console\Commands; | ||
|
||
use Fleetbase\FleetOps\Models\ServiceQuote; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class PurgeUnpurchasedServiceQuotes extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'fleetops:purge-service-quotes'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Deletes all unpurchased service quotes that are older than 48 hours.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$thresholdDate = now()->subHours(48); | ||
|
||
Schema::disableForeignKeyConstraints(); | ||
DB::beginTransaction(); | ||
|
||
try { | ||
// Get service quotes that are expired and have not been purchased | ||
$deletedCount = ServiceQuote::where('created_at', '<', $thresholdDate) | ||
->whereNotIn('uuid', function ($query) { | ||
$query->select('service_quote_uuid')->from('purchase_rates')->whereNotNull('service_quote_uuid'); | ||
}) | ||
->withTrashed() | ||
->forceDelete(); | ||
|
||
DB::commit(); | ||
|
||
if ($deletedCount > 0) { | ||
$this->info("Successfully deleted {$deletedCount} unpurchased service quotes."); | ||
} else { | ||
$this->info('No unpurchased service quotes found for deletion.'); | ||
} | ||
} catch (\Exception $e) { | ||
DB::rollBack(); | ||
Schema::enableForeignKeyConstraints(); | ||
$this->error('Error deleting unpurchased service quotes: ' . $e->getMessage()); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
Schema::enableForeignKeyConstraints(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters