Skip to content

Commit

Permalink
Merge pull request #530 from NFDI4Chem/development
Browse files Browse the repository at this point in the history
fix: Extended clean command to maintain projects
  • Loading branch information
NishaSharma14 authored Oct 21, 2022
2 parents 89a5593 + cc33a15 commit c81b1e9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions app/Console/Commands/SanitizeProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,71 @@ public function handle()
return $this->cleanDrafts();
} elseif ($name == 'localise') {
return $this->localise();
} elseif ($name == 'projects') {
return $this->cleanProjects();
}
}

public function cleanProjects(){
$projects = Project::where([
['is_public', false],
['draft_id', null],
])->get();

return DB::transaction(function () use ($projects) {
foreach ($projects as $project) {
$user_id = $project->owner->id;
$team_id = $project->team_id;
$id = Str::uuid();
$environment = env('APP_ENV', 'local');
$path = preg_replace(
'~//+~',
'/',
$environment.'/'.$user_id.'/drafts/'.$id
);

$draft = Draft::create([
'name' => $project->name,
'slug' => Str::slug('$project->name'),
'description' => $project->description,
'relative_url' => rtrim(
preg_replace('~//+~', '/', '/'.$id),
'/'
),
'path' => $path,
'owner_id' => $user_id,
'team_id' => $team_id ? $team_id : null,
'key' => $id,
]);

$projectFSObjects = FileSystemObject::with('children')
->where([
['project_id', $project->id],
['level', 0],
])
->get();

foreach ($projectFSObjects as $FSObject) {
$this->moveFolder($FSObject, $project, $draft);
}

$project->status = null;
$project->process_logs = null;
$project->draft_id = $draft->id;
$project->save();

foreach ($project->studies as $study) {
$study->draft_id = $draft->id;
$study->save();
foreach ($study->datasets as $dataset) {
$dataset->draft_id = $draft->id;
$dataset->save();
}
}
}
});
}

public function cleanDrafts()
{
$privateProjects = Project::where([
Expand Down

0 comments on commit c81b1e9

Please sign in to comment.