Skip to content

Commit

Permalink
Update NotificationSettingsController, request validation, and migrat…
Browse files Browse the repository at this point in the history
…ion for new notification settings
  • Loading branch information
EmpireSeeker committed Aug 13, 2024
1 parent 7c3ab73 commit c4f67b9
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 80 deletions.
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);
}
}
9 changes: 9 additions & 0 deletions app/Http/Controllers/Api/V1/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public function subscribe(Request $request)

return response()->json(['message' => 'Subscription successful and notification sent!'], 201);
}

private function sendSubscriptionEmail($subscription){
$user =$subscription->user;
$notificationSettings = $user->notificationSetting;
if($notificationSettings->email_notification){
Mail::to($request->user()->email)->send(new SubscriptionSuccessful($subscription));

}
}
}
109 changes: 39 additions & 70 deletions app/Http/Controllers/NotificationSettingController.php
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);
}
}
36 changes: 36 additions & 0 deletions app/Http/Requests/UpdateNotificationSettingsRequest.php
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),
]);
}
}
28 changes: 28 additions & 0 deletions app/Mail/NotificationSettingsUpdated.php
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);
}
}
14 changes: 14 additions & 0 deletions app/Services/NotificationService.php
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));
}
}
17 changes: 7 additions & 10 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,7 @@
Route::post('/timezones', [ProfileController::class, 'storeTimezones']);
Route::get('/timezones', [ProfileController::class, 'getAllTimezones']);
Route::put('/timezones/{id}', [ProfileController::class, 'updateTimezones']);





Route::put('/notification-preferences/{user_id}', [NotificationPreferenceController::class, 'update']);
Route::post('/subscribe', [NotificationPreferenceController::class, 'subscribe']);

Route::patch('/notification-settings', [NotificationSettingController::class, 'update']);
});
Route::get('/notification-settings', [NotificationSettingController::class, 'show']);

Route::middleware(['auth:api', 'admin'])->get('/customers', [CustomerController::class, 'index']);

Expand Down Expand Up @@ -336,3 +326,10 @@
Route::get('/faqs', [FaqController::class, 'index']);

});



Route::middleware('auth:sanctum')->group(function () {
Route::post('/notification-settings', [NotificationSettingsController::class, 'update']);
Route::get('/notification-settings', [NotificationSettingsController::class, 'show']);
});

0 comments on commit c4f67b9

Please sign in to comment.