feat: add fcm_token for update

This commit is contained in:
2025-12-12 13:19:05 +03:30
parent 771e0b41b1
commit 14c531274e
3 changed files with 736 additions and 722 deletions
+16
View File
@@ -464,6 +464,11 @@ public function updateProfile(Request $request)
'avatar' => 'image' 'avatar' => 'image'
]); ]);
$packageNameData = $request->validate([
'package_name' => 'string',
'fcm_token' => 'string',
]);
if (isset($data['email'])) { if (isset($data['email'])) {
$user = User::where('email', $data['email'])->first(); $user = User::where('email', $data['email'])->first();
if ($user && $data['email'] != auth()->user()->email) { if ($user && $data['email'] != auth()->user()->email) {
@@ -481,6 +486,11 @@ public function updateProfile(Request $request)
], 400); ], 400);
} }
} }
if (isset($packageNameData['package_name']) && !$packageName = PackageName::where('name', $packageNameData['package_name'])->first()) {
return response()->json([
'message' => 'package name not found'
], 404);
}
$user = auth()->user(); $user = auth()->user();
if (isset($data['avatar'])) { if (isset($data['avatar'])) {
@@ -490,6 +500,12 @@ public function updateProfile(Request $request)
$user->update($data); $user->update($data);
if ($packageName ?? null) {
$user->packageNames()->updateExistingPivot($packageName->id, [
'fcm_token' => $packageNameData['fcm_token'] ?? null,
]);
}
return response()->json([ return response()->json([
'user' => $user, 'user' => $user,
'message' => 'user updated' 'message' => 'user updated'
+8 -1
View File
@@ -36,8 +36,14 @@ public function handle(): void
->withServiceAccount($firebaseFile); ->withServiceAccount($firebaseFile);
$push = $factory->createMessaging(); $push = $factory->createMessaging();
$fcmToken = $this->user->packageNames()->where('package_names.id', $this->packageName->id)->first()->pivot->fcm_token;
if (!$fcmToken) {
return;
}
$message = CloudMessage::fromArray([ $message = CloudMessage::fromArray([
'token' => $this->user->packageNames()->where('package_names.id', $this->packageName->id)->first()->pivot->fcm_token, 'token' => $fcmToken,
'notification' => [ 'notification' => [
'title' => $this->title, 'title' => $this->title,
'body' => $this->description, 'body' => $this->description,
@@ -46,6 +52,7 @@ public function handle(): void
]); ]);
$push->send($message); $push->send($message);
} }
/** /**
Generated
+712 -721
View File
File diff suppressed because it is too large Load Diff