Customer profile API
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin\APIs\Customer_API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\IamPrincipal;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Services\APIs\CustomerAPIs\CustomerApiServices;
|
||||
use Carbon\Carbon;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CustomerControllerApi extends Controller
|
||||
{
|
||||
protected $CustomerApiServices;
|
||||
|
||||
public function __construct(CustomerApiServices $CustomerApiServices)
|
||||
{
|
||||
$this->CustomerApiServices = $CustomerApiServices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Created By : sayli Raut
|
||||
* Created at : 27 May 2024
|
||||
* Use : To get user profile detail.
|
||||
*/
|
||||
public function getUserProfileDetail()
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
|
||||
if ($token) {
|
||||
$customerIamId = $token['sub'];
|
||||
$response = $this->CustomerApiServices->getUserProfileDetailService($customerIamId);
|
||||
return jsonResponseWithSuccessMessageApi(__('auth.data_fetched_successfully'), $response, 200);
|
||||
} else {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
||||
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Created By : Sayli Raut
|
||||
* Created at : 27 May 2024
|
||||
* Use : To update user profile detail.
|
||||
*/
|
||||
public function updateUserProfileDetail(Request $request)
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
$email = $request->input('email_address');
|
||||
$existingEmails = IamPrincipal::where('email_address', $email)
|
||||
->whereNull('deleted_at')
|
||||
->get();
|
||||
|
||||
if ($token) {
|
||||
$customerIamId = $token['sub'];
|
||||
$validator = Validator::make($request->all(), [
|
||||
'date_of_birth' => [
|
||||
'required',
|
||||
'date',
|
||||
function ($attribute, $value, $fail) {
|
||||
$dob = Carbon::parse($value);
|
||||
$age = $dob->age;
|
||||
if ($age < 21) {
|
||||
$fail('You must be at least 21 years old.');
|
||||
}
|
||||
},
|
||||
],
|
||||
'email_address' => 'required|email',
|
||||
'first_name' => 'required|string|min:2|max:100',
|
||||
'last_name' => 'required|string|min:2|max:100',
|
||||
'phone_number' => 'required|min:10|numeric',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return jsonResponseWithErrorMessage($validator->errors()->first(), 400);
|
||||
}
|
||||
|
||||
$response = $this->CustomerApiServices->updateUserProfileDetailService($customerIamId, $request);
|
||||
return response()->json(['status' => 'success', 'message' => 'User profile updated successfully']);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return jsonResponseWithErrorMessage($e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Created By : sayli Raut
|
||||
* Created at : 27 May 2024
|
||||
* Use : Reset Customer password .
|
||||
*/
|
||||
public function resetUserPassword(Request $request)
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
if ($token) {
|
||||
$customerIamId = $token['sub'];
|
||||
$validator = Validator::make($request->all(), [
|
||||
'current_password' => 'required',
|
||||
'new_password' => 'required|string|min:6',
|
||||
'confirm_password' => 'required|same:new_password',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400);
|
||||
}
|
||||
|
||||
return $this->CustomerApiServices->resetUserPassword($customerIamId, $request);
|
||||
} else {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
||||
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Created By : Sayli Raut
|
||||
* Created at : 27 May 2024
|
||||
* Use : Logout Customer Account .
|
||||
*/
|
||||
|
||||
|
||||
public function customerLogout(Request $request)
|
||||
{
|
||||
try {
|
||||
$jwtToken = $request->header('access-token');
|
||||
if ($jwtToken) {
|
||||
$iamPrincipalId = $request->user()->id;
|
||||
$user = IamPrincipal::find($iamPrincipalId);
|
||||
$user->one_signal_player_id = null;
|
||||
$user->save();
|
||||
|
||||
JWTAuth::invalidate($jwtToken);
|
||||
|
||||
Session::forget('vendorToken');
|
||||
|
||||
return jsonResponseWithSuccessMessageApi(__('auth.logout'), 200);
|
||||
} else {
|
||||
return jsonResponseWithErrorMessage(__('auth.invalid_token'), 401);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
Log::error('Account Logout failed: ' . $e->getMessage());
|
||||
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Created By : sayli Raut
|
||||
* Created at : 25 January 2024
|
||||
* Use : Delete Customer Account .
|
||||
*/
|
||||
public function destroyAccount(Request $request)
|
||||
{
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
|
||||
if ($token) {
|
||||
$iamPrincipalId = $token['sub'];
|
||||
$deleteUser = IamPrincipal::find($iamPrincipalId);
|
||||
$deleteUser->one_signal_player_id = null;
|
||||
$deleteUser->save();
|
||||
|
||||
if (!$deleteUser) {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.user_not_found'), 404);
|
||||
}
|
||||
|
||||
$deleteUser->delete();
|
||||
|
||||
return jsonResponseWithSuccessMessageApi(__('auth.delete_user'), 200);
|
||||
} else {
|
||||
return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Account deletion failed: ' . $e->getMessage());
|
||||
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
148
app/Services/APIs/CustomerAPIs/CustomerApiServices.php
Normal file
148
app/Services/APIs/CustomerAPIs/CustomerApiServices.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\APIs\CustomerAPIs;
|
||||
|
||||
use App\Models\IamPrincipal;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CustomerApiServices
|
||||
{
|
||||
public function getUserProfileDetailService($customerIamId)
|
||||
{
|
||||
try {
|
||||
$user = IamPrincipal::findOrFail($customerIamId);
|
||||
|
||||
$data = IamPrincipal::select(
|
||||
'id',
|
||||
'first_name',
|
||||
'last_name',
|
||||
'email_address',
|
||||
'phone_number',
|
||||
'date_of_birth',
|
||||
'state_xid',
|
||||
'profile_photo'
|
||||
)->find($user->id);
|
||||
|
||||
|
||||
if ($data->profile_photo) {
|
||||
$data->profile_photo = ListingImageUrl('profile_image', $data->profile_photo);;
|
||||
} else {
|
||||
$data->profile_photo = asset('public/assets/img/blankProfile.png');
|
||||
}
|
||||
// $data['voucher_redeemed'] = MyPassportVoucher::where([['iam_principal_xid', $customerIamId], ['is_redeem', 1]])->count();
|
||||
return $data;
|
||||
} catch (Exception $ex) {
|
||||
Log::error('Customer Get data service failed : ' . $ex->getMessage());
|
||||
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function updateUserProfileDetailService($customerIamId, $request)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$data = IamPrincipal::findOrFail($customerIamId);
|
||||
|
||||
if (!$data) {
|
||||
DB::rollBack();
|
||||
return jsonResponseWithErrorMessage(__('error_message.user_details_not_found'), 404);
|
||||
}
|
||||
|
||||
if ($request->has('image')) {
|
||||
$image = $request->image;
|
||||
$tnormalImage = saveSingleImageWithoutCrop($image, 'profile_image', null);
|
||||
$data->profile_photo = $tnormalImage;
|
||||
$data->save();
|
||||
DB::commit();
|
||||
|
||||
}
|
||||
|
||||
if ($request->has('date_of_birth')) {
|
||||
$data->date_of_birth = $request->date_of_birth;
|
||||
$data->save();
|
||||
DB::commit();
|
||||
|
||||
}
|
||||
if ($request->has('first_name')) {
|
||||
$data->first_name = $request->first_name;
|
||||
$data->save();
|
||||
DB::commit();
|
||||
|
||||
}
|
||||
if ($request->has('last_name')) {
|
||||
$data->last_name = $request->last_name;
|
||||
$data->save();
|
||||
DB::commit();
|
||||
|
||||
}
|
||||
|
||||
if ($request->has('phone_number')) {
|
||||
$data->phone_number = $request->phone_number;
|
||||
$data->save();
|
||||
DB::commit();
|
||||
|
||||
}
|
||||
|
||||
if ($request->has('email_address')) {
|
||||
$email = $request->input('email_address');
|
||||
if ($email !== $data->email_address) {
|
||||
$existingUser = IamPrincipal::where('email_address', $email)
|
||||
->where('id', '!=', $customerIamId)
|
||||
->whereNull('deleted_at')
|
||||
->exists();
|
||||
|
||||
if ($existingUser) {
|
||||
return jsonResponseWithErrorMessage(__('auth.email_already_exist'), 400);
|
||||
}
|
||||
|
||||
$data->email_address = $email;
|
||||
}
|
||||
}
|
||||
|
||||
$data->save();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json(['status' => 'success', 'message' => 'User profile updated successfully']);
|
||||
} catch (Exception $ex) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Customer update profile service failed : ' . $ex->getMessage());
|
||||
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function resetUserPassword($customerIamId, $request)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$user = IamPrincipal::findOrFail($customerIamId);
|
||||
if (!Hash::check($request->current_password, $user->password)) {
|
||||
DB::rollBack();
|
||||
return jsonResponseWithErrorMessageApi(__('auth.invalid_current_passsword'), 404);
|
||||
} else {
|
||||
$user->update([
|
||||
'password' => Hash::make($request->new_password)
|
||||
]);
|
||||
DB::commit();
|
||||
Log::info("Reset password Successfully");
|
||||
return jsonResponseWithSuccessMessageApi(__('auth.password_updated_successfully'));
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
DB::rollBack();
|
||||
Log::error('Update password service failed : ' . $ex->getMessage());
|
||||
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -54,10 +54,11 @@ return [
|
||||
|
||||
'stack' => [
|
||||
'driver' => 'stack',
|
||||
'channels' => explode(',', env('LOG_STACK', 'single')),
|
||||
'channels' => ['single', 'daily'],
|
||||
'ignore_exceptions' => false,
|
||||
],
|
||||
|
||||
|
||||
'single' => [
|
||||
'driver' => 'single',
|
||||
'path' => storage_path('logs/laravel.log'),
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use App\Http\Controllers\Admin\APIs\Customer_API\AuthController;
|
||||
use App\Http\Controllers\Admin\APIs\Customer_API\CMSApiController;
|
||||
use App\Http\Controllers\Admin\APIs\Customer_API\ContactUsApiController;
|
||||
use App\Http\Controllers\Admin\APIs\Customer_API\CustomerControllerApi;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
||||
@@ -30,10 +31,20 @@ Route::get('/v1/list-of-news-articles', [CMSApiController::class, 'getNewsArticl
|
||||
Route::get('/v1/list-of-terms-conditions', [CMSApiController::class, 'getTermsConditon']);
|
||||
|
||||
|
||||
//*******************************************************contact us********************************************************
|
||||
//*******************************************************Contact Us********************************************************
|
||||
|
||||
Route::post('/v1/contact-us', [ContactUsApiController::class, 'addContactForm']);
|
||||
|
||||
//*******************************************************customer profile********************************************************
|
||||
|
||||
Route::get('/v1/fetch-user-profile', [CustomerControllerApi::class, 'getUserProfileDetail']);
|
||||
Route::post('/v1/update-user-profile', [CustomerControllerApi::class, 'updateUserProfileDetail']);
|
||||
Route::post('/v1/reset-user-password', [CustomerControllerApi::class, 'resetUserPassword']);
|
||||
Route::post('/v1/customer-logout', [CustomerControllerApi::class, 'customerLogout']);
|
||||
Route::post('/v1/delete_account', [CustomerControllerApi::class, 'destroyAccount']);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user