CustomScheduleController.php 6.6 KB

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