name: doccorretor-upload-link-flow
description: Upload link flow for DocCorretor — public link generates → broker creates client → uploads documents
DocCorretor — Upload Link Flow
Goal
Generate a shareable link (no auth required). Broker opens link → fills client data (name, CPF/CNPJ, plan) → uploads documents.
Database table structure
- -
client_upload_links:id, client_id (FK→clients, nullable), token, folder_id (FK→client_folders), expires_at (NOT NULL), used_at, created_at - -
client_folders:id, client_id (FK→clients, nullable after ALTER), name, created_by (NOT NULL), created_at - - Creates folder (
client_folders) withclient_id=NULL - - Creates link (
client_upload_links) withclient_id=NULL - - Returns:
{token, url}where url =https://documentos.rochasalesseguros.com.br/upload/{token} - - Validates token exists and has no client yet
- - Creates
clientsrow - - Updates
client_upload_links.client_id - - Updates
client_folders.client_id(same folder from step 1 — reuse, don't create new) - - Returns:
{clientId, folderId, clientName} - - Uses
clientIdandfolderIdreturned from step 2 - - Uploads file to Supabase Storage
- - Inserts
documentsrow - - Management API:
POST https://api.supabase.com/v1/projects/{ref}/database/querywith PAT header — bypasses RLS, respects constraints - - REST API (
supabase.rest.v1/): Service role JWT does NOT work asapikeyfor inserts - - Rule: All public Supabase writes go through Management API via
fetch()server-side (same-origin, no CORS) - -
/var/www/documentos2/app/src/routes/api/public/generate-upload-link.ts - -
/var/www/documentos2/app/src/routes/api/public/create-client-with-link.ts - -
/var/www/documentos2/app/src/routes/api/public/upload-with-link.ts - -
/var/www/documentos2/app/src/routes/api/public/validate-upload-link.ts— uses LEFT JOIN to support links without client yet - -
/var/www/documentos2/app/src/routes/upload.$token.tsx— public upload page with inline client creation form - -
/var/www/documentos2/app/src/routes/index.tsx— dashboard with "Link para corretor" button - -
23502 null in column "client_id" violates not-null constraint→ run ALTER TABLE or use system user ID - -
23503 FK constraint violation→ system user not inauth.users→ insert it first - -
Link inválidoon validate → LEFT JOIN needed (not INNER JOIN) for links without client yet
Flow (3 steps)
Step 1 — Generate link
Route: POST /api/public/generate-upload-link
Step 2 — Broker creates client
Route: POST /api/public/create-client-with-link
Step 3 — Upload documents
Route: POST /api/public/upload-with-link
Critical FK constraints and fixes
clients.owner_id → auth.users (NOT NULL)
clients.owner_id is NOT NULL and FK → auth.users(id). For system-created clients:
1. Create system user first (run once):
`sql
INSERT INTO auth.users (id, email, created_at, aud, role)
VALUES ('00000000-0000-0000-0000-000000000001', 'system@rochasales.com.br', now(), 'authenticated', 'authenticated')
ON CONFLICT (id) DO NOTHING;
`
2. Use 00000000-0000-0000-0000-000000000001 as owner_id in all system-created clients
client_folders.client_id — originally NOT NULL
`sql
ALTER TABLE public.client_folders ALTER COLUMN client_id DROP NOT NULL;
`
Allow client_id to be NULL initially (filled when broker creates client).
Management API vs REST API
PAT: sbp_REDACTED
Project ref: dauftiqcvgaydddoxqhh
Files
Build & Deploy
`bash
cd /var/www/documentos2/app
rm -rf .tanstack dist node_modules/.vite .vite
PATH="/root/.nvm/versions/node/v20.20.2/bin:$PATH" npm run build
cp -r dist/server/assets/* dist/client/assets/ # CRITICAL
sudo systemctl restart documentos
`
Validate endpoint test
`bash
curl -s "http://localhost:3010/api/public/validate-upload-link?token={token}"
Returns: {client_id, client_name, health_plan, folder_id, is_new_client: true}
`