mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 10:24:26 +00:00
Implementación completa de la Fase 1.1 y 1.2 del proyecto SalonOS: ## Cambios en Reglas de Negocio (PRD.md, AGENTS.md, TASKS.md) - Actualizado reset de invitaciones de mensual a semanal (Lunes 00:00 UTC) - Jerarquía de roles actualizada: Admin > Manager > Staff > Artist > Customer - Artistas (antes colaboradoras) ahora tienen rol 'artist' - Staff/Manager/Admin pueden ver PII de customers - Artist solo ve nombre y notas de customers (restricción de privacidad) ## Estructura del Proyecto (Next.js 14) - app/boutique/: Frontend de cliente - app/hq/: Dashboard administrativo - app/api/: API routes - components/: Componentes UI reutilizables (boutique, hq, shared) - lib/: Lógica de negocio (supabase, db, utils) - db/: Esquemas, migraciones y seeds - integrations/: Stripe, Google Calendar, WhatsApp - scripts/: Scripts de utilidad y automatización - docs/: Documentación del proyecto ## Esquema de Base de Datos (Supabase PostgreSQL) 8 tablas creadas: - locations: Ubicaciones con timezone - resources: Recursos físicos (estaciones, habitaciones, equipos) - staff: Personal con roles jerárquicos - services: Catálogo de servicios - customers: Información de clientes con tier (free/gold) - invitations: Sistema de invitaciones semanales - bookings: Sistema de reservas con short_id (6 caracteres) - audit_logs: Registro de auditoría automática 14 funciones creadas: - generate_short_id(): Generador de Short ID (6 chars, collision-safe) - generate_invitation_code(): Generador de códigos de invitación (10 chars) - reset_weekly_invitations_for_customer(): Reset individual de invitaciones - reset_all_weekly_invitations(): Reset masivo de invitaciones - validate_secondary_artist_role(): Validación de secondary_artist - log_audit(): Trigger de auditoría automática - get_current_user_role(): Obtener rol del usuario actual - is_staff_or_higher(): Verificar si es admin/manager/staff - is_artist(): Verificar si es artist - is_customer(): Verificar si es customer - is_admin(): Verificar si es admin - update_updated_at(): Actualizar timestamps - generate_booking_short_id(): Generar Short ID automáticamente - get_week_start(): Obtener inicio de semana 17+ triggers activos: - Auditores automáticos en tablas críticas - Timestamps updated_at en todas las tablas - Validación de secondary_artist (trigger en lugar de constraint) 20+ políticas RLS configuradas: - Restricción crítica: Artist no ve email/phone de customers - Jerarquía de roles: Admin > Manager > Staff > Artist > Customer - Políticas granulares por tipo de operación y rol 6 tipos ENUM: - user_role: admin, manager, staff, artist, customer - customer_tier: free, gold - booking_status: pending, confirmed, cancelled, completed, no_show - invitation_status: pending, used, expired - resource_type: station, room, equipment - audit_action: create, update, delete, reset_invitations, payment, status_change ## Scripts de Utilidad - check-connection.sh: Verificar conexión a Supabase - simple-verify.sh: Verificar migraciones instaladas - simple-seed.sh: Crear datos de prueba - create-auth-users.js: Crear usuarios de Auth en Supabase - verify-migration.sql: Script de verificación SQL completo - seed-data.sql: Script de seed de datos SQL completo ## Documentación - docs/STEP_BY_STEP_VERIFICATION.md: Guía paso a paso de verificación - docs/STEP_BY_STEP_AUTH_CONFIG.md: Guía paso a paso de configuración Auth - docs/POST_MIGRATION_SUCCESS.md: Guía post-migración - docs/MIGRATION_CORRECTION.md: Detalle de correcciones aplicadas - docs/QUICK_START_POST_MIGRATION.md: Guía rápida de referencia - docs/SUPABASE_DASHBOARD_MIGRATION.md: Guía de ejecución en Dashboard - docs/00_FULL_MIGRATION_FINAL_README.md: Guía de migración final - SIMPLE_GUIDE.md: Guía simple de inicio - FASE_1_STATUS.md: Estado de la Fase 1 ## Configuración - package.json: Dependencias y scripts de npm - tsconfig.json: Configuración TypeScript con paths aliases - next.config.js: Configuración Next.js - tailwind.config.ts: Tema personalizado con colores primary, secondary, gold - postcss.config.js: Configuración PostCSS - .gitignore: Archivos excluidos de git - .env.example: Template de variables de entorno ## Correcciones Aplicadas 1. Constraint de subquery en CHECK reemplazado por trigger de validación - PostgreSQL no permite subqueries en CHECK constraints - validate_secondary_artist_role() ahora es un trigger 2. Variable no declarada en loop - customer_record RECORD; añadido en bloque DECLARE ## Principios Implementados - UTC-first: Todos los timestamps se almacenan en UTC - Sistema Doble Capa: Validación Staff/Artist + Recurso físico - Reset semanal: Invitaciones se resetean cada Lunes 00:00 UTC - Idempotencia: Procesos de reset son idempotentes y auditados - Privacidad: Artist solo ve nombre y notas de customers - Auditoría: Todas las acciones críticas se registran automáticamente - Short ID: 6 caracteres alfanuméricos como referencia humana - UUID: Identificador primario interno ## Próximos Pasos - Ejecutar scripts de verificación y seed - Configurar Auth en Supabase Dashboard - Implementar Tarea 1.3: Short ID & Invitaciones (backend) - Implementar Tarea 1.4: CRM Base (endpoints CRUD)
440 lines
12 KiB
JavaScript
440 lines
12 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Script de seed de datos - SalonOS
|
|
* Crea datos de prueba para development
|
|
*/
|
|
|
|
const { createClient } = require('@supabase/supabase-js')
|
|
|
|
// Cargar variables de entorno
|
|
require('dotenv').config({ path: '.env.local' })
|
|
|
|
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
|
const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY
|
|
|
|
if (!supabaseUrl || !supabaseServiceKey) {
|
|
console.error('❌ ERROR: Faltan variables de entorno')
|
|
console.error('Asegúrate de tener NEXT_PUBLIC_SUPABASE_URL y SUPABASE_SERVICE_ROLE_KEY en .env.local')
|
|
process.exit(1)
|
|
}
|
|
|
|
const supabase = createClient(supabaseUrl, supabaseServiceKey)
|
|
|
|
console.log('==========================================')
|
|
console.log('SALONOS - SEED DE DATOS')
|
|
console.log('==========================================')
|
|
console.log()
|
|
|
|
async function seedLocations() {
|
|
console.log('📍 Creando locations...')
|
|
|
|
const { data, error } = await supabase.from('locations').insert([
|
|
{
|
|
name: 'Salón Principal - Centro',
|
|
timezone: 'America/Mexico_City',
|
|
address: 'Av. Reforma 222, Centro Histórico, Ciudad de México',
|
|
phone: '+52 55 1234 5678',
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Salón Norte - Polanco',
|
|
timezone: 'America/Mexico_City',
|
|
address: 'Av. Masaryk 123, Polanco, Ciudad de México',
|
|
phone: '+52 55 2345 6789',
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Salón Sur - Coyoacán',
|
|
timezone: 'America/Mexico_City',
|
|
address: 'Calle Hidalgo 456, Coyoacán, Ciudad de México',
|
|
phone: '+52 55 3456 7890',
|
|
is_active: true,
|
|
},
|
|
]).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear locations:', error)
|
|
return []
|
|
}
|
|
|
|
console.log(`✅ ${data.length} locations creadas`)
|
|
return data
|
|
}
|
|
|
|
async function seedResources(locations) {
|
|
console.log('🪑 Creando resources...')
|
|
|
|
const resources = []
|
|
|
|
for (const location of locations) {
|
|
const { data, error } = await supabase.from('resources').insert([
|
|
{
|
|
location_id: location.id,
|
|
name: `Estación ${Math.floor(Math.random() * 100)}`,
|
|
type: 'station',
|
|
capacity: 1,
|
|
is_active: true,
|
|
},
|
|
{
|
|
location_id: location.id,
|
|
name: `Sala VIP ${Math.floor(Math.random() * 100)}`,
|
|
type: 'room',
|
|
capacity: 2,
|
|
is_active: true,
|
|
},
|
|
]).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear resources:', error)
|
|
continue
|
|
}
|
|
|
|
resources.push(...data)
|
|
}
|
|
|
|
console.log(`✅ ${resources.length} resources creadas`)
|
|
return resources
|
|
}
|
|
|
|
async function seedStaff(locations) {
|
|
console.log('👥 Creando staff...')
|
|
|
|
const { data, error } = await supabase.from('staff').insert([
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000001',
|
|
location_id: locations[0].id,
|
|
role: 'admin',
|
|
display_name: 'Admin Principal',
|
|
phone: '+52 55 1111 2222',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000002',
|
|
location_id: locations[0].id,
|
|
role: 'manager',
|
|
display_name: 'Manager Centro',
|
|
phone: '+52 55 2222 3333',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000003',
|
|
location_id: locations[0].id,
|
|
role: 'staff',
|
|
display_name: 'Staff Coordinadora',
|
|
phone: '+52 55 3333 4444',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000004',
|
|
location_id: locations[0].id,
|
|
role: 'artist',
|
|
display_name: 'Artist María García',
|
|
phone: '+52 55 4444 5555',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000005',
|
|
location_id: locations[0].id,
|
|
role: 'artist',
|
|
display_name: 'Artist Ana Rodríguez',
|
|
phone: '+52 55 5555 6666',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000006',
|
|
location_id: locations[1].id,
|
|
role: 'manager',
|
|
display_name: 'Manager Polanco',
|
|
phone: '+52 55 6666 7777',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000007',
|
|
location_id: locations[1].id,
|
|
role: 'artist',
|
|
display_name: 'Artist Carla López',
|
|
phone: '+52 55 7777 8888',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '00000000-0000-0000-0000-000000000008',
|
|
location_id: locations[2].id,
|
|
role: 'artist',
|
|
display_name: 'Artist Laura Martínez',
|
|
phone: '+52 55 8888 9999',
|
|
is_active: true,
|
|
},
|
|
]).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear staff:', error)
|
|
return []
|
|
}
|
|
|
|
console.log(`✅ ${data.length} staff creados`)
|
|
return data
|
|
}
|
|
|
|
async function seedServices() {
|
|
console.log('💇 Creando services...')
|
|
|
|
const { data, error } = await supabase.from('services').insert([
|
|
{
|
|
name: 'Corte y Estilizado',
|
|
description: 'Corte de cabello profesional con lavado y estilizado',
|
|
duration_minutes: 60,
|
|
base_price: 500.00,
|
|
requires_dual_artist: false,
|
|
premium_fee_enabled: false,
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Color Completo',
|
|
description: 'Tinte completo con protección capilar',
|
|
duration_minutes: 120,
|
|
base_price: 1200.00,
|
|
requires_dual_artist: false,
|
|
premium_fee_enabled: true,
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Balayage Premium',
|
|
description: 'Técnica de balayage con productos premium',
|
|
duration_minutes: 180,
|
|
base_price: 2000.00,
|
|
requires_dual_artist: true,
|
|
premium_fee_enabled: true,
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Tratamiento Kératina',
|
|
description: 'Tratamiento de kératina para cabello dañado',
|
|
duration_minutes: 90,
|
|
base_price: 1500.00,
|
|
requires_dual_artist: false,
|
|
premium_fee_enabled: false,
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Peinado Evento',
|
|
description: 'Peinado para eventos especiales',
|
|
duration_minutes: 45,
|
|
base_price: 800.00,
|
|
requires_dual_artist: false,
|
|
premium_fee_enabled: true,
|
|
is_active: true,
|
|
},
|
|
{
|
|
name: 'Servicio Express (Dual Artist)',
|
|
description: 'Servicio rápido con dos artists simultáneas',
|
|
duration_minutes: 30,
|
|
base_price: 600.00,
|
|
requires_dual_artist: true,
|
|
premium_fee_enabled: true,
|
|
is_active: true,
|
|
},
|
|
]).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear services:', error)
|
|
return []
|
|
}
|
|
|
|
console.log(`✅ ${data.length} services creados`)
|
|
return data
|
|
}
|
|
|
|
async function seedCustomers() {
|
|
console.log('👩 Creando customers...')
|
|
|
|
const { data, error } = await supabase.from('customers').insert([
|
|
{
|
|
user_id: '10000000-0000-0000-0000-000000000001',
|
|
first_name: 'Sofía',
|
|
last_name: 'Ramírez',
|
|
email: 'sofia.ramirez@example.com',
|
|
phone: '+52 55 1111 1111',
|
|
tier: 'gold',
|
|
notes: 'Cliente VIP. Prefiere Artists María y Ana.',
|
|
total_spent: 15000.00,
|
|
total_visits: 25,
|
|
last_visit_date: '2025-12-20',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '10000000-0000-0000-0000-000000000002',
|
|
first_name: 'Valentina',
|
|
last_name: 'Hernández',
|
|
email: 'valentina.hernandez@example.com',
|
|
phone: '+52 55 2222 2222',
|
|
tier: 'gold',
|
|
notes: 'Cliente regular. Prefiere horarios de la mañana.',
|
|
total_spent: 8500.00,
|
|
total_visits: 15,
|
|
last_visit_date: '2025-12-15',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '10000000-0000-0000-0000-000000000003',
|
|
first_name: 'Camila',
|
|
last_name: 'López',
|
|
email: 'camila.lopez@example.com',
|
|
phone: '+52 55 3333 3333',
|
|
tier: 'free',
|
|
notes: 'Nueva cliente. Referida por Valentina.',
|
|
total_spent: 500.00,
|
|
total_visits: 1,
|
|
last_visit_date: '2025-12-10',
|
|
is_active: true,
|
|
},
|
|
{
|
|
user_id: '10000000-0000-0000-0000-000000000004',
|
|
first_name: 'Isabella',
|
|
last_name: 'García',
|
|
email: 'isabella.garcia@example.com',
|
|
phone: '+52 55 4444 4444',
|
|
tier: 'gold',
|
|
notes: 'Cliente VIP. Requiere servicio de Balayage.',
|
|
total_spent: 22000.00,
|
|
total_visits: 30,
|
|
last_visit_date: '2025-12-18',
|
|
is_active: true,
|
|
},
|
|
]).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear customers:', error)
|
|
return []
|
|
}
|
|
|
|
console.log(`✅ ${data.length} customers creados`)
|
|
return data
|
|
}
|
|
|
|
async function seedInvitations(customers) {
|
|
console.log('💌 Creando invitations...')
|
|
|
|
const weekStart = new Date()
|
|
weekStart.setDate(weekStart.getDate() - weekStart.getDay() + 1) // Monday
|
|
weekStart.setHours(0, 0, 0, 0)
|
|
|
|
const invitations = []
|
|
|
|
for (const customer of customers) {
|
|
if (customer.tier === 'gold') {
|
|
for (let i = 0; i < 5; i++) {
|
|
const { data, error } = await supabase.from('invitations').insert({
|
|
inviter_id: customer.id,
|
|
code: await generateRandomCode(),
|
|
status: 'pending',
|
|
week_start_date: weekStart.toISOString().split('T')[0],
|
|
expiry_date: new Date(weekStart.getTime() + 6 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
|
|
}).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear invitations:', error)
|
|
continue
|
|
}
|
|
|
|
invitations.push(...data)
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`✅ ${invitations.length} invitations creadas`)
|
|
return invitations
|
|
}
|
|
|
|
async function generateRandomCode() {
|
|
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
let code = ''
|
|
for (let i = 0; i < 10; i++) {
|
|
code += chars.charAt(Math.floor(Math.random() * chars.length))
|
|
}
|
|
return code
|
|
}
|
|
|
|
async function seedBookings(customers, staff, resources, services, locations) {
|
|
console.log('📅 Creando bookings de prueba...')
|
|
|
|
const now = new Date()
|
|
const bookings = []
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
const startTime = new Date(now.getTime() + (i + 1) * 24 * 60 * 60 * 1000)
|
|
const endTime = new Date(startTime.getTime() + 60 * 60 * 1000)
|
|
|
|
const { data, error } = await supabase.from('bookings').insert({
|
|
customer_id: customers[i % customers.length].id,
|
|
staff_id: staff.filter(s => s.role === 'artist')[i % staff.filter(s => s.role === 'artist').length].id,
|
|
location_id: locations[0].id,
|
|
resource_id: resources[0].id,
|
|
service_id: services[i % services.length].id,
|
|
start_time_utc: startTime.toISOString(),
|
|
end_time_utc: endTime.toISOString(),
|
|
status: 'confirmed',
|
|
deposit_amount: 200.00,
|
|
total_amount: services[i % services.length].base_price,
|
|
is_paid: true,
|
|
payment_reference: `pay_${Math.random().toString(36).substring(7)}`,
|
|
}).select()
|
|
|
|
if (error) {
|
|
console.error('❌ Error al crear bookings:', error)
|
|
continue
|
|
}
|
|
|
|
bookings.push(...data)
|
|
}
|
|
|
|
console.log(`✅ ${bookings.length} bookings creados`)
|
|
return bookings
|
|
}
|
|
|
|
async function main() {
|
|
try {
|
|
const locations = await seedLocations()
|
|
if (locations.length === 0) throw new Error('No se crearon locations')
|
|
|
|
const resources = await seedResources(locations)
|
|
const staff = await seedStaff(locations)
|
|
if (staff.length === 0) throw new Error('No se creó staff')
|
|
|
|
const services = await seedServices()
|
|
if (services.length === 0) throw new Error('No se crearon services')
|
|
|
|
const customers = await seedCustomers()
|
|
if (customers.length === 0) throw new Error('No se crearon customers')
|
|
|
|
const invitations = await seedInvitations(customers)
|
|
const bookings = await seedBookings(customers, staff, resources, services, locations)
|
|
|
|
console.log()
|
|
console.log('==========================================')
|
|
console.log('✅ SEED DE DATOS COMPLETADO')
|
|
console.log('==========================================')
|
|
console.log()
|
|
console.log('📊 Resumen:')
|
|
console.log(` Locations: ${locations.length}`)
|
|
console.log(` Resources: ${resources.length}`)
|
|
console.log(` Staff: ${staff.length}`)
|
|
console.log(` Services: ${services.length}`)
|
|
console.log(` Customers: ${customers.length}`)
|
|
console.log(` Invitations: ${invitations.length}`)
|
|
console.log(` Bookings: ${bookings.length}`)
|
|
console.log()
|
|
console.log('🎉 La base de datos está lista para desarrollo')
|
|
console.log()
|
|
console.log('📝 Próximos pasos:')
|
|
console.log(' 1. Configurar Auth en Supabase Dashboard')
|
|
console.log(' 2. Probar la API de bookings')
|
|
console.log(' 3. Implementar endpoints faltantes')
|
|
} catch (error) {
|
|
console.error('❌ Error inesperado:', error)
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
main()
|