Skip to content

Commit

Permalink
feat: terms and newsletter (#299)
Browse files Browse the repository at this point in the history
* refactor registration validation rules

* add terms and newsletter subscription checkboxes

* add newsletter subscription

* wip

---------

Co-authored-by: Lupu Gheorghe <[email protected]>
  • Loading branch information
andreiio and gheorghelupu17 authored Oct 16, 2023
1 parent cbc55f1 commit 3868797
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/Concerns/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function initializeHasRole(): void
$this->casts['role'] = UserRole::class;
}

private function hasRole(UserRole $role): bool
public function hasRole(UserRole $role): bool
{
return $this->role === $role;
}
Expand Down
10 changes: 7 additions & 3 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\RegistrationRequest;
use App\Models\User;
use App\Services\NewsletterService;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -44,12 +45,11 @@ public function store(RegistrationRequest $request): RedirectResponse
'email' => $attributes['user']['email'],
'password' => Hash::make($attributes['user']['password']),
'role' => $attributes['type'] === 'organization' ? UserRole::ADMIN : UserRole::USER,

]);

event(new Registered($user));
// $user = User::find($user->id);
if ($user->role===UserRole::ADMIN) {

if ($user->hasRole(UserRole::ADMIN)) {
$attributes['ngo']['status'] = OrganizationStatus::draft;

$organization = $user->organization()->create($attributes['ngo']);
Expand All @@ -63,6 +63,10 @@ public function store(RegistrationRequest $request): RedirectResponse
$user->save();
}

if ($attributes['subscribe']) {
NewsletterService::subscribe($user->email, $user->name);
}

Auth::login($user);

return redirect()->route('register')
Expand Down
30 changes: 16 additions & 14 deletions app/Http/Requests/RegistrationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ class RegistrationRequest extends FormRequest
public function rules(): array
{
$rules = [
'type' => ['string', 'required'],
'user' => ['array', 'required'],
'user.name' => ['string', 'required'],
'user.email' => ['email', 'required', 'unique:users,email'],
'user.password' => ['string', 'required', 'confirmed'],
'type' => ['required', 'string'],
'terms' => ['required', 'accepted'],
'subscribe' => ['boolean'],
'user' => ['required', 'array'],
'user.name' => ['required', 'string'],
'user.email' => ['required', 'email', 'unique:users,email'],
'user.password' => ['required', 'string', 'confirmed'],
];

if ($this->type === 'organization') {
$rules = array_merge($rules, [
'ngo' => ['array', 'required'],
'ngo.name' => ['string', 'required'],
'ngo.description' => ['string', 'required', 'max:1000'],
'ngo' => ['required', 'array'],
'ngo.name' => ['required', 'string'],
'ngo.description' => ['required', 'string', 'max:1000'],
'ngo.logo' => ['required', 'image'],
'ngo.statute' => ['required', 'file'],
'ngo.street_address' => ['string', 'required'],
'ngo.cif' => ['string', 'required', 'unique:organizations,cif', new ValidCIF],
'ngo.street_address' => ['required', 'string'],
'ngo.cif' => ['required', 'string', 'unique:organizations,cif', new ValidCIF],
'ngo.contact_email' => ['required', 'email'],
'ngo.contact_phone' => ['string', 'required'],
'ngo.contact_person' => ['string', 'required'],
'ngo.domains' => ['array', 'required'],
'ngo.counties' => ['array', 'required'],
'ngo.contact_phone' => ['required', 'string'],
'ngo.contact_person' => ['required', 'string'],
'ngo.domains' => ['required', 'array'],
'ngo.counties' => ['required', 'array'],
'ngo.volunteer' => ['boolean'],
'ngo.why_volunteer' => ['string', 'nullable', 'max:1000'],
'ngo.website' => ['string', 'nullable', 'url'],
Expand Down
34 changes: 34 additions & 0 deletions app/Services/NewsletterService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Services;

use Spatie\Newsletter\Facades\Newsletter;

class NewsletterService
{
public static function subscribe(string $email, ?string $name = null)
{
$mergeFields = [
'MERGE1' => $name,
];

$options = [
'pending' => true,
];

$response = rescue(
fn () => Newsletter::subscribe($email, $mergeFields, options: $options),
false
);

if (false !== $response) {
// TODO: check if email registered as user
// and assign subscriber badge if the
// user doesn't already have one.
}

return $response;
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"php": "^8.1",
"awcodes/filament-tiptap-editor": "^2.6",
"camya/filament-title-with-slug": "^0.5.6",
"drewm/mailchimp-api": "^2.5",
"filament/filament": "^2.17",
"filament/spatie-laravel-media-library-plugin": "^2.17",
"filament/spatie-laravel-translatable-plugin": "^2.17",
Expand All @@ -23,6 +24,7 @@
"pxlrbt/filament-excel": "^1.1",
"spatie/laravel-activitylog": "^4.7",
"spatie/laravel-medialibrary": "^10.11",
"spatie/laravel-newsletter": "^5.1",
"spatie/laravel-query-builder": "^5.3",
"stevegrunwell/time-constants": "^1.1",
"tightenco/ziggy": "^1.6"
Expand Down
131 changes: 130 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions config/newsletter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

return [

'driver' => env('NEWSLETTER_DRIVER', \Spatie\Newsletter\Drivers\MailChimpDriver::class),

/*
* These arguments will be given to the driver.
*/
'driver_arguments' => [
'api_key' => env('NEWSLETTER_API_KEY'),

'endpoint' => env('NEWSLETTER_ENDPOINT'),
],

/*
* The list name to use when no list name is specified in a method.
*/
'default_list_name' => 'subscribers',

'lists' => [
/*
* This key is used to identify this list. It can be used
* as the listName parameter provided in the various methods.
*
* You can set it to any string you want and you can add
* as many lists as you want.
*/
'subscribers' => [
/*
* When using the Mailcoach driver, this should be Email list UUID
* which is displayed in the Mailcoach UI
*
* When using the MailChimp driver, this should be a MailChimp list id.
* http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
*/
'id' => env('NEWSLETTER_LIST_ID'),
],
],
];
5 changes: 4 additions & 1 deletion lang/ro/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@
'updated_at' => 'actualizat la',
'username' => 'nume de utilizator',
'year' => 'an',
'user.name' => 'nume',
'user.email' => 'email',
'user.password' => 'parolă',
'end' => "Data finală perioada de donații",
],
];
];
3 changes: 3 additions & 0 deletions resources/js/Pages/Auth/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
statute: '',
},
type: '',
terms: false,
subscribe: false,
});
const social = useForm({
Expand Down Expand Up @@ -135,6 +137,7 @@
onError: (error) => {
/** Set active component in case of validation errors. */
if (
error['terms'] ||
error['user.name'] ||
error['user.password'] ||
error['user.email'] ||
Expand Down
Loading

0 comments on commit 3868797

Please sign in to comment.