api code global jain
This commit is contained in:
379
app/Repositories/Backend/Vihar/ViharService.php
Normal file
379
app/Repositories/Backend/Vihar/ViharService.php
Normal file
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Backend\Vihar;
|
||||
|
||||
use Exception;
|
||||
use App\Models\Sant;
|
||||
use App\Models\User;
|
||||
use App\Models\Vihar;
|
||||
use App\Constant\Constant;
|
||||
use App\Models\ThanaMember;
|
||||
use App\Services\BaseService;
|
||||
use App\Models\UserDeviceToken;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Exceptions\GeneralException;
|
||||
use App\Traits\PushNotificationTraits;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Jobs\Notifications\Vihaar\SendNewVihaar;
|
||||
use App\Jobs\Notifications\Vihaar\SendUpdateVihaar;
|
||||
|
||||
class ViharService extends BaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* Associated Service Model.
|
||||
*/
|
||||
protected const MODEL = Vihar::class;
|
||||
|
||||
use PushNotificationTraits;
|
||||
|
||||
/**
|
||||
* @var UserDeviceToken
|
||||
*/
|
||||
protected $userDeviceToken;
|
||||
|
||||
/**
|
||||
* @param Vihar $vihar
|
||||
* ViharService constructor.
|
||||
*
|
||||
*/
|
||||
public function __construct(UserDeviceToken $userDeviceToken)
|
||||
{
|
||||
$this->userDeviceToken = $userDeviceToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is use for search query in Datatable.
|
||||
* Also for sorting function we are using join query for appropriate sorting result
|
||||
* @param $request
|
||||
* @return $this|array|Builder
|
||||
*/
|
||||
public function prepareSearchQuery($request, $santId)
|
||||
{
|
||||
$searchQuery = [];
|
||||
try {
|
||||
$searchQuery = $this->query()->with('sant')->select(
|
||||
'id',
|
||||
'sant_id',
|
||||
'from',
|
||||
'to',
|
||||
'start_date',
|
||||
'start_time',
|
||||
'end_date',
|
||||
'created_at',
|
||||
)->where('sant_id', $santId);
|
||||
|
||||
$columns = ['', 'id', 'from', 'to', 'start_date', 'end_date', 'created_at'];
|
||||
|
||||
if (isset($request['from']) && $request['from'] != '') {
|
||||
$searchQuery = $searchQuery->where('from', 'like', '%' . $request['from'] . '%');
|
||||
}
|
||||
|
||||
if (isset($request['to']) && $request['to'] != '') {
|
||||
$searchQuery = $searchQuery->where('to', 'like', '%' . $request['to'] . '%');
|
||||
}
|
||||
|
||||
if (isset($request['order']) && $request['order'] != "") {
|
||||
$searchQuery = $searchQuery->orderBy(
|
||||
$columns[$request['order'][0]['column']],
|
||||
$request['order'][0]['dir']
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
return $searchQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the format the Datatable columns
|
||||
*
|
||||
* @param $request
|
||||
* @return object
|
||||
*/
|
||||
public function getForDataTable($request, $santId): object
|
||||
{
|
||||
$response = (object)[];
|
||||
try {
|
||||
$dataTableQuery = $this->prepareSearchQuery($request, $santId);
|
||||
// ->orderBy('id', 'DESC');
|
||||
|
||||
$response = Datatables::of($dataTableQuery)
|
||||
->addColumn(
|
||||
'checkbox_action',
|
||||
function ($vihar) {
|
||||
return $vihar->vihar_checkbox_action;
|
||||
}
|
||||
)
|
||||
->addIndexColumn()
|
||||
->addColumn(
|
||||
'start_date',
|
||||
function ($vihar) {
|
||||
return (($vihar->start_date) ? defaultDateTimeFormat($vihar->start_date) : '-');
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'start_time',
|
||||
function ($vihar) {
|
||||
return date("g:i A", strtotime($vihar->start_time)) ?? '-';
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'created_at',
|
||||
function ($vihar) {
|
||||
return (($vihar->created_at) ? defaultDateTimeFormat($vihar->created_at) : '-');
|
||||
}
|
||||
)
|
||||
->addColumn(
|
||||
'action',
|
||||
function ($vihar) {
|
||||
if (
|
||||
!empty($vihar->roles[0]['name']) != config('access.users.admin_role')
|
||||
|| !empty($vihar->roles[0]['name']) != config('access.users.super_admin_role')
|
||||
|| loggedInUser()->isSuperAdmin()
|
||||
|| loggedInUser()->isAdmin()
|
||||
) {
|
||||
return $vihar->vihar_action_buttons;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
)
|
||||
->setRowId(
|
||||
function ($vihar) {
|
||||
return 'recordRow-' . $vihar->id;
|
||||
}
|
||||
)
|
||||
->rawColumns(['status', 'action', 'checkbox_action'])
|
||||
->make(true);
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the create vihar
|
||||
*
|
||||
* @param $request
|
||||
* @return response
|
||||
* @throws GeneralException
|
||||
*/
|
||||
public function create($request, $santId)
|
||||
{
|
||||
try {
|
||||
$user = loggedInUser();
|
||||
$thanaID = Constant::NULL;
|
||||
$thanaSantID = [];
|
||||
|
||||
$thanaID = ThanaMember::where('sant_id', $santId)->value('thana_id');
|
||||
$thanaSantID = ThanaMember::where('thana_id', $thanaID)->pluck('sant_id');
|
||||
|
||||
if (!empty($thanaSantID) && count($thanaSantID) > 0) {
|
||||
foreach ($thanaSantID as $santID) {
|
||||
$vihar = $this->query()->create(
|
||||
[
|
||||
'from' => $request['from'] ?? Constant::NULL,
|
||||
'to' => $request['to'] ?? Constant::NULL,
|
||||
'from_latitude' => $request['from_latitude'] ?? Constant::NULL,
|
||||
'from_longitude' => $request['from_longitude'] ?? Constant::NULL,
|
||||
'to_latitude' => $request['to_latitude'] ?? Constant::NULL,
|
||||
'to_longitude' => $request['to_longitude'] ?? Constant::NULL,
|
||||
'start_date' => $request['start_date'] ?? Constant::NULL,
|
||||
'start_time' => $request['start_time'] ?? Constant::NULL,
|
||||
// 'end_date' => $request['end_date'] ?? Constant::NULL,
|
||||
// 'end_time' => $request['end_time'] ?? Constant::NULL,
|
||||
'sant_id' => $santID,
|
||||
'thana_sant_id' => $thanaID,
|
||||
'created_by' => loggedInUser()->id,
|
||||
'updated_by' => Constant::NULL,
|
||||
'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
|
||||
]
|
||||
);
|
||||
$sant = Sant::find($santID);
|
||||
|
||||
//Push Notification to followers of sant
|
||||
dispatch(new SendNewVihaar($sant, $user));
|
||||
}
|
||||
} else {
|
||||
$vihar = $this->query()->create(
|
||||
[
|
||||
'from' => $request['from'] ?? Constant::NULL,
|
||||
'to' => $request['to'] ?? Constant::NULL,
|
||||
'from_latitude' => $request['from_latitude'] ?? Constant::NULL,
|
||||
'from_longitude' => $request['from_longitude'] ?? Constant::NULL,
|
||||
'to_latitude' => $request['to_latitude'] ?? Constant::NULL,
|
||||
'to_longitude' => $request['to_longitude'] ?? Constant::NULL,
|
||||
'start_date' => $request['start_date'] ?? Constant::NULL,
|
||||
'start_time' => $request['start_time'] ?? Constant::NULL,
|
||||
// 'end_date' => $request['end_date'] ?? Constant::NULL,
|
||||
// 'end_time' => $request['end_time'] ?? Constant::NULL,
|
||||
'sant_id' => $santId,
|
||||
'thana_sant_id' => Constant::NULL,
|
||||
'created_by' => loggedInUser()->id,
|
||||
'updated_by' => Constant::NULL,
|
||||
'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
|
||||
]
|
||||
);
|
||||
$sant = Sant::find($santId);
|
||||
|
||||
//Push Notification to followers of sant
|
||||
dispatch(new SendNewVihaar($sant, $user));
|
||||
}
|
||||
|
||||
if (!empty($vihar)) {
|
||||
return $vihar;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
throw new GeneralException(__('message.create_vihar_error'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $sant
|
||||
* @param $data
|
||||
* @return sant
|
||||
*/
|
||||
public function update(object $vihar,$request, $santId)
|
||||
{
|
||||
try {
|
||||
$user = loggedInUser();
|
||||
$viharData = Vihar::find($vihar->id);
|
||||
$viharID = Vihar::where('thana_sant_id', $vihar->thana_sant_id)->where('thana_sant_id', '!=', null)
|
||||
->where('created_at', $viharData->created_at)
|
||||
->pluck('id');
|
||||
|
||||
if (!empty($user)) {
|
||||
if (count($viharID) > 0) {
|
||||
foreach ($viharID as $id) {
|
||||
$viharDetails = Vihar::find($id);
|
||||
$vihar = $this->query()->where('id',$id)->update(
|
||||
[
|
||||
'from' => $request['from'] ?? $viharDetails->from,
|
||||
'to' => $request['to'] ?? $viharDetails->to,
|
||||
'from_latitude' => $request['from_latitude'] ?? Constant::NULL,
|
||||
'from_longitude' => $request['from_longitude'] ?? Constant::NULL,
|
||||
'to_latitude' => $request['to_latitude'] ?? Constant::NULL,
|
||||
'to_longitude' => $request['to_longitude'] ?? Constant::NULL,
|
||||
'start_date' => $request['start_date'] ?? $viharDetails->start_date,
|
||||
'start_time' => $request['start_time'] ?? $viharDetails->start_time,
|
||||
// 'end_date' => $request['end_date'] ?? $viharDetails->end_date,
|
||||
// 'end_time' => $request['end_time'] ?? $viharDetails->end_time,
|
||||
'sant_id' => $viharDetails->sant_id ?? Constant::NULL,
|
||||
'thana_sant_id' => $viharDetails->thana_sant_id ?? Constant::NULL,
|
||||
'user_id' => !empty($user->id) ? $user->id : Constant::NULL,
|
||||
'updated_by' => !empty($viharDetails->updated_by) ? $viharDetails->updated_by : Constant::NULL
|
||||
]
|
||||
);
|
||||
$sant = Sant::find($viharDetails->sant_id);
|
||||
//Push Notification to followers of sant
|
||||
dispatch(new SendUpdateVihaar($sant, $user, $vihar));
|
||||
|
||||
if (isset($request['is_approved']) && $request['is_approved'] == Constant::STATUS_ZERO) {
|
||||
$viharDetails->is_approved = Constant::STATUS_ONE;
|
||||
$viharDetails->save();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$vihar = $this->query()->where('id',$vihar->id)->update(
|
||||
[
|
||||
'from' => $request['from'] ?? Constant::NULL,
|
||||
'to' => $request['to'] ?? Constant::NULL,
|
||||
'from_latitude' => $request['from_latitude'] ?? Constant::NULL,
|
||||
'from_longitude' => $request['from_longitude'] ?? Constant::NULL,
|
||||
'to_latitude' => $request['to_latitude'] ?? Constant::NULL,
|
||||
'to_longitude' => $request['to_longitude'] ?? Constant::NULL,
|
||||
'start_date' => $request['start_date'] ?? Constant::NULL,
|
||||
'start_time' => $request['start_time'] ?? Constant::NULL,
|
||||
// 'end_date' => $request['end_date'] ?? Constant::NULL,
|
||||
// 'end_time' => $request['end_time'] ?? Constant::NULL,
|
||||
'sant_id' => $santId ?? $viharData->sant_id,
|
||||
'thana_sant_id' => $request['thana_sant_id'] ?? $viharData->thana_sant_id,
|
||||
'user_id' => !empty($user->id) ? $user->id : Constant::NULL,
|
||||
'updated_by' => !empty($viharData->updated_by) ? $viharData->updated_by : Constant::NULL
|
||||
]
|
||||
);
|
||||
$sant = Sant::find($santId);
|
||||
//Push Notification to followers of sant
|
||||
dispatch(new SendUpdateVihaar($sant, $user, $vihar));
|
||||
|
||||
if (isset($request['is_approved']) && $request['is_approved'] == Constant::STATUS_ZERO) {
|
||||
$viharData->is_approved = Constant::STATUS_ONE;
|
||||
$viharData->save();
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($vihar)) {
|
||||
return $vihar;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
throw new GeneralException(__('message.update_vihar_error'));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is for the multiple records action from the grid
|
||||
*
|
||||
* @param string $actionType
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws GeneralException
|
||||
*/
|
||||
public function gridActions(string $actionType, array $data): bool
|
||||
{
|
||||
try {
|
||||
if (!empty($actionType) && !empty($data)) {
|
||||
$viharDetails = Vihar::whereIn('id', $data)->where('thana_sant_id', '!=', null)->get();
|
||||
|
||||
if (count($viharDetails) > 0) {
|
||||
foreach ($viharDetails as $viharDetail) {
|
||||
$viharSantThana = Vihar::where('id', $viharDetail->id)->pluck('thana_sant_id');
|
||||
$vihar = $this->query()->whereIn('thana_sant_id', $viharSantThana)
|
||||
->where('thana_sant_id', '!=', null)
|
||||
->where('created_at', $viharDetail->created_at)
|
||||
->get();
|
||||
|
||||
if (count($vihar) > 0) {
|
||||
foreach ($vihar as $data) {
|
||||
$data->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->query()->whereIn('id', $data)->delete();
|
||||
}
|
||||
|
||||
if (!empty($viharDetails)) {
|
||||
foreach ($viharDetails as $vihar) {
|
||||
//Delete Vihaar Activity
|
||||
$vihar->activity()->whereJsonContains('properties->attributes->vihar_information->id', $vihar->id)->delete();
|
||||
|
||||
//Delete karma points
|
||||
$user = User::where('id', $vihar->created_by)->first();
|
||||
$points = $vihar->addPoints()->where('pointable_id', $vihar->id)->first();
|
||||
if ($points) {
|
||||
// Remove karma points of vihar
|
||||
$viharPoint = $points->points;
|
||||
$points->delete();
|
||||
$user->decrement('karma_dhan', $viharPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Constant::STATUS_TRUE;
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex->getMessage());
|
||||
throw new GeneralException(__('message.delete_vihar_error'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user