diff --git a/app/Console/Commands/Areafix/Rescan.php b/app/Console/Commands/Areafix/Rescan.php index ddd29bb..38a22a3 100644 --- a/app/Console/Commands/Areafix/Rescan.php +++ b/app/Console/Commands/Areafix/Rescan.php @@ -60,6 +60,7 @@ class Rescan extends Command ->when($this->argument('days'),function($query) { return $query->where('created_at','>=',Carbon::now()->subDays($this->argument('days'))->startOfDay()); }) + ->orderBy('datetime') ->cursor() as $eo) { // Echomail hasnt been exported before diff --git a/app/Console/Commands/Filefix/Rescan.php b/app/Console/Commands/Filefix/Rescan.php new file mode 100644 index 0000000..d191fc6 --- /dev/null +++ b/app/Console/Commands/Filefix/Rescan.php @@ -0,0 +1,90 @@ +argument('ftn')); + + if (! $ao) + throw new \Exception('FTN not found: '.$this->argument('ftn')); + + // Check that the area belongs to the domain for the FTN + if (! $this->argument('area')) + throw new \Exception('Areaname is required'); + + $fao = Filearea::where('name',$this->argument('area'))->singleOrFail(); + if ($fao->domain_id !== $ao->zone->domain_id) + throw new \Exception(sprintf('File area [%s] is not in domain [%s] for FTN [%s]',$fao->name,$ao->zone->domain->name,$ao->ftn)); + + // Check that the user is subscribed + if (! $ao->fileareas->contains($fao->id)) + throw new \Exception(sprintf('FTN [%s] is not subscribed to [%s]',$ao->ftn,$fao->name)); + + // Check that an FTN can read the area + if (! $fao->can_read($ao->security)) + throw new \Exception(sprintf('FTN [%s] doesnt have permission to receive [%s]',$ao->ftn,$fao->name)); + + foreach (File::select('id') + ->where('filearea_id',$fao->id) + ->when($this->argument('file'),function($query) { + return $query->where('name','=',$this->argument('days')); + }) + ->orderBy('datetime') + ->cursor() as $fo) { + + // File hasnt been exported before + if (! $fo->seenby->count()) { + $fo->seenby()->attach($ao->id,['export_at'=>Carbon::now()]); + $this->info(sprintf('Exported [%d] to [%s]',$fo->id,$ao->ftn3d)); + + } else { + $export = $fo->seenby->where('id',$ao->id)->pop(); + + // File is pending export + if ($export && $export->pivot->export_at && is_null($export->pivot->sent_at) && is_null($export->pivot->sent_pkt)) { + $this->warn(sprintf('Not exporting [%d] already queued for [%s]',$fo->id,$ao->ftn3d)); + + // File has been exported + } elseif ($export) { + $fo->seenby()->updateExistingPivot($ao,['export_at'=>Carbon::now(),'sent_at'=>NULL]); + $this->info(sprintf('Re-exported [%d] to [%s]',$fo->id,$ao->ftn3d)); + + // File has not been exported + } else { + $fo->seenby()->attach($ao,['export_at'=>Carbon::now(),'sent_at'=>NULL]); + $this->info(sprintf('Exported [%d] to [%s]',$fo->id,$ao->ftn3d)); + } + } + } + + return self::SUCCESS; + } +} \ No newline at end of file