1521 lines
68 KiB
PHP
1521 lines
68 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Api\Access\Sangh;
|
|
|
|
use App\Jobs\Sangh\SanghRequestSentJob;
|
|
use App\Models\Chaturmas;
|
|
use App\Models\Sant;
|
|
use App\Models\User;
|
|
use App\Models\Sangh;
|
|
use App\Constant\Constant;
|
|
use App\Models\SanghMember;
|
|
use App\Models\Notifications;
|
|
use App\Models\SanghFollower;
|
|
use App\Models\UserDetail;
|
|
use App\Models\UserDeviceToken;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Vihar;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Models\KarmaPointsTransaction;
|
|
use App\Traits\PushNotificationTraits;
|
|
use App\Jobs\Notifications\Sangh\SendAdminUpdateRole;
|
|
use App\Jobs\Notifications\Sangh\SendUserAcceptSangh;
|
|
use App\Jobs\Notifications\Sangh\SendUserJoinedSangh;
|
|
use App\Jobs\Notifications\Sangh\SendAdminAcceptSangh;
|
|
use App\Jobs\Notifications\Sangh\SendUserDeclineSangh;
|
|
use App\Jobs\Notifications\Sangh\SendAdminDeclineSangh;
|
|
use App\Jobs\Notifications\Sangh\SendAdminRemoveMember;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
class SanghRepository implements SanghInterface
|
|
{
|
|
use PushNotificationTraits;
|
|
|
|
/**
|
|
* @var Sangh
|
|
*/
|
|
protected $sangh;
|
|
|
|
/**
|
|
* @var SanghMember
|
|
*/
|
|
protected $sanghMember;
|
|
|
|
/**
|
|
* @var User
|
|
*/
|
|
protected $user;
|
|
|
|
/**
|
|
* @var UserDeviceToken
|
|
*/
|
|
protected $userDeviceToken;
|
|
|
|
/**
|
|
* @param Sangh $sangh
|
|
* @param SanghMember $sanghMember
|
|
* @param User $user
|
|
* @param UserDeviceToken $userDeviceToken
|
|
* SanghRepository constructor.
|
|
*
|
|
*/
|
|
public function __construct(Sangh $sangh, SanghMember $sanghMember, User $user, UserDeviceToken $userDeviceToken)
|
|
{
|
|
$this->sangh = $sangh;
|
|
$this->sanghMember = $sanghMember;
|
|
$this->user = $user;
|
|
$this->userDeviceToken = $userDeviceToken;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getSanghList($data)
|
|
{
|
|
|
|
$response = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$sanghData = $this->sangh->with('dharma', 'sampraday', 'createdBy');
|
|
|
|
if (isset($data['name']) && !empty($data['name'])) {
|
|
$sanghData = $sanghData->where('name', 'LIKE', "%{$data['name']}%");
|
|
}
|
|
|
|
if (isset($data['dharma_id']) && !empty($data['dharma_id'])) {
|
|
$sanghData = $sanghData->where('dharma_id', $data['dharma_id']);
|
|
} else {
|
|
// $sanghData = $sanghData->orderByRaw("FIELD(dharma_id , $user->dharma_id) DESC");
|
|
}
|
|
|
|
if (isset($data['sampraday_id']) && !empty($data['sampraday_id'])) {
|
|
$sanghData = $sanghData->where('sampraday_id', $data['sampraday_id']);
|
|
}
|
|
|
|
$sanghData = $sanghData->where('sangh_status', 1)
|
|
//->where('created_by', $user->id)
|
|
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
|
|
|
$response['data'] = $sanghData;
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('sant.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Summary of inReviewSant
|
|
*/
|
|
public function inReviewSangh($data)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$sanghInReview = $this->sangh
|
|
->with('dharma', 'sampraday', 'createdby')
|
|
->whereIn('sangh_status', [0, 2])
|
|
->where(function ($query) {
|
|
$query->where('created_by', loggedInUser()->id)
|
|
->orWhere('updated_by', loggedInUser()->id);
|
|
})
|
|
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
|
$response['data'] = $sanghInReview;
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.show');
|
|
unset($response['error']);
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function createSangh($data)
|
|
{
|
|
try {
|
|
$user = loggedInUser();
|
|
|
|
if ($user) {
|
|
$sanghData['user_id'] = $user->id ?? Constant::NULL;
|
|
$sanghData['name'] = $data['name'] ?? Constant::NULL;
|
|
$sanghData['sangh_type'] = $data['sangh_type'] ?? Constant::NULL;
|
|
$sanghData['dharma_id'] = $data['dharma_id'] ?? Constant::NULL;
|
|
$sanghData['sampraday_id'] = $data['sampraday_id'] ?? Constant::NULL;
|
|
$sanghData['reg_number'] = $data['reg_number'] ?? Constant::NULL;
|
|
$sanghData['reg_date'] = $data['reg_date'] ?? Constant::NULL;
|
|
$sanghData['address'] = $data['address'] ?? Constant::NULL;
|
|
$sanghData['city'] = $data['city'] ?? Constant::NULL;
|
|
$sanghData['pincode'] = $data['pincode'] ?? Constant::NULL;
|
|
$sanghData['longitude'] = $data['longitude'] ?? Constant::NULL;
|
|
$sanghData['latitude'] = $data['latitude'] ?? Constant::NULL;
|
|
$sanghData['mulnayak_bhagwan_name'] = $data['mulnayak_bhagwan_name'] ?? Constant::NULL;
|
|
$sanghData['email'] = $data['email'] ?? Constant::NULL;
|
|
$sanghData['state'] = $data['state'] ?? Constant::NULL;
|
|
$sanghData['apartment_name'] = $data['apartment_name'] ?? Constant::NULL;
|
|
$sanghData['mobile_number'] = $data['mobile_number'] ?? Constant::NULL;
|
|
$sanghData['sangh_website'] = $data['sangh_website'] ?? Constant::NULL;
|
|
$sanghData['number_of_members'] = $data['number_of_members'] ?? Constant::NULL;
|
|
$sanghData['aaradhana_bhavan_status'] = $data['aaradhana_bhavan_status'] ?? Constant::NULL;
|
|
$sanghData['pathshala_status'] = $data['pathshala_status'] ?? Constant::NULL;
|
|
$sanghData['guruji_name'] = $data['guruji_name'] ?? Constant::NULL;
|
|
$sanghData['no_of_students'] = $data['no_of_students'] ?? Constant::NULL;
|
|
$sanghData['library_name'] = $data['library_name'] ?? Constant::NULL;
|
|
$sanghData['librarian_name'] = $data['librarian_name'] ?? Constant::NULL;
|
|
$sanghData['librarian_mobile_number'] = $data['librarian_mobile_number'] ?? Constant::NULL;
|
|
$sanghData['number_of_books'] = $data['number_of_books'] ?? Constant::NULL;
|
|
$sanghData['library_status'] = $data['library_status'] ?? Constant::NULL;
|
|
$sanghData['aayambilshala_status'] = $data['aayambilshala_status'] ?? Constant::NULL;
|
|
$sanghData['bhojanshala_status'] = $data['bhojanshala_status'] ?? Constant::NULL;
|
|
$sanghData['tifin_service_status'] = $data['tifin_service_status'] ?? Constant::NULL;
|
|
$sanghData['swapna_chadawa_organised_status'] = $data['swapna_chadawa_organised_status'] ?? Constant::NULL;
|
|
$sanghData['type_of_template'] = $data['type_of_template'] ?? Constant::NULL;
|
|
$sanghData['mahila_mandal_status'] = $data['mahila_mandal_status'] ?? Constant::NULL;
|
|
$sanghData['yuva_mandal_status'] = $data['yuva_mandal_status'] ?? Constant::NULL;
|
|
$sanghData['bank_account_number'] = $data['bank_account_number'] ?? Constant::NULL;
|
|
$sanghData['bank_ifsc'] = $data['bank_ifsc'] ?? Constant::NULL;
|
|
$sanghData['bank_branch'] = $data['bank_branch'] ?? Constant::NULL;
|
|
$sanghData['bank_upi_id'] = $data['bank_upi_id'] ?? Constant::NULL;
|
|
$sanghData['bank_qr_code'] = $data['bank_qr_code'] ?? Constant::NULL;
|
|
$sanghData['about'] = $data['about'] ?? Constant::NULL;
|
|
$sanghData['avatar'] = $data['avatar'] ?? Constant::NULL;
|
|
$sanghData['created_by'] = $user->id ?? Constant::NULL;
|
|
$sanghData['updated_by'] = $user->id ?? Constant::NULL;
|
|
$sanghData['sangh_status'] = Constant::STATUS_ZERO;
|
|
|
|
if (!empty($data['avatar'])) {
|
|
$imageName = uploadImage($data, 'avatar', Constant::SANGH_IMAGE_UPLOAD_PATH . Constant::SLASH);
|
|
$sanghData['avatar'] = $imageName['image_name'] ?? Constant::NULL;
|
|
}
|
|
|
|
//Bank QR Code
|
|
if (!empty($data['bank_qr_code'])) {
|
|
$imageName = uploadImage($data, 'bank_qr_code', Constant::SANGH_IMAGE_UPLOAD_PATH . Constant::SLASH);
|
|
$sanghData['bank_qr_code'] = $imageName['image_name'] ?? Constant::NULL;
|
|
}
|
|
|
|
$sangh = $this->sangh->create($sanghData);
|
|
if ($sangh) {
|
|
$this->sanghMember->create([
|
|
'sangh_id' => $sangh->id,
|
|
'member_id' => '0001',
|
|
'user_id' => $user->id,
|
|
'core_committee' => Constant::STATUS_ONE,
|
|
'role' => Constant::ADMIN,
|
|
'status' => Constant::STATUS_ONE,
|
|
'is_owner' => Constant::STATUS_ONE
|
|
]);
|
|
|
|
|
|
|
|
// Add karma points to shravak on add sangh
|
|
$sangh->addKarmaPoints($sangh, $sangh->created_by, config('config-variables.karma_points_message.add_new_sangh'), config('config-variables.karma_points.add_new_sangh'), config('config-variables.karma_points_key.add_new_sangh'), []);
|
|
|
|
//Sent Request Email
|
|
dispatch(new SanghRequestSentJob($sangh));
|
|
}
|
|
$responseData['message'] = trans('sangh.create_sangh_success');
|
|
$responseData['status'] = Constant::CODE_200;
|
|
|
|
} else {
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $responseData;
|
|
}
|
|
|
|
/**
|
|
* @param object $sangh
|
|
* @param $data
|
|
* @return array
|
|
*/
|
|
public function updateSangh(object $sangh,$data)
|
|
{
|
|
try {
|
|
$user = loggedInUser();
|
|
$sanghExist = $this->sangh->where('id', $sangh->id)->first();
|
|
if (empty($data['avatar']) && basename($sanghExist->avatar) == 'sangh.png') {
|
|
return [
|
|
'message' => 'Please update sangh avatar',
|
|
'status' => Constant::CODE_403
|
|
];
|
|
}
|
|
|
|
$sanghData['name'] = $data['name'] ?? $sangh->name;
|
|
$sanghData['sangh_type'] = $data['sangh_type'] ?? $sangh->sangh_type;
|
|
$sanghData['reg_number'] = $data['reg_number'] ?? $sangh->reg_number;
|
|
$sanghData['reg_date'] = $data['reg_date'] ?? $sangh->reg_date;
|
|
$sanghData['address'] = $data['address'] ?? $sangh->address;
|
|
$sanghData['city'] = $data['city'] ?? $sangh->city;
|
|
$sanghData['pincode'] = $data['pincode'] ?? $sangh->pincode;
|
|
$sanghData['longitude'] = $data['longitude'] ?? $sangh->longitude;
|
|
$sanghData['latitude'] = $data['latitude'] ?? $sangh->latitude;
|
|
$sanghData['about'] = $data['about'] ?? $sangh->about;
|
|
$sanghData['email'] = $data['email'] ?? $sangh->email;
|
|
$sanghData['state'] = $data['state'] ?? $sangh->state;
|
|
$sanghData['apartment_name'] = $data['apartment_name'] ?? $sangh->apartment_name;
|
|
$sanghData['mobile_number'] = $data['mobile_number'] ?? $sangh->mobile_number;
|
|
$sanghData['sangh_website'] = $data['sangh_website'] ?? $sangh->sangh_website;
|
|
$sanghData['number_of_members'] = $data['number_of_members'] ?? $sangh->number_of_members;
|
|
$sanghData['aaradhana_bhavan_status'] = $data['aaradhana_bhavan_status'] ?? $sangh->aaradhana_bhavan_status;
|
|
$sanghData['pathshala_status'] = $data['pathshala_status'] ?? $sangh->pathshala_status;
|
|
$sanghData['guruji_name'] = $data['guruji_name'] ?? $sangh->guruji_name;
|
|
$sanghData['no_of_students'] = $data['no_of_students'] ?? $sangh->no_of_students;
|
|
$sanghData['library_status'] = $data['library_status'] ?? $sangh->library_status;
|
|
$sanghData['library_name'] = $data['library_name'] ?? $sangh->library_name;
|
|
$sanghData['librarian_name'] = $data['librarian_name'] ?? $sangh->librarian_name;
|
|
$sanghData['librarian_mobile_number'] = $data['librarian_mobile_number'] ?? $sangh->librarian_mobile_number;
|
|
$sanghData['number_of_books'] = $data['number_of_books'] ?? $sangh->number_of_books;
|
|
$sanghData['aayambilshala_status'] = $data['aayambilshala_status'] ?? $sangh->aayambilshala_status;
|
|
$sanghData['bhojanshala_status'] = $data['bhojanshala_status'] ?? $sangh->bhojanshala_status;
|
|
$sanghData['tifin_service_status'] = $data['tifin_service_status'] ?? $sangh->tifin_service_status;
|
|
$sanghData['swapna_chadawa_organised_status'] = $data['swapna_chadawa_organised_status'] ?? $sangh->swapna_chadawa_organised_status;
|
|
$sanghData['type_of_template'] = $data['type_of_template'] ?? $sangh->type_of_template;
|
|
$sanghData['mahila_mandal_status'] = $data['mahila_mandal_status'] ?? $sangh->mahila_mandal_status;
|
|
$sanghData['yuva_mandal_status'] = $data['yuva_mandal_status'] ?? $sangh->yuva_mandal_status;
|
|
$sanghData['bank_account_number'] = $data['bank_account_number'] ?? $sangh->bank_account_number;
|
|
$sanghData['bank_ifsc'] = $data['bank_ifsc'] ?? $sangh->bank_ifsc;
|
|
$sanghData['bank_branch'] = $data['bank_branch'] ?? $sangh->bank_branch;
|
|
$sanghData['bank_upi_id'] = $data['bank_upi_id'] ?? $sangh->bank_upi_id;
|
|
$sanghData['mulnayak_bhagwan_name'] = $data['mulnayak_bhagwan_name'] ?? $sangh->mulnayak_bhagwan_name;
|
|
// $sanghData['dharma_id'] = $data['dharma_id'] ?? $sangh->dharma_id;
|
|
// $sanghData['sampraday_id'] = $data['sampraday_id'] ?? $sangh->sampraday_id;
|
|
$sanghData['updated_by'] = $user->id;
|
|
$sanghData['sangh_status'] = $data['sangh_status'] ?? $sangh->sangh_status;
|
|
|
|
|
|
if (!empty($data['avatar'])) {
|
|
$imageName = uploadImage($data, 'avatar', Constant::SANGH_IMAGE_UPLOAD_PATH . Constant::SLASH, $sangh->getRawOriginal('avatar'));
|
|
$sanghData['avatar'] = $imageName['image_name'] ?? Constant::NULL;
|
|
}
|
|
|
|
//Bank QR Code
|
|
if (key_exists('bank_qr_code', $data->all())) {
|
|
if ($data['bank_qr_code'] != '') {
|
|
$imageName = uploadImage($data, 'bank_qr_code', Constant::SANGH_IMAGE_UPLOAD_PATH . Constant::SLASH);
|
|
$sanghData['bank_qr_code'] = $imageName['image_name'] ?? Constant::NULL;
|
|
} else if ($data['bank_qr_code'] == '') {
|
|
$sanghData['bank_qr_code'] = Constant::NULL;
|
|
}
|
|
}
|
|
|
|
if (!empty($data['dharma_id'])) {
|
|
$sanghData['dharma_id'] = $data['dharma_id'];
|
|
} else if (isset($data['dharma_id']) === true) {
|
|
$sanghData['dharma_id'] = Constant::NULL;
|
|
} else {
|
|
$sanghData['dharma_id'] = $sangh->dharma_id;
|
|
}
|
|
|
|
if (!empty($data['sampraday_id'])) {
|
|
$sanghData['sampraday_id'] = $data['sampraday_id'];
|
|
} else if (isset($data['sampraday_id']) === true) {
|
|
$sanghData['sampraday_id'] = Constant::NULL;
|
|
} else {
|
|
$sanghData['sampraday_id'] = $sangh->sampraday_id;
|
|
}
|
|
|
|
$sanghExist = $sanghExist->update($sanghData);
|
|
|
|
if ($sanghExist) {
|
|
$responseData['data'] = $this->sangh->where('id', $sangh->id)->get()->toArray();
|
|
$responseData['message'] = trans('sangh.update_sangh_success');
|
|
$responseData['status'] = Constant::CODE_200;
|
|
} else {
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $responseData;
|
|
}
|
|
|
|
/**
|
|
* Delete sangh
|
|
*/
|
|
public function deleteSangh($id)
|
|
{
|
|
try {
|
|
$sangh = $this->sangh->find($id);
|
|
if ($sangh->sangh_status == 0 || $sangh->sangh_status == 2) {
|
|
$sangh->delete();
|
|
$responseData['message'] = trans('Sangh deleted successfully');
|
|
$responseData['status'] = Constant::CODE_200;
|
|
} else {
|
|
$responseData['message'] = trans('An approved Sangh cannot be deleted');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $responseData;
|
|
}
|
|
/**
|
|
* Get info of sangh
|
|
*/
|
|
public function showSangh(object $sangh)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$karmaPoints = KarmaPointsTransaction::whereIn('key', ['sangh_add_post_dharmik', 'add_new_sangh', 'new_sangh_member'])->where(function ($subQuery) use ($sangh) {
|
|
$subQuery->where('pointable_id', $sangh->id)->orWhere('user_id', $sangh->id);
|
|
})->sum('points');
|
|
|
|
//Followers Count
|
|
$sanghMembers = $sangh->sanghMembers()
|
|
->where('sangh_members.status', Constant::STATUS_ONE)
|
|
->get()
|
|
->pluck('pivot.user_id');
|
|
|
|
// Get chaturmas sant data
|
|
$sanghChaturmas = Chaturmas::where('sangh_id', $sangh->id)
|
|
->with([
|
|
'sant' => function ($query) {
|
|
$query->select('id', 'name', 'avatar');
|
|
}
|
|
])
|
|
->orderBy('year', 'desc')
|
|
->get()
|
|
->map(function ($chaturma) {
|
|
$chaturma->year = isset($chaturma->year) && !empty($chaturma->year)
|
|
? $chaturma->year : '';
|
|
return $chaturma;
|
|
});
|
|
// Get vihar sant data
|
|
$sanghVihar = Vihar::where('to_sangh_id', $sangh->id)
|
|
->with([
|
|
'sant' => function ($query) {
|
|
$query->select('id', 'name', 'avatar');
|
|
}
|
|
])
|
|
->orderBy('start_date', 'desc')
|
|
->get()
|
|
->map(function ($vihar) {
|
|
$vihar->start_date = isset($vihar->start_date) && !empty($vihar->start_date)
|
|
? date('d-m-Y', strtotime($vihar->start_date)): '';
|
|
return $vihar;
|
|
});
|
|
$sanghFollowers = $sangh->followers()
|
|
->whereNotIn('user_id', $sanghMembers);
|
|
|
|
$sangh = $sangh->loadCount(['sanghMembers' => function ($query) {
|
|
$query->where('sangh_members.status', Constant::STATUS_ONE);
|
|
}])
|
|
->load([
|
|
'followers' => function ($query) {
|
|
$query->limit(3);
|
|
},
|
|
'dharma:id,name',
|
|
'sampraday:id,name',
|
|
'createdBy:id,name,avatar',
|
|
]);
|
|
$sangh['karma_points'] = $karmaPoints ?? "";
|
|
$sangh['followers_count'] = $sanghFollowers->count() ?? Constant::STATUS_ZERO;
|
|
$sangh['chaturmas'] = $sanghChaturmas;
|
|
$sangh['vihar'] = $sanghVihar;
|
|
$response['data'] = $sangh;
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.show');
|
|
unset($response['error']);
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get list of sant which is created by user
|
|
*/
|
|
public function userSanghList($data)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
try {
|
|
$santInReview = loggedInUser()->sanghMembers()->select('sanghs.id', 'sanghs.user_id', 'sanghs.name', 'sanghs.avatar')
|
|
->where(function ($query) {
|
|
$query->where('sangh_members.core_committee', Constant::STATUS_ZERO)
|
|
->orWhere('sangh_members.core_committee', Constant::STATUS_ONE);
|
|
})
|
|
->where('sangh_members.status', Constant::STATUS_ONE)
|
|
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
|
|
|
$response['data'] = $santInReview;
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.show');
|
|
unset($response['error']);
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function sanghFollow($data)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
|
|
if (!$user->sanghFollowings->contains($data['sangh_id'])) {
|
|
$user->sanghFollowings()->attach(['sangh_id' => $data['sangh_id']]);
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.follow');
|
|
unset($response['error']);
|
|
} else {
|
|
$user->sanghFollowings()->detach(['sangh_id' => $data['sangh_id']]);
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.unfollow');
|
|
unset($response['error']);
|
|
}
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('api.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function sanghSuggestion($data)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$userMemberIdSangh = loggedInUser()->sanghMembers()->where('sangh_members.status', Constant::STATUS_ONE)->pluck('sangh_id')->toArray();
|
|
|
|
// $userIds = $user->friendRequests()->where('requests.status', Constant::STATUS_ONE)->get()->pluck('id')->toArray();
|
|
|
|
$myFollowingList = SanghFollower::where('user_id', $user->id)->pluck('sangh_id')->toArray();
|
|
|
|
// $sanghSuggestionIds = SanghFollower::whereIn('user_id', $userIds)->whereNotIn('sangh_id', $myFollowingList)->pluck('sangh_id');
|
|
|
|
$mySanghSuggestionList = Sangh::select('id', 'name', 'sampraday_id', 'dharma_id', 'avatar')->where('created_by', '!=', $user->id)
|
|
->where('sangh_status', Constant::STATUS_ONE);
|
|
|
|
if (!empty($user->dharma_id)) {
|
|
$mySanghSuggestionList = $mySanghSuggestionList->where('dharma_id', $user->dharma_id);
|
|
}
|
|
// ->whereIn('id', $sanghSuggestionIds)
|
|
$mySanghSuggestionList = $mySanghSuggestionList
|
|
->whereNotIn('id', $myFollowingList)
|
|
->whereNotIn('id', $userMemberIdSangh)
|
|
->with([
|
|
'sampraday:id,name',
|
|
'dharma:id,name'
|
|
])
|
|
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
|
|
|
unreadSanghMenuCounter($user->id, Constant::STATUS_ZERO); //Set sangh menu badge to default state
|
|
$response['data'] = $mySanghSuggestionList->toArray();
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.unfollow');
|
|
unset($response['error']);
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('api.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get followers of sangh
|
|
*/
|
|
public function sanghFollowers(array $data, object $sangh)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$sanghMembers = $sangh->sanghMembers()
|
|
->where('sangh_members.status', Constant::STATUS_ONE)
|
|
->get()
|
|
->pluck('pivot.user_id');
|
|
|
|
$sangh = $sangh->followers()
|
|
->whereNotIn('user_id', $sanghMembers)
|
|
->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.sangh.followlist');
|
|
unset($response['error']);
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function addSanghMember($data)
|
|
{
|
|
$responseData = [];
|
|
$users = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$sanghId = $data['sangh_id'];
|
|
$roleName = $data['role'] ?? Constant::NULL;
|
|
$status = Constant::STATUS_ZERO;
|
|
$count = 1;
|
|
if ($user) {
|
|
|
|
if (!empty(array_values($data['user_id']))) {
|
|
|
|
foreach ($data['user_id'] as $member) {
|
|
//Check if request is exist
|
|
$memberExist = $this->sanghMember->query()->where(
|
|
'sangh_id', $sanghId)->where(
|
|
'user_id', $member
|
|
)->first();
|
|
|
|
$admin = $this->sanghMember->query()
|
|
->where([
|
|
'sangh_id' => $sanghId,
|
|
'user_id' => $user->id
|
|
])
|
|
->where(function ($subQuery) {
|
|
$subQuery->where('role', Constant::ADMIN)
|
|
->orWhere('core_committee', Constant::STATUS_ONE);
|
|
})
|
|
// ->orWhere('role', Constant::ADMIN)
|
|
// ->where('core_committee', Constant::STATUS_ONE)
|
|
->first();
|
|
|
|
if (!empty($admin)) {
|
|
$status = Constant::STATUS_ONE;
|
|
}
|
|
|
|
$maxId = $this->sanghMember->where('sangh_id', $sanghId)
|
|
->max('member_id');
|
|
|
|
$newId = str_pad(((int) $maxId + 1), 4, '0', STR_PAD_LEFT);
|
|
if (empty($memberExist)) {
|
|
|
|
$this->sanghMember->create(
|
|
[
|
|
'sangh_id' => $sanghId,
|
|
'member_id' => $newId ?? '0001',
|
|
'user_id' => $member,
|
|
'core_committee' => $data['core_committee'] ?? Constant::STATUS_ZERO,
|
|
'role' => $roleName,
|
|
'added_by' => $user->id ?? Constant::NULL,
|
|
'status' => $status
|
|
]
|
|
);
|
|
$sangh = $this->sangh->where('id', $sanghId)->first();
|
|
$sanghMembers = $sangh->sanghMembers()
|
|
->where('sangh_members.core_committee', Constant::STATUS_ONE)
|
|
->get()
|
|
->pluck('pivot.user_id');
|
|
|
|
$isCommitteeMember = SanghMember::where('sangh_id', $sanghId)
|
|
->where('user_id', $member)
|
|
->where('core_committee', Constant::STATUS_ONE)->first();
|
|
if ($isCommitteeMember) {
|
|
$coreMember = true;
|
|
} else {
|
|
$coreMember = false;
|
|
}
|
|
|
|
if (isset($data['is_join']) && $data['is_join'] === Constant::STATUS_ONE) {
|
|
foreach ($sanghMembers as $sanghMember) {
|
|
$sanghMemberId = $sanghMember;
|
|
|
|
if (isset($data['core_committee'])) {
|
|
// Delete previous relation request
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$sanghMemberId])
|
|
->where('type', 'SanghAddedCoreCommitteeMember')
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->whereJsonContains('extra_fields->from_id', $user->id);
|
|
|
|
if ($relationshipRequestExist->first()) {
|
|
$relationshipRequestExist->delete();
|
|
}
|
|
} else {
|
|
// Delete previous relation request
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$sanghMemberId])
|
|
->where('type', 'SanghAddedMember')
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->whereJsonContains('extra_fields->from_id', $user->id);
|
|
|
|
if ($relationshipRequestExist->first()) {
|
|
$relationshipRequestExist->delete();
|
|
}
|
|
}
|
|
|
|
// Delete previous request
|
|
$requestForMemberExist = Notifications::where('type', 'UserSentMemberRequest')
|
|
->whereJsonContains('extra_fields->from_id', $user->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'UserSentMemberRequest')
|
|
->whereJsonContains('extra_fields->from_id', $user->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
|
|
//Push Notification to admin when user send join request
|
|
if ($member == loggedInUser()->id && $sanghMemberId != loggedInUser()->id) {
|
|
dispatch(new SendUserJoinedSangh($user, $sangh, $sanghMemberId, $coreMember, $roleName));
|
|
}
|
|
|
|
//Push Notification to admin when user send join request
|
|
// if ($sanghMemberId != loggedInUser()->id) {
|
|
// dispatch(new SendUserJoinedSangh($user, $sangh, $sanghMemberId, $coreMember, $roleName));
|
|
// }
|
|
}
|
|
} else {
|
|
if (isset($data['core_committee'])) {
|
|
// Delete previous relation request
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$member])
|
|
->where('type', 'SanghAddedCoreCommitteeMember')
|
|
->whereJsonContains('extra_fields->sangh_id', $sanghId)
|
|
->whereJsonContains('extra_fields->from_id', $user->id);
|
|
|
|
if ($relationshipRequestExist->first()) {
|
|
$relationshipRequestExist->delete();
|
|
}
|
|
} else {
|
|
// Delete previous relation request
|
|
$relationshipRequestExist = Notifications::whereIn('user_id', [$member])
|
|
->where('type', 'SanghAddedMember')
|
|
->whereJsonContains('extra_fields->sangh_id', $sanghId)
|
|
->whereJsonContains('extra_fields->from_id', $user->id);
|
|
|
|
if ($relationshipRequestExist->first()) {
|
|
$relationshipRequestExist->delete();
|
|
}
|
|
}
|
|
|
|
//Push Notification to admin when user send join request
|
|
if ($member != loggedInUser()->id) {
|
|
// $user = $member;
|
|
// $isCoreCommittee = $data['core_committee'] ?? Constant::NULL;
|
|
dispatch(new SendAdminAcceptSangh($member, $sangh, $roleName));
|
|
}
|
|
}
|
|
$responseData['message'] = trans('sangh.sangh_member_success');
|
|
$responseData['status'] = Constant::CODE_200;
|
|
|
|
} else {
|
|
$responseData['message'] = trans('sangh.sangh_request_exist');
|
|
$responseData['status'] = Constant::CODE_200;
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $responseData;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function updateSanghMember($data)
|
|
{
|
|
$responseData = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$sangh = $this->sangh::where('id', $data['sangh_id'])->first();
|
|
$memberId = $data['user_id'];
|
|
$roleName = $data['role'];
|
|
|
|
$sanghMember = $this->sanghMember->where([
|
|
'sangh_id' => $data['sangh_id'],
|
|
'user_id' => $data['user_id']
|
|
])->first();
|
|
|
|
if ($user) {
|
|
if ($sanghMember) {
|
|
$sanghMember->sangh_id = $data['sangh_id'] ?? $sanghMember->sangh_id;
|
|
$sanghMember->user_id = $data['user_id'] ?? $sanghMember->user_id;
|
|
$sanghMember->core_committee = $data['core_committee'] ?? $sanghMember->core_committee;
|
|
$sanghMember->role = $data['role'] ?? $sanghMember->role;
|
|
$sanghMember->save();
|
|
|
|
//Push Notification to user when admin updates role
|
|
dispatch(new SendAdminUpdateRole($user, $memberId, $sangh, $roleName));
|
|
|
|
$responseData['message'] = trans('sangh.sangh_member_update');
|
|
$responseData['status'] = Constant::CODE_200;
|
|
} else {
|
|
$responseData['message'] = trans('sangh.not_exist');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
} else {
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$responseData['message'] = trans('sangh.something_went_wrong');
|
|
$responseData['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $responseData;
|
|
}
|
|
|
|
/**
|
|
* Get member of sangh
|
|
*/
|
|
public function getSanghMembers(array $data, object $sangh)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
|
|
if ($sangh) {
|
|
$query = $sangh->sanghMembers()->where(function ($query) {
|
|
$query->where('sangh_members.core_committee', Constant::STATUS_ZERO);
|
|
// ->orWhere('sangh_members.core_committee', Constant::STATUS_ONE);
|
|
});
|
|
|
|
$isUserAdminOrCommittee = $sangh->sanghMembers()
|
|
->where('user_id', loggedInUser()->id)
|
|
->where(function ($subQuery) {
|
|
$subQuery->where('is_owner', Constant::STATUS_ONE)
|
|
->orWhere('core_committee', Constant::STATUS_ONE);
|
|
})->exists();
|
|
|
|
if (!$isUserAdminOrCommittee) {
|
|
$query->where('sangh_members.status', Constant::STATUS_ONE);
|
|
}
|
|
// ->where('sangh_members.status', Constant::STATUS_ONE)
|
|
$sanghMembers = $query->paginate($data['limit'] ?? 100,
|
|
[
|
|
'users.id',
|
|
'users.name',
|
|
'users.avatar',
|
|
'sangh_members.member_id'
|
|
],
|
|
'page', $data['page'] ?? 1);
|
|
|
|
$sanghMembers->getCollection()->map(function($user) use($sangh) {
|
|
$user->sangh_member_status = $user->getSanghMemberStatus($sangh->id);
|
|
return $user;
|
|
});
|
|
$response['data'] = $sanghMembers->toArray();
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.followlist');
|
|
unset($response['error']);
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get committe member of sangh
|
|
*/
|
|
public function getSanghCommitteeMembers(array $data, object $sangh)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$sanghMembers = $sangh->sanghMembers()->select('users.id', 'users.name', 'users.avatar')
|
|
->where('sangh_members.core_committee', Constant::STATUS_ONE)
|
|
// ->where('sangh_members.status', Constant::STATUS_ONE)
|
|
->paginate($data['limit'] ?? 10,
|
|
[
|
|
'users.id',
|
|
'users.name',
|
|
'users.avatar',
|
|
'sangh_members.member_id'
|
|
],
|
|
'page', $data['page'] ?? 1);
|
|
$sanghMembers->getCollection()->map(function($user) use($sangh) {
|
|
$user->sangh_member_status = $user->getSanghMemberStatus($sangh->id);
|
|
return $user;
|
|
});
|
|
if ($sanghMembers) {
|
|
$response['data'] = $sanghMembers->toArray();
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
$response['message'] = trans('api.sangh.followlist');
|
|
unset($response['error']);
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get invited member of sangh
|
|
*/
|
|
public function getInvitedSanghMembers()
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
// $sanghMembers = $this->sanghMember->with('user', 'addedBy','sangh')->where('user_id', $user->id)
|
|
// ->where('status', Constant::STATUS_ZERO)
|
|
// ->paginate($data['limit'] ?? Constant::PAGINATE_LIMIT, ['*'], 'page', $data['page'] ?? 1);
|
|
$sanghMembers = Notifications::where('user_id', $user->id)
|
|
->Where('type', 'SanghAddedMember')
|
|
->latest()
|
|
->paginate($data['limit'] ?? Constant::PAGINATE_LIMIT, ['*'], 'page', $data['page'] ?? 1);
|
|
|
|
unreadSanghInviteCounter($user->id, Constant::STATUS_ZERO); //Set sangh invite badge to default state
|
|
$response['data'] = $sanghMembers;
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
unset($response['error']);
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function deleteSanghMember($data)
|
|
{
|
|
$response = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$sangh = $this->sangh->where('id', $data['sangh_id'])->first();
|
|
$roleName = $data['role'] ?? '';
|
|
$userId = (int) $data['user_id'];
|
|
$sanghId = (int) $data['sangh_id'];
|
|
|
|
if ($user && !empty($data['user_id']) && !empty($data['sangh_id'])) {
|
|
$sanghMemberData = $this->sanghMember->where(['sangh_id' => $data['sangh_id'],'user_id' => $data['user_id']])->first();
|
|
|
|
if ($sanghMemberData) {
|
|
if ($sanghMemberData->core_committee == 1) {
|
|
// If request is pending
|
|
if ($sanghMemberData->status == 0) {
|
|
// Delete previous relation request
|
|
|
|
$relationshipRequestExist = Notifications::where('type', 'SanghAddedCoreCommitteeMember')
|
|
->where('user_id', $userId)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
// ->whereJsonContains('extra_fields->from_id', $user->id);
|
|
if ($relationshipRequestExist->first()) {
|
|
$relationshipRequestExist->delete();
|
|
}
|
|
|
|
$requestForMemberExist = Notifications::where('type', 'UserSentMemberRequest')
|
|
->orWhere('type', 'SanghAddedMember')
|
|
->where('user_id', $userId)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'UserSentMemberRequest')
|
|
->orWhere('type', 'SanghAddedMember')
|
|
->where('user_id', $userId)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
}
|
|
} else {
|
|
// Delete previous relation request
|
|
// If request is pending
|
|
if ($sanghMemberData->status == 0) {
|
|
$relationshipRequestExist = Notifications::where('type', 'SanghAddedMember')
|
|
->where('user_id', $userId)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($relationshipRequestExist->latest()) {
|
|
$relationshipRequestExist->delete();
|
|
}
|
|
|
|
$requestForMemberExist = Notifications::where('type', 'UserSentMemberRequest')
|
|
->where('user_id', $userId)
|
|
->whereJsonContains('extra_fields->sangh_id', $sanghId);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'UserSentMemberRequest')
|
|
->where('user_id', $userId)
|
|
->whereJsonContains('extra_fields->sangh_id', $sanghId)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
}
|
|
}
|
|
$sanghMemberData->delete();
|
|
// Remove karma points from sangh
|
|
$points = KarmaPointsTransaction::where([
|
|
'user_id' => $sangh->id,
|
|
'pointable_id' => $sangh->id
|
|
])->where('key', 'new_sangh_member')->first();
|
|
if ($points) {
|
|
$points->delete();
|
|
}
|
|
$response['message'] = trans('sangh.sangh_member_delete');
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
//Push Notification to user when admin removes from sangh
|
|
if ($sanghMemberData->status === Constant::STATUS_ONE) {
|
|
dispatch(new SendAdminRemoveMember($userId, $sangh, $roleName));
|
|
}
|
|
} else {
|
|
$response['message'] = trans('sangh.not_exist');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
} 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 getUserList($data)
|
|
{
|
|
$response = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$members = $this->sanghMember->where('sangh_id', $data['sangh_id'])->where('user_id', '!=', null)->pluck('user_id');
|
|
|
|
$users = $this->user->select('id', 'name', 'avatar')->whereNotIn('id', $members)
|
|
->where('id', '!=', $user->id)
|
|
->where('id', '!=', Constant::STATUS_ONE)
|
|
->where('is_passive', '!=', Constant::STATUS_ONE);
|
|
|
|
if (isset($data['name']) && !empty($data['name'])) {
|
|
$users = $users->where('name', 'LIKE', "%{$data['name']}%");
|
|
}
|
|
|
|
$users = $users->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
|
|
|
$response['data'] = $users;
|
|
$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 array $data
|
|
* @return array
|
|
*/
|
|
public function acceptMemberRequest(array $data)
|
|
{
|
|
$response = [];
|
|
$users = [];
|
|
|
|
try {
|
|
$loggedInUser = loggedInUser();
|
|
$sangh = $this->sangh->where('id', $data['sangh_id'])->first();
|
|
$user = $this->user->where('id', $data['user_id'])->first();
|
|
$roleName = $data['role'] ?? '';
|
|
|
|
$committeeMembers = $this->sanghMember->query()->where([
|
|
'sangh_id' => $data['sangh_id'],
|
|
'core_committee' => Constant::STATUS_ONE
|
|
])->distinct()->pluck('user_id');
|
|
|
|
//Check if request is exist
|
|
$memberExist = $this->sanghMember->query()->where([
|
|
'sangh_id' => $data['sangh_id'],
|
|
'user_id' => $loggedInUser->id
|
|
])->first();
|
|
|
|
//Check if request is exist
|
|
$userExist = $this->sanghMember->query()->where([
|
|
'sangh_id' => $data['sangh_id'],
|
|
'user_id' => $data['user_id']
|
|
])->first();
|
|
|
|
//Check if any Admin exist
|
|
$sanghAdmin = $this->sanghMember->query()->where([
|
|
'user_id' => loggedInUser()->id,
|
|
'sangh_id' => $data['sangh_id'],
|
|
'core_committee' => Constant::STATUS_ONE,
|
|
'role' => Constant::ADMIN,
|
|
'status' => Constant::STATUS_ONE
|
|
])->first();
|
|
|
|
if (!empty($loggedInUser)) {
|
|
if ((!empty($userExist) && !empty($memberExist) && empty($sanghAdmin)) && ($data['status'] === Constant::STATUS_ONE && $data['is_commiittee'] === Constant::STATUS_ZERO)) {
|
|
$memberExist->status = $data['status'];
|
|
$memberExist->save();
|
|
|
|
//Push Notification to admin when user accepts join request
|
|
if (!empty($committeeMembers)) {
|
|
foreach ($committeeMembers as $committeeMember) {
|
|
if ($committeeMember != $loggedInUser->id) {
|
|
dispatch(new SendUserAcceptSangh($loggedInUser, $user, $sangh, $committeeMember, $roleName));
|
|
}
|
|
}
|
|
}
|
|
// Add karma points to sangh on joining sangh
|
|
$sangh->addKarmaPoints($sangh, $sangh->id, config('config-variables.karma_points_message.new_sangh_member'), config('config-variables.karma_points.new_sangh_member'), config('config-variables.karma_points_key.new_sangh_member'), []);
|
|
$loggedInUser->sanghFollowings()->syncWithoutDetaching(['sangh_id' => $sangh->id]); //Add user as follower
|
|
$response['message'] = trans('sangh.sangh_request_accept');
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
// Delete previous request
|
|
$requestForMemberExist = Notifications::where('type', 'SanghAddedMember')
|
|
->where('user_id', $loggedInUser->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'SanghAddedMember')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $loggedInUser->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
|
|
} else if (!empty($userExist) && isset($data['user_id']) && isset($data['status']) && $data['status'] === Constant::STATUS_ONE && isset($data['is_commiittee']) && $data['is_commiittee'] === Constant::STATUS_ONE) {
|
|
$userExist->status = $data['status'] ?? "";
|
|
$userExist->save();
|
|
|
|
//Push Notification to user when admin accepts join request
|
|
if ($user->id != $loggedInUser->id) {
|
|
dispatch(new SendAdminAcceptSangh($user->id, $sangh, $roleName));
|
|
}
|
|
|
|
// Add karma points to sangh on joining sangh
|
|
$sangh->addKarmaPoints($sangh, $sangh->id, config('config-variables.karma_points_message.new_sangh_member'), config('config-variables.karma_points.new_sangh_member'), config('config-variables.karma_points_key.new_sangh_member'), []);
|
|
$user->sanghFollowings()->syncWithoutDetaching(['sangh_id' => $sangh->id]); //Add user as follower
|
|
$response['message'] = trans('sangh.sangh_request_accept');
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
// Delete previous request
|
|
$requestForMemberExist = Notifications::where('type', 'SanghAddedMember')
|
|
->orWhere('type', 'UserSentMemberRequest')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $user->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'SanghAddedMember')
|
|
->orWhere('type', 'UserSentMemberRequest')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $loggedInUser->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
|
|
} else if ((!empty($userExist) && !empty($sanghAdmin)) && !empty($data['user_id']) && (!empty($data['status']) && $data['status'] === Constant::STATUS_ONE) && (!empty($data['is_commiittee']) && $data['is_commiittee'] === Constant::STATUS_ZERO)) {
|
|
$sanghAdmin->role = Constant::CORE_COMMITTEE;
|
|
$sanghAdmin->is_owner = Constant::STATUS_ZERO;
|
|
$memberExist->role = Constant::ADMIN;
|
|
$memberExist->is_owner = Constant::STATUS_ONE;
|
|
$memberExist->status= Constant::STATUS_ONE;
|
|
$sanghAdmin->save();
|
|
$memberExist->save();
|
|
|
|
//Push Notification to user when admin accepts join request
|
|
if ($user->id != $loggedInUser->id) {
|
|
dispatch(new SendAdminAcceptSangh($user->id, $sangh, $roleName));
|
|
}
|
|
|
|
// Add karma points to sangh on joining sangh
|
|
$sangh->addKarmaPoints($sangh, $sangh->id, config('config-variables.karma_points_message.new_sangh_member'), config('config-variables.karma_points.new_sangh_member'), config('config-variables.karma_points_key.new_sangh_member'), []);
|
|
$user->sanghFollowings()->syncWithoutDetaching(['sangh_id' => $sangh->id]); //Add user as follower
|
|
$response['message'] = trans('sangh.sangh_request_accept');
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
// Delete previous request
|
|
$requestForMemberExist = Notifications::where('type', 'SanghAddedMember')
|
|
->orWhere('type', 'UserSentMemberRequest')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $user->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'SanghAddedMember')
|
|
->orWhere('type', 'UserSentMemberRequest')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $loggedInUser->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
|
|
} else {
|
|
$response['message'] = trans('sangh.sangh_request_not_exist');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
//Delete requests if declined
|
|
// if (isset($data['status']) && $data['status'] == Constant::STATUS_TWO && !empty($userExist) && $memberExist && $data['is_commiittee'] === Constant::STATUS_ZERO) {
|
|
if (isset($data['status']) && $data['status'] == Constant::STATUS_TWO && $memberExist && isset($data['is_commiittee']) && $data['is_commiittee'] === Constant::STATUS_ZERO) {
|
|
//Check if request is exist
|
|
$memberOwnerExist = $this->sanghMember->query()->where([
|
|
'sangh_id' => $data['sangh_id'],
|
|
'user_id' => $loggedInUser->id,
|
|
'is_owner' => Constant::STATUS_ONE
|
|
])->first();
|
|
|
|
if ($memberOwnerExist) {
|
|
|
|
} else {
|
|
$memberExist->delete();
|
|
}
|
|
|
|
// Delete previous request
|
|
$requestForMemberExist = Notifications::where('type', 'UserSentMemberRequest')
|
|
->orWhere('type', 'SanghAddedMember')
|
|
->where('user_id', $loggedInUser->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'UserSentMemberRequest')
|
|
->orWhere('type', 'SanghAddedMember')
|
|
->where('user_id', $loggedInUser->id)
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
|
|
//Push Notification to admin when user declines join request
|
|
if (!empty($committeeMembers)) {
|
|
foreach ($committeeMembers as $committeeMember) {
|
|
if ($committeeMember != $loggedInUser->id) {
|
|
dispatch(new SendUserDeclineSangh($loggedInUser, $user, $sangh, $committeeMember, $roleName));
|
|
}
|
|
}
|
|
}
|
|
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['message'] = trans('sangh.sangh_request_decline');
|
|
|
|
} else if (isset($data['status']) && $data['status'] == Constant::STATUS_TWO && $userExist) {
|
|
//Check if request is exist
|
|
$userOwnerExist = $this->sanghMember->query()->where([
|
|
'sangh_id' => $data['sangh_id'],
|
|
'user_id' => $data['user_id'],
|
|
'is_owner' => Constant::STATUS_ONE
|
|
])->first();
|
|
|
|
// Delete previous request
|
|
$requestForMemberExist = Notifications::where('type', 'UserSentMemberRequest')
|
|
->orWhere('type', 'SanghAddedMember')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $data['user_id'])
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id);
|
|
|
|
if ($requestForMemberExist->count() > 1) {
|
|
Notifications::where('type', 'UserSentMemberRequest')
|
|
->orWhere('type', 'SanghAddedMember')
|
|
->where('user_id', $loggedInUser->id)
|
|
// ->whereJsonContains('extra_fields->user_id', $data['user_id'])
|
|
->whereJsonContains('extra_fields->sangh_id', $sangh->id)
|
|
->delete();
|
|
} else {
|
|
$requestForMemberExist->delete();
|
|
}
|
|
|
|
if ($userOwnerExist) {
|
|
|
|
} else {
|
|
$userExist->delete();
|
|
}
|
|
|
|
//Push Notification to user when admin declines join request
|
|
if ($user->id != $loggedInUser->id) {
|
|
dispatch(new SendAdminDeclineSangh($user, $sangh, $roleName));
|
|
}
|
|
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['message'] = trans('sangh.sangh_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;
|
|
}
|
|
|
|
/**
|
|
* Get names of sangh if logged in user is member of any sangh
|
|
*/
|
|
public function getSanghNames()
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
$sanghIds = $this->sanghMember->where('user_id', $user->id)
|
|
->where('sangh_id', '!=', Constant::NULL)
|
|
->where('core_committee', Constant::STATUS_ONE)
|
|
->where('status', Constant::STATUS_ONE)
|
|
// ->orWhere('role', Constant::ADMIN)
|
|
// ->orWhere('is_owner', Constant::STATUS_ONE)
|
|
->pluck('sangh_id');
|
|
|
|
$sangh = $this->sangh->whereIn('id', $sanghIds)
|
|
->select('id', 'avatar', 'name', 'address')
|
|
->get()
|
|
->toArray();
|
|
if ($sangh) {
|
|
$response['data'] = $sangh;
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
unset($response['error']);
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get the sangh member card data by logged in user
|
|
*/
|
|
public function getMemberCard($id)
|
|
{
|
|
$response = [
|
|
'status' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
|
|
$memberCard = $this->sanghMember->select('member_id', 'sangh_id', 'user_id', 'profile_image', 'role')
|
|
->with('sangh:id,name,avatar,city,state', 'user:id,name', 'user.userDetail:id,user_id,location')
|
|
->where('user_id', $user->id)
|
|
->where('sangh_id', $id)
|
|
->whereNotNull('sangh_id')
|
|
->get()
|
|
->toArray();
|
|
|
|
if ($memberCard) {
|
|
$response['data'] = $memberCard;
|
|
$response['status'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
unset($response['error']);
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Get the sangh member card data by logged in user
|
|
*/
|
|
|
|
public function createSanghMemberCard($data)
|
|
{
|
|
$response = [
|
|
'status_code' => Constant::CODE_403,
|
|
'error' => trans('api.something_went_wrong'),
|
|
'success' => Constant::STATUS_FALSE
|
|
];
|
|
try {
|
|
$memberData = $this->sanghMember->with('sangh', 'user')
|
|
->where('sangh_id', $data['sangh_id'])
|
|
->where('member_id', $data['member_id'])
|
|
->first();
|
|
|
|
if ($memberData) {
|
|
if (!empty($data['profile_image'])) {
|
|
$sanghImageName = uploadImage($data, 'profile_image', Constant::SANGH_MEMBER_IMAGE_UPLOAD_PATH . Constant::SLASH);
|
|
|
|
$sanghMemberImage['profile_image'] = $sanghImageName['image_name'] ?? Constant::NULL;
|
|
$memberData->update($sanghMemberImage);
|
|
|
|
if (basename($memberData->user->avatar) == 'shravak.png') {
|
|
$userImageName = uploadImage($data, 'profile_image', Constant::USER_IMAGE_UPLOAD_PATH . Constant::SLASH);
|
|
$userProfileImage['avatar'] = $userImageName['image_name'] ?? Constant::NULL;
|
|
$memberData->user->update($userProfileImage);
|
|
}
|
|
}
|
|
|
|
$this->user->find($memberData->user_id)->update(['name' => $data['name']]);
|
|
UserDetail::updateOrCreate(
|
|
['user_id' => $memberData->user_id],
|
|
[
|
|
'location' => $data['location'],
|
|
'longitude' => $data['longitude'],
|
|
'latitude' => $data['latitude']
|
|
]
|
|
);
|
|
|
|
$dataArr = [
|
|
'sangh_name' => $memberData->sangh->name,
|
|
'sangh_image' => $memberData->sangh->avatar,
|
|
'sangh_location' => $memberData->sangh->address,
|
|
'user_role' => $memberData->role,
|
|
'member_id' => $data['member_id'],
|
|
'user_location' => $data['location'],
|
|
'user_name' => $data['name'],
|
|
'sangh_member_profile' => $sanghMemberImage['profile_image'] ?? Constant::NULL,
|
|
'user_profile' => $userProfileImage['avatar'] ?? Constant::NULL
|
|
];
|
|
|
|
$response['data'] = $dataArr;
|
|
$response['status_code'] = Constant::CODE_200;
|
|
$response['success'] = Constant::STATUS_TRUE;
|
|
unset($response['error']);
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|
|
|