-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update NotificationSettingsController, request validation, and migrat…
…ion for new notification settings
- Loading branch information
1 parent
7c3ab73
commit c4f67b9
Showing
7 changed files
with
200 additions
and
80 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
app/Http/Controllers/Api/V1/Admin/NotificationSettingsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\Admin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\UpdateNotificationSettingsRequest; | ||
use App\Models\NotificationSetting; | ||
use Illuminate\Http\Request; | ||
use App\Services\NotificationService; | ||
|
||
class NotificationSettingsController extends Controller | ||
{ | ||
/** | ||
* Update the user's notification settings. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function update(Request $request) | ||
{ | ||
// Validate request data | ||
$validated = $request->validate([ | ||
'email_notification_activity_in_workspace' => 'required|boolean', | ||
'email_notification_always_send_email_notifications' => 'required|boolean', | ||
'email_notification_email_digest' => 'required|boolean', | ||
'email_notification_announcement_and_update_emails' => 'required|boolean', | ||
'slack_notifications_activity_on_your_workspace' => 'required|boolean', | ||
'slack_notifications_always_send_email_notifications' => 'required|boolean', | ||
'slack_notifications_announcement_and_update_emails' => 'required|boolean', | ||
'mobile_push_notifications' => 'required|boolean', | ||
]); | ||
|
||
// Get authenticated user | ||
$user = $request->user(); | ||
|
||
// Update or create notification settings | ||
$settings = NotificationSetting::updateOrCreate( | ||
['user_id' => $user->id], | ||
$validated | ||
); | ||
|
||
// Send notification about updated settings | ||
NotificationService::sendEmail($user, 'Notification Settings Updated', 'Your notification settings have been updated.'); | ||
|
||
return response()->json([ | ||
'message' => 'Notification settings updated successfully.', | ||
'data' => $settings | ||
]); | ||
} | ||
|
||
/** | ||
* Display the user's notification settings. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function show(Request $request) | ||
{ | ||
// Get authenticated user | ||
$user = $request->user(); | ||
|
||
// Retrieve user's notification settings | ||
$settings = NotificationSetting::where('user_id', $user->id)->firstOrFail(); | ||
|
||
return response()->json($settings); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
namespace App\Http\Controllers\Api\V1\Admin; | ||
|
||
use App\Models\NotificationSetting; | ||
use Illuminate\Http\JsonResponse; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\NotificationPreference; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Auth; | ||
use App\Services\NotificationService; | ||
|
||
class NotificationSettingController extends Controller | ||
class NotificationSettingsController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* Update the user's notification settings. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function index() | ||
public function update(Request $request) | ||
{ | ||
// | ||
} | ||
// Validate request data | ||
$validated = $request->validate([ | ||
'email_notifications' => 'required|boolean', | ||
'push_notifications' => 'required|boolean', | ||
'sms_notifications' => 'required|boolean', | ||
]); | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
*/ | ||
public function create() | ||
{ | ||
// | ||
} | ||
// Get authenticated user | ||
$user = $request->user(); | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
// | ||
} | ||
// Update or create notification preferences | ||
$preferences = NotificationPreference::updateOrCreate( | ||
['user_id' => $user->id], | ||
$validated | ||
); | ||
|
||
// Send notification about updated settings | ||
NotificationService::sendEmail($user, 'Notification Settings Updated', 'Your notification settings have been updated.'); | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show() | ||
{ | ||
$user = \request()->user(); | ||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Notification preferences retrieved successfully', | ||
'status_code' => 200, | ||
'data' => $user->notificationSetting | ||
], Response::HTTP_OK); | ||
'message' => 'Notification settings updated successfully.', | ||
'data' => $preferences | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
* Display the user's notification settings. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function edit(NotificationSetting $notificationSetting) | ||
public function show(Request $request) | ||
{ | ||
// | ||
} | ||
// Get authenticated user | ||
$user = $request->user(); | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(Request $request): JsonResponse | ||
{ | ||
$request->validate([ | ||
'mobile_push_notifications' => 'required|boolean', | ||
'email_notification_activity_in_workspace' => 'required|boolean', | ||
'email_notification_always_send_email_notifications' => 'required|boolean', | ||
'email_notification_email_digest' => 'required|boolean', | ||
'email_notification_announcement_and_update_emails' => 'required|boolean', | ||
'slack_notifications_activity_on_your_workspace' => 'required|boolean', | ||
'slack_notifications_always_send_email_notifications' => 'required|boolean', | ||
'slack_notifications_announcement_and_update_emails' => 'required|boolean', | ||
]); | ||
$user = Auth::user(); | ||
$settings = $user->notificationSetting; | ||
$settings->update($request->all()); | ||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Notification preferences updated successfully', | ||
'status_code' => 200, | ||
'data' => $settings | ||
], 200); | ||
} | ||
// Retrieve user's notification preferences | ||
$preferences = NotificationPreference::where('user_id', $user->id)->firstOrFail(); | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(NotificationSetting $notificationSetting) | ||
{ | ||
// | ||
return response()->json($preferences); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateNotificationSettingsRequest extends FormRequest | ||
{ | ||
public function rules() | ||
{ | ||
return [ | ||
'email_notification_activity_in_workspace' => 'required|boolean', | ||
'email_notification_always_send_email_notifications' => 'required|boolean', | ||
'email_notification_email_digest' => 'required|boolean', | ||
'email_notification_announcement_and_update_emails' => 'required|boolean', | ||
'slack_notifications_activity_on_your_workspace' => 'required|boolean', | ||
'slack_notifications_always_send_email_notifications' => 'required|boolean', | ||
'slack_notifications_announcement_and_update_emails' => 'required|boolean', | ||
'mobile_push_notifications' => 'required|boolean', | ||
]; | ||
} | ||
|
||
protected function prepareForValidation() | ||
{ | ||
$this->merge([ | ||
'email_notification_activity_in_workspace' => filter_var($this->input('email_notification_activity_in_workspace'), FILTER_VALIDATE_BOOLEAN), | ||
'email_notification_always_send_email_notifications' => filter_var($this->input('email_notification_always_send_email_notifications'), FILTER_VALIDATE_BOOLEAN), | ||
'email_notification_email_digest' => filter_var($this->input('email_notification_email_digest'), FILTER_VALIDATE_BOOLEAN), | ||
'email_notification_announcement_and_update_emails' => filter_var($this->input('email_notification_announcement_and_update_emails'), FILTER_VALIDATE_BOOLEAN), | ||
'slack_notifications_activity_on_your_workspace' => filter_var($this->input('slack_notifications_activity_on_your_workspace'), FILTER_VALIDATE_BOOLEAN), | ||
'slack_notifications_always_send_email_notifications' => filter_var($this->input('slack_notifications_always_send_email_notifications'), FILTER_VALIDATE_BOOLEAN), | ||
'slack_notifications_announcement_and_update_emails' => filter_var($this->input('slack_notifications_announcement_and_update_emails'), FILTER_VALIDATE_BOOLEAN), | ||
'mobile_push_notifications' => filter_var($this->input('mobile_push_notifications'), FILTER_VALIDATE_BOOLEAN), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class NotificationSettingsUpdated extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public $subject; | ||
public $message; | ||
|
||
public function __construct($subject, $message) | ||
{ | ||
$this->subject = $subject; | ||
$this->message = $message; | ||
} | ||
|
||
public function build() | ||
{ | ||
return $this->subject($this->subject) | ||
->view('emails.notification_settings_updated') | ||
->with('message', $this->message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Illuminate\Support\Facades\Mail; | ||
use App\Mail\NotificationSettingsUpdated; | ||
|
||
class NotificationService | ||
{ | ||
public static function sendEmail($user, $subject, $message) | ||
{ | ||
Mail::to($user->email)->send(new NotificationSettingsUpdated($subject, $message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters