Planificacion y avances de backend

This commit is contained in:
2021-04-29 01:59:08 -04:00
parent 6e4076cf15
commit b6ea4a7ce2
21 changed files with 486 additions and 156 deletions

View File

@@ -2,9 +2,12 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController; use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController class Controller extends BaseController
{ {
// protected function buildFailedValidationResponse(Request $request, array $errors) {
return ["error" => "validation_error", "message" => $errors];
}
} }

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Http\Controllers;
class ExampleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
//
}

View File

@@ -0,0 +1,110 @@
<?php
namespace App\Http\Controllers;
use App\Models\Usuario;
use App\Services\Auth0Service;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;
class UsuariosController extends Controller {
/**
* @return JsonResponse
*/
public function all() {
$usuarios = Usuario::all();
return response()->json(['usuarios' => $usuarios]);
}
/**
* @param $id
* @return JsonResponse
*/
public function get($id) {
if (!Uuid::isValid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id debe ser un UUID valido'
]);
}
$usuario = Usuario::find($id);
if (!$usuario) {
return response()->json([
'error' => 'not_found',
'message' => 'El usuario con id ' . $id . ' no existe'
], 404);
}
return response()->json(['usuario' => $usuario]);
}
/**
* @throws ValidationException
*/
public function create(Request $request) {
$this->validate($request, [
'nombre' => 'required',
'email' => 'required|email',
'username' => 'required',
'password' => 'required',
'type' => 'required|array',
'type.*' => ['required', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])],
'restaurant' => 'required|exists:restaurantes,id',
]);
$restaurant = $request->input('restaurant');
// solo un global admin puede crear usuarios en cualquier restaurant
if (!in_array('global_admin', $request->user->roles)) {
// si el usuario no es administrador no puede crear usuarios
if (!in_array('admin', $request->user->roles)) {
return response()->json([
'error' => 'not_allowed',
'message' => 'El usuario no puede tiene permisos para crear usuarios'
]);
}
// los administradores solo pueden crear restaurantes en su propio restaurant
if (!$request->user->restaurantes->contains($restaurant)) {
return response()->json([
'error' => 'not_allowed',
'message' => 'El usuario no puede crear un usuario en un restaurant al que no pertenece'
]);
}
}
$auth0 = app(Auth0Service::class);
$auth0User = $auth0->createUser(
email: $request->input('email'),
username: $request->input('username'),
password: $request->input('password'),
metadata: [
'roles' => $request->input('type'),
'restaurantes' => [$restaurant],
]
);
if (array_key_exists('error', $auth0User)) {
return response()->json([
'error' => $auth0User['errorCode'],
'message' => $auth0User['message'],
], $auth0User['statusCode']);
}
$usuario = new Usuario();
$usuario->id = Uuid::uuid4();
$usuario->auth0_id = $auth0User['identities'][0]['provider'] . '|' . $auth0User['identities'][0]['user_id'];
$usuario->nombre = $request->input('nombre');
$usuario->save();
$usuario->restaurantes()->attach($restaurant);
return response()->json($usuario);
}
}

View File

@@ -3,26 +3,37 @@
namespace App\Http\Middleware; namespace App\Http\Middleware;
use Closure; use Closure;
use App\Models\Usuario;
use Auth0\SDK\Exception\InvalidTokenException; use Auth0\SDK\Exception\InvalidTokenException;
use Auth0\SDK\Helpers\JWKFetcher; use Auth0\SDK\Helpers\JWKFetcher;
use Auth0\SDK\Helpers\Tokens\AsymmetricVerifier; use Auth0\SDK\Helpers\Tokens\AsymmetricVerifier;
use Auth0\SDK\Helpers\Tokens\TokenVerifier; use Auth0\SDK\Helpers\Tokens\TokenVerifier;
use Illuminate\Support\Facades\Log;
class Auth0Middleware { class Auth0Middleware {
/**
* @throws InvalidTokenException
*/
public function handle($request, Closure $next) { public function handle($request, Closure $next) {
$token = $request->bearerToken(); $token = $request->bearerToken();
if (!$token) { if (!$token) {
return response()->json('No token provided', 401); return response()->json(['error' => 'no_token', 'message' => 'No se envío el token'], 401);
} }
$this->validateToken($token); $validated = $this->validateToken($token);
$user = Usuario::where('auth0_id', $validated['sub'])->first();
$request = $request->merge(['user' => $user]);
return $next($request); return $next($request);
} }
/**
* @throws InvalidTokenException
*/
public function validateToken($token) { public function validateToken($token) {
try { try {
$jwksUri = env('AUTH0_DOMAIN') . '.well-known/jwks.json'; $jwksUri = env('AUTH0_DOMAIN') . '.well-known/jwks.json';
@@ -30,9 +41,9 @@ class Auth0Middleware {
$signatureVerifier = new AsymmetricVerifier($jwksFetcher); $signatureVerifier = new AsymmetricVerifier($jwksFetcher);
$tokenVerifier = new TokenVerifier(env('AUTH0_DOMAIN'), env('AUTH0_AUD'), $signatureVerifier); $tokenVerifier = new TokenVerifier(env('AUTH0_DOMAIN'), env('AUTH0_AUD'), $signatureVerifier);
$decoded = $tokenVerifier->verify($token); return $tokenVerifier->verify($token);
} catch (InvalidTokenException $e) { } catch (InvalidTokenException $e) {
throw $e; throw $e;
}; }
} }
} }

View File

@@ -6,6 +6,9 @@ use App\Traits\UuidPrimaryKey;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @method static find(mixed $restaurant)
*/
class Restaurante extends Model { class Restaurante extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;

View File

@@ -2,16 +2,53 @@
namespace App\Models; namespace App\Models;
use App\Services\Auth0Service;
use App\Traits\UuidPrimaryKey; use App\Traits\UuidPrimaryKey;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Usuario
*
* @property $id
* @property $auth0_id
* @property $nombre
*
* @method static find($id)
* @method static where(string $string, $sub)
*
* @package App\Models
*
*/
class Usuario extends Model { class Usuario extends Model {
use UuidPrimaryKey, SoftDeletes; use UuidPrimaryKey, SoftDeletes;
protected $table = 'usuarios'; protected $table = 'usuarios';
protected $appends = ['roles'];
public function restaurantes() { public function restaurantes() {
return $this->belongsToMany(Restaurante::class, 'usuarios_restaurantes', 'usuario_id', 'restaurante_id'); return $this->belongsToMany(Restaurante::class, 'usuarios_restaurantes', 'usuario_id', 'restaurante_id');
} }
public function administrador() {
return $this->hasOne(Administrador::class);
}
public function recaudador() {
return $this->hasOne(Recaudador::class);
}
public function mesero() {
return $this->hasOne(Mesero::class);
}
public function productor() {
return $this->hasOne(Productor::class);
}
public function getRolesAttribute() {
$auth0Service = app(Auth0Service::class);
$auth0User = $auth0Service->getUser($this->auth0_id);
return $this->attributes['roles'] = array_key_exists('app_metadata', $auth0User) ? $auth0User['app_metadata']['roles'] : [];
}
} }

View File

@@ -2,6 +2,7 @@
namespace App\Providers; namespace App\Providers;
use App\Services\Auth0Service;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
@@ -13,6 +14,8 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
// $this->app->singleton(Auth0Service::class, function($app) {
return new Auth0Service($app);
});
} }
} }

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\ServiceProvider;
class Auth0Service extends ServiceProvider {
public function getToken() {
$token = Redis::get('auth0_token');
if (empty($token)) {
$auth0Token = $this->queryAuth0ForToken();
if ($auth0Token == null)
return null;
Redis::set(
'auth0_token', $auth0Token['access_token'],
'EX', $auth0Token['expires_in']
);
$token = $auth0Token['access_token'];
}
return $token;
}
private function queryAuth0ForToken() {
$endpoint = env('AUTH0_DOMAIN') . 'oauth/token';
$payload = [
"client_id" => env('AUTH0_CLIENT_ID'),
"client_secret" => env('AUTH0_CLIENT_SECRET'),
"audience" => env('AUTH0_API_AUD'),
"grant_type" => "client_credentials"
];
$response = Http::post($endpoint, $payload);
$json = $response->json();
if (array_key_exists('error', $json)) {
return null;
} else {
return $json;
}
}
public function getUser($id) {
$endpoint = env('AUTH0_DOMAIN') . 'api/v2/users/' . $id;
return Http::withToken($this->getToken())
->get($endpoint)
->json();
}
public function createUser(
$email,
$username,
$password,
$metadata,
) {
$endpoint = env('AUTH0_DOMAIN') . 'api/v2/users';
$payload = [
"email" => $email,
"username" => $username,
"password" => $password,
"app_metadata" => $metadata,
"connection" => env('AUTH0_CONNECTION'),
"verify_email" => env('AUTH0_VERIFY_MAIL'),
];
return Http::withToken($this->getToken())
->post($endpoint, $payload)
->json();
}
}

View File

@@ -91,9 +91,10 @@ $app->routeMiddleware([
| |
*/ */
// $app->register(App\Providers\AppServiceProvider::class); $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class); // $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class); // $app->register(App\Providers\EventServiceProvider::class);
$app->register(Illuminate\Redis\RedisServiceProvider::class);
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@@ -7,7 +7,10 @@
"require": { "require": {
"php": "^7.3|^8.0", "php": "^7.3|^8.0",
"auth0/auth0-php": "^7.8", "auth0/auth0-php": "^7.8",
"laravel/lumen-framework": "^8.0" "guzzlehttp/guzzle": "^7.3",
"illuminate/redis": "^8.38",
"laravel/lumen-framework": "^8.0",
"predis/predis": "^1.1"
}, },
"require-dev": { "require-dev": {
"fakerphp/faker": "^1.9.1", "fakerphp/faker": "^1.9.1",

122
backend/composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "187e06bbce99a322bf2d6b2628d34204", "content-hash": "73e3da6538bda6b13d5240cfc43b8a7f",
"packages": [ "packages": [
{ {
"name": "auth0/auth0-php", "name": "auth0/auth0-php",
@@ -1875,6 +1875,60 @@
}, },
"time": "2021-04-15T11:53:14+00:00" "time": "2021-04-15T11:53:14+00:00"
}, },
{
"name": "illuminate/redis",
"version": "v8.38.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/redis.git",
"reference": "8c4acca0e65b6a7eaaff1ecf9b6d8267b09004e2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/redis/zipball/8c4acca0e65b6a7eaaff1ecf9b6d8267b09004e2",
"reference": "8c4acca0e65b6a7eaaff1ecf9b6d8267b09004e2",
"shasum": ""
},
"require": {
"illuminate/collections": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/macroable": "^8.0",
"illuminate/support": "^8.0",
"php": "^7.3|^8.0"
},
"suggest": {
"ext-redis": "Required to use the phpredis connector (^4.0|^5.0).",
"predis/predis": "Required to use the predis connector (^1.1.2)."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "8.x-dev"
}
},
"autoload": {
"psr-4": {
"Illuminate\\Redis\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "The Illuminate Redis package.",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-03-26T15:23:04+00:00"
},
{ {
"name": "illuminate/session", "name": "illuminate/session",
"version": "v8.38.0", "version": "v8.38.0",
@@ -2692,6 +2746,72 @@
], ],
"time": "2020-07-20T17:29:33+00:00" "time": "2020-07-20T17:29:33+00:00"
}, },
{
"name": "predis/predis",
"version": "v1.1.7",
"source": {
"type": "git",
"url": "https://github.com/predis/predis.git",
"reference": "b240daa106d4e02f0c5b7079b41e31ddf66fddf8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/predis/predis/zipball/b240daa106d4e02f0c5b7079b41e31ddf66fddf8",
"reference": "b240daa106d4e02f0c5b7079b41e31ddf66fddf8",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"suggest": {
"ext-curl": "Allows access to Webdis when paired with phpiredis",
"ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
},
"type": "library",
"autoload": {
"psr-4": {
"Predis\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Daniele Alessandri",
"email": "suppakilla@gmail.com",
"homepage": "http://clorophilla.net",
"role": "Creator & Maintainer"
},
{
"name": "Till Krüss",
"homepage": "https://till.im",
"role": "Maintainer"
}
],
"description": "Flexible and feature-complete Redis client for PHP and HHVM",
"homepage": "http://github.com/predis/predis",
"keywords": [
"nosql",
"predis",
"redis"
],
"support": {
"issues": "https://github.com/predis/predis/issues",
"source": "https://github.com/predis/predis/tree/v1.1.7"
},
"funding": [
{
"url": "https://github.com/sponsors/tillkruss",
"type": "github"
}
],
"time": "2021-04-04T19:34:46+00:00"
},
{ {
"name": "psr/container", "name": "psr/container",
"version": "1.1.1", "version": "1.1.1",

View File

@@ -1,29 +0,0 @@
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
];
}
}

View File

@@ -1,18 +0,0 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// $this->call('UsersTableSeeder');
}
}

View File

@@ -3,22 +3,16 @@
/** @var \Laravel\Lumen\Routing\Router $router */ /** @var \Laravel\Lumen\Routing\Router $router */
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) { $router->get('/', function () use ($router) {
return 'Public View'; return 'Public View';
}); });
$router->group(['prefix' => 'api/v1', 'middleware' => 'auth'], function () use ($router) { $router->group(['prefix' => 'api/v1', 'middleware' => 'auth'], function () use ($router) {
$router->get('/', function () use ($router) { $router->group(['prefix' => '/users'], function () use ($router) {
return \App\Models\Venta::with('productos')->get(); $router->get('/', ['as' => 'users.all', 'uses' => 'UsuariosController@all']);
$router->get('/{id}', ['as' => 'users.get', 'uses' => 'UsuariosController@get']);
$router->post('/', ['as' => 'users.create', 'uses' => 'UsuariosController@create']);
$router->put('/{id}', ['as' => 'users.update', 'uses' => 'UsuariosController@update']);
$router->delete('/{id}', ['as' => 'users.delete', 'uses' => 'UsuariosController@delete']);
}); });
}); });

View File

@@ -0,0 +1,33 @@
<?php
use App\Services\Auth0Service;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Http;
class Auth0ServiceTest extends TestCase {
public function testIsNotInCache() {
Redis::shouldReceive('get')->once()->with('auth0_token')->andReturns(null);
Http::fake([
'*' => Http::response([
"access_token" => "token",
"scope" => "read:client_grants",
"expires_in" => 86400,
"token_type" => "Bearer"
])
]);
Redis::shouldReceive('set')->with('auth0_token', "token", 'EX', 86400);
$tokenService = app(Auth0Service::class);
$this->assertEquals("token", $tokenService->getToken());
}
public function testIsInCache() {
Redis::shouldReceive('get')->once()->with('auth0_token')->andReturns("token");
Redis::shouldReceive('set')->never();
Http::shouldReceive('post')->never();
$tokenService = app(Auth0Service::class);
$this->assertEquals("token", $tokenService->getToken());
}
}

View File

@@ -1,21 +0,0 @@
<?php
use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->get('/');
$this->assertEquals(
$this->app->version(), $this->response->getContent()
);
}
}

Binary file not shown.

View File

@@ -1,38 +1,44 @@
create table restaurantes ( create table restaurantes (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
nombre text not null, nombre text not null,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table categorias ( create table categorias (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
nombre text not null, nombre text not null,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table zonas_produccion ( create table zonas_produccion (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
nombre text not null, nombre text not null,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table usuarios ( create table usuarios (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
auth0_id text not null, auth0_id text not null,
nombre text not null nombre text not null,
created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table usuarios_restaurantes ( create table usuarios_restaurantes (
usuario_id uuid references usuarios, usuario_id uuid references usuarios,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp, updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz,
primary key (usuario_id, restaurante_id) primary key (usuario_id, restaurante_id)
); );
@@ -40,29 +46,33 @@ create table productores (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
usuario_id uuid references usuarios, usuario_id uuid references usuarios,
zona_produccion_id uuid references zonas_produccion, zona_produccion_id uuid references zonas_produccion,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table recaudadores ( create table recaudadores (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
usuario_id uuid references usuarios, usuario_id uuid references usuarios,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table meseros ( create table meseros (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
usuario_id uuid references usuarios, usuario_id uuid references usuarios,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table administradores ( create table administradores (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
usuario_id uuid references usuarios, usuario_id uuid references usuarios,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table ingredientes ( create table ingredientes (
@@ -70,8 +80,9 @@ create table ingredientes (
nombre text not null, nombre text not null,
medida text not null, medida text not null,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table productos ( create table productos (
@@ -81,16 +92,18 @@ create table productos (
categoria_id uuid references categorias, categoria_id uuid references categorias,
zona_produccion_id uuid references zonas_produccion, zona_produccion_id uuid references zonas_produccion,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table recetas ( create table recetas (
producto_id uuid references productos, producto_id uuid references productos,
ingrediente_id uuid references ingredientes, ingrediente_id uuid references ingredientes,
unidades numeric not null, unidades numeric not null,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp, updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz,
primary key (producto_id, ingrediente_id) primary key (producto_id, ingrediente_id)
); );
@@ -102,8 +115,9 @@ create table proveedores (
direccion text null, direccion text null,
telefono text null, telefono text null,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table compras ( create table compras (
@@ -111,8 +125,9 @@ create table compras (
fecha_compra date not null, fecha_compra date not null,
proveedor_id uuid references proveedores, proveedor_id uuid references proveedores,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table facturas ( create table facturas (
@@ -125,8 +140,9 @@ create table facturas (
fecha_emision date not null, fecha_emision date not null,
fecha_vencimiento date not null, fecha_vencimiento date not null,
compra_id uuid references compras, compra_id uuid references compras,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table compra_ingredientes ( create table compra_ingredientes (
@@ -136,23 +152,26 @@ create table compra_ingredientes (
monto_unitario_neto bigint not null, monto_unitario_neto bigint not null,
compra_id uuid references compras, compra_id uuid references compras,
ingrediente_id uuid references ingredientes, ingrediente_id uuid references ingredientes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table sectores ( create table sectores (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
nombre text not null, nombre text not null,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table tipos_canal ( create table tipos_canal (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
nombre text not null, nombre text not null,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table canales_venta ( create table canales_venta (
@@ -161,8 +180,9 @@ create table canales_venta (
sector_id uuid references sectores, sector_id uuid references sectores,
tipo_canal_id uuid references tipos_canal, tipo_canal_id uuid references tipos_canal,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table ventas ( create table ventas (
@@ -171,8 +191,9 @@ create table ventas (
mesero_id uuid references meseros, mesero_id uuid references meseros,
canal_id uuid references canales_venta, canal_id uuid references canales_venta,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table boletas_electronicas ( create table boletas_electronicas (
@@ -180,23 +201,26 @@ create table boletas_electronicas (
numero_boleta text not null, numero_boleta text not null,
venta_id uuid references ventas, venta_id uuid references ventas,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table boletas_exentas ( create table boletas_exentas (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
venta_id uuid references ventas, venta_id uuid references ventas,
restaurante_id uuid references restaurantes, restaurante_id uuid references restaurantes,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table estados_produccion ( create table estados_produccion (
id uuid primary key default gen_random_uuid(), id uuid primary key default gen_random_uuid(),
nombre text not null, nombre text not null,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
create table venta_productos ( create table venta_productos (
@@ -206,8 +230,9 @@ create table venta_productos (
venta_id uuid references ventas, venta_id uuid references ventas,
producto_id uuid references productos, producto_id uuid references productos,
estado_id uuid references estados_produccion, estado_id uuid references estados_produccion,
inserted_at timestamptz not null default current_timestamp, created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp updated_at timestamptz not null default current_timestamp,
deleted_at timestamptz
); );
------------------------------------ ------------------------------------

Binary file not shown.

BIN
planificacion/mockup.xd Normal file

Binary file not shown.