# Guardar usuario - [saveUsersLogeds]

## 🧾 **Descripción**

*Registra y actualiza información de usuarios que inician sesión desde la aplicación móvil.*  
*Además, valida si la versión de la app instalada es compatible con la versión mínima definida en el servidor.*

**Funcionalidad:**

1. **Verifica la versión de la app** contra<span style="color: rgb(224, 62, 45);"> </span><span style="color: rgb(186, 55, 42);">`<strong><span style="color: rgb(224, 62, 45);">versiones.json</span></strong>`</span>.
2. **Crea o actualiza** el registro de un usuario en la base de datos `mysql2.users`.
3. Devuelve mensaje de éxito o error según el proceso.

---

# 🚀 **Endpoint**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-post-%2Fsave-users-log"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr"><span style="color: rgb(224, 62, 45);">**`POST /save-users-logeds`**</span></div></div>> El nombre real depende del archivo de rutas de Laravel (web.php / api.php).

---

# 🔐 **Seguridad**

- Requiere enviar parámetros en el body (no hay autenticación nativa).
- Se recomienda usarlo en rutas **API protegidas**.

---

# 📥 **Request Body**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--3"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2">  
</div></div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div><table border="1" id="bkmrk-%7B-%22user%22%3A-%22string%22%2C-" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

### Campos

<div class="_tableContainer_1rjym_1" id="bkmrk-campo-tipo-requerido"><div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1"><table class="w-fit min-w-(--thread-content-width)" data-end="1320" data-start="1068"><thead data-end="1115" data-start="1068"><tr data-end="1115" data-start="1068"><th data-col-size="sm" data-end="1079" data-start="1068">Campo</th><th data-col-size="sm" data-end="1088" data-start="1079">Tipo</th><th data-col-size="sm" data-end="1100" data-start="1088">Requerido</th><th data-col-size="md" data-end="1115" data-start="1100">Descripción</th></tr></thead><tbody data-end="1320" data-start="1164"><tr data-end="1237" data-start="1164"><td data-col-size="sm" data-end="1175" data-start="1164">user</td><td data-col-size="sm" data-end="1184" data-start="1175">string</td><td data-col-size="sm" data-end="1196" data-start="1184">Sí</td><td data-col-size="md" data-end="1237" data-start="1196">Username que se registra o actualiza.</td></tr><tr data-end="1320" data-start="1238"><td data-col-size="sm" data-end="1249" data-start="1238">version</td><td data-col-size="sm" data-end="1258" data-start="1249">string</td><td data-col-size="sm" data-end="1270" data-start="1258">Sí</td><td data-col-size="md" data-end="1320" data-start="1270">Versión de la app instalada en el dispositivo.</td></tr></tbody></table>

</div></div>---

# 📤 **Responses**

### ❗ Importante

En tu código actual, la primera línea retorna **inmediatamente**:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--5"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2">  
</div></div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div><table border="1" id="bkmrk-return-%5B-%22valor%22%3D%3Etr" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-keyword">return</span> [   <span class="hljs-string">"valor"</span>=><span class="hljs-literal">true</span>,   <span class="hljs-string">"msn"</span>=> <span class="hljs-string">"Registro actualizado"</span>,];`</td></tr></tbody></table>

Por lo tanto **todo el servicio está anulado**.  
Si esto es accidental, debes eliminar ese `return`.

Aun así, la documentación refleja lo que *debería hacer* según el resto del código.

---

## ✔️ **Versión actualizada (200 OK)**

Si la versión enviada es igual o mayor que la versión mínima del archivo `versiones.json`:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--7"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2">  
</div></div><div class="overflow-y-auto p-4" dir="ltr">  
</div></div><table border="1" id="bkmrk-%7B-%22valor%22%3A-true%2C-%22ms" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Registro Exitoso"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

o

<table border="1" id="bkmrk-%7B-%22valor%22%3A-true%2C-%22ms-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Registro actualizado"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

## ❌ **Versión desactualizada**

<table border="1" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Actualize su aplicacion en Play Store"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"version_app"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"1.0.1"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"version_service"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"1.0.5"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

## ❌ **Error al registrar o actualizar usuario**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--10"><div class="sticky top-9"></div><div class="overflow-y-auto p-4" dir="ltr"></div></div><table border="1" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Registre un usuario"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

o

<table border="1" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m-2" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Registro Actualizado"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

# 🧩 **Lógica Interna (detallada)**

### 1. Leer versión mínima del archivo:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%2Fpublic%2Fversion-app%2F"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr"><span style="color: rgb(224, 62, 45);">**`/<span class="hljs-built_in">public</span>/<span class="hljs-keyword">version</span>-app/versiones.json`**</span></div></div>**Campo utilizado:** `<span class="hljs-attribute">familia</span>`

### 2. Convertir versiones a enteros

**Ejemplo:**  
“1.2.3” → 123

Esto permite compararlas.

### 3. Validación:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-si-intversionapp-%3C-i"><div class="overflow-y-auto p-4" dir="ltr">**`<span class="hljs-built_in">Si</span> intVersionApp < intVersionDefine → debe actualizar`**</div></div>### 4. Registrar usuario:

- Si no existe → insertar username + primera sesión + última sesión
- Si existe → actualizar última sesión
- Base de datos usada: **mysql2**

Tablas y campos usados:

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-users.username-users"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`users.usernameusers.fecha_primera_susers.fecha_ultima_s`</div></div>---

# 📚 **Esquema del JSON de version-app/versiones.json**

**Se utiliza solo el campo:**

<table border="1" id="bkmrk-%7B-%22familia%22%3A-%221.0.5%22" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"familia"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"1.0.5"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

# 🗃 **Schemas**

### Request

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22user%22%3A-%22string%22%2C--1"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div><table border="1" id="bkmrk-%7B-%22user%22%3A-%22string%22%2C--2" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

### Response

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-%22boolean%22"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div><table border="1" id="bkmrk-%7B-%22valor%22%3A-%22boolean%22-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"boolean"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"version_app"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string(optional)"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"version_service"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string(optional)"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

# 🧪 **Ejemplo de uso (curl)**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-curl--x-post-https%3A%2F"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div><table border="1" id="bkmrk-curl--x-post-https%3A%2F-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`curl -X POST https://midominio.com/api/save-users-logeds \-H <span class="hljs-string">"Content-Type: application/json"</span> \-d <span class="hljs-string">'{"user":"jhon.doe","version":"1.0.2"}'</span>`</td></tr></tbody></table>

---

# ⚠️ Notas importantes para documentación

### 🔴 Problema detectado

**La primera línea del método cancela toda la lógica:**

<table border="1" id="bkmrk-return-%5B-%22valor%22%3D%3Etr-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-keyword">return</span> [   <span class="hljs-string">"valor"</span>=><span class="hljs-literal">true</span>,   <span class="hljs-string">"msn"</span>=> <span class="hljs-string">"Registro actualizado"</span>,];`</td></tr></tbody></table>

Si quieres que el servicio funcione como en la documentación, debes eliminar ese `return`.