- Prefijo v2: MCH - TRACEABILITY-MASTER.yml creado - Listo para integracion como submodulo Workspace: v2.0.0 | SIMCO: v4.0.0
695 lines
20 KiB
YAML
695 lines
20 KiB
YAML
# DATABASE INVENTORY - MiChangarrito
|
|
# Version: 2.0.0
|
|
# Ultima actualizacion: 2026-01-10
|
|
# Sistema: SIMCO - NEXUS v4.0
|
|
|
|
metadata:
|
|
proyecto: "michangarrito"
|
|
componente: "database"
|
|
db_name: "michangarrito_dev"
|
|
version_pg: "16+"
|
|
estado: "100% DDL completado"
|
|
actualizado: "2026-01-10"
|
|
actualizado_por: "Agente Orquestador"
|
|
|
|
# ============================================================================
|
|
# RESUMEN
|
|
# ============================================================================
|
|
|
|
resumen:
|
|
total_schemas: 9
|
|
total_tablas: 29
|
|
total_archivos_ddl: 13
|
|
total_extensiones: 4
|
|
total_funciones: 5
|
|
total_triggers: 18
|
|
total_enums: 2
|
|
rls_habilitado: true
|
|
multi_tenant: true
|
|
tenant_column: "tenant_id"
|
|
|
|
# ============================================================================
|
|
# EXTENSIONES POSTGRESQL
|
|
# ============================================================================
|
|
|
|
extensiones:
|
|
- nombre: "uuid-ossp"
|
|
proposito: "Generacion de UUIDs para PKs"
|
|
archivo: "00-extensions.sql"
|
|
|
|
- nombre: "pgcrypto"
|
|
proposito: "Funciones criptograficas"
|
|
archivo: "00-extensions.sql"
|
|
|
|
- nombre: "unaccent"
|
|
proposito: "Full-text search sin acentos"
|
|
archivo: "00-extensions.sql"
|
|
|
|
- nombre: "pg_trgm"
|
|
proposito: "Indices de texto para busqueda fuzzy"
|
|
archivo: "00-extensions.sql"
|
|
|
|
# ============================================================================
|
|
# ARCHIVOS DDL
|
|
# ============================================================================
|
|
|
|
archivos_ddl:
|
|
- archivo: "00-extensions.sql"
|
|
lineas: 16
|
|
descripcion: "Extensiones PostgreSQL"
|
|
estado: completado
|
|
|
|
- archivo: "01-schemas.sql"
|
|
lineas: 51
|
|
descripcion: "Creacion de 9 schemas con permisos"
|
|
schemas_creados:
|
|
- public
|
|
- auth
|
|
- catalog
|
|
- sales
|
|
- inventory
|
|
- customers
|
|
- orders
|
|
- subscriptions
|
|
- messaging
|
|
estado: completado
|
|
|
|
- archivo: "02-functions.sql"
|
|
lineas: 115
|
|
descripcion: "Funciones utilitarias y triggers"
|
|
funciones:
|
|
- update_updated_at()
|
|
- sales.generate_ticket_number()
|
|
- orders.generate_order_number()
|
|
- customers.update_customer_fiado_balance()
|
|
- inventory.update_stock_on_sale()
|
|
estado: completado
|
|
|
|
- archivo: "03-public.sql"
|
|
lineas: 101
|
|
descripcion: "Schema public - tenants y configuracion"
|
|
tablas:
|
|
- tenants
|
|
- tenant_configs
|
|
estado: completado
|
|
|
|
- archivo: "04-auth.sql"
|
|
lineas: 85
|
|
descripcion: "Autenticacion y usuarios"
|
|
tablas:
|
|
- users
|
|
- sessions
|
|
- otp_codes
|
|
estado: completado
|
|
|
|
- archivo: "05-catalog.sql"
|
|
lineas: 100
|
|
descripcion: "Catalogo de productos"
|
|
tablas:
|
|
- categories
|
|
- products
|
|
- product_templates
|
|
estado: completado
|
|
|
|
- archivo: "06-sales.sql"
|
|
lineas: 149
|
|
descripcion: "Punto de venta"
|
|
tablas:
|
|
- sales
|
|
- sale_items
|
|
- payments
|
|
- daily_closures
|
|
estado: completado
|
|
|
|
- archivo: "07-inventory.sql"
|
|
lineas: 58
|
|
descripcion: "Control de inventario"
|
|
tablas:
|
|
- inventory_movements
|
|
- stock_alerts
|
|
estado: completado
|
|
|
|
- archivo: "08-customers.sql"
|
|
lineas: 107
|
|
descripcion: "Clientes y fiados"
|
|
tablas:
|
|
- customers
|
|
- fiados
|
|
- fiado_payments
|
|
estado: completado
|
|
|
|
- archivo: "09-orders.sql"
|
|
lineas: 73
|
|
descripcion: "Pedidos"
|
|
tablas:
|
|
- orders
|
|
- order_items
|
|
estado: completado
|
|
|
|
- archivo: "10-subscriptions.sql"
|
|
lineas: 131
|
|
descripcion: "Planes y suscripciones"
|
|
tablas:
|
|
- plans
|
|
- subscriptions
|
|
- token_packages
|
|
- token_usage
|
|
- tenant_token_balance
|
|
estado: completado
|
|
|
|
- archivo: "11-messaging.sql"
|
|
lineas: 89
|
|
descripcion: "Mensajeria WhatsApp"
|
|
tablas:
|
|
- conversations
|
|
- messages
|
|
- notifications
|
|
estado: completado
|
|
|
|
- archivo: "12-integrations.sql"
|
|
lineas: 146
|
|
descripcion: "Integraciones por tenant"
|
|
enums:
|
|
- integration_type
|
|
- integration_provider
|
|
tablas:
|
|
- tenant_integration_credentials
|
|
- tenant_whatsapp_numbers
|
|
estado: completado
|
|
|
|
# ============================================================================
|
|
# SCHEMAS DETALLADOS
|
|
# ============================================================================
|
|
|
|
schemas:
|
|
public:
|
|
descripcion: "Datos globales y tenants"
|
|
tablas:
|
|
- nombre: tenants
|
|
columnas:
|
|
- id (UUID PK)
|
|
- name, slug (VARCHAR UNIQUE)
|
|
- business_type, phone, email
|
|
- address, city, state, zip_code
|
|
- timezone (default America/Mexico_City)
|
|
- currency (default MXN)
|
|
- tax_rate (default 16.00)
|
|
- whatsapp_number, whatsapp_verified
|
|
- current_plan_id (FK plans)
|
|
- subscription_status, status
|
|
- onboarding_completed
|
|
- created_at, updated_at
|
|
rls: false
|
|
descripcion: "Organizaciones/negocios"
|
|
|
|
- nombre: tenant_configs
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK UNIQUE)
|
|
- opening_hour, closing_hour
|
|
- working_days (INTEGER[])
|
|
- ticket_header, ticket_footer
|
|
- fiados_enabled, default_fiado_limit
|
|
- delivery_enabled, delivery_fee
|
|
- payment_cash, payment_card, payment_codi
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Configuraciones por tenant"
|
|
|
|
- nombre: tenant_integration_credentials
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- integration_type (ENUM)
|
|
- provider (ENUM)
|
|
- credentials (JSONB)
|
|
- config (JSONB)
|
|
- is_active, is_verified
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Credenciales de integraciones"
|
|
|
|
- nombre: tenant_whatsapp_numbers
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- phone_number_id (VARCHAR UNIQUE)
|
|
- phone_number, display_name
|
|
- is_platform_number, is_active
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Mapeo de numeros WhatsApp"
|
|
|
|
auth:
|
|
descripcion: "Autenticacion y sesiones"
|
|
tablas:
|
|
- nombre: users
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- phone, email, name
|
|
- pin_hash (bcrypt)
|
|
- biometric_enabled, biometric_key
|
|
- role (owner, employee, viewer)
|
|
- permissions (JSONB)
|
|
- status, failed_attempts, locked_until
|
|
- last_login_at, created_at, updated_at
|
|
rls: true
|
|
descripcion: "Usuarios del sistema"
|
|
|
|
- nombre: sessions
|
|
columnas:
|
|
- id (UUID PK)
|
|
- user_id (UUID FK)
|
|
- token_hash, refresh_token_hash
|
|
- device_type, device_info (JSONB)
|
|
- ip_address
|
|
- expires_at, refresh_expires_at
|
|
- created_at, last_activity_at
|
|
rls: true
|
|
descripcion: "Sesiones activas"
|
|
|
|
- nombre: otp_codes
|
|
columnas:
|
|
- id (UUID PK)
|
|
- phone
|
|
- code (6 digitos)
|
|
- purpose (login, verify_phone, reset_pin)
|
|
- attempts, max_attempts
|
|
- expires_at, used_at
|
|
- created_at
|
|
rls: false
|
|
descripcion: "Codigos OTP"
|
|
|
|
catalog:
|
|
descripcion: "Catalogo de productos"
|
|
tablas:
|
|
- nombre: categories
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- name, description
|
|
- icon, color
|
|
- sort_order, status
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Categorias de productos"
|
|
|
|
- nombre: products
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- category_id (UUID FK nullable)
|
|
- name, description, sku, barcode
|
|
- price, cost_price, compare_price
|
|
- track_inventory, stock_quantity
|
|
- low_stock_threshold, unit
|
|
- image_url, status, is_featured
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Productos"
|
|
|
|
- nombre: product_templates
|
|
columnas:
|
|
- id (UUID PK)
|
|
- provider_name (Bimbo, Coca-Cola, etc)
|
|
- name, description, barcode
|
|
- suggested_price, category_suggestion
|
|
- unit, image_url
|
|
- business_types (TEXT[])
|
|
- popularity
|
|
- created_at, updated_at
|
|
rls: false
|
|
descripcion: "Templates de productos predefinidos"
|
|
|
|
sales:
|
|
descripcion: "Punto de venta"
|
|
tablas:
|
|
- nombre: sales
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- ticket_number (UNIQUE)
|
|
- subtotal, discount_amount, tax_amount, total
|
|
- payment_method, payment_status
|
|
- cash_received, change_amount
|
|
- customer_id (FK nullable)
|
|
- is_fiado, fiado_id (FK nullable)
|
|
- created_by (UUID FK)
|
|
- status, notes
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Ventas registradas"
|
|
|
|
- nombre: sale_items
|
|
columnas:
|
|
- id (UUID PK)
|
|
- sale_id (UUID FK CASCADE)
|
|
- product_id (UUID FK nullable)
|
|
- product_name, product_sku
|
|
- quantity, unit_price
|
|
- discount_amount, subtotal
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Detalle de venta"
|
|
|
|
- nombre: payments
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- sale_id, fiado_id, subscription_id (FKs nullable)
|
|
- method, provider
|
|
- amount, fee_amount, net_amount
|
|
- external_id, external_status
|
|
- status, metadata (JSONB)
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Pagos registrados"
|
|
|
|
- nombre: daily_closures
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- closure_date (UNIQUE per tenant)
|
|
- opened_at, closed_at
|
|
- expected_cash, actual_cash
|
|
- cash_difference
|
|
- total_sales, total_cancelled, total_fiados
|
|
- closed_by (UUID FK)
|
|
- status, notes
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Cortes de caja"
|
|
|
|
inventory:
|
|
descripcion: "Control de inventario"
|
|
tablas:
|
|
- nombre: inventory_movements
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id, product_id (UUIDs FK)
|
|
- movement_type (purchase, sale, adjustment, loss, return)
|
|
- quantity (positivo o negativo)
|
|
- previous_stock, new_stock
|
|
- unit_cost, total_cost
|
|
- reference_type, reference_id
|
|
- notes, created_by
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Movimientos de stock"
|
|
|
|
- nombre: stock_alerts
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id, product_id (UUIDs FK)
|
|
- current_stock, threshold
|
|
- status (active, resolved, ignored)
|
|
- notified_at, resolved_at
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Alertas de stock bajo"
|
|
|
|
customers:
|
|
descripcion: "Clientes y credito"
|
|
tablas:
|
|
- nombre: customers
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- name, phone, email
|
|
- address, address_reference
|
|
- latitude, longitude
|
|
- fiado_enabled, fiado_limit
|
|
- current_fiado_balance
|
|
- total_purchases, purchase_count
|
|
- last_purchase_at
|
|
- whatsapp_opt_in, notes, status
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Clientes"
|
|
|
|
- nombre: fiados
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id, customer_id, sale_id (UUIDs FK)
|
|
- original_amount, paid_amount, remaining_amount
|
|
- due_date
|
|
- status (pending, partial, paid, overdue, cancelled)
|
|
- description
|
|
- last_reminder_at, reminder_count
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Cuentas de credito"
|
|
|
|
- nombre: fiado_payments
|
|
columnas:
|
|
- id (UUID PK)
|
|
- fiado_id (UUID FK)
|
|
- amount
|
|
- payment_method
|
|
- notes
|
|
- created_by (UUID FK)
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Pagos de fiados"
|
|
|
|
orders:
|
|
descripcion: "Pedidos"
|
|
tablas:
|
|
- nombre: orders
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id, customer_id (UUIDs FK)
|
|
- order_number (UNIQUE)
|
|
- channel (whatsapp, app, web)
|
|
- subtotal, delivery_fee, discount_amount, total
|
|
- order_type (pickup, delivery)
|
|
- delivery_address, delivery_notes
|
|
- estimated_delivery_at
|
|
- status (pending, confirmed, preparing, ready, delivered, completed, cancelled)
|
|
- payment_status, payment_method
|
|
- timestamps por estado
|
|
- customer_notes, internal_notes
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Pedidos"
|
|
|
|
- nombre: order_items
|
|
columnas:
|
|
- id (UUID PK)
|
|
- order_id, product_id (UUIDs FK)
|
|
- product_name
|
|
- quantity, unit_price, subtotal
|
|
- notes
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Detalle de pedido"
|
|
|
|
subscriptions:
|
|
descripcion: "Planes y tokens IA"
|
|
tablas:
|
|
- nombre: plans
|
|
columnas:
|
|
- id (UUID PK)
|
|
- name, code (UNIQUE)
|
|
- description
|
|
- price_monthly, price_yearly
|
|
- currency (default MXN)
|
|
- included_tokens
|
|
- features (JSONB)
|
|
- max_products, max_users
|
|
- whatsapp_own_number
|
|
- stripe_price_id_monthly, stripe_price_id_yearly
|
|
- status, created_at, updated_at
|
|
rls: false
|
|
descripcion: "Planes disponibles"
|
|
|
|
- nombre: subscriptions
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id, plan_id (UUIDs FK)
|
|
- billing_cycle (monthly, yearly)
|
|
- current_period_start, current_period_end
|
|
- status (trialing, active, past_due, cancelled)
|
|
- cancel_at_period_end
|
|
- payment_method
|
|
- stripe_subscription_id, stripe_customer_id
|
|
- trial_ends_at
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Suscripciones activas"
|
|
|
|
- nombre: token_packages
|
|
columnas:
|
|
- id (UUID PK)
|
|
- name, tokens, price
|
|
- currency, bonus_tokens
|
|
- stripe_price_id
|
|
- status, created_at
|
|
rls: false
|
|
descripcion: "Paquetes de tokens"
|
|
|
|
- nombre: token_usage
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK)
|
|
- tokens_used
|
|
- action (chat, report, ocr, transcription)
|
|
- description, model
|
|
- input_tokens, output_tokens
|
|
- reference_type, reference_id
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Consumo de tokens"
|
|
|
|
- nombre: tenant_token_balance
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK UNIQUE)
|
|
- available_tokens, used_tokens
|
|
- last_reset_at
|
|
- updated_at
|
|
rls: true
|
|
descripcion: "Balance de tokens"
|
|
|
|
messaging:
|
|
descripcion: "WhatsApp y notificaciones"
|
|
tablas:
|
|
- nombre: conversations
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id (UUID FK nullable)
|
|
- phone_number, contact_name
|
|
- conversation_type (owner, customer, support, onboarding)
|
|
- status (active, archived, blocked)
|
|
- last_message_at, last_message_preview
|
|
- unread_count
|
|
- wa_conversation_id
|
|
- created_at, updated_at
|
|
rls: true
|
|
descripcion: "Conversaciones WhatsApp"
|
|
|
|
- nombre: messages
|
|
columnas:
|
|
- id (UUID PK)
|
|
- conversation_id (UUID FK)
|
|
- direction (inbound, outbound)
|
|
- message_type (text, image, audio, video, document, location)
|
|
- content, media_url, media_mime_type
|
|
- processed_by_llm, llm_response_id, tokens_used
|
|
- wa_message_id, wa_status, wa_timestamp
|
|
- error_code, error_message
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Mensajes"
|
|
|
|
- nombre: notifications
|
|
columnas:
|
|
- id (UUID PK)
|
|
- tenant_id, user_id (UUIDs FK)
|
|
- notification_type (low_stock, new_order, fiado_reminder, daily_summary)
|
|
- channels (TEXT[])
|
|
- title, body
|
|
- data (JSONB)
|
|
- push_sent, push_sent_at
|
|
- whatsapp_sent, whatsapp_sent_at
|
|
- read_at
|
|
- created_at
|
|
rls: true
|
|
descripcion: "Notificaciones push y WhatsApp"
|
|
|
|
# ============================================================================
|
|
# ENUMS
|
|
# ============================================================================
|
|
|
|
enums:
|
|
- nombre: integration_type
|
|
valores:
|
|
- whatsapp
|
|
- llm
|
|
- stripe
|
|
- mercadopago
|
|
- clip
|
|
archivo: "12-integrations.sql"
|
|
|
|
- nombre: integration_provider
|
|
valores:
|
|
- meta
|
|
- openai
|
|
- openrouter
|
|
- anthropic
|
|
- ollama
|
|
- azure_openai
|
|
- stripe
|
|
- mercadopago
|
|
- clip
|
|
archivo: "12-integrations.sql"
|
|
|
|
# ============================================================================
|
|
# SEEDS
|
|
# ============================================================================
|
|
|
|
seeds:
|
|
- archivo: "01-plans.sql"
|
|
lineas: 17
|
|
descripcion: "Planes de suscripcion y paquetes de tokens"
|
|
datos:
|
|
- "Plan Changarrito: $99/mes, 500 tokens, 100 productos max"
|
|
- "Plan Tiendita: $199/mes, 2000 tokens, sin limite"
|
|
- "Paquetes: 1000, 3000, 8000, 20000 tokens"
|
|
|
|
- archivo: "02-templates.sql"
|
|
lineas: 103
|
|
descripcion: "Templates de productos por proveedor"
|
|
proveedores:
|
|
- Bimbo (10 productos)
|
|
- Coca-Cola (15 productos)
|
|
- Sabritas (10 productos)
|
|
- Pepsi (7 productos)
|
|
- Ricolino (7 productos)
|
|
- Lacteos (6 productos)
|
|
- Basicos (9 productos)
|
|
- Comida (11 productos)
|
|
total_productos: 75
|
|
|
|
# ============================================================================
|
|
# SCRIPTS
|
|
# ============================================================================
|
|
|
|
scripts:
|
|
creacion: "database/scripts/create-database.sh"
|
|
recreacion: "database/scripts/recreate-database.sh"
|
|
validacion: "database/scripts/validate-integrations.sh"
|
|
|
|
# ============================================================================
|
|
# RUTAS
|
|
# ============================================================================
|
|
|
|
rutas:
|
|
schemas_base: "database/schemas/"
|
|
seeds_base: "database/seeds/"
|
|
scripts_base: "database/scripts/"
|
|
init_base: "database/init/"
|
|
|
|
# ============================================================================
|
|
# COMANDOS
|
|
# ============================================================================
|
|
|
|
comandos:
|
|
crear_db: "./database/scripts/create-database.sh"
|
|
recrear_db: "./database/scripts/recreate-database.sh"
|
|
validar: "./database/scripts/validate-integrations.sh"
|
|
conectar: "psql -d michangarrito_dev"
|
|
|
|
# ============================================================================
|
|
# NOTAS
|
|
# ============================================================================
|
|
|
|
notas:
|
|
- "RLS habilitado en todas las tablas excepto tenants, plans, token_packages, otp_codes, product_templates"
|
|
- "Multi-tenant por columna tenant_id en todas las tablas"
|
|
- "Trigger updated_at automatico en tablas con updated_at"
|
|
- "4 extensiones PostgreSQL para UUIDs, criptografia y busqueda de texto"
|
|
- "2 ENUMs para tipos de integracion y proveedores"
|
|
- "75 productos predefinidos en templates para onboarding rapido"
|