MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //home/cafsindia/snap.cafsinfotech.in/app/Console/Commands/RemoveDuplicateIntervals.php
<?php

namespace App\Console\Commands;

use App\Models\TimeInterval;
use DB;
use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'cattr:intervals:dedupe')]
class RemoveDuplicateIntervals extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cattr:intervals:dedupe';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Removes duplicates in the Time intervals table';

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        $interval_ids = DB::table('time_intervals as ti1')
            ->join('time_intervals as ti2', static function ($join) {
                $join->on('ti2.start_at', '=', 'ti1.start_at');
                $join->on('ti2.end_at', '=', 'ti1.end_at');
                $join->on('ti2.user_id', '=', 'ti1.user_id');
                $join->on('ti2.id', '>', 'ti1.id');
            })
            ->whereNull('ti2.deleted_at')
            ->pluck('ti2.id')
            ->unique();

        TimeInterval::destroy($interval_ids->toArray());

        $count = $interval_ids->count();
        $this->info("Removed $count intervals.");
    }
}