Files
Global-Jain/app/Repositories/Api/Access/User/UserRepository.php
2025-11-05 10:37:10 +05:30

2149 lines
102 KiB
PHP

<?php
namespace App\Repositories\Api\Access\User;
use Carbon\Carbon;
use App\Models\Sant;
use App\Models\User;
use App\Models\Sangh;
use App\Models\Request;
use App\Constant\Constant;
use App\Models\UserDetail;
use App\Models\PostMention;
use App\Models\SanghMember;
use App\Models\UserBlocked;
use Illuminate\Support\Str;
use App\Models\UserRelation;
use App\Models\UserWorkDetail;
use App\Models\UserDeviceToken;
use App\Models\UserSocialLogin;
use App\Models\UserQualification;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
use App\Models\KarmaPointsTransaction;
use App\Jobs\Notifications\Shravak\SendNewSignUp;
use App\Repositories\Api\Access\Relationship\RelationshipRepository;
class UserRepository implements UserInterface
{
/**
* @var User
* @var UserSocialLogin
* @var UserDetail
* @var UserWorkDetail
* @var UserQualification
* @var Request
* @var RelationshipRepository
* @var UserRelation
*
*/
protected $user;
protected $userSocialLogin;
protected $userDetail;
protected $userWorkDetail;
protected $userQualification;
protected $request;
protected $relationshipRepository;
protected $userRelation;
/**
* @param User $user
* @param UserSocialLogin $userSocialLogin
* @param UserDetail $ususerDetailer
* @param UserWorkDetail $userWorkDetail
* @param UserQualification $userQualification
* @param Request $request
* @param RelationshipRepository $relationshipRepository
* @param UserRelation $userRelation
* UserRepository constructor.
*
*/
public function __construct(
User $user,
UserSocialLogin $userSocialLogin,
UserDetail $userDetail,
UserWorkDetail $userWorkDetail,
UserQualification $userQualification,
Request $request,
RelationshipRepository $relationshipRepository,
UserRelation $userRelation
)
{
$this->user = $user;
$this->userSocialLogin = $userSocialLogin;
$this->userDetail = $userDetail;
$this->userWorkDetail = $userWorkDetail;
$this->userQualification = $userQualification;
$this->request = $request;
$this->relationshipRepository = $relationshipRepository;
$this->userRelation = $userRelation;
}
/**
* @param array $data
* @return array
*/
public function loginVerification(array $data)
{
$response = [];
try {
$emailHash = md5(strtolower($data['email'])); //converted to hash
$verified = $data['verified'] ?? Constant::NULL;
$via = $data['via'];
$user = $this->user->query()
->where([
'mobile' => $data['email'],
'country_code' => $data['country_code'],
])->first();
if ($verified == Constant::STATUS_ONE && !empty($user)) {
$user->verification_confirmed = Constant::STATUS_TWO;
$user->app_version = $data['app_version'] ?? Constant::STATUS_ZERO;
$user->save();
}
if (!empty($user)) {
if ($via == Constant::OTP) {
if ($user->verification_confirmed == Constant::STATUS_TWO && $verified != Constant::NULL) {
//Creating token for authentication
$token = $user->createToken('app-token');
//return logged user details
$userData = loggedUserDetail($user);
$response['token'] = $token->plainTextToken;
$response['user'] = $userData;
$response['status'] = Constant::CODE_200;
$response['message'] = trans('auth.otp.verification_success');
$response['success'] = Constant::STATUS_TRUE;
} else {
//Delete existing records of this user
removeUserMetaValue($user->id,'confirmation_code');
//Helper function to generate OTP
$otp = generateOtp();
$dataConfirmationCode['user_id'] = $user->id;
$dataConfirmationCode['meta_key'] = 'confirmation_code';
$dataConfirmationCode['meta_value'] = bcrypt($otp);
$dataRegister['user_id'] = $user->id;
$dataRegister['meta_key'] = 'registered_from';
$dataRegister['meta_value'] = 'Mobile';
$dataRegisterOs['user_id'] = $user->id;
$dataRegisterOs['meta_key'] = 'registered_os';
$dataRegisterOs['meta_value'] = 'Android';
$dataRegisterWith['user_id'] = $user->id;
$dataRegisterWith['meta_key'] = 'registered_with';
$dataRegisterWith['meta_value'] = 'Normal';
//Adding details to user meta
addUserMultipleMetaValue([$dataConfirmationCode, $dataRegister, $dataRegisterOs, $dataRegisterWith]);
//For email/mobile type
$email = $data['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$type = Constant::STATUS_ONE;
event(new \App\Events\UserConfirmation($user, $otp)); //event for sending mail of email verification
} else {
$type = Constant::STATUS_TWO;
}
$response['user'] = [
'type' => $type,
'email' => $user->email ?? '-',
'country_code' => $user->country_code ?? '-',
'mobile' => $user->mobile ?? '-',
'email_preview' => hideEmailAddress($user->email)
];
$response['message'] = trans('auth.check_otp');
$response['status'] = Constant::CODE_200;
}
} else {
if (Hash::check($data['password'], $user->password)) {
$token = $user->createToken('app-token');
//return logged user details
$userData = loggedUserDetail($user);
$response['token'] = $token->plainTextToken;
$response['user'] = $userData;
$response['status'] = Constant::CODE_200;
$response['message'] = trans('auth.social_media.login_success');
$response['success'] = Constant::STATUS_TRUE;
} else {
$response['message'] = trans('auth.failed');
$response['status'] = Constant::CODE_422;
}
}
} else {
$response['message'] = trans('auth.provide_valid_contact');
$response['status'] = Constant::CODE_401;
}
return $response;
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
}
/**
* @param array $data
* @param $emailHash
* @return array
*/
public function createUser(array $data, $emailHash)
{
try {
$userData['name'] = $data['name'] ?? Constant::NULL;
$userData['username'] = generateUsername($data['name']);
$userData['dharma_id'] = $data['dharma'] ?? Constant::NULL;
$userData['birth_date'] = $data['birth_date'] ?? Constant::NULL;
$userData['birth_date_privacy'] = Constant::STATUS_THREE;
$userData['password'] = Hash::make(Str::random(10));
$userData['verification_confirmed'] = Constant::STATUS_TWO;
$email = $data['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$userData['email'] = $data['email'] ?? Constant::NULL;
$userData['email_hash'] = $data['email'] ?? Constant::NULL;
} else {
$userData['country_code'] = $data['country_code'] ?? Constant::NULL;
$userData['mobile'] = $data['email'] ?? Constant::NULL;
}
$userProfileCompletion = calculateActivity($data);
$userData['profile_statistics'] = $userProfileCompletion;
if ($user = $this->user->create($userData)) {
// $user->assignRole('user');
$otp = generateOtp();
$dataConfirmationCode['user_id'] = $user->id;
$dataConfirmationCode['meta_key'] = 'confirmation_code';
$dataConfirmationCode['meta_value'] = bcrypt($otp);
$dataRegister['user_id'] = $user->id;
$dataRegister['meta_key'] = 'registered_from';
$dataRegister['meta_value'] = 'Mobile';
$dataRegisterOs['user_id'] = $user->id;
$dataRegisterOs['meta_key'] = 'registered_os';
$dataRegisterOs['meta_value'] = 'Android';
$dataRegisterWith['user_id'] = $user->id;
$dataRegisterWith['meta_key'] = 'registered_with';
$dataRegisterWith['meta_value'] = 'Normal';
//Adding details to user meta
addUserMultipleMetaValue([$dataConfirmationCode, $dataRegister, $dataRegisterOs, $dataRegisterWith]);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$response['type'] = Constant::STATUS_ONE;
event(new \App\Events\UserConfirmation($user, $otp)); //event for sending mail of email verification
} else {
$response['type'] = Constant::STATUS_TWO;
}
$token = $user->createToken('app-token');
// Add karma points on success full register
$user->addKarmaPoints($user, $user->id, config('config-variables.karma_points_message.platform_registration'), config('config-variables.karma_points.platform_registration'), config('config-variables.karma_points_key.platform_registration'), []);
//Push notification to all users
// $allUsers = User::where('id', '!=', Constant::STATUS_ONE)
// ->where('verification_confirmed', Constant::STATUS_TWO)
// ->pluck('id');
// dispatch(new SendNewSignUp($user, $allUsers));
//return logged user details
$userData = loggedUserDetail($user);
$responseData['token'] = $token->plainTextToken;
$responseData['user'] = $userData;
$responseData['message'] = trans('auth.otp.verification_success');
$responseData['status'] = Constant::CODE_200;
} else {
$responseData['message'] = trans('auth.something_went_wrong');
$responseData['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$responseData['message'] = trans('auth.something_went_wrong');
$responseData['status'] = Constant::CODE_403;
}
return $responseData;
}
/**
* @param array $data
* @return array
*/
public function confirmOtp(array $data)
{
$response = [];
try {
$user = $this->findByEmail($data['email']);
$originalOtp = getUserMetaValue($user->id, 'confirmation_code');
if ($user && !empty($originalOtp) && Hash::check($data['otp'], $originalOtp)) {
$user->verification_confirmed = Constant::STATUS_TWO;
$user->status = Constant::STATUS_TWO;
$user->save();
removeUserMetaValue($user->id, 'confirmation_code'); //Remove confirmation code
addUserSingleMetaValue($user->id,'confirmed_at',Carbon::now()->format(Constant::DATE_TIME_FORMAT)); //Adding confirmation time&date
//Creating token for authentication
$token = $user->createToken('app-token');
//return logged user details
$userData = loggedUserDetail($user);
$response['token'] = $token->plainTextToken;
$response['user'] = $userData;
$response['status'] = Constant::CODE_200;
$response['message'] = trans('auth.otp.verification_success');
$response['success'] = Constant::STATUS_TRUE;
} else {
$response['status'] = Constant::CODE_403;
$response['message'] = trans('auth.otp.invalid');
$response['success'] = Constant::STATUS_FALSE;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['status'] = Constant::CODE_403;
$response['message'] = trans('auth.something_went_wrong');
$response['success'] = Constant::STATUS_FALSE;
}
return $response;
}
/**
* @param string $email
* @return object
*/
public function findByEmail($email)
{
$emailHash = md5(strtolower($email)); //converted to hash
return $this->user->query()->where('email_hash', $emailHash)->first();
}
/**
*
* @param array $data
* @return bool
*
* Social media login and adding user.
*/
public function findOrCreateSocial(array $data)
{
$responseData = [];
try {
if (env('SOCIAL_MEDIA_AUTH')) { //if social media login allows
$provider = $data['provider'];
// User email may not provided.
$user_email = $data['email'] ?? "{$data['id']}@{$provider}.com";
// Check to see if there is a user with this email first.
$account = $this->userSocialLogin->where('provider', $provider)
->where('provider_id', $data['id'])
->first();
//If already added in our system it will return values
if ($account) {
$user = $account->user;
$token = $user->createToken('app-token');
//return logged user details
$userData = loggedUserDetail($user);
} else { // Insert as new user
$user = $this->findByEmail($data['email']);
//Checking email is registered or not if not then save it
if (!$user) {
$user = $this->user->create([
'email' => $user_email,
'email_hash' => $data['email'],
'name' => $data['name'],
'username' => generateUsername($data['name']),
'email_verified_at' => date('Y-m-d H:i:s'),
'verification_confirmed' => Constant::STATUS_TWO,
'status' => Constant::STATUS_TWO,
]);
$dataRegister['user_id'] = $user->id;
$dataRegister['meta_key'] = 'registered_from';
$dataRegister['meta_value'] = 'Social media';
$dataRegisterOs['user_id'] = $user->id;
$dataRegisterOs['meta_key'] = 'registered_os';
$dataRegisterOs['meta_value'] = 'Android';
$dataRegisterWith['user_id'] = $user->id;
$dataRegisterWith['meta_key'] = 'registered_with';
$dataRegisterWith['meta_value'] = $provider;
//Adding details to user meta
addUserMultipleMetaValue([$dataRegister, $dataRegisterOs, $dataRegisterWith]);
}
//Store social media account id and provider
$user->userSocialLogins()->create([
'provider_id' => $data['id'],
'provider' => $provider,
]);
//deleting previous generated tokens
if (!empty($user)) {
$user->tokens()->delete();
}
//Generating new token
$token = $user->createToken('app-token');
//return logged user details
$userData = loggedUserDetail($user);
}
$responseData['token'] = $token->plainTextToken;
$responseData['user'] = $userData;
$responseData['message'] = trans('auth.social_media.login_success', ['provider' => $provider]);
$responseData['status'] = Constant::CODE_200;
} else {
$responseData['message'] = trans('auth.social_media.not_allowed');
$responseData['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$responseData['message'] = trans('auth.something_went_wrong');
$responseData['status'] = Constant::CODE_403;
}
return $responseData;
}
/**
* @return array
*/
public function getUserList(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
$alreadyTaggedId = [];
if (isset($data['already_tagged']) && !empty($data['already_tagged'])) {
$alreadyTaggedId = $data['already_tagged'];
}
if (!empty($data['name'])) {
$users = $this->user->where('name','like','%'.$data['name'].'%')
->where('id','<>',$user->id)
->whereNotIn('id', $alreadyTaggedId)
->where('id','!=',Constant::STATUS_ONE)
->where('is_passive','!=',Constant::STATUS_ONE)
->get()->toArray();
} else {
$users = $this->user->where('id','<>',$user->id)
->where('id','!=',Constant::STATUS_ONE)
->whereNotIn('id', $alreadyTaggedId)
->where('is_passive','!=',Constant::STATUS_ONE)
->get()->toArray();
}
$response['data'] = $users;
$response['status'] = Constant::CODE_200;
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function getUserDetail()
{
$response = [];
$responseData = [];
try {
$user = loggedInUser();
$userData = $this->user->with(['userDetail', 'userWorkDetail', 'userRelationshipRequests.relationships', 'userRelationshipRequests.sentUsers', 'colleges', 'schools', 'passiveUser' => function ($q) {
$q->select('id',
'name',
'parent_id',
'is_passive',
'relationship',
'about',
'school_name',
'avatar',
'birth_date');
}])->where('id', $user->id)->first()->toArray();
$sentRequests = $user->friendRequests()->with('userDetail')->select('users.id', 'users.name', 'users.avatar')->where('requests.status', Constant::STATUS_ONE)->count();
$receivedRequests = $user->myFriendRequests()->with('userDetail')->select('users.id', 'users.name', 'users.avatar')->where('requests.status', Constant::STATUS_ONE)->count();
$karmaPoints = KarmaPointsTransaction::whereNotIn('key', ['sangh_add_post_dharmik', 'add_new_sangh', 'new_sangh_member'])->where(function ($subQuery) use ($user) {
$subQuery->where('user_id', $user->id);
// ->orWhere('pointable_id', $user->id);
})->sum('points');
$sanghMembersCount = $user->sanghMembers()->where('status', Constant::STATUS_ONE)->count();
if ($user && !empty($userData)) {
$userData['user_qualification']['schools'] = $userData['schools'] ?? [];
$userData['user_qualification']['colleges'] = $userData['colleges'] ?? [];
$responseData['data'] = [
'id' => $userData['id'] ?? "",
'uuid' => $userData['uuid'] ?? "",
'name' => $userData['name'] ?? "",
'email' => $userData['email'] ?? "",
'email_privacy' => $userData['email_privacy'] ?? Constant::STATUS_ONE,
'username' => $userData['username'] ?? "",
'country_code' => $userData['country_code'] ?? "",
'mobile' => $userData['mobile'] ?? "",
'mobile_privacy' => $userData['mobile_privacy'] ?? Constant::STATUS_THREE,
'dharma_id' => $userData['dharma_id'] ?? "",
'dharma_name' => $userData['dharma_id'] ? getDharmaName($userData['dharma_id']) : "",
'dharma_privacy' => $userData['dharma_privacy'] ?? Constant::STATUS_ONE,
'birth_date' => $userData['birth_date'] ?? "",
'birth_date_privacy' => $userData['birth_date_privacy'] ?? Constant::STATUS_THREE,
'gender_id' => $userData['gender'] ? $userData['gender'] : "",
'gender' => $userData['gender'] ? getGenderTypeName($userData['gender']) : "",
'gender_privacy' => $userData['gender_privacy'] ?? Constant::STATUS_ONE,
'email_verified_at' => $userData['email_verified_at'] ?? "",
'mobile_verified_at' => $userData['mobile_verified_at'] ?? "",
'avatar' => $userData['avatar'] ?? "",
'avatar_privacy' => $userData['avatar_privacy'] ?? Constant::STATUS_ONE,
'verification_confirmed' => $userData['verification_confirmed'] ?? "",
'is_profile_completed' => $userData['is_profile_completed'],
'status' => $userData['status'] ?? "",
'timezone' => $userData['timezone'] ?? "",
'last_login_at' => $userData['last_login_at'] ?? "",
'last_login_ip' => $userData['last_login_ip'] ?? "",
'created_at' => $userData['created_at'] ?? "",
'updated_at' => $userData['updated_at'] ?? "",
'deleted_at' => $userData['deleted_at'] ?? "",
'jati_id' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['jati_id'] : "",
'jati_name' => isset($userData['user_detail'][Constant::STATUS_ZERO]) ? getJatiName($userData['user_detail'][Constant::STATUS_ZERO]['jati_id']) : "",
'jati_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['jati_privacy'] : Constant::STATUS_ONE,
'location' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['location'] : "",
'location_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['location_privacy'] : Constant::STATUS_THREE,
'longitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['longitude'] : "",
'longitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['longitude'] : "",
'latitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['latitude'] : "",
'native_place' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_place'] : "",
'native_place_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_place_privacy'] : Constant::STATUS_ONE,
'native_longitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_longitude'] : "",
'native_latitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_latitude'] : "",
'mother_tongue_id' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_id'] : "",
'mother_tongue' => isset($userData['user_detail'][Constant::STATUS_ZERO]) ? getMotherTongue($userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_id']) : "",
'mother_tongue_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_privacy'] : Constant::STATUS_ONE,
'blood_group_id' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['blood_group_id'] : "",
'blood_group' => isset($userData['user_detail'][Constant::STATUS_ZERO]) ? getBloodGroup($userData['user_detail'][Constant::STATUS_ZERO]['blood_group_id']) : "",
'blood_group_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['blood_group_privacy'] : Constant::STATUS_ONE,
'marital_status' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['marital_status'] : "",
'marital_status_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['marital_status_privacy'] : Constant::STATUS_ONE,
'marriage_anniversary' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['marriage_anniversary'] : "",
'marriage_anniversary_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['marriage_anniversary_privacy'] : Constant::STATUS_THREE,
'family_members' => $userData['user_relationship_requests'] ?? [],
'passive_users' => $userData['passive_user'] ?? [],
'company_details' => $userData['user_work_detail'] ?? [],
'profession' => !empty($userData['user_work_detail']) ? $userData['user_work_detail'][Constant::STATUS_ZERO]['profession'] : "",
'profession_privacy' => !empty($userData['user_work_detail']) ? $userData['user_work_detail'][Constant::STATUS_ZERO]['profession_privacy'] : Constant::STATUS_ONE,
'qualificaion_details' => $userData['user_qualification'] ?? [],
'karma_dhan' => $karmaPoints ?? 0,
'friend_count' => ($sentRequests + $receivedRequests),
'sangh_member_count' => $sanghMembersCount,
'profile_statistics' => $userData['profile_statistics'] ?? [],
'is_working' => $userData['is_working'],
'share_content' => 'https://globaljain.focalat.com/'
];
$response = $responseData;
$response['status'] = Constant::CODE_200;
} else {
$response['data'] = [];
$response['status'] = Constant::CODE_200;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function getUserDetailById($id)
{
$response = [];
$responseData = [];
try {
$user = loggedInUser();
$userData = $this->user->withCount(['sanghMembers' => function ($query) {
$query->where('sangh_members.status', Constant::STATUS_ONE);
}])->with(['userDetail', 'userWorkDetail', 'userQualification', 'userRelationshipRequests' => function ($query) {
$query->where('status', Constant::STATUS_ONE);
}, 'userRelationshipRequests.sentUsers', 'colleges', 'schools', 'passiveUser' => function ($q) {
$q->select('id',
'name',
'parent_id',
'is_passive',
'relationship',
'about',
'school_name',
'avatar',
'birth_date');
}])->where('id', $id)->first()->toArray();
//For friend requests
$sentRequest = $this->request->where(['sender_id' => $user->id, 'receiver_id' => $id])->value('status');
$receivedRequest = $this->request->where(['sender_id' => $id, 'receiver_id' => $user->id])->value('status');
//For relationship requests
$sentRelationRequest = $this->userRelation->where(['from_id' => $user->id, 'to_id' => $id])->value('status');
$receivedRelationRequest = $this->userRelation->where(['from_id' => $id, 'to_id' => $user->id])->value('status');
// Count of friend
$currentUser = User::find($id);
$sentRequests = $currentUser->friendRequests()->with('userDetail')->select('users.id', 'users.name', 'users.avatar')->where('requests.status', Constant::STATUS_ONE)->count();
$receivedRequests = $currentUser->myFriendRequests()->with('userDetail')->select('users.id', 'users.name', 'users.avatar')->where('requests.status', Constant::STATUS_ONE)->count();
$karmaPoints = KarmaPointsTransaction::whereNotIn('key', ['sangh_add_post_dharmik', 'add_new_sangh', 'new_sangh_member'])->where(function ($subQuery) use ($id) {
$subQuery->where('user_id', $id);
// ->orWhere('pointable_id', $id);
})->sum('points');
if ($user && !empty($userData)) {
$userData['user_qualification']['schools'] = $userData['schools'] ?? [];
$userData['user_qualification']['colleges'] = $userData['colleges'] ?? [];
// dd($userData['user_relationship_requests']);
$responseData['data'] = [
'id' => $userData['id'] ?? "",
'uuid' => $userData['uuid'] ?? "",
'name' => $userData['name'] ?? "",
'email' => $userData['email'] ? checkUserPrivacy($userData['email_privacy'],$userData['email'], $userData['is_friends']) : "",
'email_privacy' => $userData['email_privacy'] ?? "",
'username' => $userData['username'] ?? "",
'country_code' => $userData['country_code'] ?? "",
'mobile' => $userData['mobile'] ? checkUserPrivacy($userData['mobile_privacy'],$userData['mobile'], $userData['is_friends']) : "",
'mobile_privacy' => $userData['mobile_privacy'] ?? "",
'dharma_id' => $userData['dharma_id'] ?? "",
'dharma_name' => $userData['dharma_id'] ? checkUserPrivacy($userData['dharma_privacy'], getDharmaName($userData['dharma_id']), $userData['is_friends']) : "",
'dharma_privacy' => $userData['dharma_privacy'] ?? "",
'birth_date' => $userData['birth_date'] ? checkUserPrivacy($userData['birth_date_privacy'],$userData['birth_date'], $userData['is_friends']) : "",
'birth_date_privacy' => $userData['birth_date_privacy'] ?? "",
'gender_id' => $userData['gender'] ? $userData['gender'] : "",
'gender' => $userData['gender'] ? checkUserPrivacy($userData['gender_privacy'], getGenderTypeName($userData['gender']), $userData['is_friends']) : "",
'gender_privacy' => $userData['gender_privacy'] ?? "",
'email_verified_at' => $userData['email_verified_at'] ?? "",
'mobile_verified_at' => $userData['mobile_verified_at'] ?? "",
'avatar' => $userData['avatar'] ? checkUserPrivacy($userData['avatar_privacy'],$userData['avatar'], $userData['is_friends']) : "",
'avatar_privacy' => $userData['avatar_privacy'] ?? "",
'verification_confirmed' => $userData['verification_confirmed'] ?? "",
'status' => $userData['status'] ?? "",
'is_profile_completed' => $userData['is_profile_completed'],
'timezone' => $userData['timezone'] ?? "",
'last_login_at' => $userData['last_login_at'] ?? "",
'last_login_ip' => $userData['last_login_ip'] ?? "",
'created_at' => $userData['created_at'] ?? "",
'updated_at' => $userData['updated_at'] ?? "",
'deleted_at' => $userData['deleted_at'] ?? "",
'jati_id' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['jati_id'] : "",
'jati_name' => isset($userData['user_detail'][Constant::STATUS_ZERO]) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['jati_privacy'], getJatiName($userData['user_detail'][Constant::STATUS_ZERO]['jati_id']), $userData['is_friends']) : "",
'jati_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['jati_privacy'] : "",
'location' => !empty($userData['user_detail']) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['location_privacy'], $userData['user_detail'][Constant::STATUS_ZERO]['location'], $userData['is_friends']) : "",
'location_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['location_privacy'] : "",
'longitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['longitude'] : "",
'longitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['longitude'] : "",
'latitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['latitude'] : "",
'native_place' => !empty($userData['user_detail']) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['native_place_privacy'], $userData['user_detail'][Constant::STATUS_ZERO]['native_place'], $userData['is_friends']) : "",
'native_place_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_place_privacy'] : "",
'native_longitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_longitude'] : "",
'native_latitude' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['native_latitude'] : "",
'mother_tongue_id' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_id'] : "",
'mother_tongue' => isset($userData['user_detail'][Constant::STATUS_ZERO]) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_privacy'], getMotherTongue($userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_id']), $userData['is_friends']) : "",
'mother_tongue_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['mother_tongue_privacy'] : "",
'blood_group_id' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['blood_group_id'] : "",
'blood_group' => isset($userData['user_detail'][Constant::STATUS_ZERO]) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['blood_group_privacy'], getBloodGroup($userData['user_detail'][Constant::STATUS_ZERO]['blood_group_id']), $userData['is_friends']) : "",
'blood_group_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['blood_group_privacy'] : "",
'marital_status' => !empty($userData['user_detail']) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['marital_status_privacy'], $userData['user_detail'][Constant::STATUS_ZERO]['marital_status'], $userData['is_friends']) : "",
'marital_status_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['marital_status_privacy'] : "",
'marriage_anniversary' => !empty($userData['user_detail']) ? checkUserPrivacy($userData['user_detail'][Constant::STATUS_ZERO]['marriage_anniversary_privacy'], $userData['user_detail'][Constant::STATUS_ZERO]['marriage_anniversary'], $userData['is_friends']) : "",
'marriage_anniversary_privacy' => !empty($userData['user_detail']) ? $userData['user_detail'][Constant::STATUS_ZERO]['marriage_anniversary_privacy'] : "",
'family_members' => checkRelationPrivacy($userData['user_relationship_requests'], $userData['is_friends']) ?? [],
'passive_users' => $userData['passive_user'] ?? [],
'company_details' => checkWorkPrivacy($userData['user_work_detail'], $userData['is_friends']) ?? [],
'profession' => !empty($userData['user_work_detail']) ? checkUserPrivacy($userData['user_work_detail'][Constant::STATUS_ZERO]['profession_privacy'], $userData['user_work_detail'][Constant::STATUS_ZERO]['profession'], $userData['is_friends']) : "",
'profession_privacy' => !empty($userData['user_work_detail']) ? $userData['user_work_detail'][Constant::STATUS_ZERO]['profession_privacy'] : "",
'qualificaion_details' => checkQualificationPrivacy($userData['user_qualification'], $userData['is_friends']) ?? [],
'karma_dhan' => $karmaPoints ?? 0,
'is_friends' => $userData['is_friends'] ?? "",
'is_blocked' => $userData['is_blocked'] ?? "",
'friend_count' => ($sentRequests + $receivedRequests),
'is_working' => $userData['is_working'],
'sangh_member_count' => isset($userData['sangh_members_count']) ? $userData['sangh_members_count'] : 0,
'share_content' => 'https://globaljain.focalat.com/'
];
//Friend request status
if ($sentRequest === Constant::STATUS_ZERO && $receivedRequest === Constant::NULL) {
$responseData['data']['request_status'] = Constant::STATUS_ZERO;
} elseif ($id == loggedInUser()->id || $sentRequest === Constant::STATUS_ONE || $receivedRequest === Constant::STATUS_ONE) {
$responseData['data']['request_status'] = Constant::STATUS_ONE;
} elseif ($sentRequest === Constant::NULL && $receivedRequest === Constant::STATUS_ZERO) {
$responseData['data']['request_status'] = Constant::STATUS_THREE;
} else {
$responseData['data']['request_status'] = Constant::STATUS_FOUR;
}
//Relationship request status
if ($sentRelationRequest == Constant::STATUS_ZERO && $receivedRelationRequest != Constant::STATUS_ONE && $receivedRelationRequest != Constant::STATUS_ZERO && $receivedRelationRequest != Constant::NULL) {
$responseData['data']['relation_request_status'] = Constant::STATUS_ZERO;
} elseif ($sentRelationRequest == Constant::STATUS_ONE || $receivedRelationRequest == Constant::STATUS_ONE) {
$responseData['data']['relation_request_status'] = Constant::STATUS_ONE;
} elseif ($receivedRelationRequest == Constant::STATUS_ZERO || $receivedRelationRequest != Constant::NULL) {
$responseData['data']['relation_request_status'] = Constant::STATUS_THREE;
} else {
$responseData['data']['relation_request_status'] = Constant::STATUS_FOUR;
}
$response = $responseData;
$response['status'] = Constant::CODE_200;
} else {
$response['data'] = [];
$response['status'] = Constant::CODE_200;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @param object $data
* @return array
*/
public function updateUser($data)
{
$response = [];
try {
$loggedInUser = loggedInUser();
if ($loggedInUser) {
$userDeatilExists = $this->userDetail->query()->where([
'user_id' => $loggedInUser->id
])->first();
if ($userDeatilExists) {
$userDeatilExists->native_place = $data['native_place'] ?? $userDeatilExists->native_place;
$userDeatilExists->native_longitude = $data['native_longitude'] ?? $userDeatilExists->native_longitude;
$userDeatilExists->native_latitude = $data['native_latitude'] ?? $userDeatilExists->native_latitude;
$userDeatilExists->location = $data['location'] ?? $userDeatilExists->location;
$userDeatilExists->longitude = $data['longitude'] ?? $userDeatilExists->longitude;
$userDeatilExists->latitude = $data['latitude'] ?? $userDeatilExists->latitude;
$userDeatilExists->mother_tongue_id = $data['mother_tongue'] ?? $userDeatilExists->mother_tongue_id;
$userDeatilExists->blood_group_id = $data['blood_group'] ?? $userDeatilExists->blood_group_id;
$userDeatilExists->marital_status = $data['marital_status'] ?? $userDeatilExists->marital_status;
$userDeatilExists->marriage_anniversary = $data['marriage_anniversary'] ?? $userDeatilExists->marriage_anniversary;
$userDeatilExists->mother_tongue_privacy = $data['mother_tongue_privacy'] ?? $userDeatilExists->mother_tongue_privacy;
$userDeatilExists->blood_group_privacy = $data['blood_group_privacy'] ?? $userDeatilExists->blood_group_privacy;
$userDeatilExists->jati_privacy = $data['jati_privacy'] ?? $userDeatilExists->jati_privacy;
$userDeatilExists->marital_status_privacy = $data['marital_status_privacy'] ?? $userDeatilExists->marital_status_privacy;
$userDeatilExists->marriage_anniversary_privacy = $data['marriage_anniversary_privacy'] ?? $userDeatilExists->marriage_anniversary_privacy;
$userDeatilExists->location_privacy = $data['location_privacy'] ?? $userDeatilExists->location_privacy;
$userDeatilExists->native_place_privacy = $data['native_place_privacy'] ?? $userDeatilExists->native_place_privacy;
if (!empty($data['jati'])) {
$userDeatilExists->jati_id = $data['jati'];
} else if (isset($data['jati']) === true) {
$userDeatilExists->jati_id = Constant::NULL;
} else {
$userDeatilExists->jati_id = $userDeatilExists->jati_id;
}
$userDeatilExists->save();
} else {
$this->userDetail->create([
'user_id' => $loggedInUser->id,
'jati_id' => $data['jati'] ?? Constant::NULL,
'native_place' => $data['native_place'] ?? Constant::NULL,
'native_longitude' => $data['native_longitude'] ?? Constant::NULL,
'native_latitude' => $data['native_latitude'] ?? Constant::NULL,
'location' => $data['location'] ?? Constant::NULL,
'longitude' => $data['longitude'] ?? Constant::NULL,
'latitude' => $data['latitude'] ?? Constant::NULL,
'mother_tongue_id' => $data['mother_tongue'] ?? Constant::NULL,
'blood_group_id' => $data['blood_group'] ?? Constant::NULL,
'marital_status' => $data['marital_status'] ?? Constant::NULL,
'marriage_anniversary' => $data['marriage_anniversary'] ?? Constant::NULL,
'mother_tongue_privacy' => $data['mother_tongue_privacy'] ?? Constant::STATUS_ONE,
'blood_group_privacy' => $data['blood_group_privacy'] ?? Constant::STATUS_ONE,
'jati_privacy' => $data['jati_privacy'] ?? Constant::STATUS_ONE,
'marital_status_privacy' => $data['marital_status_privacy'] ?? Constant::STATUS_ONE,
'marriage_anniversary_privacy' => $data['marriage_anniversary_privacy'] ?? Constant::STATUS_ONE,
'location_privacy' => $data['location_privacy'] ?? Constant::STATUS_ONE,
'native_place_privacy' => $data['native_place_privacy'] ?? Constant::STATUS_ONE
]);
}
//Storing/Updating data to user work details table
if (isset($data['company_details']) && !empty(array_values(($data['company_details'])))) {
$this->updateWorkDetail($data->toArray());
}
//Storing/Updating data to user qualifications table
if (isset($data['qualification_details']) && !empty(array_values($data['qualification_details']))) {
$this->updateQualificationDetail($data->toArray());
}
//Updating relation privacy
if (isset($data['family_members']) && !empty(array_values($data['family_members']))) {
$this->relationshipRepository->updateRelationPrivacy($data->toArray());
}
//Storing data to users table
$user = $this->user->with('userDetail', 'userWorkDetail', 'userQualification')->where('id', $loggedInUser->id)->first();
$user->name = $data['name'] ?? $user->name;
$user->birth_date = $data['birth_date'] ?? $user->birth_date;
$user->dharma_id = $data['dharma'] ?? $user->dharma_id;
$user->gender = $data['gender'] ?? $user->gender;
$user->country_code = $data['country_code'] ?? $user->country_code;
$user->mobile = $data['mobile'] ?? $user->mobile;
$user->mobile_privacy = $data['mobile_privacy'] ?? $user->mobile_privacy;
$user->email_privacy = $data['email_privacy'] ?? $user->email_privacy;
$user->avatar_privacy = $data['avatar_privacy'] ?? $user->avatar_privacy;
$user->birth_date_privacy = $data['birth_date_privacy'] ?? $user->birth_date_privacy;
$user->dharma_privacy = $data['dharma_privacy'] ?? $user->dharma_privacy;
$user->gender_privacy = $data['gender_privacy'] ?? $user->gender_privacy;
if (!empty($data['avatar'])) {
$imageName = uploadImage($data, 'avatar', Constant::USER_IMAGE_UPLOAD_PATH . Constant::SLASH, $user->getRawOriginal('avatar'));
$user->avatar = $imageName['image_name'] ?? $user->avatar;
}
if (!empty($data['email'])) {
$user->email = $data['email'];
$user->email_hash = $data['email'];
} else if (isset($data['email']) === false) {
$user->email = $user->email;
$user->email_hash = $user->email_hash;
} else {
$user->email = Constant::NULL;
$user->email_hash = Constant::NULL;
}
$userProfileCompletion = calculateActivity($user->toArray());
$user->profile_statistics = $userProfileCompletion;
$checkUser = User::where([
'mobile' => $user->mobile,
'country_code' => $user->country_code,
'deleted_at' => Constant::NULL
])->where('id', '!=', $user->id)->first();
if (!empty($checkUser)) {
$response['status'] = Constant::CODE_403;
$response['message'] = trans('auth.contact_exists');
} else {
$emailHash = md5(strtolower($data['email']));
$checkEmail = User::where('email_hash', $emailHash)->where('id', '!=', $loggedInUser->id)->first();
if (!empty($checkEmail)) {
$response['message'] = trans('auth.email_exists');
$response['status'] = Constant::CODE_403;
} else {
$user->email = $data['email'] ?? $user->email;
$user->email_hash = $data['email'] ?? $user->email_hash;
if ($user->profile_statistics['profile_completion_percentage'] != 100 && $loggedInUser->is_profile_completed === 1) {
$user->is_profile_completed = Constant::STATUS_TWO;
//Remove karma points
$karmaPoints = KarmaPointsTransaction::where('key', 'profile_complition')->where('user_id', $loggedInUser->id)->delete();
if ($karmaPoints) {
//Adds 50 karma points
$user->addKarmaPoints($user, $user->id, config('config-variables.karma_points_message.shravak_revert_half'), config('config-variables.karma_points.shravak_revert_half'), config('config-variables.karma_points_key.shravak_revert_half'), []);
}
}
// Add karma points on profile complition
if ($user->profile_statistics['profile_completion_percentage'] == 100 && $loggedInUser->is_profile_completed == 0) {
$user->addKarmaPoints($user, $user->id, config('config-variables.karma_points_message.profile_complition'), config('config-variables.karma_points.profile_complition'), config('config-variables.karma_points_key.profile_complition'), []);
}
// Add karma points on profile complition
if ($user->profile_statistics['profile_completion_percentage'] == 100 && $loggedInUser->is_profile_completed == 2) {
//Remove karma points
$karmaPoints = KarmaPointsTransaction::where('key', 'shravak_revert_half')->where('user_id', $loggedInUser->id)->delete();
if ($karmaPoints) {
$user->addKarmaPoints($user, $user->id, config('config-variables.karma_points_message.profile_complition'), config('config-variables.karma_points.profile_complition'), config('config-variables.karma_points_key.profile_complition'), []);
}
}
if ($user->profile_statistics['profile_completion_percentage'] === 100) {
$user->is_profile_completed = Constant::STATUS_ONE;
}
$user->save();
$response['message'] = trans('auth.user.update_profile');
$response['status'] = Constant::CODE_200;
}
// $user->save();
// $response['message'] = trans('auth.user.update_profile');
// $response['status'] = Constant::CODE_200;
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @param array $data
* @return array
*/
public function updateWorkDetail(array $data)
{
$response = [];
try {
$loggedInUser = loggedInUser();
if ($loggedInUser) {
foreach ($data['company_details'] as $workDetail) {
$userWorkDetailExists = $this->userWorkDetail->where([
'user_id' => $loggedInUser->id,
'id' => $workDetail['work_id']
])->first();
//Updating data to user work details table
if ($userWorkDetailExists) {
$userWorkDetailExists->company_name = $workDetail['company_name'] ?? $userWorkDetailExists->company_name;
$userWorkDetailExists->company_name_privacy = $workDetail['company_name_privacy'] ?? $userWorkDetailExists->company_name_privacy;
$userWorkDetailExists->profession_id = $workDetail['profession_id'] ?? $userWorkDetailExists->profession_id;
$userWorkDetailExists->profession = $workDetail['profession'] ?? $userWorkDetailExists->profession;
$userWorkDetailExists->profession_privacy = $workDetail['profession_privacy'] ?? $userWorkDetailExists->profession_privacy;
$userWorkDetailExists->profession_speciality = $workDetail['profession_speciality'] ?? $userWorkDetailExists->profession_speciality;
$userWorkDetailExists->profession_speciality_privacy = $workDetail['profession_speciality_privacy'] ?? $userWorkDetailExists->profession_speciality_privacy;
$userWorkDetailExists->position = $workDetail['position'] ?? Constant::NULL;
$userWorkDetailExists->position_privacy = $workDetail['position_privacy'] ?? $userWorkDetailExists->position_privacy;
$userWorkDetailExists->city = $workDetail['city'] ?? Constant::NULL;
$userWorkDetailExists->city_privacy = $workDetail['city_privacy'] ?? $userWorkDetailExists->city_privacy;
$userWorkDetailExists->about = $workDetail['about'] ?? Constant::NULL;
$userWorkDetailExists->about_privacy = $workDetail['about_privacy'] ?? $userWorkDetailExists->about_privacy;
$userWorkDetailExists->is_working = $workDetail['is_working'] ?? $userWorkDetailExists->is_working;
$userWorkDetailExists->website = $workDetail['website'] ?? Constant::NULL;
$userWorkDetailExists->website_privacy = $workDetail['website_privacy'] ?? $userWorkDetailExists->website_privacy;
$userWorkDetailExists->start_year = $workDetail['start_year'] ?? $userWorkDetailExists->start_year;
$userWorkDetailExists->end_year = $workDetail['end_year'] ?? $userWorkDetailExists->end_year;
$userWorkDetailExists->work_duration_privacy = $workDetail['work_duration_privacy'] ?? $userWorkDetailExists->work_duration_privacy;
$userWorkDetailExists->save();
}
//Storing data to user work details table
if (!$userWorkDetailExists && $workDetail['work_id'] == Constant::NULL) {
$this->userWorkDetail->create([
'user_id' => $loggedInUser->id,
'company_name' => $workDetail['company_name'] ?? Constant::NULL,
'company_name_privacy' => $workDetail['company_name_privacy'] ?? Constant::STATUS_ONE,
'profession_id' => $workDetail['profession_id'] ?? Constant::NULL,
'profession' => $workDetail['profession'] ?? Constant::NULL,
'profession_privacy' => $workDetail['profession_privacy'] ?? Constant::STATUS_ONE,
'profession_speciality' => $workDetail['profession_speciality'] ?? Constant::NULL,
'profession_speciality_privacy' => $workDetail['profession_speciality_privacy'] ?? Constant::STATUS_ONE,
'position' => $workDetail['position'] ?? Constant::STATUS_ONE,
'position_privacy' => $workDetail['position_privacy'] ?? Constant::STATUS_ONE,
'city' => $workDetail['city'] ?? Constant::NULL,
'city_privacy' => $workDetail['city_privacy'] ?? Constant::STATUS_ONE,
'about' => $workDetail['about'] ?? Constant::NULL,
'about_privacy' => $workDetail['about_privacy'] ?? Constant::STATUS_ONE,
'is_working' => $workDetail['is_working'] ?? Constant::NULL,
'website' => $workDetail['website'] ?? Constant::NULL,
'website_privacy' => $workDetail['website_privacy'] ?? Constant::STATUS_ONE,
'start_year' => $workDetail['start_year'] ?? Constant::NULL,
'end_year' => $workDetail['end_year'] ?? Constant::NULL,
'work_duration_privacy' => $workDetail['work_duration_privacy'] ?? Constant::STATUS_ONE,
]);
}
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @param array $data
* @return array
*/
public function updateQualificationDetail(array $data)
{
$response = [];
try {
$loggedInUser = loggedInUser();
if ($loggedInUser) {
foreach ($data['qualification_details'] as $qualificationDetail) {
$userQualificationExists = $this->userQualification->where([
'user_id' => $loggedInUser->id,
'id' => $qualificationDetail['qualification_id']
])->first();
//Updating data to user qualifications table
if ($userQualificationExists) {
$userQualificationExists->qualification = $qualificationDetail['qualification'] ?? $userQualificationExists->qualification;
// $userQualificationExists->qualification_privacy = $qualificationDetail['qualification_privacy'] ?? $userQualificationExists->qualification_privacy;
$userQualificationExists->university = $qualificationDetail['university'] ?? $userQualificationExists->university;
// $userQualificationExists->university_privacy = $qualificationDetail['university_privacy'] ?? $userQualificationExists->university_privacy;
$userQualificationExists->highschool = $qualificationDetail['highschool'] ?? $userQualificationExists->highschool;
// $userQualificationExists->highschool_privacy = $qualificationDetail['highschool_privacy'] ?? $userQualificationExists->highschool_privacy;
$userQualificationExists->is_graduate = $qualificationDetail['is_graduate'] ?? $userQualificationExists->is_graduate;
$userQualificationExists->is_pursuing = $qualificationDetail['is_pursuing'] ?? $userQualificationExists->is_pursuing;
$userQualificationExists->passing_year = $qualificationDetail['passing_year'] ?? $userQualificationExists->passing_year;
$userQualificationExists->starting_year = $qualificationDetail['starting_year'] ?? $userQualificationExists->starting_year;
$userQualificationExists->start_date = $qualificationDetail['start_date'] ?? $userQualificationExists->start_date;
$userQualificationExists->end_date = $qualificationDetail['end_date'] ?? $userQualificationExists->end_date;
$userQualificationExists->privacy = $qualificationDetail['privacy'] ?? $userQualificationExists->end_date;
$userQualificationExists->type = $qualificationDetail['type'] ?? $userQualificationExists->end_date;
$userQualificationExists->save();
}
//Storing data to user qualifications table
if (!$userQualificationExists && $qualificationDetail['qualification_id'] == Constant::NULL) {
$this->userQualification->create([
'user_id' => $loggedInUser->id,
'qualification' => $qualificationDetail['qualification'] ?? Constant::NULL,
// 'qualification_privacy' => $qualificationDetail['qualification_privacy'] ?? Constant::STATUS_ONE,
'university' => $qualificationDetail['university'] ?? Constant::NULL,
// 'university_privacy' => $qualificationDetail['university_privacy'] ?? Constant::NULL,
'highschool' => $qualificationDetail['highschool'] ?? Constant::NULL,
// 'highschool_privacy' => $qualificationDetail['highschool_privacy'] ?? Constant::NULL,
'is_graduate' => $qualificationDetail['is_graduate'] ?? Constant::STATUS_ZERO,
'is_pursuing' => $qualificationDetail['is_pursuing'] ?? Constant::STATUS_ZERO,
'passing_year' => $qualificationDetail['passing_year'] ?? Constant::NULL,
'starting_year' => $qualificationDetail['starting_year'] ?? Constant::NULL,
'start_date' => $qualificationDetail['start_date'] ?? Constant::NULL,
'end_date' => $qualificationDetail['end_date'] ?? Constant::NULL,
'privacy' => $qualificationDetail['privacy'] ?? Constant::NULL,
'type' => $qualificationDetail['type'] ?? Constant::NULL,
]);
}
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @param array $data
* @return array
*/
public function deleteWorkDetail(array $data)
{
$response = [];
try {
$loggedInUser = loggedInUser()->load(['userDetail', 'userWorkDetail', 'userQualification']);
if ($loggedInUser && $data['work_id']) {
$workData = $this->userWorkDetail->where('id', $data['work_id'])->first();
if ($workData->delete()) {
$loggedInUser->refresh();
$userProfileCompletion = calculateActivity($loggedInUser->toArray());
$loggedInUser->profile_statistics = $userProfileCompletion;
if ($loggedInUser->profile_statistics['work'] === 0 && $loggedInUser->is_profile_completed === 1) {
$loggedInUser->is_profile_completed = Constant::STATUS_TWO;
//Remove karma points
$karmaPoints = KarmaPointsTransaction::where('key', 'profile_complition')->where('user_id', $loggedInUser->id)->delete();
if ($karmaPoints) {
//Adds 50 karma points
$loggedInUser->addKarmaPoints($loggedInUser, $loggedInUser->id, config('config-variables.karma_points_message.shravak_revert_half'), config('config-variables.karma_points.shravak_revert_half'), config('config-variables.karma_points_key.shravak_revert_half'), []);
}
}
$loggedInUser->save();
$response['message'] = trans('auth.delete_work');
$response['status'] = Constant::CODE_200;
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @param array $data
* @return array
*/
public function deleteQualificationDetail(array $data)
{
$response = [];
try {
$loggedInUser = loggedInUser()->load(['userDetail', 'userWorkDetail', 'userQualification']);
if ($loggedInUser && $data['qualification_id']) {
$qualificationData = $this->userQualification->where('id',$data['qualification_id'])->first();
if ($qualificationData->delete()) {
$loggedInUser->refresh();
$userProfileCompletion = calculateActivity($loggedInUser->toArray());
$loggedInUser->profile_statistics = $userProfileCompletion;
if ($loggedInUser->profile_statistics['qualification'] === 0 && $loggedInUser->is_profile_completed === 1) {
$loggedInUser->is_profile_completed = Constant::STATUS_TWO;
//Remove karma points
$karmaPoints = KarmaPointsTransaction::where('key', 'profile_complition')->where('user_id', $loggedInUser->id)->delete();
if ($karmaPoints) {
//Adds 50 karma points
$loggedInUser->addKarmaPoints($loggedInUser, $loggedInUser->id, config('config-variables.karma_points_message.shravak_revert_half'), config('config-variables.karma_points.shravak_revert_half'), config('config-variables.karma_points_key.shravak_revert_half'), []);
}
}
$loggedInUser->save();
$response['message'] = trans('auth.delete_qualification');
$response['status'] = Constant::CODE_200;
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function logoutUser($data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
$deviceID = $data['device_id'] ?? '';
$deviceToken = UserDeviceToken::where('device_id', $deviceID)->first();
if (!empty($deviceToken)) {
$deviceToken->delete();
}
$user->currentAccessToken()->delete();
$response['message'] = trans('auth.logout_success');
$response['status'] = Constant::CODE_200;
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* Get following of sant
*/
public function userFollowing(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$sant = loggedInUser()->followings()->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $sant->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.sant.followlist');
unset($response['error']);
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get following of sangh
*/
public function sanghFollowing(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$sanghIds = loggedInUser()->sanghMembers()
->where('sangh_members.status', Constant::STATUS_ONE)
->where('sangh_members.user_id', loggedInUser()->id)
->pluck('sangh_members.sangh_id');
$sangh = loggedInUser()->sanghFollowings()
->whereNotIn('sanghs.id', $sanghIds)
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $sangh->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.sant.followlist');
unset($response['error']);
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get following of sant
*/
public function storeChildUser($data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = User::where('id', loggedInUser()->id)->with('userDetail')->first();
$passiveUserData = [
'user_id' => $user->id,
'mother_tongue_id' => $user->mother_tongue ?? Constant::NULL,
'dharma_id' => $user->dharma_id ?? Constant::NULL,
'name' => $data['name'] ?? Constant::NULL,
'relationship' => $data['relationship'] ?? Constant::NULL,
'birth_date' => $data['birth_date'] ?? Constant::NULL,
'gender' => $data['gender'] ?? Constant::NULL,
'school_name' => $data['school_name'] ?? Constant::NULL,
'about' => $data['about'] ?? Constant::NULL,
'is_passive' => Constant::STATUS_ONE,
'parent_id' => $user->id,
'username' => generateUsername($data['name']),
];
if (!empty($data['avatar'])) {
$imageName = uploadImage($data, 'avatar', Constant::USER_IMAGE_UPLOAD_PATH . Constant::SLASH);
$passiveUserData['avatar'] = $imageName['image_name'] ?? Constant::NULL;
}
$passiveUser = $user->passiveUser()->create($passiveUserData);
$passiveUser->userDetail()->create([
'user_id' => $passiveUser->id,
'jati_id' => $user->userDetail[Constant::STATUS_ZERO]->jati_id ?? Constant::NULL,
'native_place' => $user->userDetail[Constant::STATUS_ZERO]->native_place ?? Constant::NULL,
'mother_tongue_id' => $user->userDetail[Constant::STATUS_ZERO]->mother_tongue_id ?? Constant::NULL,
]);
$response['data'] = $passiveUser->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.passive.store');
unset($response['error']);
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get following of sant
*/
public function updateChildUser(object $passiveUser, $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = User::where('id', loggedInUser()->id)->with('userDetail')->first();
$passiveUserData = [
'dharma_id' => $user->dharma_id ?? $passiveUser->dharma_id,
'name' => $data['name'] ?? $passiveUser->name,
'relationship' => $data['relationship'] ?? $passiveUser->relationship,
'birth_date' => $data['birth_date'] ?? $passiveUser->birth_date,
'gender' => $data['gender'] ?? $passiveUser->gender,
'school_name' => $data['school_name'] ?? Constant::NULL,
'about' => $data['about'] ?? Constant::NULL,
'is_passive' => Constant::STATUS_ONE,
'parent_id' => $user->id,
];
if (!empty($data['avatar'])) {
$imageName = uploadImage($data, 'avatar', Constant::USER_IMAGE_UPLOAD_PATH, $passiveUser->getRawOriginal('avatar'));
$passiveUserData['avatar'] = $imageName['image_name'] ?? Constant::NULL;
}
$user->passiveUser()->where('id', $passiveUser['id'])->update($passiveUserData);
$passiveUser->userDetail()->where('user_id', $passiveUser['id'])->update([
'user_id' => $passiveUser->id,
'jati_id' => $user->userDetail[Constant::STATUS_ZERO]->jati_id ?? $passiveUser->jati_id,
'native_place' => $user->userDetail[Constant::STATUS_ZERO]->native_place ?? $passiveUser->native_place,
'mother_tongue_id' => $user->userDetail[Constant::STATUS_ZERO]->mother_tongue_id ?? $passiveUser->mother_tongue_id,
]);
$response['data'] = $passiveUser->select('id',
'name',
'parent_id',
'is_passive',
'relationship',
'about',
'school_name',
'avatar',
'birth_date')->where('id', $passiveUser->id)->first()->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.passive.update');
unset($response['error']);
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get following of sant
*/
public function destroyChildUser($passiveUser)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
deleteFile(Constant::USER_IMAGE_UPLOAD_PATH, $passiveUser->getRawOriginal('avatar'));
$passiveUser->userDetail()->delete();
$passiveUser->delete();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.passive.destroy');
unset($response['error']);
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get following of sant
*/
public function showChildUser($passiveUser)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = $passiveUser->select(
'id',
'name',
'parent_id',
'is_passive',
'relationship',
'about',
'school_name',
'avatar',
'birth_date',
)->where('id', $passiveUser->id)->first()->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.passive.show');
unset($response['error']);
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Update password for user
*/
public function resetPassword(array $request): array
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
if (Hash::check($request['current_password'], $user->password)) {
$user->password = Hash::make($request['new_password']);
if ($user->save()) {
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('account_setting.validate.change_password.message_1');
unset($response['error']);
}
} else {
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('account_setting.validate.change_password.message_2');
unset($response['error']);
}
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Update password for user
*/
public function setPassword(array $data): array
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = User::where([
'mobile' => $data['mobile'],
'country_code' => $data['country_code']
])->first();
if(isset($user) && !empty($user)) {
$user->password = Hash::make($data['new_password']);
if ($user->save()) {
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('account_setting.validate.change_password.message_1');
unset($response['error']);
}
} else {
unset($response['error']);
$response['message'] = trans('auth.provide_valid_contact');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* @return array
*/
public function globalSearch(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
// $result = DB::select("SELECT * FROM ((SELECT id, name, avatar, 'user' as type FROM users WHERE name LIKE '%".$data['name']."%' LIMIT 5)
// UNION
// (SELECT id, name, avatar, 'sant' as type FROM sants WHERE name LIKE '%".$data['name']."%' LIMIT 5)
// UNION
// (SELECT id, name, avatar, 'sangh' as type FROM sanghs WHERE name LIKE '%".$data['name']."%' LIMIT 5)
// )
// AS combined_tables order by name LIMIT 15");
// if (!empty($data['name'])) {
$users = $this->user->select(
'users.id',
'users.name',
'users.avatar',
)
->where('id','<>',$user->id)
->where('id','!=',Constant::STATUS_ONE)
->where('is_passive','!=',Constant::STATUS_ONE)
->addSelect(DB::raw("'user' as type"));
if (isset($data['name']) && !empty($data['name'])) {
$users = $users->where('users.name', 'LIKE', "%{$data['name']}%");
}
$users = $users->limit(5)->get();
$sants = Sant::select('id', 'name', 'avatar')
->where('status', Constant::STATUS_TWO)
->where('verification_status', Constant::STATUS_ONE)
->addSelect(DB::raw("'sant' as type"));
if (isset($data['name']) && !empty($data['name'])) {
$sants = $sants->where('name', 'LIKE', "%{$data['name']}%");
}
$sants = $sants->limit(5)->get();
$sants = $users->concat($sants);
$sanghs = Sangh::select('id', 'name', 'avatar')
->addSelect(DB::raw("'sangh' as type"));
if (isset($data['name']) && !empty($data['name'])) {
$sanghs = $sanghs->where('name', 'LIKE', "%{$data['name']}%");
}
$sanghs = $sanghs->limit(5)->get();
$result = $sanghs->concat($sants)->shuffle();
// } else {
// $result = $this->user->select('id', 'name', 'avatar')
// ->where('id','<>',$user->id)
// ->where('id','!=',Constant::STATUS_ONE)
// ->where('is_passive','!=',Constant::STATUS_ONE)
// ->get();
// }
$response['data'] = $result->toArray();
$response['status'] = Constant::CODE_200;
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* Update password for user
*/
public function userExist(array $data): array
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = User::where([
'mobile' => $data['mobile'],
'country_code' => $data['country_code']
])->first();
if(isset($user) && !empty($user)) {
$response['status'] = Constant::CODE_409;
$response['success'] = Constant::STATUS_FALSE;
$response['message'] = trans('auth.contact_exists');
unset($response['error']);
} else {
$response['status'] = Constant::CODE_200;
$response['message'] = trans('auth.check_otp');
$response['success'] = Constant::STATUS_TRUE;
unset($response['error']);
}
} catch (\Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* @return array
*/
public function getGLobalUserlist(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
$users = $this->user->with('dharma', 'userDetail.jati', 'blockedUser')
->select('users.*')
->where('users.id','<>',$user->id)
->where('users.id', '!=', Constant::STATUS_ONE)
->where('users.is_passive', '!=', Constant::STATUS_ONE);
if (isset($data['name']) && !empty($data['name'])) {
$users = $users->where('name', 'LIKE', "%{$data['name']}%");
}
if (isset($data['dharma_id']) && count($data['dharma_id']) > 0) {
$users = $users->whereIn('dharma_id', $data['dharma_id']);
}
if (isset($data['gender_id']) && count($data['gender_id']) > 0) {
$users = $users->whereIn('gender', $data['gender_id']);
}
if (isset($data['jati_id']) && count($data['jati_id']) > 0) {
$users = $users->orWhereHas('userDetail', function($query) use ($data) {
$query->whereIn('user_details.jati_id', $data['jati_id']);
});
}
$users = $users->whereDoesntHave('blockedUser', function($query) use ($user) {
$query->where('user_blocked.blocked_by', '=', $user->id);
});
if (isset($data['latitude']) && !empty($data['latitude']) && isset($data['longitude']) && !empty($data['longitude'])) {
$latitude = (float) $data['latitude'];
$longitude = (float) $data['longitude'];
$users = $users->whereHas('userDetail', function($query) use ($latitude, $longitude){
$query->select()->selectRaw('3959 * ACOS
(
COS(RADIANS(?)) * COS(RADIANS(user_details.latitude))
* COS(
RADIANS(user_details.longitude) - RADIANS(?)
) + SIN(RADIANS(?)) * SIN(RADIANS(user_details.latitude))
) as distance', [$latitude, $longitude, $latitude])->having("distance", "<=", $data['range'] ?? 10);
});
}
if (isset($data['native_latitude']) && !empty($data['native_latitude']) && isset($data['native_longitude']) && !empty($data['native_longitude'])) {
$nativeLatitude = (float) $data['native_latitude'];
$nativeLongitude = (float) $data['native_longitude'];
$users = $users->whereHas('userDetail', function($query) use ($nativeLatitude, $nativeLongitude){
$query->select()->selectRaw('3959 * ACOS
(
COS(RADIANS(?)) * COS(RADIANS(user_details.native_latitude))
* COS(
RADIANS(user_details.native_longitude) - RADIANS(?)
) + SIN(RADIANS(?)) * SIN(RADIANS(user_details.native_latitude))
) as native_distance', [$nativeLatitude, $nativeLongitude, $nativeLatitude])->having("native_distance", "<=", $data['range'] ?? 10);
});
}
// $users->dd();
$response['data'] = $users->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);;
$response['status'] = Constant::CODE_200;
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function getGLobalSanghlist(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
$sanghs = Sangh::with(['dharma', 'sampraday.gachadhipati:id,name,avatar']);
if (isset($data['name']) && !empty($data['name'])) {
$sanghs = $sanghs->where('name', 'LIKE', "%{$data['name']}%");
}
if (isset($data['dharma_id']) && count($data['dharma_id']) > 0) {
$sanghs = $sanghs->whereIn('dharma_id', $data['dharma_id']);
}
if (isset($data['sampraday_id']) && count($data['sampraday_id']) > 0) {
$sanghs = $sanghs->whereIn('sampraday_id', $data['sampraday_id']);
}
// To search by miles instead of kilometers, replace 6371 with 3959.
if (isset($data['latitude']) && !empty($data['latitude']) && isset($data['longitude']) && !empty($data['longitude'])) {
$latitude = (float) $data['latitude'];
$longitude = (float) $data['longitude'];
$sanghs = $sanghs->select()->selectRaw('3959 * ACOS
(
COS(RADIANS(?)) * COS(RADIANS(latitude))
* COS(
RADIANS(longitude) - RADIANS(?)
) + SIN(RADIANS(?)) * SIN(RADIANS(latitude))
) as distance', [$latitude, $longitude, $latitude])->having("distance", "<=", $data['range'] ?? 10);
}
$response['data'] = $sanghs->where('sangh_status',1)->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);;
$response['status'] = Constant::CODE_200;
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function getGLobalSantlist(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
$sants = Sant::with(['dharm', 'sampraday.gachadhipati:id,name,avatar', 'editedLocation'])
->where('status',2)
->whereIn('verification_status', [1,4,5]);
if (isset($data['name']) && !empty($data['name'])) {
$sants = $sants->where('name', 'LIKE', "%{$data['name']}%");
}
if (isset($data['dharma_id']) && count($data['dharma_id']) > 0) {
$sants = $sants->whereIn('dharma_id', $data['dharma_id']);
}
if (isset($data['gender_id']) && count($data['gender_id']) > 0) {
$sants = $sants->whereIn('gender', $data['gender_id']);
}
if (isset($data['sampraday_id']) && count($data['sampraday_id']) > 0) {
$sants = $sants->whereIn('sampraday_id', $data['sampraday_id']);
}
if (isset($data['latitude']) && !empty($data['latitude']) && isset($data['longitude']) && !empty($data['longitude'])) {
$latitude = (float) $data['latitude'];
$longitude = (float) $data['longitude'];
$sants = $sants->whereHas('editedLocation', function ($sants) use ($data, $latitude, $longitude) {
return $sants->selectRaw('3959 * ACOS
(
COS(RADIANS(?)) * COS(RADIANS(sant_locations.latitude))
* COS(
RADIANS(sant_locations.longitude) - RADIANS(?)
) + SIN(RADIANS(?)) * SIN(RADIANS(sant_locations.latitude))
) as distance', [$latitude, $longitude, $latitude])->having("distance", "<=", $data['range'] ?? 10);
});
// $sants = $sants->select()->selectRaw('3959 * ACOS
// (
// COS(RADIANS(?)) * COS(RADIANS(latitude))
// * COS(
// RADIANS(longitude) - RADIANS(?)
// ) + SIN(RADIANS(?)) * SIN(RADIANS(latitude))
// ) as distance', [$latitude, $longitude, $latitude])->having("distance", "<=", $data['range'] ?? 10);
}
$response['data'] = $sants->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['status'] = Constant::CODE_200;
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function getUserSangh(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (isset($data['user_id']) && !empty($data['user_id'])) {
$user = User::find($data['user_id']);
$sanghs = $user->sanghMembers()->where('status', Constant::STATUS_ONE);
} else {
$sanghs = $user->sanghMembers()->where('status', Constant::STATUS_ONE);
}
$response['data'] = $sanghs->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['status'] = Constant::CODE_200;
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function deleteUserAccount(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
//Check if user is associated with any sangh
$sangh = SanghMember::where(['user_id' => $user->id,
'role' => Constant::ADMIN,
'status' => Constant::STATUS_ONE
])->get()->toArray();
if (!empty($sangh)) {
$response['message'] = trans('auth.sangh_exist_error');
$response['status'] = Constant::CODE_403;
} else {
//Null on sants table
$sants = Sant::with('viharsCreatedBy', 'santTemp', 'createdBy.sentMessageThread.sentMessages',
'createdBy.receivedMessageThread.receivedMessages')
->where('user_id', $user->id)
->get() ?? [];
if (!empty($sants)) {
foreach ($sants as $sant) {
//Set NULL in sent message thread and messages
if (!empty($sant->createdBy->sentMessageThread)) {
foreach ($sant->createdBy->sentMessageThread as $sentMessageThread) {
$sentMessageThread->sender_id = Constant::NULL;
if (!empty($sentMessageThread->sentMessages)) {
foreach ($sentMessageThread->sentMessages as $sentMessage) {
$sentMessage->user_id = Constant::NULL;
$sentMessage->save();
}
}
$sentMessageThread->save();
}
}
//Set NULL in received message thread and messages
if (!empty($sant->createdBy->receivedMessageThread)) {
foreach ($sant->createdBy->receivedMessageThread as $receivedMessageThread) {
$receivedMessageThread->sender_id = Constant::NULL;
if (!empty($receivedMessageThread->receivedMessages)) {
foreach ($receivedMessageThread->receivedMessages as $receivedMessage) {
$receivedMessage->user_id = Constant::NULL;
$receivedMessage->save();
}
}
$receivedMessageThread->save();
}
}
//Set NULL on vihaars
if (!empty($sant->viharsCreatedBy)) {
foreach ($sant->viharsCreatedBy as $vihar) {
$vihar->user_id = Constant::NULL;
$vihar->created_by = Constant::NULL;
$vihar->updated_by = Constant::NULL;
$vihar->activity()->whereJsonContains('properties->attributes->vihar_information->user_id', $user->id)->delete();
$vihar->save();
}
}
//Set NULL in Sants & Sant Temp data
$sant->santTemp->user_id = Constant::NULL;
$sant->santTemp->created_by = Constant::NULL;
$sant->santTemp->updated_by = Constant::NULL;
$sant->user_id = Constant::NULL;
$sant->created_by = Constant::NULL;
$sant->updated_by = Constant::NULL;
$sant->santTemp->save();
$sant->save();
}
}
//Delete post mentions
PostMention::where('mention_id', $user->id)->where('type', Constant::STATUS_ONE)->delete();
//Delete from friend lists
Request::where('sender_id', $user->id)->orWhere('receiver_id', $user->id)->delete();
$deviceID = $data['device_id'] ?? '';
$deviceToken = UserDeviceToken::where('device_id', $deviceID)->first();
if (!empty($deviceToken)) {
$deviceToken->delete();
}
$user->currentAccessToken()->delete();
if ($user->delete()) {
$response['message'] = trans('auth.account_delete_success');
$response['status'] = Constant::CODE_200;
}
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function blockUser(array $data)
{
$response = [];
try {
$user = loggedInUser();
$alreadyBlocked = UserBlocked::where(['user_id' => $data['id'], 'blocked_by' => $user->id])->first();
if (!empty($user)) {
if (!$alreadyBlocked && isset($data['status']) && $data['status'] === Constant::STATUS_ONE) {
UserBlocked::create([
'user_id' => $data['id'],
'blocked_by' => $user->id
]);
$requestExist = Request::where([
'receiver_id' => $user->id,
'sender_id' => $data['id'],
])->first();
$senderId = Request::where([
'receiver_id' => $data['id'],
'sender_id' => $user->id
])->first();
if (!empty($requestExist)) {
$requestExist->delete();
}
if (!empty($senderId)) {
$senderId->delete();
}
$response['message'] = trans('api.user.block');
$response['status'] = Constant::CODE_200;
} else if ($alreadyBlocked && (isset($data['status']) && $data['status'] === Constant::STATUS_ZERO)) {
$alreadyBlocked->delete();
$response['message'] = trans('api.user.unblock');
$response['status'] = Constant::CODE_200;
} else if ($alreadyBlocked && isset($data['status']) && $data['status'] === Constant::STATUS_ONE) {
$response['message'] = trans('api.user.already_blocked');
$response['status'] = Constant::CODE_403;
} else if (!$alreadyBlocked && isset($data['status']) && $data['status'] === Constant::STATUS_ZERO) {
$response['message'] = trans('api.user.already_unblocked');
$response['status'] = Constant::CODE_403;
}
} else {
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_401;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
/**
* @return array
*/
public function getblockedUserList(array $data)
{
$response = [];
try {
$user = loggedInUser();
if (!empty($user)) {
$data = UserBlocked::with('user:id,name,avatar')
->where('blocked_by', $user->id)
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $data;
$response['status'] = Constant::CODE_200;
} else {
$response['data'] = [];
$response['status'] = Constant::CODE_200;
}
} catch (\Exception $ex) {
Log::error($ex);
$response['message'] = trans('auth.something_went_wrong');
$response['status'] = Constant::CODE_403;
}
return $response;
}
}