| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Requests\CustomScheduleRequest;
- use App\Http\Requests\CustomScheduleProposeRequest;
- use App\Http\Requests\CustomScheduleRefuseOpportunityRequest;
- use App\Http\Requests\CustomScheduleVerifyCodeRequest;
- use App\Http\Resources\CustomScheduleResource;
- use App\Services\CustomScheduleService;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\Log;
- class CustomScheduleController extends Controller
- {
- protected $customScheduleService;
- public function __construct(CustomScheduleService $customScheduleService)
- {
- $this->customScheduleService = $customScheduleService;
- }
- public function index(): JsonResponse
- {
- $customSchedules = $this->customScheduleService->getAll();
- return $this->successResponse(
- CustomScheduleResource::collection($customSchedules),
- );
- }
- public function store(CustomScheduleRequest $request): JsonResponse
- {
- try {
- $validated = $request->validated();
- $customSchedules = $this->customScheduleService->create($validated);
- $count = count($customSchedules);
- $message = $count > 1
- ? "{$count} oportunidades criadas com sucesso!"
- : __("messages.created");
- return $this->successResponse(
- payload: CustomScheduleResource::collection($customSchedules),
- message: $message,
- code: 201,
- );
- } catch (\Exception $e) {
- return $this->errorResponse($e->getMessage(), 422);
- }
- }
- public function show(string $id): JsonResponse
- {
- $customSchedule = $this->customScheduleService->getById($id);
- return $this->successResponse(
- new CustomScheduleResource($customSchedule),
- );
- }
- public function update(CustomScheduleRequest $request, string $id): JsonResponse
- {
- try {
- $customSchedule = $this->customScheduleService->update($id, $request->validated());
- return $this->successResponse(
- payload: new CustomScheduleResource($customSchedule),
- message: __("messages.updated"),
- );
- } catch (\Exception $e) {
- return $this->errorResponse($e->getMessage(), 422);
- }
- }
- public function destroy(string $id): JsonResponse
- {
- try {
- $this->customScheduleService->delete($id);
- return $this->successResponse(
- message: __("messages.deleted"),
- );
- } catch (\Exception $e) {
- return $this->errorResponse($e->getMessage(), 422);
- }
- }
- public function groupedByClientCustom(): JsonResponse
- {
- $grouped = $this->customScheduleService->getSchedulesCustomGroupedByClient();
- return $this->successResponse($grouped);
- }
- public function available(CustomScheduleRequest $request)
- {
- try {
- $providerId = $request->query('provider_id');
- if (!$providerId) {
- return $this->errorResponse('Provider ID é obrigatório', 400);
- }
- $opportunities = $this->customScheduleService->getAvailableOpportunities($providerId);
- return $this->successResponse($opportunities);
- } catch (\Exception $e) {
- Log::error('Error fetching available opportunities: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 500);
- }
- }
- public function providerProposals(CustomScheduleRequest $request)
- {
- try {
- $providerId = $request->query('provider_id');
- if (!$providerId) {
- return $this->errorResponse('Provider ID é obrigatório', 400);
- }
- $proposals = $this->customScheduleService->getProviderProposals($providerId);
- return $this->successResponse($proposals);
- } catch (\Exception $e) {
- Log::error('Error fetching provider proposals: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 500);
- }
- }
- public function opportunityProposals($scheduleId)
- {
- try {
- $proposals = $this->customScheduleService->getOpportunityProposals($scheduleId);
- return $this->successResponse($proposals);
- } catch (\Exception $e) {
- Log::error('Error fetching opportunity proposals: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 500);
- }
- }
- public function propose(CustomScheduleProposeRequest $request, $scheduleId)
- {
- try {
- $providerId = $request->input('provider_id');
- if (!$providerId) {
- return $this->errorResponse('Provider ID é obrigatório', 400);
- }
- $proposal = $this->customScheduleService->proposeOpportunity($scheduleId, $providerId);
- return $this->successResponse($proposal, 'Proposta enviada com sucesso!', 201);
- } catch (\Exception $e) {
- Log::error('Error creating proposal: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 400);
- }
- }
- public function acceptProposal($proposalId)
- {
- try {
- $schedule = $this->customScheduleService->acceptProposal($proposalId);
- return $this->successResponse($schedule, 'Prestador aceito com sucesso!');
- } catch (\Exception $e) {
- Log::error('Error accepting proposal: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 400);
- }
- }
- public function refuseProposal($proposalId)
- {
- try {
- $this->customScheduleService->refuseProposal($proposalId);
- return $this->successResponse(null, 'Prestador recusado.');
- } catch (\Exception $e) {
- Log::error('Error refusing proposal: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 400);
- }
- }
- public function getProvidersProposalsAndOpportunities($providerId)
- {
- try {
- $data = $this->customScheduleService->getProvidersProposalsAndOpportunities($providerId);
- return $this->successResponse($data);
- } catch (\Exception $e) {
- Log::error('Error fetching provider proposals and opportunities: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 500);
- }
- }
- public function verifyCode($scheduleId, CustomScheduleVerifyCodeRequest $request)
- {
- try {
- $result = $this->customScheduleService->verifyScheduleCode($scheduleId, $request->input('code'));
- return $this->successResponse($result, 'Código verificado com sucesso!');
- } catch (\Exception $e) {
- Log::error('Error verifying schedule code: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 500);
- }
- }
- public function refuseOpportunity($scheduleId, CustomScheduleRefuseOpportunityRequest $request)
- {
- try {
- $this->customScheduleService->refuseOpportunity($scheduleId, $request->input('provider_id'));
- return $this->successResponse(null, 'Oportunidade recusada.');
- } catch (\Exception $e) {
- Log::error('Error refusing opportunity: ' . $e->getMessage());
- return $this->errorResponse($e->getMessage(), 400);
- }
- }
- }
|