insta_manager

Contrato de la API interna (v0.1)

Este documento es el contrato entre server/ (Fastify) y web/ (React). Si un cambio lo rompe, actualiza este archivo en el mismo commit.

Salud

GET /api/health → { "status": "ok", "version": "0.1.0" }

Autenticación

El primer usuario se crea al arrancar a partir de ADMIN_USERNAME / ADMIN_PASSWORD si la tabla está vacía.

Cuentas de Instagram

type Account = {
  id: string;
  label: string;               // nombre interno, p. ej. "Moments Viajes"
  igUserId: string;            // ID de la cuenta profesional de Instagram
  username: string | null;     // rellenado al verificar
  profilePictureUrl: string | null;
  followersCount: number | null;
  apiMode: "instagram_login" | "facebook_login"; // por defecto "facebook_login" al crear
  appId: string;
  hasAppSecret: boolean;       // los secretos nunca se devuelven
  hasAccessToken: boolean;
  tokenExpiresAt: string | null;
  timezone: string;            // por defecto "Europe/Madrid"
  status: "unverified" | "ok" | "token_expiring" | "error";
  lastError: string | null;
  createdAt: string; updatedAt: string;
};

Modos de API (apiMode)

Renovación automática

Una tarea diaria renueva los tokens instagram_login que caducan en < 10 días (o sin caducidad conocida y con > 24 h). Si falla y el token sigue vigente → token_expiring; si ha caducado → error. En facebook_login con caducidad < 10 días → token_expiring (renovación manual).

Medios

type Media = {
  id: string; accountId: string | null;
  kind: "image" | "video";
  mimeType: string; sizeBytes: number;
  width: number | null; height: number | null; durationSec: number | null;
  url: string;                 // URL pública absoluta (PUBLIC_BASE_URL + /media/<archivo>)
  createdAt: string;
};

Publicaciones

type PostType = "image" | "carousel" | "reel" | "story";
type PostStatus = "draft" | "scheduled" | "publishing" | "published" | "failed" | "cancelled";
type Post = {
  id: string; accountId: string;
  type: PostType;
  caption: string;             // máx. 2200 caracteres, máx. 30 hashtags
  mediaIds: string[];          // ordenados; carousel 2-10; image/reel/story 1
  shareToFeed: boolean;        // solo reels
  coverUrl: string | null;     // portada opcional de reel
  locationId: string | null;   // no admitido en stories
  altText: string | null;      // texto alternativo; solo type "image", máx. 1000 caracteres
  collaborators: string[];     // usernames (sin @), máx. 3; no admitido en stories
  scheduledAt: string | null;  // UTC; null = borrador o "publicar ahora"
  status: PostStatus;
  attempts: number;
  lastError: string | null;
  igMediaId: string | null;
  permalink: string | null;
  publishedAt: string | null;
  createdAt: string; updatedAt: string;
};

Programador

Reglas de validación (compartidas)