File: /var/www/console.fixgini.com/app/Http/Controllers/Profile/UpdateDetail.php
<?php
namespace App\Http\Controllers\Profile;
use App\Models\Shop;
use App\Models\User;
use App\Models\UserPin;
use Illuminate\Http\Request;
use App\Models\OtpVerification;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use App\Mail\EmailChangedNotification;
use Illuminate\Validation\ValidationException;
class UpdateDetail extends Controller
{
public function updateProfile(Request $request)
{
try {
$validatedData = $request->validate([
'email' => ['nullable', 'string'],
'profile_photo_url' => ['nullable'],
'name' => ['nullable', 'string'],
'phone' => ['nullable', 'string'],
'lastname' => ['nullable', 'string'],
'is_fingerprint' => ['nullable', 'boolean'],
'is_pin' => ['nullable', 'boolean'],
'latitude' => ['nullable', 'string'],
'longitude' => ['nullable', 'string'],
'city' => ['nullable', 'string'],
'state' => ['nullable', 'string'],
'address' => ['nullable', 'string'],
'nationality_id' => ['nullable', 'string'],
'role' => ['nullable', 'string'],
]);
$user = Auth::user();
$user = User::find($user->id);
if ($user->role == "buyer") {
$user->email = $validatedData['email'] ?? $user->email;
$user->profile_photo_url = $validatedData['profile_photo_url'] ?? $user->profile_photo_url;
$user->name = $validatedData['name'] ?? $user->name;
$user->phone = $validatedData['phone'] ?? $user->phone;
$user->lastname = $validatedData['lastname'] ?? $user->lastname;
$user->is_fingerprint = $validatedData['is_fingerprint'] ?? $user->is_fingerprint;
$user->is_pin = $validatedData['is_pin'] ?? $user->is_pin;
$user->latitude = $validatedData['latitude'] ?? $user->latitude; // Changed to keep previous value if not set
$user->longitude = $validatedData['longitude'] ?? $user->longitude; // Same as above
$user->city = $validatedData['city'] ?? $user->city;
$user->state = $validatedData['state'] ?? $user->state;
$user->address = $validatedData['address'] ?? $user->address;
$user->role = $validatedData['role'] ?? $user->role;
$user->nationality_id = $validatedData['nationality_id'] ?? $user->nationality_id;
} else {
$user->email = $validatedData['email'] ?? $user->email;
$user->profile_photo_url = $validatedData['profile_photo_url'] ?? $user->profile_photo_url;
$user->is_fingerprint = $validatedData['is_fingerprint'] ?? $user->is_fingerprint;
$user->is_pin = $validatedData['is_pin'] ?? $user->is_pin;
$user->latitude = $validatedData['latitude'] ?? $user->latitude; // Changed to keep previous value if not set
$user->longitude = $validatedData['longitude'] ?? $user->longitude; // Same as above
$user->city = $validatedData['city'] ?? $user->city;
$user->state = $validatedData['state'] ?? $user->state;
}
$user->save();
return response()->json(['status' => 'success', 'message' => 'Profile updated successfully.', 'data' => $user], 200);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['status' => 'error', 'message' => $e->getMessage()], 400);
}
}
public function updatePhoto(Request $request)
{
try {
$data = $request->validate([
'user_id' => ['required'],
'profile_photo_url' => ['required', 'string'],
'profile_photo_public_id' => ['nullable', 'string'],
]);
$user = User::where('id', $data['user_id'])->first();
if ($user) {
$user->profile_photo_url = $data['profile_photo_url']; // Assuming you're storing the path or URL
$user->save();
}
$shop = Shop::where('user_id', $data['user_id'])->first();
if ($shop) {
$shop->profile_photo_url = $data['profile_photo_url']; // Assuming you're storing the path or URL
$shop->profile_photo_public_id = $data['profile_photo_public_id']; // Assuming you're storing the path or URL
$shop->save();
}
return response()->json(['status' => 'error', 'message' => 'Your photo was updated', 'data' => $user], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
}
}
public function queryUser(Request $request)
{
try {
if ($request->user_id) {
$user_id = $request->user_id;
$user = User::find($user_id);
return response()->json(['status' => 'success', 'message' => 'user found', 'data' => $user], 200);
} else {
$user_id = Auth::user()->id;
$user = User::find($user_id);
return response()->json(['status' => 'success', 'message' => 'user found', 'data' => $user], 200);
}
if ($user) {
} else {
return response()->json(['status' => 'error', 'message' => 'user not found'], 404);
}
} catch (\Throwable $th) {
return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
}
}
public function queryUserEmail(Request $request)
{
try {
$validatedData = $request->validate([
'email' => 'required'
]);
$user = User::where('email', $validatedData['email'])->first();
if ($user) {
$isPin = UserPin::where('user_id', $user->id)->exists();
}
if ($user) {
return response()->json(['status' => 'success', 'message' => 'user found', 'data' => $user->id, 'IsPin' => $isPin], 200);
} else {
return response()->json(['status' => 'error', 'message' => 'user not found', 'data' => null], 404);
}
} catch (\Throwable $th) {
return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
}
}
public function ChangeEmail(Request $request)
{
try {
$validatedData = $request->validate([
'email' => 'required|email',
'otp' => 'required|digits:6',
]);
$otpRecord = OtpVerification::where([
['email', '=', $validatedData['email']],
['otp', '=', $validatedData['otp']],
])->latest()->first();
if (!$otpRecord) {
return response()->json(['message' => 'Your OTP is invalid. Please double-check it again.', 'status' => 'error'], 401);
}
$otpRecord->update(['status' => 'verified']);
// Ensure the user is authenticated before accessing user data
$authenticatedUser = Auth::user();
if ($authenticatedUser) {
$user = User::where('email', $authenticatedUser->email)->first();
if ($user) {
// Check if the incoming email is different from the existing email
if ($user->email !== $validatedData['email']) {
// Send a notification to the existing email
$oldEmail = $user->email;
Mail::to($oldEmail)->send(new EmailChangedNotification($user, $validatedData['email']));
// Update the user's email with the new one
$user->update(['email' => $validatedData['email']]);
}
$user->update(['email_verified_at' => now(), 'status' => 'active']);
}
} else {
return response()->json(['message' => 'User not authenticated', 'status' => 'error'], 401);
}
$otpRecord->delete();
return response()->json(['message' => 'Email verified successfully', 'status' => 'success', 'data' => $authenticatedUser], 200);
} catch (ValidationException $e) {
Log::error($e->getMessage());
return response()->json(['status' => 'error', 'message' => $e->getMessage()], 422);
}
}
public function ChangePhone(Request $request)
{
try {
$validatedData = $request->validate([
'phone' => 'required|digits:11|unique:users,phone',
'user_id' => 'sometimes|exists:users,id',
]);
// Ensure the user is authenticated
$authenticatedUser = Auth::user();
// Retrieve the user based on the provided user_id or the authenticated user
$user = User::find($validatedData['user_id'] ?? $authenticatedUser->id);
if ($user) {
// Check if the user already has a phone
if (!$user->phone) {
// User does not have a phone; assign the new phone
$user->update(['phone' => $validatedData['phone']]);
} else {
// User already has a phone; check if the new phone is different
if ($user->phone !== $validatedData['phone']) {
// Send notification to the user's existing email
Mail::to($user->email)->send(new EmailChangedNotification($user, $validatedData['phone']));
// Update the phone number
$user->update(['phone' => $validatedData['phone']]);
}
}
} else {
// If no user is found, return an error response
return response()->json(['status' => 'error', 'message' => 'User not found'], 404);
}
return response()->json([
'message' => 'Phone number updated successfully',
'status' => 'success',
'data' => $user,
], 200);
} catch (ValidationException $e) {
Log::error($e->getMessage());
return response()->json([
'status' => 'error',
'message' => $e->errors(),
], 422);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json([
'status' => 'error',
'message' => 'An unexpected error occurred.',
], 500);
}
}
public function ChangePassword(Request $request)
{
try {
// Validate the incoming request
$validatedData = $request->validate([
'exist_password' => 'nullable',
'new_password' => 'required|min:8',
]);
$user = Auth::user();
if ($user->password == null) {
// Update user's password with the new hashed password
$user->password = Hash::make($validatedData['new_password']);
$user->save();
return response()->json([
'status' => 'success',
'message' => 'Password changed successfully.',
], 200);
}
// Check if the existing password matches the user's current password
if (!Hash::check($validatedData['exist_password'], $user->password)) {
return response()->json([
'status' => 'error',
'message' => 'The existing password is incorrect.',
], 403);
}
$user->password = Hash::make($validatedData['new_password']);
$user->save();
return response()->json([
'status' => 'success',
'message' => 'Password changed successfully.',
], 200);
} catch (ValidationException $e) {
Log::error($e->getMessage());
return response()->json([
'status' => 'error',
'message' => $e->getMessage(),
], 422);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json([
'status' => 'error',
'message' => 'An error occurred while changing the password.',
], 500);
}
}
public function setNewPassword(Request $request)
{
try {
// Validate the incoming request
$validatedData = $request->validate([
'newPassword' => 'required|min:8',
]);
$user = Auth::user();
// Update user's password with the new hashed password
$user->password = Hash::make($validatedData['newPassword']);
$user->google_id = null;
$user->save();
return response()->json([
'status' => 'success',
'data' => $user,
'message' => 'Password created successfully.',
], 200);
} catch (ValidationException $e) {
Log::error($e->getMessage());
return response()->json([
'status' => 'error',
'message' => $e->getMessage(),
], 422);
}
}
// delete account
public function deleteAccount()
{
try {
$user = Auth::user();
$user->delete();
return response()->json([
'status' => 'success',
'message' => 'Account deleted successfully.',
], 200);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json([
'status' => 'error',
'message' => 'An error occurred while deleting the account.',
], 500);
}
}
}