Overview
Creates a new personalized workout and meal plan for a user. This endpoint uses a PostgreSQL function (create_user_plan) that automatically handles:
- Deactivating any existing active plans
- Setting appropriate expiration dates (5 weeks for free, 1 year for paid)
- Updating the user’s profile with plan metadata
- Validating freemium constraints
Authentication
Requires authenticated user via Supabase Auth. Uses Row Level Security (RLS) policies.Endpoint
Request Parameters
The authenticated user’s UUID from Supabase Auth
The complete WizardState object containing all plan configuration. See WizardState structure below.
Plan type:
"free" or "paid". Defaults to "free".WizardState Structure
Thep_plan_data parameter must contain a complete WizardState object:
Training Levels
principiante- Beginner, no prior experiencebasico- Basic, 1-3 months experienceintermedio- Intermediate, 3-12 months experienceavanzado- Advanced, 1+ years experienceelite- Elite athlete level
Training Goals
perder_grasa- Fat lossganar_musculo- Muscle gaintonificar- Toning/definitionresistencia- Enduranceflexibilidad- Flexibilityfuerza- Pure strengthenergia- Energy/vitalitysalud- General health
Equipment Types
sin_equipo- No equipment (bodyweight)gym_completo- Full gym accessmancuernas- Dumbbellsbandas- Resistance bandsbarra- Barbellbanco- Benchpull_up_bar- Pull-up barkettlebell- Kettlebellmaquinas- Machinestrx- TRX suspension- Plus more (see source code for complete list)
Freemium Logic
Free Plan Rules
- Users can create one free plan (lifetime)
- Free plans last 5 weeks from creation
- Once a free plan is created,
has_free_plan_usedis set totrue - Users cannot create another free plan after using their free one
Paid Plan Rules
- Requires active subscription (checked via
has_active_subscription(user_uuid)) - Paid plans last 1 year from creation
- Paid users can create unlimited plans
- Each new plan deactivates the previous one
Validation
Before creating a plan, the function checks:- Has active subscription? → If yes, allow paid plan
- Already has active plan? → Return error
already_has_plan - Already used free plan? → Return error
free_used - Otherwise → Allow free plan creation
Response
The ID of the newly created plan
Example Usage
Database Schema
Error Responses
Error message if plan creation fails
Common Errors
"Ya tienes un plan activo"- User already has an active plan"Ya usaste tu plan gratuito. Actualiza a premium para crear más planes."- Free plan already used"No se pudo verificar tu cuenta"- Authentication error
Related
- Check Plan Access - Check if user can create a plan
- Download Plan - Generate and download plan as PDF
- Subscription System - Learn about paid subscriptions