Files
Global-Jain/app/Repositories/Api/Access/Chaturmas/ChaturmasRepository.php

384 lines
16 KiB
PHP
Raw Normal View History

2025-11-05 10:37:10 +05:30
<?php
namespace App\Repositories\Api\Access\Chaturmas;
use Exception;
use Carbon\Carbon;
use App\Models\Sant;
use App\Models\Chaturmas;
use App\Constant\Constant;
use App\Models\ThanaMember;
use App\Models\ChaturmasDate;
use App\Models\UserDeviceToken;
use Illuminate\Support\Facades\Log;
use App\Traits\PushNotificationTraits;
use App\Jobs\Notifications\Chaturmas\SendNewChaturmas;
class ChaturmasRepository implements ChaturmasInterface
{
use PushNotificationTraits;
/**
* @var Chaturmas
*/
protected $chaturmas;
/**
* @var ChaturmasDate
*/
protected $chaturmasDate;
/**
* @var UserDeviceToken
*/
protected $userDeviceToken;
/**
* @param Chaturmas $chaturmas
* @param ChaturmasDate $chaturmasDate
* @param UserDeviceToken $userDeviceToken
* ChaturmasRepository constructor.
*
*/
public function __construct(Chaturmas $chaturmas, ChaturmasDate $chaturmasDate, UserDeviceToken $userDeviceToken)
{
$this->chaturmas = $chaturmas;
$this->chaturmasDate = $chaturmasDate;
$this->userDeviceToken = $userDeviceToken;
}
/**
*
*/
public function getChaturmasList(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = [];
$allChaturmasObject = $this->chaturmas->query()
->join('chaturmas_dates', 'chaturmas_dates.id', '=', 'chaturmas.chaturmas_date_id')
->select('chaturmas.*')
// ->whereHas('chaturmasThana', function ($query) use ($data) {
// $query->where('sant_id', $data['sant_id']);
// })
->with(['sangh', 'chaturmasThana:id,name,avatar', 'chaturmasInfo', 'createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])
->where('sant_id', $data['sant_id']);
$allChaturmasObject = $allChaturmasObject
->orderBy('chaturmas_dates.year', 'DESC')
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $allChaturmasObject;
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
*
*/
public function storeChaturmas(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
if (empty($user->mobile)) {
$response['error'] = trans('api.mobile');
} else {
// ->orWhereHas('chaturmasThana', function ($query) use ($data) {
// $query->where('sant_id', $data['sant_id']);
// })
$chaturmasExistForYear = $this->chaturmas->query()->where('sant_id', $data['sant_id'])->where('chaturmas_date_id', $data['year'])->first();
$response['status'] = Constant::CODE_403;
$response['success'] = Constant::STATUS_FALSE;
$response['error'] = trans('api.chaturmas.exist');
if (empty($chaturmasExistForYear)) {
$thanaID = ThanaMember::where('sant_id', $data['sant_id'])->value('thana_id');
$thanaSantID = ThanaMember::where('thana_id', $thanaID)->pluck('sant_id');
if (!empty($thanaSantID) && count($thanaSantID) > 0) {
foreach ($thanaSantID as $santID) {
$chaturmas = $this->chaturmas->query()->create(
[
'place' => $data['place'] ?? Constant::NULL,
'latitude' => $data['latitude'] ?? Constant::NULL,
'longitude' => $data['longitude'] ?? Constant::NULL,
'chaturmas_date_id' => $data['year'],
'is_approved' => Constant::STATUS_ZERO,
'thana_sant_id' => $thanaID,
'sant_id' => $santID,
'sangh_id' => $data['sangh_id'] ?? Constant::NULL,
'created_by' => loggedInUser()->id,
'updated_by' => Constant::NULL,
'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
]
)->load('sangh', 'chaturmasInfo', 'createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code');
$sant = Sant::find($santID);
if (isset($data['place']) && !empty($data['place'])) {
$properties = [
'attributes' => [
'chaturmas_information' => $chaturmas->toArray(),
'message' => 'Chaturmas added by '.$user->name.' Location is '.$data['place'],
'sant_id' => $santID,
],
];
} else {
$properties = [
'attributes' => [
'chaturmas_information' => $chaturmas->toArray(),
'message' => 'Chaturmas added by '.$user->name,
'sant_id' => $santID,
],
];
}
logActivity($sant, loggedInUser(), $properties, 'chaturmas_created');
//Push Notification to followers of sant
// dispatch(new SendNewChaturmas($sant, $user));
}
if (!empty($chaturmas)) {
$response['data'] = $chaturmas->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.store');
unset($response['error']);
}
} else {
$chaturmas = $this->chaturmas->query()->create(
[
'place' => $data['place'] ?? Constant::NULL,
'latitude' => $data['latitude'] ?? Constant::NULL,
'longitude' => $data['longitude'] ?? Constant::NULL,
'chaturmas_date_id' => $data['year'],
'is_approved' => Constant::STATUS_ZERO,
'thana_sant_id' => Constant::NULL,
'sant_id' => $data['sant_id'],
'sangh_id' => $data['sangh_id'] ?? Constant::NULL,
'created_by' => loggedInUser()->id,
'updated_by' => Constant::NULL,
'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
]
)->load('sangh', 'chaturmasInfo', 'createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code');
$sant = Sant::find($data['sant_id']);
//Push Notification to followers of sant
// dispatch(new SendNewChaturmas($sant, $user));
if (isset($data['place']) && !empty($data['place'])) {
$properties = [
'attributes' => [
'chaturmas_information' => $chaturmas->toArray(),
'message' => 'Chaturmas added by '.$user->name.' Location is '.$data['place'],
'sant_id' => $data['sant_id'],
],
];
} else {
$properties = [
'attributes' => [
'chaturmas_information' => $chaturmas->toArray(),
'message' => 'Chaturmas added by '.$user->name,
'sant_id' => $data['sant_id'],
],
];
}
logActivity($sant, loggedInUser(), $properties, 'chaturmas_created');
if (!empty($chaturmas)) {
$response['data'] = $chaturmas->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.store');
unset($response['error']);
}
}
}
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
*
*/
public function updateChaturmas(array $data, object $chaturmas)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
if (empty($user->mobile)) {
$response['error'] = trans('api.mobile');
} else {
// ->orWhereHas('chaturmasThana', function ($query) use ($data) {
// $query->where('sant_id', $data['sant_id']);
// })
$chaturmasExistForYear = $this->chaturmas->query()->with(['createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->where('id', '!=', $chaturmas->id)->where('sant_id', $chaturmas->sant_id)->where('chaturmas_date_id', $data['year'])->first();
$response['status'] = Constant::CODE_403;
$response['success'] = Constant::STATUS_FALSE;
$response['error'] = trans('api.chaturmas.exist');
if (empty($chaturmasExistForYear)) {
// $thanaSantID = $this->chaturmas->thana_sant_id;
// dd($thanaSantID);
// $thanaID = ThanaMember::where('sant_id', $data['sant_id'])->value('thana_id');
$id = $chaturmas->id;
$chaturmas = $chaturmas->update(
[
'place' => $data['place'] ?? Constant::NULL,
'latitude' => $data['latitude'] ?? Constant::NULL,
'longitude' => $data['longitude'] ?? Constant::NULL,
'sangh_id' => $data['sangh_id'] ?? Constant::NULL,
'chaturmas_date_id' => $data['year'],
// 'sant_id' => $data['sant_id'],
'updated_by' => loggedInUser()->id,
]
);
if (!empty($chaturmas)) {
$chaturmas = $this->chaturmas->find($id)->load('sangh', 'chaturmasInfo', 'createdBy:id,name,mobile,country_code', 'updatedBy:id,name,mobile,country_code');
$sant = Sant::find($this->chaturmas->find($id)->sant_id);
$properties = [
'attributes' => [
'chaturmas_information' => $this->chaturmas->find($id)->toArray(),
'message' => 'Chaturmas updated by '.$user->name.' Location is '.$data['place'],
'sant_id' => $this->chaturmas->find($id)->sant_id,
],
];
logActivity($sant, loggedInUser(), $properties, 'chaturmas_updated');
$response['data'] = $chaturmas->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.update');
unset($response['error']);
}
}
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get list of chaturmas years
*/
public function getChaturmasYears()
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = [];
$now = Carbon::now()->subYear();
$year = $now->year;
$chaturmasYears = $this->chaturmasDate->query()->where('year', '>=', $year)->select('year', 'id')->orderBy('year', 'DESC')->get()->toArray();
$response['data'] = collect($chaturmasYears)->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get list of past 100 years from now
*/
public function getHundredChaturmasYears()
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = [];
$data = array();
for ($i = 100; $i >= 0; $i--) {
$year = Carbon::now()->subYear($i)->format('Y');
array_push($data, array(
'id' => $year,
'value' => $year
));
}
$response['data'] = collect($data)->sortByDesc('value')->values();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get info of chaturmas years
*/
public function showChaturmas(object $chaturmas)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = $chaturmas->load('chaturmasInfo')->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
}