Skip to content

Commit

Permalink
Merge pull request #135 from fleetbase/dev-v0.5.18
Browse files Browse the repository at this point in the history
added unpurchased service quote purge command
  • Loading branch information
roncodes authored Jan 29, 2025
2 parents 64e4782 + 70a3997 commit 0d38c54
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/fleetops-api",
"version": "0.5.17",
"version": "0.5.18",
"description": "Fleet & Transport Management Extension for Fleetbase",
"keywords": [
"fleetbase-extension",
Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Fleet-Ops",
"version": "0.5.17",
"version": "0.5.18",
"description": "Fleet & Transport Management Extension for Fleetbase",
"repository": "https://github.com/fleetbase/fleetops",
"license": "AGPL-3.0-or-later",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/fleetops-engine",
"version": "0.5.17",
"version": "0.5.18",
"description": "Fleet & Transport Management Extension for Fleetbase",
"fleetbase": {
"route": "fleet-ops"
Expand Down
66 changes: 66 additions & 0 deletions server/src/Console/Commands/PurgeUnpurchasedServiceQuotes.php
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;
}
}
2 changes: 2 additions & 0 deletions server/src/Providers/FleetOpsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class FleetOpsServiceProvider extends CoreServiceProvider
\Fleetbase\FleetOps\Console\Commands\AssignCustomerRoles::class,
\Fleetbase\FleetOps\Console\Commands\SimulateOrderRouteNavigation::class,
\Fleetbase\FleetOps\Console\Commands\DebugOrderTracker::class,
\Fleetbase\FleetOps\Console\Commands\PurgeUnpurchasedServiceQuotes::class,
];

/**
Expand Down Expand Up @@ -91,6 +92,7 @@ public function boot()
$schedule->command('fleetops:dispatch-orders')->everyMinute()->withoutOverlapping()->storeOutputInDb();
$schedule->command('fleetops:dispatch-adhoc')->everyMinute()->withoutOverlapping()->storeOutputInDb();
$schedule->command('fleetops:update-estimations')->everyFiveMinutes()->withoutOverlapping();
$schedule->command('fleetops:purge-service-quotes')->daily()->withoutOverlapping();
});
$this->registerNotifications();
$this->registerExpansionsFrom(__DIR__ . '/../Expansions');
Expand Down

0 comments on commit 0d38c54

Please sign in to comment.