CustomScheduleController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\CustomScheduleAvailableRequest;
  4. use App\Http\Requests\CustomScheduleRequest;
  5. use App\Http\Requests\CustomScheduleProposeRequest;
  6. use App\Http\Requests\CustomScheduleRefuseOpportunityRequest;
  7. use App\Http\Requests\CustomScheduleVerifyCodeRequest;
  8. use App\Http\Resources\CustomScheduleResource;
  9. use App\Models\Client;
  10. use App\Services\CustomScheduleService;
  11. use Auth;
  12. use Illuminate\Http\JsonResponse;
  13. use Illuminate\Support\Facades\Log;
  14. class CustomScheduleController extends Controller
  15. {
  16. protected $customScheduleService;
  17. public function __construct(CustomScheduleService $customScheduleService)
  18. {
  19. $this->customScheduleService = $customScheduleService;
  20. }
  21. public function index(): JsonResponse
  22. {
  23. $customSchedules = $this->customScheduleService->getAll();
  24. return $this->successResponse(
  25. CustomScheduleResource::collection($customSchedules),
  26. );
  27. }
  28. public function store(CustomScheduleRequest $request): JsonResponse
  29. {
  30. try {
  31. $validated = $request->validated();
  32. $customSchedules = $this->customScheduleService->create($validated);
  33. $count = count($customSchedules);
  34. $message = $count > 1
  35. ? "{$count} oportunidades criadas com sucesso!"
  36. : __("messages.created");
  37. return $this->successResponse(
  38. payload: CustomScheduleResource::collection($customSchedules),
  39. message: $message,
  40. code: 201,
  41. );
  42. } catch (\Exception $e) {
  43. return $this->errorResponse($e->getMessage(), 422);
  44. }
  45. }
  46. public function show(string $id): JsonResponse
  47. {
  48. $customSchedule = $this->customScheduleService->getById($id);
  49. return $this->successResponse(
  50. new CustomScheduleResource($customSchedule),
  51. );
  52. }
  53. public function update(CustomScheduleRequest $request, string $id): JsonResponse
  54. {
  55. try {
  56. $customSchedule = $this->customScheduleService->update($id, $request->validated());
  57. return $this->successResponse(
  58. payload: new CustomScheduleResource($customSchedule),
  59. message: __("messages.updated"),
  60. );
  61. } catch (\Exception $e) {
  62. return $this->errorResponse($e->getMessage(), 422);
  63. }
  64. }
  65. public function destroy(string $id): JsonResponse
  66. {
  67. try {
  68. $this->customScheduleService->delete($id);
  69. return $this->successResponse(
  70. message: __("messages.deleted"),
  71. );
  72. } catch (\Exception $e) {
  73. return $this->errorResponse($e->getMessage(), 422);
  74. }
  75. }
  76. public function groupedByClientCustom(): JsonResponse
  77. {
  78. $grouped = $this->customScheduleService->getSchedulesCustomGroupedByClient();
  79. return $this->successResponse($grouped);
  80. }
  81. public function available(CustomScheduleAvailableRequest $request)
  82. {
  83. try {
  84. $providerId = $request->query('provider_id');
  85. if (!$providerId) {
  86. return $this->errorResponse('Provider ID é obrigatório', 400);
  87. }
  88. $opportunities = $this->customScheduleService->getAvailableOpportunities($providerId);
  89. return $this->successResponse($opportunities);
  90. } catch (\Exception $e) {
  91. Log::error('Error fetching available opportunities: ' . $e->getMessage());
  92. return $this->errorResponse($e->getMessage(), 500);
  93. }
  94. }
  95. public function providerProposals(CustomScheduleRequest $request)
  96. {
  97. try {
  98. $providerId = $request->query('provider_id');
  99. if (!$providerId) {
  100. return $this->errorResponse('Provider ID é obrigatório', 400);
  101. }
  102. $proposals = $this->customScheduleService->getProviderProposals($providerId);
  103. return $this->successResponse($proposals);
  104. } catch (\Exception $e) {
  105. Log::error('Error fetching provider proposals: ' . $e->getMessage());
  106. return $this->errorResponse($e->getMessage(), 500);
  107. }
  108. }
  109. public function opportunityProposals($scheduleId)
  110. {
  111. try {
  112. $proposals = $this->customScheduleService->getOpportunityProposals($scheduleId);
  113. return $this->successResponse($proposals);
  114. } catch (\Exception $e) {
  115. Log::error('Error fetching opportunity proposals: ' . $e->getMessage());
  116. return $this->errorResponse($e->getMessage(), 500);
  117. }
  118. }
  119. public function propose(CustomScheduleProposeRequest $request, $scheduleId)
  120. {
  121. try {
  122. $providerId = $request->input('provider_id');
  123. if (!$providerId) {
  124. return $this->errorResponse('Provider ID é obrigatório', 400);
  125. }
  126. $proposal = $this->customScheduleService->proposeOpportunity($scheduleId, $providerId);
  127. return $this->successResponse($proposal, 'Proposta enviada com sucesso!', 201);
  128. } catch (\Exception $e) {
  129. Log::error('Error creating proposal: ' . $e->getMessage());
  130. return $this->errorResponse($e->getMessage(), 400);
  131. }
  132. }
  133. public function acceptProposal($proposalId)
  134. {
  135. try {
  136. $schedule = $this->customScheduleService->acceptProposal($proposalId);
  137. return $this->successResponse($schedule, 'Prestador aceito com sucesso!');
  138. } catch (\Exception $e) {
  139. Log::error('Error accepting proposal: ' . $e->getMessage());
  140. return $this->errorResponse($e->getMessage(), 400);
  141. }
  142. }
  143. public function refuseProposal($proposalId)
  144. {
  145. try {
  146. $this->customScheduleService->refuseProposal($proposalId);
  147. return $this->successResponse(null, 'Prestador recusado.');
  148. } catch (\Exception $e) {
  149. Log::error('Error refusing proposal: ' . $e->getMessage());
  150. return $this->errorResponse($e->getMessage(), 400);
  151. }
  152. }
  153. public function getProvidersProposalsAndOpportunities($providerId)
  154. {
  155. try {
  156. $data = $this->customScheduleService->getProvidersProposalsAndOpportunities($providerId);
  157. return $this->successResponse($data);
  158. } catch (\Exception $e) {
  159. Log::error('Error fetching provider proposals and opportunities: ' . $e->getMessage());
  160. return $this->errorResponse($e->getMessage(), 500);
  161. }
  162. }
  163. public function verifyCode($scheduleId, CustomScheduleVerifyCodeRequest $request)
  164. {
  165. try {
  166. $result = $this->customScheduleService->verifyScheduleCode($scheduleId, $request->input('code'));
  167. return $this->successResponse($result, 'Código verificado com sucesso!');
  168. } catch (\Exception $e) {
  169. Log::error('Error verifying schedule code: ' . $e->getMessage());
  170. return $this->errorResponse($e->getMessage(), 500);
  171. }
  172. }
  173. public function refuseOpportunity($scheduleId, CustomScheduleRefuseOpportunityRequest $request)
  174. {
  175. try {
  176. $this->customScheduleService->refuseOpportunity($scheduleId, $request->input('provider_id'));
  177. return $this->successResponse(null, 'Oportunidade recusada.');
  178. } catch (\Exception $e) {
  179. Log::error('Error refusing opportunity: ' . $e->getMessage());
  180. return $this->errorResponse($e->getMessage(), 400);
  181. }
  182. }
  183. }