466 lines
17 KiB
PHP
466 lines
17 KiB
PHP
|
|
<?php
|
||
|
|
namespace App\Repositories\Api\Access\Relationship;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Constant\Constant;
|
||
|
|
use App\Models\Relationship;
|
||
|
|
use App\Models\UserRelation;
|
||
|
|
use App\Models\Notifications;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use App\Traits\PushNotificationTraits;
|
||
|
|
use App\Jobs\Notifications\Shravak\SendRelationRequest;
|
||
|
|
use App\Jobs\Notifications\Shravak\SendAcceptRelationRequest;
|
||
|
|
use App\Jobs\Notifications\Shravak\SendUpdateRelationRequest;
|
||
|
|
|
||
|
|
class RelationshipRepository implements RelationshipInterface
|
||
|
|
{
|
||
|
|
use PushNotificationTraits;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var User
|
||
|
|
*/
|
||
|
|
protected $user;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var Relationship
|
||
|
|
*/
|
||
|
|
protected $relationship;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var UserRelation
|
||
|
|
*/
|
||
|
|
protected $userRelation;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param User $user
|
||
|
|
* @param Relationship $relationship
|
||
|
|
* @param UserRelation $userRelation
|
||
|
|
* RelationshipRepository constructor.
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
public function __construct(User $user, Relationship $relationship, UserRelation $userRelation)
|
||
|
|
{
|
||
|
|
$this->user = $user;
|
||
|
|
$this->relationship = $relationship;
|
||
|
|
$this->userRelation = $userRelation;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function getRelationships()
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
|
||
|
|
if ($loggedInUser) {
|
||
|
|
$relations = $this->relationship->get()->toArray();
|
||
|
|
|
||
|
|
if ($relations) {
|
||
|
|
$response['data'] = $relations;
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
} else {
|
||
|
|
$response['data'] = [];
|
||
|
|
$response['status'] = Constant::CODE_401;
|
||
|
|
}
|
||
|
|
} 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;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $data
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function sendRelationshipRequest(array $data)
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
$receiver = $this->user->where('id', $data['to_id'])->first();
|
||
|
|
$relationName = $data['relation'] ?? "";
|
||
|
|
|
||
|
|
//Check if request is exist
|
||
|
|
$requestExist = $this->userRelation->query()->where([
|
||
|
|
'to_id' => $data['to_id'],
|
||
|
|
'from_id' => $loggedInUser->id
|
||
|
|
])->first();
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
if ($data['to_id'] == $loggedInUser->id || !empty($requestExist) && $requestExist->status == Constant::STATUS_ZERO) {
|
||
|
|
$response['message'] = trans('auth.relation_request.request_already_sent');
|
||
|
|
$response['status'] = Constant::CODE_403;
|
||
|
|
|
||
|
|
} else if ($data['to_id'] == $loggedInUser->id || !empty($requestExist) && $requestExist->status == Constant::STATUS_ONE) {
|
||
|
|
$response['message'] = trans('auth.relation_request.already_added', ['relation' => $requestExist->relation]);
|
||
|
|
$response['status'] = Constant::CODE_403;
|
||
|
|
|
||
|
|
} else {
|
||
|
|
$this->userRelation->from_id = $loggedInUser->id;
|
||
|
|
$this->userRelation->to_id = $data['to_id'];
|
||
|
|
$this->userRelation->relation = $data['relation'];
|
||
|
|
$this->userRelation->relation_privacy = $data['relation_privacy'] ?? Constant::STATUS_ONE;
|
||
|
|
$this->userRelation->save();
|
||
|
|
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_request_success');
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
|
||
|
|
//Push Notification
|
||
|
|
dispatch(new SendRelationRequest($loggedInUser, $receiver, $relationName));
|
||
|
|
|
||
|
|
// Delete previous relation request
|
||
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$receiver->id])
|
||
|
|
->where('type', 'RelationshipRequest')
|
||
|
|
->whereJsonContains('extra_fields->from_id', $loggedInUser->id);
|
||
|
|
|
||
|
|
if ($relationshipRequestExist->first()) {
|
||
|
|
$relationshipRequestExist->delete();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} 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;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $data
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function acceptRelationshipRequest(array $data)
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
$receiver = $this->user->where('id', $data['from_id'])->first();
|
||
|
|
|
||
|
|
//Check if request is exist
|
||
|
|
$requestExist = $this->userRelation->query()->where([
|
||
|
|
'to_id' => $loggedInUser->id,
|
||
|
|
'from_id' => $data['from_id'],
|
||
|
|
])->first();
|
||
|
|
|
||
|
|
$senderId = $this->userRelation->query()->where([
|
||
|
|
'to_id' => $data['from_id'],
|
||
|
|
'from_id' => $loggedInUser->id
|
||
|
|
])->first();
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
if (!empty($requestExist) && !empty($senderId) && $data['status'] != Constant::STATUS_TWO) {
|
||
|
|
$senderId->status = Constant::STATUS_ONE;
|
||
|
|
$senderId->save();
|
||
|
|
|
||
|
|
$requestExist->status = $data['status'];
|
||
|
|
$requestExist->save();
|
||
|
|
|
||
|
|
//Push Notification
|
||
|
|
dispatch(new SendAcceptRelationRequest($loggedInUser, $receiver, $requestExist));
|
||
|
|
|
||
|
|
$relationshipRequestExist = Notifications::where('type', 'RelationshipRequest')
|
||
|
|
->where('user_id', $loggedInUser->id)
|
||
|
|
->whereJsonContains('extra_fields->from_id', $receiver->id);
|
||
|
|
|
||
|
|
if ($relationshipRequestExist) {
|
||
|
|
$relationshipRequestExist->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_request_accept');
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!empty($requestExist) && isset($data['relation']) && $data['status'] != Constant::STATUS_TWO) {
|
||
|
|
$this->userRelation->relation = $data['relation'];
|
||
|
|
$this->userRelation->from_id = $loggedInUser->id;
|
||
|
|
$this->userRelation->to_id = $data['from_id'];
|
||
|
|
$this->userRelation->status = Constant::STATUS_ONE;
|
||
|
|
$this->userRelation->save();
|
||
|
|
|
||
|
|
$requestExist->status = $data['status'];
|
||
|
|
$requestExist->save();
|
||
|
|
|
||
|
|
//Push Notification
|
||
|
|
dispatch(new SendAcceptRelationRequest($loggedInUser, $receiver, $requestExist));
|
||
|
|
|
||
|
|
$relationshipRequestExist = Notifications::where('type', 'RelationshipRequest')
|
||
|
|
->where('user_id', $loggedInUser->id)
|
||
|
|
->whereJsonContains('extra_fields->from_id', $receiver->id);
|
||
|
|
|
||
|
|
if ($relationshipRequestExist) {
|
||
|
|
$relationshipRequestExist->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_request_accept');
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//Delete requests if declined
|
||
|
|
if (isset($data['status']) && $data['status'] === Constant::STATUS_TWO && $requestExist && $senderId) {
|
||
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$receiver->id])
|
||
|
|
->where('type', 'RelationshipRequest')
|
||
|
|
->whereJsonContains('extra_fields->from_id', $loggedInUser->id);
|
||
|
|
|
||
|
|
if ($relationshipRequestExist->first()) {
|
||
|
|
$relationshipRequestExist->delete();
|
||
|
|
}
|
||
|
|
$requestExist->delete();
|
||
|
|
$senderId->delete();
|
||
|
|
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_request_decline');
|
||
|
|
|
||
|
|
} else if (isset($data['status']) && $data['status'] === Constant::STATUS_TWO && $senderId) {
|
||
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$receiver->id])
|
||
|
|
->where('type', 'RelationshipRequest')
|
||
|
|
->whereJsonContains('extra_fields->from_id', $loggedInUser->id);
|
||
|
|
|
||
|
|
if ($relationshipRequestExist->first()) {
|
||
|
|
$relationshipRequestExist->delete();
|
||
|
|
}
|
||
|
|
$senderId->delete();
|
||
|
|
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_request_decline');
|
||
|
|
|
||
|
|
} else if (isset($data['status']) && $data['status'] === Constant::STATUS_TWO && $requestExist) {
|
||
|
|
$relationshipRequestExist = Notifications::where('type', 'RelationshipRequest')
|
||
|
|
->where('user_id', $loggedInUser->id)
|
||
|
|
->whereJsonContains('extra_fields->from_id', $receiver->id);
|
||
|
|
|
||
|
|
if ($relationshipRequestExist) {
|
||
|
|
$relationshipRequestExist->delete();
|
||
|
|
}
|
||
|
|
$requestExist->delete();
|
||
|
|
|
||
|
|
$response['status'] = Constant::CODE_200;
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_request_decline');
|
||
|
|
}
|
||
|
|
|
||
|
|
} 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_401;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function listRelationshipRequest()
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
$requests = $this->userRelation->with('receivedUsers')->where([
|
||
|
|
'to_id' => $loggedInUser->id,
|
||
|
|
'status' => Constant::STATUS_ZERO
|
||
|
|
])->get()->toArray();
|
||
|
|
|
||
|
|
$response['data'] = $requests;
|
||
|
|
$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_401;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function sentRelationshipRequestList()
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
$requests = $this->userRelation->with('sentUsers')->where([
|
||
|
|
'from_id' => $loggedInUser->id
|
||
|
|
])->get()->toArray();
|
||
|
|
|
||
|
|
$response['data'] = $requests;
|
||
|
|
$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_401;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $data
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function updateRelationPrivacy(array $data)
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
foreach ($data['family_members'] as $relation) {
|
||
|
|
$request = $this->userRelation->where([
|
||
|
|
'from_id' => $loggedInUser->id,
|
||
|
|
'to_id' => $relation['to_id'],
|
||
|
|
'relation' => $relation['relation']
|
||
|
|
])->first();
|
||
|
|
|
||
|
|
if ($request) {
|
||
|
|
$request->relation_privacy = $relation['relation_privacy'];
|
||
|
|
$request->save();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} 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_401;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $id
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function updateRelationshipRequest(int $id, array $request)
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
$userRelation = UserRelation::find($id);
|
||
|
|
$relationName = $request['relation'];
|
||
|
|
$relationPrivacy = $request['relation_privacy'] ?? Constant::STATUS_ONE;
|
||
|
|
$receiver = $this->user->where('id', $userRelation->to_id)->first();
|
||
|
|
$userRelation = $userRelation->update(['relation' => $relationName, 'relation_privacy' => $relationPrivacy]);
|
||
|
|
|
||
|
|
//Push Notification
|
||
|
|
dispatch(new SendUpdateRelationRequest($loggedInUser, $receiver, $relationName));
|
||
|
|
|
||
|
|
$response['message'] = trans('auth.relation_request.relation_update');
|
||
|
|
$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_401;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function userList($data)
|
||
|
|
{
|
||
|
|
$response = [];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$loggedInUser = loggedInUser();
|
||
|
|
$sentRequests = $loggedInUser->friendRequests();
|
||
|
|
$receivedRequests = $loggedInUser->myFriendRequests();
|
||
|
|
$addedUsers = $loggedInUser->userRelationshipRequests();
|
||
|
|
$addedUsers = $addedUsers->where('user_relations.status', Constant::STATUS_ONE)->pluck('to_id');
|
||
|
|
|
||
|
|
if (!empty($loggedInUser)) {
|
||
|
|
|
||
|
|
if (isset($data['name']) && !empty($data['name'])) {
|
||
|
|
$sentRequests = $sentRequests->where('name', 'LIKE', "%{$data['name']}%");
|
||
|
|
}
|
||
|
|
$sentRequests = $sentRequests->with('userDetail')
|
||
|
|
->select('users.id', 'users.name', 'users.avatar')
|
||
|
|
->where('requests.status', Constant::STATUS_ONE)
|
||
|
|
->whereNotIn('users.id', $addedUsers)
|
||
|
|
->get();
|
||
|
|
|
||
|
|
if (isset($data['name']) && !empty($data['name'])) {
|
||
|
|
$receivedRequests = $receivedRequests->where('name', 'LIKE', "%{$data['name']}%");
|
||
|
|
}
|
||
|
|
$receivedRequests = $receivedRequests->with('userDetail')
|
||
|
|
->select('users.id', 'users.name', 'users.avatar')
|
||
|
|
->where('requests.status', Constant::STATUS_ONE)
|
||
|
|
->whereNotIn('users.id', $addedUsers)
|
||
|
|
->get();
|
||
|
|
|
||
|
|
$requests = $sentRequests->merge($receivedRequests);
|
||
|
|
|
||
|
|
$response['data'] = $requests;
|
||
|
|
$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_401;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
}
|