-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\TagShortcut; | ||
use App\Models\TagShortcutItem; | ||
use App\Models\TagShortcutItemTag; | ||
use App\Models\User; | ||
use Illuminate\Console\Command; | ||
|
||
class CopyTagShortcutCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'app:copy-tag-shortcut-command {tagShortcutId} {--to=}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Copies a tag shortcut from a user to another user or users'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
/** @var TagShortcut $tagShortcut */ | ||
$tagShortcut = TagShortcut::findOrFail($this->argument('tagShortcutId')); | ||
|
||
$this->components->info("Copying tag shortcut [{$tagShortcut->shortcut}] to user(s)"); | ||
|
||
$to = $this->option('to'); | ||
$users = $to ? [User::findOrFail($to)] : User::all(); | ||
|
||
/** @var User $user */ | ||
foreach ($users as $user) { | ||
if ($user->tagShortcuts()->where('shortcut', $tagShortcut->shortcut)->exists()) { | ||
$this->components->warn("Tag shortcut [{$tagShortcut->shortcut}] already exists for user [{$user->name}]"); | ||
|
||
continue; | ||
} | ||
|
||
$newShortcut = $tagShortcut->replicate(); | ||
$newShortcut->user_id = $user->id; | ||
$newShortcut->save(); | ||
|
||
// copy the tag_shortcut_items | ||
$tagShortcut->tagShortcutItems->each(function (TagShortcutItem $item) use ($newShortcut): void { | ||
$newItem = $item->replicate(); | ||
$newItem->tag_shortcut_id = $newShortcut->id; | ||
$newItem->save(); | ||
|
||
// copy the tags | ||
$item->tagShortcutItemTags->each(function (TagShortcutItemTag $tag) use ($newItem): void { | ||
$newTag = $tag->replicate(); | ||
$newTag->tag_shortcut_item_id = $newItem->id; | ||
$newTag->save(); | ||
}); | ||
}); | ||
|
||
$this->components->info("Tag shortcut [{$tagShortcut->shortcut}] copied to user [{$user->name}]"); | ||
} | ||
|
||
$this->components->info('Tag shortcut copied successfully'); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
use App\Models\TagShortcut; | ||
use App\Models\TagShortcutItem; | ||
use App\Models\TagShortcutItemTag; | ||
use App\Models\User; | ||
|
||
it('copies a tag shortcut from a user to another user or users', function (): void { | ||
$tagShortcut = TagShortcut::factory()->create(); | ||
$tagShortcutItem = TagShortcutItem::factory()->create([ | ||
'tag_shortcut_id' => $tagShortcut->id, | ||
]); | ||
$tagShortcutItemTag = TagShortcutItemTag::factory()->create([ | ||
'tag_shortcut_item_id' => $tagShortcutItem->id, | ||
]); | ||
$to = User::factory()->create(); | ||
$tagShortcut->user->tagShortcuts()->save($tagShortcut); | ||
$tagShortcut->tagShortcutItems()->save($tagShortcutItem); | ||
$tagShortcutItem->tagShortcutItemTags()->save($tagShortcutItemTag); | ||
|
||
$this->artisan('app:copy-tag-shortcut-command', [ | ||
'tagShortcutId' => $tagShortcut->id, | ||
'--to' => $to->id, | ||
]); | ||
|
||
$this->assertDatabaseHas('tag_shortcuts', [ | ||
'user_id' => $to->id, | ||
'shortcut' => $tagShortcut->shortcut, | ||
]); | ||
$this->assertDatabaseHas('tag_shortcut_items', [ | ||
'tag_shortcut_id' => $to->tagShortcuts->first()->id, | ||
]); | ||
$this->assertDatabaseHas('tag_shortcut_item_tag', [ | ||
'tag_shortcut_item_id' => $to->tagShortcuts->first()->tagShortcutItems->first()->id, | ||
]); | ||
}); |