Skip to content

Commit

Permalink
Add command to copy tag shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
GeniJaho committed Dec 30, 2024
1 parent 47c09fa commit 1cea57a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
71 changes: 71 additions & 0 deletions app/Console/Commands/CopyTagShortcutCommand.php
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');
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Commands/CopyTagShortcutCommandTest.php
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,
]);
});

0 comments on commit 1cea57a

Please sign in to comment.