CustomScheduleController.php 6.7 KB

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