Skip to content

Commit

Permalink
fix: address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Jan 19, 2025
1 parent f50e155 commit 93a487a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
11 changes: 11 additions & 0 deletions app/Filament/Resources/UserUsernameResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Filament\Notifications\Notification;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class UserUsernameResource extends Resource
{
Expand Down Expand Up @@ -51,6 +52,11 @@ public static function form(Form $form): Form
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn (Builder $query) => $query->where(function ($query) {
$query->whereNotNull('approved_at')
->orWhereNotNull('denied_at')
->orWhere('created_at', '>', now()->subDays(30));
}))
->columns([
Tables\Columns\TextColumn::make('user.username')
->label('Original Username')
Expand Down Expand Up @@ -143,6 +149,11 @@ public static function table(Table $table): Table
->action(function (UserUsername $record) {
$record->update(['denied_at' => now()]);

/** @var User $user */
$user = $record->user;

sendDisplayNameChangeDeclineEmail($user, $record->username);

Notification::make()
->success()
->title('Success')
Expand Down
23 changes: 23 additions & 0 deletions app/Helpers/util/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,31 @@ function sendDisplayNameChangeConfirmationEmail(

$msg = "Hello,<br><br>" .
"Great news! Your username change request to {$newDisplayName} has been approved.<br><br>" .

"You can now use your new username to log in everywhere on RetroAchievements.org.<br><br>" .

"Check out your updated profile {$profileLink}.<br><br>" .

"-- Your friends at RetroAchievements.org<br>";

return mail_utf8($user->EmailAddress, $emailTitle, $msg);
}

/**
* Sends an email to a user informing them that their display name change request was declined.
*/
function sendDisplayNameChangeDeclineEmail(
User $user,
string $desiredDisplayName,
): bool {
$emailTitle = "About Your Username Change Request";

$msg = "Hello,<br><br>" .
"We've reviewed your request to change your username to {$desiredDisplayName}. " .
"Unfortunately we weren't able to approve this change at this time.<br><br>" .

"You're welcome to submit another request after a 30 day cooldown period has ended.<br><br> " .

"-- Your friends at RetroAchievements.org<br>";

return mail_utf8($user->EmailAddress, $emailTitle, $msg);
Expand Down
20 changes: 20 additions & 0 deletions app/Models/UserUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public function getIsDeniedAttribute(): bool
return $this->denied_at !== null;
}

public function getIsExpiredAttribute(): bool
{
if ($this->approved_at !== null || $this->denied_at !== null) {
return false;
}

return $this->created_at->isPast() && $this->created_at->diffInDays(now()) >= 30;
}

// == mutators

// == relations
Expand Down Expand Up @@ -69,6 +78,17 @@ public function scopeDenied(Builder $query): Builder
->where('denied_at', '>', now()->subDays(30));
}

/**
* @param Builder<UserUsername> $query
* @return Builder<UserUsername>
*/
public function scopeExpired(Builder $query): Builder
{
return $query->whereNull('approved_at')
->whereNull('denied_at')
->where('created_at', '<=', now()->subDays(30));
}

/**
* @param Builder<UserUsername> $query
* @return Builder<UserUsername>
Expand Down
2 changes: 1 addition & 1 deletion lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@
"You can only request a new username once every 30 days, even if your new username is not approved. Are you sure you want to do this?": "You can only request a new username once every 30 days, even if your new username is not approved. Are you sure you want to do this?",
"Submitting username change request...": "Submitting username change request...",
"Submitted username change request!": "Submitted username change request!",
"Must contain only letters and numbers.": "Must contain only letters and numbers.",
"Must only contain unaccented letters and numbers.": "Must only contain unaccented letters and numbers.",
"Change Username": "Change Username",
"Current Username": "Current Username",
"New Username": "New Username",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ describe('Component: ChangeUsernameSectionCard', () => {

// ASSERT
expect(postSpy).not.toHaveBeenCalled();
expect(screen.getAllByText(/only letters and numbers/i)[0]).toBeVisible();
expect(
screen.getAllByText(/must only contain unaccented letters and numbers./i)[0],
).toBeVisible();
});

it('given the user submits valid form data but cancels the confirmation, does not submit', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export function useChangeUsernameForm() {
.string()
.min(4)
.max(20)
.regex(/^[a-zA-Z0-9]+$/, t('Must contain only letters and numbers.')),
.regex(/^[a-zA-Z0-9]+$/, t('Must only contain unaccented letters and numbers.')),
confirmUsername: z
.string()
.min(4)
.max(20)
.regex(/^[a-zA-Z0-9]+$/, t('Must contain only letters and numbers.')),
.regex(/^[a-zA-Z0-9]+$/, t('Must only contain unaccented letters and numbers.')),
})
.refine((data) => data.newUsername === data.confirmUsername, {
message: t('New usernames must match.'),
Expand Down

0 comments on commit 93a487a

Please sign in to comment.