Skip to content

Commit

Permalink
Added timezone to registration
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Mar 19, 2024
1 parent fb1d127 commit 47673f1
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 5 deletions.
9 changes: 8 additions & 1 deletion app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Organization;
use App\Models\User;
use App\Service\TimezoneService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
Expand Down Expand Up @@ -48,11 +49,17 @@ public function create(array $input): User
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();

return DB::transaction(function () use ($input) {
$timezone = 'UTC';
if (array_key_exists('timezone', $input) && is_string($input['timezone']) && app(TimezoneService::class)->isValid($input['timezone'])) {
$timezone = $input['timezone'];
}

return DB::transaction(function () use ($input, $timezone) {
return tap(User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'timezone' => $timezone,
]), function (User $user) {
$this->createTeam($user);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function importData(string $data): void
'email' => $record['Email'],
], [
'name' => $record['User'],
'timezone' => 'UTC',
'is_placeholder' => true,
]);
$clientId = null;
Expand Down
8 changes: 8 additions & 0 deletions app/Service/Import/Importers/DefaultImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Models\User;
use App\Service\ColorService;
use App\Service\Import\ImportDatabaseHelper;
use App\Service\TimezoneService;
use Illuminate\Database\Eloquent\Builder;

abstract class DefaultImporter implements ImporterContract
Expand Down Expand Up @@ -47,6 +48,8 @@ abstract class DefaultImporter implements ImporterContract

protected ColorService $colorService;

protected TimezoneService $timezoneService;

public function init(Organization $organization): void
{
$this->organization = $organization;
Expand All @@ -62,6 +65,10 @@ public function init(Organization $organization): void
'required',
'max:255',
],
'timezone' => [
'required',
'timezone:all',
],
]);
$this->projectImportHelper = new ImportDatabaseHelper(Project::class, ['name', 'organization_id'], true, function (Builder $builder) {
return $builder->where('organization_id', $this->organization->id);
Expand Down Expand Up @@ -97,6 +104,7 @@ public function init(Organization $organization): void
]);
$this->timeEntriesCreated = 0;
$this->colorService = app(ColorService::class);
$this->timezoneService = app(TimezoneService::class);
}

#[\Override]
Expand Down
1 change: 1 addition & 0 deletions app/Service/Import/Importers/TogglDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function importData(string $data): void
'email' => $workspaceUser->email,
], [
'name' => $workspaceUser->name,
'timezone' => $workspaceUser->timezone ?? 'UTC',
'is_placeholder' => true,
], (string) $workspaceUser->id);
}
Expand Down
1 change: 1 addition & 0 deletions app/Service/Import/Importers/TogglTimeEntriesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function importData(string $data): void
'email' => $record['Email'],
], [
'name' => $record['User'],
'timezone' => 'UTC',
'is_placeholder' => true,
]);
$clientId = null;
Expand Down
5 changes: 5 additions & 0 deletions app/Service/TimezoneService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function getSelectOptions(): array

return $options;
}

public function isValid(string $timezone): bool
{
return in_array($timezone, $this->getTimezones(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function up(): void
$table->boolean('is_placeholder')->default(false);
$table->foreignUuid('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->string('timezone')->nullable();
$table->string('timezone');
$table->timestamps();

$table->uniqueIndex('email')
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
/* Retry on CI only */
retries: process.env.CI ? 1 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
workers: process.env.CI ? 1 : 1,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: process.env.CI ? 'line' : 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Auth/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const form = useForm({
password: '',
password_confirmation: '',
terms: false,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone ?? null,
});
const submit = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,17 @@ const page = usePage<{
<!-- Timezone -->
<div class="col-span-6 sm:col-span-4">
<InputLabel for="timezone" value="Timezone" />
<select name="timezone" id="timezone" v-model="form.timezone" class="mt-1 block w-full border-input-border bg-input-background text-white focus:border-input-border-active rounded-md shadow-sm">
<option v-for="timezone in $page.props.timezones" :value="timezone">
<select
name="timezone"
id="timezone"
v-model="form.timezone"
required
class="mt-1 block w-full border-input-border bg-input-background text-white focus:border-input-border-active rounded-md shadow-sm">
<option value="" disabled>Select a Timezone</option>
<option
v-for="timezone in $page.props.timezones"
:key="timezone"
:value="timezone">
{{ timezone }}
</option>
</select>
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ public function test_new_users_can_register(): void

$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
$user = User::where('email', '[email protected]')->firstOrFail();
$this->assertSame('UTC', $user->timezone);
}

public function test_new_users_can_register_and_frontend_can_send_timezone_for_user(): void
{
$response = $this->post('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature(),
'timezone' => 'Europe/Berlin',
]);

$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
$user = User::where('email', '[email protected]')->firstOrFail();
$this->assertSame('Europe/Berlin', $user->timezone);
}

public function test_new_users_can_register_and_ignores_invalid_timezones_from_frontend(): void
{
$response = $this->post('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature(),
'timezone' => 'Unknown timezone',
]);

$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
$user = User::where('email', '[email protected]')->firstOrFail();
$this->assertSame('UTC', $user->timezone);
}

public function test_new_users_can_not_register_if_user_with_email_already_exists(): void
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Service/Import/ImportDatabaseHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function test_get_key_attach_to_existing_creates_model_if_not_existing():
'email' => '[email protected]',
], [
'name' => 'Test',
'timezone' => 'UTC',
]);

// Assert
Expand Down

0 comments on commit 47673f1

Please sign in to comment.