diff --git a/src/app/actions/entity.ts b/src/app/actions/entity.ts index 12ef12f..376bfee 100644 --- a/src/app/actions/entity.ts +++ b/src/app/actions/entity.ts @@ -71,14 +71,39 @@ export async function updateEntityConfig(data: any) { revalidatePath('/'); return result; } else { - const result = await databases.createDocument( - DATABASE_ID, - COLLECTION_ID, - SINGLETON_ID, - parsedData - ); - revalidatePath('/'); - return result; + try { + const result = await databases.createDocument( + DATABASE_ID, + COLLECTION_ID, + SINGLETON_ID, + parsedData + ); + revalidatePath('/'); + return result; + } catch (createErr: any) { + // If the collection genuinely does not exist, auto-create it natively + if (createErr?.code === 404 && createErr?.type === 'collection_not_found') { + console.log('Colección entity_config no existe. Creando automáticamente...'); + await databases.createCollection(DATABASE_ID, COLLECTION_ID, 'Configuración de Entidad'); + await new Promise(r => setTimeout(r, 1500)); // Propagate + + // Create minimal schema attributes lazily, Appwrite handles conflicts on re-runs + const textAttrs = ['razon_social', 'logotipo', 'ciudad', 'direccion', 'numero_contacto', 'numero_whatsapp', 'email', 'sitio_web', 'tv_url', 'radio_url', 'facebook_url', 'instagram_url', 'x_url', 'youtube_url', 'telegram_url']; + for (const a of textAttrs) { + try { await databases.createStringAttribute(DATABASE_ID, COLLECTION_ID, a, 500, false); } catch (e) {} + } + try { await databases.createBooleanAttribute(DATABASE_ID, COLLECTION_ID, 'mostrar_texto_logo', false, false); } catch(e){} + + console.log('Atributos inyectados. Esperando 3 segundos...'); + await new Promise(r => setTimeout(r, 3500)); // Wait for attributes to be Available + + const result = await databases.createDocument(DATABASE_ID, COLLECTION_ID, SINGLETON_ID, parsedData); + revalidatePath('/'); + return result; + } else { + throw createErr; + } + } } } catch (error) { console.error('Error updating entity config:', error); diff --git a/src/components/publications/PublicationLayout.tsx b/src/components/publications/PublicationLayout.tsx index 7df905c..ef3f3d3 100644 --- a/src/components/publications/PublicationLayout.tsx +++ b/src/components/publications/PublicationLayout.tsx @@ -377,7 +377,7 @@ export const PublicationLayout: React.FC = ({ options={relationData.tipo_publicacion_id || []} labelKey="descripcion" value={ - typeof initialData?.tipo_publicacion_id === 'object' + initialData?.tipo_publicacion_id && typeof initialData.tipo_publicacion_id === 'object' ? initialData.tipo_publicacion_id.$id : initialData?.tipo_publicacion_id || '' } @@ -462,7 +462,7 @@ export const PublicationLayout: React.FC = ({ options={relationData.cliente_id || []} labelKey="nombre_entidad" value={ - typeof initialData?.cliente_id === 'object' + initialData?.cliente_id && typeof initialData.cliente_id === 'object' ? initialData.cliente_id.$id : initialData?.cliente_id || '' }