Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customer data transfer CLI messaging #3276

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/console/controllers/TransferCustomerDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function options($actionID): array
*/
public function actionIndex(): int
{
$this->stdout("This command will transfer all commerce data from one user to another.\n");
$this->stdout('This command will transfer all commerce data from one user to another.' . PHP_EOL);

$this->fromUser = $this->prompt('Move Commerce data from user (email or username):', [
'required' => true,
Expand All @@ -61,36 +61,47 @@ public function actionIndex(): int
]);

if ($this->fromUser === '' || $this->toUser === '') {
$this->stderr("No 'fromUser' or 'toUser' specified.\n", Console::FG_RED);
$this->stderr('You must specify both a “to” and “from” user.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$fromUser = Craft::$app->getUsers()->getUserByUsernameOrEmail($this->fromUser);
$toUser = Craft::$app->getUsers()->getUserByUsernameOrEmail($this->toUser);

if ($fromUser === null) {
$this->stderr("No user with {$this->fromUser} found.\n", Console::FG_RED);
$this->stderr("No user found with a username or email of `{$this->fromUser}`" . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

if ($toUser === null) {
$this->stderr("No user with {$this->toUser} found.\n", Console::FG_RED);
$this->stderr("No user found with a username or email of `{$this->toUser}`" . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$confirm = $this->confirm('Are you sure you want to move all Commerce data from user: ' . $this->fromUser . ' to user: ' . $this->toUser . '? (y/n)');
// Make sure they're not the same!
if ($fromUser->id === $toUser->id) {
$this->stderr('The transfer must happen between different users.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$confirm = $this->confirm('Are you sure you want to move all Commerce data from user: ' . $this->fromUser . ' to user: ' . $this->toUser . '?');
if (!$confirm) {
$this->stdout('Aborting.');
$this->stdout('No data will be moved.', Console::FG_YELLOW);
return ExitCode::OK;
}

$this->stdout('Moving data... ');

try {
Plugin::getInstance()->getCustomers()->transferCustomerData($fromUser, $toUser);
} catch (Exception $e) {
$this->stderr($e->getMessage() . "\n", Console::FG_RED);
$this->stderr('failed!' . PHP_EOL, Console::FG_RED);
$this->stderr($e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$this->stdout('done!' . PHP_EOL, Console::FG_GREEN);

return ExitCode::OK;
}
}
Loading