%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/workeasy-api/app/Http/Controllers/Api/
Upload File :
Create Path :
Current File : /var/www/html/workeasy-api/app/Http/Controllers/Api/InterviewsController.php

<?php

namespace Workeasy\Http\Controllers\Api;


use EllipseSynergie\ApiResponse\Contracts\Response;
use Illuminate\Support\Facades\DB;
use Workeasy\Http\Requests\Interviews\RateRequest;
use Workeasy\Models\Candidate;
use Workeasy\Models\Company;
use Workeasy\Models\Interview;
use Workeasy\Models\Offer;
use Workeasy\Transformers\InterviewTransformer;

class InterviewsController extends ApiController
{
    private $transformer;

    public function __construct(Response $response, InterviewTransformer $transformer)
    {
        parent::__construct($response);
        $this->transformer = $transformer;
    }

    public function index($offer)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        $interviews = $offer->interviews()->where('status', Interview::PENDING)->get();
        return $this->response->withCollection($interviews, $this->transformer);
    }

    public function show($offer, $interview)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        $interview = $offer->interviews()->findOrFail($interview);
        return $this->response->withItem($interview, $this->transformer);
    }

    public function acceptInterview($offer, $candidate)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        /** @var Interview $interview */
        $interview = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer->id]])->first();
        if ($interview) {
            $interview->status = Interview::ACCEPTED;
            $interview->save();
        }
        return $this->response->withItem($interview->fresh(), $this->transformer);
    }

    public function rejectInterview($offer, $candidate)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        /** @var Interview $interview */
        $interview = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer->id]])->first();
        if ($interview) {
            $interview->status = Interview::REJECTED;
            $interview->save();
        }
        return $this->response->withItem($interview->fresh(), $this->transformer);
    }

    public function hireCandidate($offer, $candidate)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        /** @var Interview $interview */
        $interview = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer->id]])->first();
        if ($interview) {
            if ($interview->status === Interview::ACCEPTED) {
                $interview->status = Interview::HIRED;
                $interview->save();
            } else {
                return $this->response->errorInternalError('This candidate can not be hired');
            }
        } else {
            return $this->response->errorNotFound('Interview not found');
        }
        return $this->response->withItem($interview->fresh(), $this->transformer);
    }

    public function rejectCandidate($offer, $candidate)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        /** @var Interview $interview */
        $interview = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer->id]])->first();
        if ($interview) {
            if ($interview->status === Interview::ACCEPTED) {
                $interview->status = Interview::NOT_HIRED;
                $interview->save();
            } else {
                return $this->response->errorInternalError('This candidate can not be rejected');
            }
        } else {
            return $this->response->errorNotFound('Interview not found');
        }
        return $this->response->withItem($interview->fresh(), $this->transformer);
    }

    public function fireCandidate($offer, $candidate)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        /** @var Interview $interview */
        $interview = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer->id]])->first();
        if ($interview) {
            if ($interview->status === Interview::HIRED) {
                $interview->status = Interview::FIRED;
                $interview->save();
            } else {
                return $this->response->errorInternalError('This candidate can not be fired');
            }
        } else {
            return $this->response->errorNotFound('Interview not found');
        }
        return $this->response->withItem($interview->fresh(), $this->transformer);
    }

    public function store($offer, $candidate)
    {
        DB::beginTransaction();
        try {
            /** @var Company $company */
            $company = $this->getUserCompany();
            $full_offer = $company->offers()->findOrFail($offer);
            $full_candidate = Candidate::query()->findOrFail($candidate);
            /** @var Interview $interview */
            $interview = Interview::query()->make([]);
            /** @var Interview $interview_check */
            $interview_check = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer]])->first();
            if (!$interview_check) {
                $interview->offer()->associate($full_offer);
                $interview->candidate()->associate($full_candidate);
                $interview->save();
                DB::commit();
                return $this->response->withItem($interview, new InterviewTransformer);
            } else {
                return $this->response->errorInternalError('This interview already exists');
            }
        } catch (\Exception $exception) {
            DB::rollback();
            return $this->response->errorInternalError($exception->getMessage());
        }
    }

    /**
     * @param RateRequest $request
     * @param             $offer
     * @param             $candidate
     *
     * @return mixed
     */
    public function rate(RateRequest $request, $offer, $candidate)
    {
        /** @var Offer $offer */
        $offer = $this->getUserCompany()->offers()->findOrFail($offer);
        /** @var Interview $interview */
        $interview = Interview::query()->where([['candidate_id', $candidate], ['offer_id', $offer->id]])->first();
        if (!$interview) {
            return $this->response->errorNotFound('Interview not found');
        }
        if ($interview->status === Interview::PENDING) {
            return $this->response->errorForbidden('Can not rate an interview in pending status');
        }
        $feedback = $interview->feedback;
        if (!$feedback) {
            $feedback = $interview->feedback()->make();
        }
        $feedback->rating = $request->get('rating');
        $feedback->save();
        return $this->response->withItem($interview->fresh(), $this->transformer);
    }

    public function acceptedInterviews($offer)
    {
        /** @var Company $company */
        $company = $this->getUserCompany();
        /** @var Offer $offer */
        $offer = $company->offers()->findOrFail($offer);
        $interviews = $offer->interviews()->where('status', Interview::ACCEPTED)->get();
        return $this->response->withCollection($interviews, $this->transformer);
    }

    /**
     * @return mixed
     */
    public function getFeedback()
    {
        /** @var Company $company */
        $company = $this->getUserCompany();
        $interviews = Interview::query()->whereHas('offer', function ($query) use ($company) {
            $query->where('company_id', $company->id);
        })->whereDoesntHave('feedback')->whereIn('status', [Interview::HIRED, Interview::NOT_HIRED])->get();
        return $this->response->withCollection($interviews, $this->transformer);
    }
}

Zerion Mini Shell 1.0