diff --git a/backend/Dockerfile b/backend/Dockerfile index 56c269d..a9fb161 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -18,14 +18,4 @@ COPY composer.lock . RUN composer install --no-ansi --no-dev --no-interaction --no-plugins \ --no-progress --no-scripts --optimize-autoloader -COPY app ./app -COPY artisan ./artisan -COPY bootstrap ./bootstrap -COPY config ./config -COPY public ./public -COPY resources ./resources -COPY routes ./routes -COPY storage ./storage -COPY .env.production .env - ENTRYPOINT ["php", "-S", "0.0.0.0:8080", "-t", "public"] diff --git a/backend/app/Exceptions/GenericException.php b/backend/app/Exceptions/GenericException.php new file mode 100644 index 0000000..22c3832 --- /dev/null +++ b/backend/app/Exceptions/GenericException.php @@ -0,0 +1,25 @@ +error = $error; + $this->message = $message; + $this->status = $status; + } + + public function render($request) { + return response()->json([ + 'error' => $this->error + 'message' => $this->message + ], $status); + } +} diff --git a/backend/app/Exceptions/InvalidUuidException.php b/backend/app/Exceptions/InvalidUuidException.php new file mode 100644 index 0000000..54f2ade --- /dev/null +++ b/backend/app/Exceptions/InvalidUuidException.php @@ -0,0 +1,21 @@ +uuid = $uuid; + } + + public function render($request) { + return response()->json([ + 'error' => 'invalid_uuid', + 'message' => 'El id ' . $this->uuid . ' no es un UUID valido' + ], 400); + } +} diff --git a/backend/app/Exceptions/ModelNotFoundException.php b/backend/app/Exceptions/ModelNotFoundException.php new file mode 100644 index 0000000..8b40465 --- /dev/null +++ b/backend/app/Exceptions/ModelNotFoundException.php @@ -0,0 +1,23 @@ +modelName= $modelName; + $this->id = $id; + } + + public function render($request) { + return response()->json([ + 'error' => $this->modelName . '_not_found', + 'message' => 'El ' . $this->modelName . ' con id ' . $this->id . ' no existe' + ], 404); + } +} diff --git a/backend/app/Http/Controllers/CanalesVentaController.php b/backend/app/Http/Controllers/CanalesVentaController.php index ac20ee7..57fc51f 100644 --- a/backend/app/Http/Controllers/CanalesVentaController.php +++ b/backend/app/Http/Controllers/CanalesVentaController.php @@ -21,21 +21,8 @@ class CanalesVentaController extends Controller { * @return JsonResponse */ public function all(Request $request, $restaurante_id) { - if (!app(UuidService::class)->is_valid($restaurante_id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $restaurante_id . ' no es un UUID valido' - ], 404); - } - - $restaurante = Restaurante::findOrNull($restaurante_id); - - if (!$restaurante) { - return response()->json([ - 'error' => 'restaurant_not_found', - 'message' => 'El restaurant con id ' . $restaurante_id . ' no existe' - ], 404); - } + app(UuidService::class)->validOrFail($restaurante_id); + $restaurante = Restaurante::findOrFail($restaurante_id); $canalesVenta = $restaurante->canalesVenta(); @@ -61,36 +48,11 @@ class CanalesVentaController extends Controller { * @return JsonResponse */ public function get($restaurante_id, $id) { - if (!app(UuidService::class)->is_valid($id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $id . ' no es un UUID valido' - ], 404); - } + app(UuidService::class)->validOrFail($id); + app(UuidService::class)->validOrFail($restaurante_id); - if (!app(UuidService::class)->is_valid($restaurante_id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $restaurante_id . ' no es un UUID valido' - ], 404); - } - - $restaurante = Restaurante::findOrNull($restaurante_id); - - if (!$restaurante) { - return response()->json([ - 'error' => 'restaurant_not_found', - 'message' => 'El restaurant con id ' . $restaurante_id . ' no existe' - ], 404); - } - - $canalVenta = CanalVenta::findOrNull($id); - if (!$canalVenta) { - return response()->json([ - 'error' => 'canal_venta_not_found', - 'message' => 'El canal de venta con id ' . $id . ' no existe' - ], 404); - } + $restaurante = Restaurante::findOrFail($restaurante_id); + $canalVenta = CanalVenta::findOrFail($id); return response()->json($canalVenta); } diff --git a/backend/app/Http/Controllers/UsuariosController.php b/backend/app/Http/Controllers/UsuariosController.php index 49bc14a..a1dcace 100644 --- a/backend/app/Http/Controllers/UsuariosController.php +++ b/backend/app/Http/Controllers/UsuariosController.php @@ -6,6 +6,8 @@ use App\Models\Restaurante; use App\Models\Usuario; use App\Services\Auth0Service; use App\Services\PaginatorService; +use App\Services\UuidService; +use App\Exceptions\GenericException; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Validation\Rule; @@ -27,33 +29,27 @@ class UsuariosController extends Controller { route: 'users.all', ); + $data = Usuario::with('restaurantes') + ->skip($paginate['from'] - 1) + ->take($paginate['per_page'])->get()->all(); + return response()->json([ 'pagination' => $paginate, - 'data' => array_values(Usuario::with('restaurantes')->skip($paginate['from'] - 1)->take($paginate['per_page'])->get()->all()) + 'data' => $data ]); } /** - * Obtiene un usuario por su id, siendo el id de auth0 o el id de la base de datos + * Obtiene un usuario por su id * @param $id * @return JsonResponse */ public function get($id) { - if (!app(UuidService::class)->is_valid($id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $id . ' no es un UUID valido' - ], 404); + if (!str_starts_with($id, 'auth0')) { + app(UuidService::class)->validOrFail($id); } - $usuario = Usuario::findByIdOrAuth0Id($id); - - if (!$usuario) { - return response()->json([ - 'error' => 'user_not_found', - 'message' => 'El usuario con id o auth0_id ' . $id . ' no existe' - ], 404); - } + $usuario = Usuario::findOrFail($id); return response()->json($usuario); } @@ -73,12 +69,8 @@ class UsuariosController extends Controller { 'restaurant' => 'required|exists:restaurantes,id', ]); - $restaurant = Restaurante::findOrNull($request->input('restaurant')); - - $cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant); - if ($cantManageUsersOrRestaurant) { - return $cantManageUsersOrRestaurant; - } + $restaurant = Restaurante::findOrFail($request->input('restaurant')); + $this->canManageUsersAndRestaurantOrFail($request->user, $restaurant); $auth0 = app(Auth0Service::class); $auth0User = $auth0->createUser( @@ -108,18 +100,15 @@ class UsuariosController extends Controller { } /** - * Actualiza un usuario, dado su id o auth0_id + * Actualiza un usuario * @param Request $request * @param $id * @return JsonResponse * @throws ValidationException */ public function update(Request $request, $id) { - if (!app(UuidService::class)->is_valid($id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $id . ' no es un UUID valido' - ], 404); + if (!str_starts_with($id, 'auth0')) { + app(UuidService::class)->validOrFail($id); } $this->validate($request, [ @@ -131,19 +120,9 @@ class UsuariosController extends Controller { 'roles.*' => ['sometimes', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])], ]); - $usuario = Usuario::findByIdOrAuth0Id($id); + $usuario = Usuario::findOrFail($id); - if (!$usuario) { - return response()->json([ - 'error' => 'not_found', - 'message' => 'El usuario con id ' . $id . ' no existe' - ], 404); - } - - $cantManageUser = $this->cantManageUser($request->user, $usuario); - if ($cantManageUser) { - return $cantManageUser; - } + $this->canManageUserOrFail($request->user, $usuario); $metadata = []; if ($request->input('roles')) $metadata['roles'] = $request->input('roles'); @@ -177,28 +156,13 @@ class UsuariosController extends Controller { * @return JsonResponse */ public function delete(Request $request, $id) { - if (!app(UuidService::class)->is_valid($id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $id . ' no es un UUID valido' - ], 404); + if (!str_starts_with($id, 'auth0')) { + app(UuidService::class)->validOrFail($id); } - /** @var Usuario $logged_user */ - $logged_user = $request->user; - $usuario = Usuario::findByIdOrAuth0Id($id); + $usuario = Usuario::findOrFail($id); - if (!$usuario) { - return response()->json([ - 'error' => 'not_found', - 'message' => 'El usuario con id ' . $id . ' no existe' - ], 404); - } - - $cantManageUser = $this->cantManageUser($request->user, $usuario); - if ($cantManageUser) { - return $cantManageUser; - } + $this->canManageUserOrFail($request->user, $usuario); $auth0 = app(Auth0Service::class); $auth0Response = $auth0->deleteUser($usuario->auth0_id); @@ -223,44 +187,17 @@ class UsuariosController extends Controller { * @return JsonResponse */ public function addToRestaurant(Request $request, $id, $restaurant) { - if (!app(UuidService::class)->is_valid($id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $id . ' no es un UUID valido' - ], 404); + if (!str_starts_with($id, 'auth0')) { + app(UuidService::class)->validOrFail($id); + } + if (!str_starts_with($restaurant, 'auth0')) { + app(UuidService::class)->validOrFail($restaurant); } - if (!app(UuidService::class)->is_valid($restaurant)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $restaurant. ' no es un UUID valido' - ], 404); - } + $usuario = Usuario::findOrFail($id); + $restaurant = Restaurante::findOrFail($restaurant); - /** @var Usuario $logged_user */ - $logged_user = $request->user; - - $usuario = Usuario::findByIdOrAuth0Id($id); - if (!$usuario) { - return response()->json([ - 'error' => 'not_found', - 'message' => 'El usuario con id ' . $id . ' no existe' - ], 404); - } - - - $restaurant = Restaurante::findOrNull($restaurant); - if (!$restaurant) { - return response()->json([ - 'error' => 'not_found', - 'message' => 'El restaurante con id ' . $id . ' no existe' - ], 404); - } - - $cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant); - if ($cantManageUsersOrRestaurant) { - return $cantManageUsersOrRestaurant; - } + $this->canManageUsersAndRestaurantOrFail($request->user, $restaurant); if ($usuario->restaurantes->contains($restaurant)) { return response()->json([ @@ -283,41 +220,17 @@ class UsuariosController extends Controller { * @return JsonResponse */ public function removeFromRestaurant(Request $request, $id, $restaurant) { - if (!app(UuidService::class)->is_valid($id)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $id . ' no es un UUID valido' - ], 404); + if (!str_starts_with($id, 'auth0')) { + app(UuidService::class)->validOrFail($id); + } + if (!str_starts_with($restaurant, 'auth0')) { + app(UuidService::class)->validOrFail($restaurant); } - if (!app(UuidService::class)->is_valid($restaurant)) { - return response()->json([ - 'error' => 'invalid_id', - 'message' => 'El id ' . $restaurant. ' no es un UUID valido' - ], 404); - } + $usuario = Usuario::findOrFail($id); + $restaurant = Restaurante::findOrFail($restaurant); - $usuario = Usuario::findByIdOrAuth0Id($id); - - if (!$usuario) { - return response()->json([ - 'error' => 'not_found', - 'message' => 'El usuario con id ' . $id . ' no existe' - ], 404); - } - - $restaurant = Restaurante::findOrNull($restaurant); - if (!$restaurant) { - return response()->json([ - 'error' => 'not_found', - 'message' => 'El restaurante con id ' . $id . ' no existe' - ], 404); - } - - $cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant); - if ($cantManageUsersOrRestaurant) { - return $cantManageUsersOrRestaurant; - } + canManageUsersAndRestaurantOrFail($request->user, $restaurant); if (!$usuario->restaurantes->contains($restaurant)) { return response()->json([ @@ -331,38 +244,31 @@ class UsuariosController extends Controller { return response()->json($usuario->fresh(['restaurantes'])); } - private function cantManageUsersOrRestaurant(Usuario $user, Restaurante $restaurante) { + private function canManageUsersAndRestaurantOrFail(Usuario $user, Restaurante $restaurante) { if (!$user->canManageUsers()) { - return response()->json([ - 'error' => 'cant_manage_users', - 'message' => 'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios' - ], 403); + throw new GenericException('cant_manage_users', + 'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios', + 403); } if (!$user->hasPermissionsOnRestaurant($restaurante)) { - return response()->json([ - 'error' => 'cant_manage_user_of_another_restaurant', - 'message' => 'El usuario ' . $user->id . ' no puede manipular un usuario en el restaurant ' . $restaurante->id . ' porque que no pertenece a el' - ], 403); + throw new GenericException('cant_manage_user_of_another_restaurant', + 'El usuario ' . $user->id . ' no puede manipular un usuario en el restaurant ' . $restaurante->id . ' porque que no pertenece a el', + 403); } - return false; } - private function cantManageUser(Usuario $manager, Usuario $user) { + private function canManageUserOrFail(Usuario $manager, Usuario $user) { if (!$manager->canManageUsers()) { - return response()->json([ - 'error' => 'cant_manage_users', - 'message' => 'El usuario ' . $manager->id . ' no tiene permisos para manipular usuarios' - ], 403); + throw new GenericException('cant_manage_users', + 'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios', + 403); } if (!$manager->hasPermissionsOverUser($user)) { - return response()->json([ - 'error' => 'cant_manage_that_user', - 'message' => 'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id - ], 403); + throw new GenericException('cant_manage_that_user', + 'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id, + 403); } - - return false; } } diff --git a/backend/app/Models/CanalVenta.php b/backend/app/Models/CanalVenta.php index 600219d..65f9098 100644 --- a/backend/app/Models/CanalVenta.php +++ b/backend/app/Models/CanalVenta.php @@ -11,13 +11,14 @@ class CanalVenta extends Model { protected $table = 'canales_venta'; - public static function findOrNull($id) { - try { - return CanalVenta::find($id); - } catch (QueryException $ex) { - Log::warning('Se intento obtener un canal de venta con un id invalido', ['id' => $id]); - return null; + public static function findOrFail($id) { + $canal_venta = CanalVenta::find($id); + + if(!$canal_venta){ + throw new ModelNotFoundException("canal_venta", $id); } + + return $canal_venta; } public function tipoCanal() { diff --git a/backend/app/Models/Restaurante.php b/backend/app/Models/Restaurante.php index 39e5db6..c51b964 100644 --- a/backend/app/Models/Restaurante.php +++ b/backend/app/Models/Restaurante.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Models\CanalVenta; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\QueryException; @@ -21,13 +22,14 @@ class Restaurante extends Model { protected $fillable = ['id', 'nombre']; - public static function findOrNull($id) { - try { - return Restaurante::find($id); - } catch (QueryException $ex) { - Log::warning('Se intento obtener un restaurante con un id invalido', ['id' => $id]); - return null; + public static function findOrFail($id) { + $restaurante = Restaurante::find($id); + + if(!$restaurante){ + throw new ModelNotFoundException("restaurant", $id); } + + return $restaurante; } public function usuarios() { diff --git a/backend/app/Models/Usuario.php b/backend/app/Models/Usuario.php index 4a239ad..5b2ca19 100644 --- a/backend/app/Models/Usuario.php +++ b/backend/app/Models/Usuario.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Services\Auth0Service; use App\Traits\UuidPrimaryKey; +use App\Exceptions\ModelNotFoundException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\QueryException; @@ -38,17 +39,18 @@ class Usuario extends Model { * @param $id * @return Usuario */ - public static function findByIdOrAuth0Id($id) { + public static function findOrFail($id) { if (str_starts_with($id, 'auth0')) { - return Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first(); + $usuario = Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first(); } else { - try { - return Usuario::where('id', $id)->with('restaurantes')->first(); - } catch (QueryException $ex) { - Log::warning('Se intento obtener un usuario con un id invalido', ['id' => $id]); - return null; - } + $usuario = Usuario::where('id', $id)->with('restaurantes')->first(); } + + if(!$usuario) { + throw new ModelNotFoundException("usuario", $id); + } + + return $usuario; } public function canManageRestaurants() { diff --git a/backend/app/Services/UuidService.php b/backend/app/Services/UuidService.php index 5f3f8b3..8e5de73 100644 --- a/backend/app/Services/UuidService.php +++ b/backend/app/Services/UuidService.php @@ -3,10 +3,17 @@ namespace App\Services; use Illuminate\Support\ServiceProvider; +use App\Exceptions\InvalidUuidException; class UuidService extends ServiceProvider { - public function is_valid(string $uuid) { + public function isValid(string $uuid) { $regex = '/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i'; return preg_match($regex, $uuid) === 1; } + + public function validOrFail(string $uuid) { + if(!$this->isValid($uuid)) { + throw new InvalidUuidException($uuid); + } + } }