| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Http/Controllers/WebService/ |
Upload File : |
<?php
namespace App\Http\Controllers\WebService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Exception;
use App\Actions\WebService\UserActions\JobAction;
use App\Helpers\RazorpayHelper;
use App\Http\Requests\WebService\JobRequests;
use App\Models\JobRequest;
use App\Models\JobSeekerSubscription;
use App\Models\Notification;
use App\Models\PostJobs;
use App\Models\Transaction;
use App\Models\User;
use App\Models\UserSeekerSubscription;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid;
class JobPostController extends Controller
{
public function GetList(Request $request)
{
try {
$list = PostJobs::GetJobs($request);
return $this->sendSuccess(trans('messages.OK'), $list);
} catch (Exception $th) {
return $this->sendError($th->getMessage());
}
}
public function GetOPList(Request $request, $jobId)
{
try {
$list = JobRequest::GetInterestedUsers($request, $jobId);
return $this->sendSuccess(trans('messages.OK'), $list);
} catch (Exception $th) {
return $this->sendError($th->getMessage());
}
}
public function GetInfo($jobId)
{
try {
$job = PostJobs::withCount('requests')->where('uuid', $jobId)->first();
if ($job) {
$userId = USER_ID();
$isRequested = JobRequest::where('job_id', $job->id)
->where('user_id', $userId)
->exists();
$job->is_requested = $isRequested;
return $this->sendSuccess(trans('messages.OK'), $job);
}
return $this->sendError(trans('messages.RECORD_NOT_FOUND'));
} catch (Exception $th) {
return $this->sendError($th->getMessage());
}
}
public function setStatus($jobId)
{
try {
$job = PostJobs::where('uuid', $jobId)->first();
$job->status = 'CLOSED';
$job->save();
return $this->sendSuccess(trans('messages.JOB_CLOSED_SUCCESS'));
} catch (Exception $th) {
return $this->sendError($th->getMessage());
}
}
public function postJobs(Request $request)
{
try {
$list = PostJobs::GetPostJobs($request);
return $this->sendSuccess(trans('messages.OK'), $list);
} catch (Exception $th) {
return $this->sendError($th->getMessage());
}
}
public function SetInfo(JobRequests $request, JobAction $action, $jobId = null)
{
try {
$response = $action->execute($request, $jobId);
return response()->json($response);
} catch (Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
}
}
public function SetRequestInfo($id)
{
$userId = USER_ID();
try {
$user = Auth::guard('api')->user();
// 1) Job must exist
$job = PostJobs::where('uuid', $id)->first();
if (!$job) {
return $this->sendError(trans('messages.RECORD_NOT_FOUND'));
}
// 2) Prevent self-request (optional but common)
if ((int)$job->user_id === (int)$userId) {
return $this->sendError(trans('messages.CANNOT_REQUEST_OWN_JOB'));
}
// 3) Already requested?
$already = JobRequest::where('job_id', $job->id)
->where('user_id', $userId)
->exists();
if ($already) {
return $this->sendError(trans('messages.JOB_REQUEST_ALREADY_SENT'));
}
// 4) Active subscription check
$today = now()->toDateString();
$subscription = UserSeekerSubscription::where('user_id', $userId)
->where('status', 'ACTIVE')
->whereDate('start_date', '<=', $today)
->whereDate('end_date', '>=', $today)
->first();
// If there is no active subscription OR remain <= 0 → prepare payment/upgrade
if (!$subscription || (int)$subscription->remain <= 0) {
$seekerSub = JobSeekerSubscription::first(); // pick appropriate product/plan for purchase
if (!$seekerSub) {
return $this->sendError(trans('messages.SUBSCRIPTION_NOT_AVAILABLE'));
}
$trx = DB::transaction(function () use ($job, $seekerSub) {
return app(\App\Services\TransactionService::class)->generate([
'type' => 'SEEKER',
'trx_type' => 'SEEKER',
'job_id' => $job->id,
'subscription' => $seekerSub,
]);
});
return response()->json([
'status' => false,
'message' => $subscription
? trans('messages.JOB_INTEREST_LIMIT_EXCEEDED') // had plan but 0 remain
: trans('messages.SUBSCRIPTION_REQUIRED'), // no active plan
'trx_id' => $trx->uuid,
'order_id' => $trx->trx_id,
'amount' => $trx->grand_total,
'taxes' => $trx->tax_amount,
'tax_percent' => $trx->taxes,
'currency' => $trx->currency,
'razorpay_key' => config('services.razorpay.key'),
'is_ended' => $subscription ? true : false,
]);
}
// 5) Consume 1 remain and create JobRequest atomically
DB::beginTransaction();
// Re-check duplicate within TX (race safety)
$dupInTx = JobRequest::where('job_id', $job->id)
->where('user_id', $userId)
->lockForUpdate()
->exists();
if ($dupInTx) {
DB::rollBack();
return $this->sendError(trans('messages.JOB_REQUEST_ALREADY_SENT'));
}
// Conditional decrement (only if remain > 0 and still active today)
$affected = UserSeekerSubscription::where('id', $subscription->id)
->where('status', 'ACTIVE')
->whereDate('start_date', '<=', $today)
->whereDate('end_date', '>=', $today)
->where('remain', '>', 0)
->decrement('remain');
if ($affected === 0) {
DB::rollBack();
return $this->sendError(
trans('messages.JOB_INTEREST_LIMIT_EXCEEDED'),
[
'limit' => (int)($subscription->limit ?? 0),
'is_ended' => true
]
);
}
JobRequest::create([
'job_id' => $job->id,
'user_id' => $userId,
]);
DB::commit();
// 6) Notify employer (outside TX)
Notification::SetNotification(
$job->user_id,
'Interested In Job',
"User {$user->name} is interested in the job {$job->job_name}",
'USER_VIEW',
$job->id,
'1'
);
return $this->sendSuccess(trans('messages.JOB_REQUEST_SUCCESS'));
} catch (\Throwable $th) {
if (DB::transactionLevel() > 0) {
DB::rollBack();
}
return $this->sendError($th->getMessage());
}
}
public function SetPublishInfo($id)
{
try {
$user = User::find(USER_ID());
if (!$user->hasActiveSubscription()) {
return $this->sendError(trans('messages.NO_SUBSCRIPTION'), 403);
}
$job = PostJobs::where('uuid', $id)->firstOrFail();
$job->is_publish = true;
$job->status = 'OPEN';
$job->save();
$users = User::GetNearbyOperatorsForJobs($job->id);
foreach ($users as $user) {
Notification::SetNotification(
$user->id,
"New Job Posted",
"New Job For {$job->job_name}",
"JOB_POST_VIEW",
$job->uuid,
"1",
);
}
return $this->sendSuccess(trans('messages.JOB_PUBLISHED_SUCCESS'));
} catch (Exception $th) {
return $this->sendError($th->getMessage());
}
}
public function getRequestInfo()
{
try {
$subs = JobSeekerSubscription::first();
return $this->sendSuccess(trans('messages.OK'), $subs);
} catch (\Throwable $th) {
return $this->sendError($th->getMessage());
}
}
private function generateTransaction($jobId)
{
try {
$trx = app(\App\Services\TransactionService::class)->generate([
'trx_type' => 'SEEKER',
'type' => 'SEEKER',
'job_id' => $jobId,
'subscription' => JobSeekerSubscription::first(),
]);
return $trx;
} catch (Exception $e) {
Log::error('Error creating Razorpay Trans: ' . $e->getMessage());
throw $e;
}
}
}