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

511 lines
23 KiB
PHP

<?php
namespace App\Repositories\Api\Access\Vihar;
use Exception;
use Carbon\Carbon;
use App\Models\Sant;
use App\Models\Sangh;
use App\Models\Vihar;
use App\Constant\Constant;
use App\Models\ThanaMember;
use App\Models\UserDeviceToken;
use Illuminate\Support\Facades\Log;
use App\Traits\PushNotificationTraits;
use App\Jobs\Notifications\Vihaar\SendNewVihaar;
use App\Jobs\Notifications\Vihaar\SendUpdateVihaar;
class ViharRepository implements ViharInterface
{
use PushNotificationTraits;
/**
* @var Vihar
*/
protected $vihar;
/**
* @var UserDeviceToken
*/
protected $userDeviceToken;
/**
* @param Vihar $vihar
* @param UserDeviceToken $userDeviceToken
* ViharRepository constructor.
*
*/
public function __construct(Vihar $vihar, UserDeviceToken $userDeviceToken)
{
$this->vihar = $vihar;
$this->userDeviceToken = $userDeviceToken;
}
/**
* Get upcoming and current vihaar list
*/
public function getViharList(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = [];
$allViharsObject = $this->vihar->query()
->with([
'viharThana:id,name,avatar',
'createdBy:id,name,mobile,country_code',
'updatedBy:id,name,mobile,country_code'
])
->orderBy('start_date', 'ASC')
->orderBy('start_time', 'ASC')
// ->where( function ($query) use ($data) {
// return $query->whereHas('viharThana', function ($subQuery) use ($data) {
// $subQuery->where('vihar_thanas.sant_id', $data['sant_id']);
// })->orWhere('vihars.sant_id', $data['sant_id']);
// })
->with(['viharThana:id,name,avatar'])
->where(function ($query) use ($data) {
// $query->where('start_date', '>=', Carbon::now()->toDateString())
// ->orWhere('start_time', '>=', Carbon::now()->subHour(2)->toTimeString());
$query->where(function ($subQuery) use ($data) {
$subQuery->where('start_date', '=', Carbon::now()->toDateString())
->where('start_time', '>=', Carbon::now()->subHour(2)->toTimeString());
})->orWhere(function ($subQuery) use ($data) {
$subQuery->where('start_date', '>', Carbon::now()->toDateString())
->where('start_time', '>=', '00:00:00');
});
})
->where('sant_id', $data['sant_id']);
$allViharsObject = $allViharsObject->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $allViharsObject;
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get only past vihaar list
*/
public function getPastViharList(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = [];
$allViharsObject = $this->vihar->query()
->orderBy('start_date', 'DESC')
->orderBy('start_time', 'DESC')
// ->where( function ($query) use ($data) {
// return $query->whereHas('viharThana', function ($subQuery) use ($data) {
// $subQuery->where('vihar_thanas.sant_id', $data['sant_id']);
// })->orWhere('vihars.sant_id', $data['sant_id']);
// })
->with([
'viharThana:id,name,avatar',
'createdBy:id,name,mobile,country_code',
'updatedBy:id,name,mobile,country_code'
])
->where('sant_id', $data['sant_id'])
->where(function ($query) use ($data) {
// $query->where('start_date', '<=', Carbon::now()->toDateString())
// ->where('start_time', '<', Carbon::now()->subHour(2)->toTimeString());
$query->where(function ($subQuery) use ($data) {
$subQuery->where('start_date', '=', Carbon::now()->toDateString())
->where('start_time', '<=', Carbon::now()->subHour(2)->toTimeString());
})->orWhere(function ($subQuery) use ($data) {
$subQuery->where('start_date', '<', Carbon::now()->toDateString())
->where('start_time', '>=', '00:00:00');
});
});
$allViharsObject = $allViharsObject->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $allViharsObject;
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
*
*/
public function storeVihar(array $viharData)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
$thanaID = Constant::NULL;
$thanaSantID = [];
if (empty($user->mobile)) {
$response['error'] = trans('api.mobile');
} else {
$thanaID = ThanaMember::where('sant_id', $viharData['sant_id'])->value('thana_id');
$thanaSantID = ThanaMember::where('thana_id', $thanaID)->pluck('sant_id');
$from = $viharData['from'] ?? Constant::NULL;
$to = $viharData['to'] ?? Constant::NULL;
$fromLatitude = $viharData['from_latitude'] ?? Constant::NULL;
$fromLongitude = $viharData['from_longitude'] ?? Constant::NULL;
$toLatitude = $viharData['to_latitude'] ?? Constant::NULL;
$toLongitude = $viharData['to_longitude'] ?? Constant::NULL;
if (isset($viharData['from_sangh_id']) && !empty($viharData['from_sangh_id'])) {
$fromSangh = Sangh::where('id', $viharData['from_sangh_id'])->first();
$from = $fromSangh->name;
$fromLatitude = $fromSangh->latitude ?? Constant::NULL;
$fromLongitude = $fromSangh->longitude ?? Constant::NULL;
}
if (isset($viharData['to_sangh_id']) && !empty($viharData['to_sangh_id'])) {
$toSangh = Sangh::where('id', $viharData['to_sangh_id'])->first();
$to = $toSangh->name;
$toLatitude = $toSangh->latitude ?? Constant::NULL;
$toLongitude = $toSangh->longitude ?? Constant::NULL;
}
if (!empty($thanaSantID) && count($thanaSantID) > 0) {
foreach ($thanaSantID as $santID) {
$vihar = $this->vihar->query()->create([
'from' => $from,
'to' => $to,
'from_sangh_id' => $viharData['from_sangh_id'] ?? Constant::NULL,
'from_latitude' => $fromLatitude,
'from_longitude' => $fromLongitude,
'to_sangh_id' => $viharData['to_sangh_id'] ?? Constant::NULL,
'to_latitude' => $toLatitude,
'to_longitude' => $toLongitude,
'start_date' => $viharData['start_date'],
'start_time' => $viharData['start_time'],
'end_time' => '08:00:00',
'sant_id' => $santID,
'thana_sant_id' => $thanaID,
'end_date' => $viharData['end_date'] ?? Constant::NULL,
'is_approved' => Constant::STATUS_ZERO,
'created_by' => loggedInUser()->id,
'updated_by' => Constant::NULL,
'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
]);
$sant = Sant::find($santID);
$properties = [
'attributes' => [
'vihar_information' => $vihar->toArray(),
'message' => 'Vihaar added by '.$user->name.' on '.Carbon::now()->format('d/m/Y'),
'sant_id' => $santID,
],
];
logActivity($sant, loggedInUser(), $properties, 'vihar_created');
//Push Notification to followers of sant
dispatch(new SendNewVihaar($sant, $user));
}
if (!empty($vihar)) {
loggedInUser()->addKarmaPoints($vihar, loggedInUser()->id, config('config-variables.karma_points_message.add_vihar'), config('config-variables.karma_points.add_vihar'), config('config-variables.karma_points_key.add_vihar'), []);
$response['data'] = $vihar->load(['createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.store');
unset($response['error']);
}
} else {
$vihar = $this->vihar->query()->create([
'from' => $from,
'to' => $to,
'from_sangh_id' => $viharData['from_sangh_id'] ?? Constant::NULL,
'from_latitude' => $fromLatitude,
'from_longitude' => $fromLongitude,
'to_sangh_id' => $viharData['to_sangh_id'] ?? Constant::NULL,
'to_latitude' => $toLatitude,
'to_longitude' => $toLongitude,
'start_date' => $viharData['start_date'],
'start_time' => $viharData['start_time'],
'end_time' => '08:00:00',
'sant_id' => (int)$viharData['sant_id'],
'end_date' => $viharData['end_date'] ?? Constant::NULL,
'is_approved' => Constant::STATUS_ZERO,
'created_by' => loggedInUser()->id,
'updated_by' => Constant::NULL,
'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
]);
$sant = Sant::find((int)$viharData['sant_id']);
$properties = [
'attributes' => [
'vihar_information' => $vihar->toArray(),
'message' => 'Vihaar added by '.$user->name.' on '.Carbon::now()->format('d/m/Y'),
'sant_id' => $viharData['sant_id'],
],
];
logActivity($sant, loggedInUser(), $properties, 'vihar_created');
//Push Notification to followers of sant
dispatch(new SendNewVihaar($sant, $user));
if (!empty($vihar)) {
loggedInUser()->addKarmaPoints($vihar, loggedInUser()->id, config('config-variables.karma_points_message.add_vihar'), config('config-variables.karma_points.add_vihar'), config('config-variables.karma_points_key.add_vihar'), []);
$response['data'] = $vihar->load(['createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.store');
unset($response['error']);
}
}
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
*
*/
public function updateVihar(array $requestData, object $vihar)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
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->mobile)) {
$response['error'] = trans('api.mobile');
} else {
$from = $requestData['from'];
$to = $requestData['to'];
$fromLatitude = $requestData['from_latitude'] ?? Constant::NULL;
$fromLongitude = $requestData['from_longitude'] ?? Constant::NULL;
$toLatitude = $requestData['to_latitude'] ?? Constant::NULL;
$toLongitude = $requestData['to_longitude'] ?? Constant::NULL;
if (isset($requestData['from_sangh_id']) && !empty($requestData['from_sangh_id'])) {
$fromSangh = Sangh::where('id', $requestData['from_sangh_id'])->first();
$from = $fromSangh->name;
$fromLatitude = $fromSangh->latitude ?? Constant::NULL;
$fromLongitude = $fromSangh->longitude ?? Constant::NULL;
}
if (isset($requestData['to_sangh_id']) && !empty($requestData['to_sangh_id'])) {
$toSangh = Sangh::where('id', $requestData['to_sangh_id'])->first();
$to = $toSangh->name;
$toLatitude = $toSangh->latitude ?? Constant::NULL;
$toLongitude = $toSangh->longitude ?? Constant::NULL;
}
if (!empty($viharID)) {
foreach ($viharID as $id) {
$viharDetails = $this->vihar->find($id);
$vihar = $this->vihar->query()->where('id', $id)->update(
[
'from' => $from ?? $viharDetails->from,
'to' => $to ?? $viharDetails->to,
'from_sangh_id' => $requestData['from_sangh_id'] ?? Constant::NULL,
'from_latitude' => $fromLatitude ?? $viharDetails->from_latitude,
'from_longitude' => $fromLongitude ?? $viharDetails->from_longitude,
'to_sangh_id' => $requestData['to_sangh_id'] ?? Constant::NULL,
'to_latitude' => $toLatitude ?? $viharDetails->to_latitude,
'to_longitude' => $toLongitude ?? $viharDetails->to_longitude,
'start_date' => $requestData['start_date'] ?? $viharDetails->start_date,
'start_time' => $requestData['start_time'] ?? $viharDetails->start_time,
'end_date' => $requestData['end_date'] ?? Constant::NULL,
'sant_id' => $viharDetails->sant_id,
'thana_sant_id' => $requestData['thana_sant_id'] ?? $viharDetails->thana_sant_id,
'updated_by' => loggedInUser()->id,
]
);
// $vihar = Vihar::find($viharData->id);
$sant = Sant::find($viharDetails->sant_id);
$properties = [
'attributes' => [
'vihar_information' => $viharDetails->toArray(),
'message' => 'Vihaar updated by '.$user->name.' on '.Carbon::now()->format('d/m/Y'),
'sant_id' => $viharDetails->sant_id,
],
];
logActivity($sant, loggedInUser(), $properties, 'vihar_updated');
//Push Notification to followers of sant
dispatch(new SendUpdateVihaar($sant, $user, $vihar));
$response['data'] = $viharDetails->load(['createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.update');
unset($response['error']);
}
} else {
$vihar = $this->vihar->query()->where('id', $vihar->id)->update(
[
'from' => $from ?? $viharData->from,
'to' => $to ?? $viharData->to,
'from_sangh_id' => $requestData['from_sangh_id'] ?? Constant::NULL,
'from_latitude' => $fromLatitude ?? $viharData->from_latitude,
'from_longitude' => $fromLongitude ?? $viharData->from_longitude,
'to_sangh_id' => $requestData['to_sangh_id'] ?? Constant::NULL,
'to_latitude' => $toLatitude ?? $viharData->to_latitude,
'to_longitude' => $toLongitude ?? $viharData->to_longitude,
'start_date' => $requestData['start_date'] ?? $viharData->start_date,
'start_time' => $requestData['start_time'] ?? $viharData->start_time,
'end_date' => $requestData['end_date'] ?? Constant::NULL,
'sant_id' => $viharData->sant_id,
'thana_sant_id' => $requestData['thana_sant_id'] ?? $viharData->thana_sant_id,
'updated_by' => loggedInUser()->id,
]
);
$vihar = Vihar::find($viharData->id);
$sant = Sant::find($vihar->sant_id);
$properties = [
'attributes' => [
'vihar_information' => $vihar->toArray(),
'message' => 'Vihaar updated by '.$user->name.' on '.Carbon::now()->format('d/m/Y'),
'sant_id' => $vihar->sant_id,
],
];
logActivity($sant, loggedInUser(), $properties, 'vihar_updated');
//Push Notification to followers of sant
dispatch(new SendUpdateVihaar($sant, $user, $vihar));
$response['data'] = $vihar->load(['createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.update');
unset($response['error']);
}
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get info of vihar
*/
public function showVihar(object $vihar)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = $vihar->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* @param $data
* @return array
*/
public function destroyVihar(object $vihar)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
$points = $vihar->addPoints()->where('pointable_id', $vihar->id)->first();
$viharDetails = Vihar::where('id', $vihar->id)->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 = Vihar::where('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 {
$vihar = $vihar->delete();
}
if (!empty($viharDetails)) {
foreach ($viharDetails as $vihars) {
//Delete Vihaar Activity
$vihars->activity()->whereJsonContains('properties->attributes->vihar_information->id', $vihars->id)->delete();
}
}
if ($points) {
// Remove karma points of vihar
$viharPoint = $points->points;
$points->delete();
$user->decrement('karma_dhan', $viharPoint);
}
if (!empty($vihar)) {
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.vihar.destroy');
unset($response['error']);
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
}