# Obtener Usuario (1,2,3,4) - [getUser3]

## 🧾 **Descripción**

*Retorna la información del usuario ingresado en la app (empleado o estudiante).*  
*El servicio consulta primero la tabla **Employee** del ERP.*  
*Si no existe o no está activo, consulta la tabla **Student**.*  
*Finalmente, devuelve la información personal, laboral/educativa, permisos y datos complementarios.*

*Funciona como un **servicio maestro de login/datos del usuario** para la aplicación móvil.*

---

# 🚀 **Endpoint**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-post-%2Fget-user3"><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 /get-user3`**</span></div></div>> El nombre real depende del archivo **<span style="color: rgb(224, 62, 45);">`routes/api.php`</span>**.

---

# 🔐 **Seguridad**

- No se ve autenticación explícita, pero las consultas al ERP (<span style="color: rgb(224, 62, 45);">**`dbErp`**</span>, <span style="color: rgb(224, 62, 45);">**`ServiceErp`**</span>) requieren token/credenciales internas.
- Se recomienda protegerlo con autenticación JWT o middleware.

---

# 📥 **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-%22username%22%3A-%22strin" 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">"username"</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="1231" data-start="1033"><thead data-end="1075" data-start="1033"><tr data-end="1075" data-start="1033"><th data-col-size="sm" data-end="1041" data-start="1033">Campo</th><th data-col-size="sm" data-end="1048" data-start="1041">Tipo</th><th data-col-size="sm" data-end="1060" data-start="1048">Requerido</th><th data-col-size="md" data-end="1075" data-start="1060">Descripción</th></tr></thead><tbody data-end="1231" data-start="1121"><tr data-end="1231" data-start="1121"><td data-col-size="sm" data-end="1132" data-start="1121">username</td><td data-col-size="sm" data-end="1141" data-start="1132">string</td><td data-col-size="sm" data-end="1146" data-start="1141">Sí</td><td data-col-size="md" data-end="1231" data-start="1146">Email o DNI/pasaporte. Si contiene “@” se asume email, de lo contrario documento.</td></tr></tbody></table>

</div></div>---

# 🧩 **Lógica Interna Detallada**

## 1️⃣ Determinar si buscar por email o documento

- Si el parámetro incluye “@” → buscar por <span style="color: rgb(224, 62, 45);">**`emp.user_id`**</span>
- Sino → buscar por <span style="color: rgb(224, 62, 45);">**`emp.passport_number`**</span>

## 2️⃣ Consultar información del empleado en ERP

Consulta SQL con múltiples JOIN:

**Tablas involucradas:**

- `tabEmployee` (principal)
- `tabUser`
- `tabBranch`
- `tabContrato de Trabajo`
- `tabDepartment`

Si no encuentra empleado → pasa a modo estudiante.

---

### ❌ Errores de empleado antes de consultar estudiante:

- No `valor` en la respuesta
- `response` vacío  
    → continuar con búsqueda en Student

---

## 3️⃣ Consultar información del estudiante (fallback)

**Si el usuario no es empleado, consulta en:**

- `tabStudent`
- `tabUser`
- `tabBranch`
- `tabJob Applicant`
- `tabRequerimiento de Personal`
- `tabDesignation`
- `tabDepartment`

### Validaciones de estudiante:

- Si no existe →
    
    <table border="1" style="border-collapse: collapse; width: 100%; height: 33.8px;"><colgroup><col style="width: 99.8711%;"></col></colgroup><tbody><tr style="height: 33.8px;"><td style="height: 33.8px;">`<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">"Sin Estudiante: ..."</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span>`</td></tr></tbody></table>
- Si el usuario está deshabilitado (<span style="color: rgb(224, 62, 45);">**`us.enabled = 0`**</span>) →

<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">"Usuario Deshabilitado..."</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>...<span class="hljs-punctuation">]</span> <span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

Si todo está correcto → retorna data del estudiante.

---

## 4️⃣ Procesamiento final de empleado

### Validaciones

- Si <span style="color: rgb(224, 62, 45);">**`status = Inactive`**</span> → usuario no puede usar la app

### Ajustes en la data:

- Formato de fecha de nacimiento → <span style="color: rgb(224, 62, 45);">**`mm-dd`**</span>
- Si contrato indeterminado → reemplazar texto
- Detectar si es renovación o primer contrato (ServiceErp)
- Normalizar campos:
    
    
    - `bank_ac_no`
    - `pets`
    - `tipo_usuario = "Empleado"`

---

## 5️⃣ Respuesta final (Empleado o Estudiante)

Retorna la data enriquecida del usuario.

---

# 📤 **Responses**

## ✔️ 200 – Usuario encontrado

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--10"><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">"Si hay data"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> ... <span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

## ❌ Usuario no es empleado ni estudiante

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--12"><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-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">"Este usuario no es empleado"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

## ❌ Usuario estudiante sin permisos

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--14"><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-false%2C-%22m-2" style="border-collapse: collapse; width: 100%; height: 29.6px;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr style="height: 29.6px;"><td style="height: 29.6px;">`<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">"Sin Estudiante: Su usuario no tiene permisos para acceder al aplicativo. Contactar con soporte."</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

## ❌ Usuario deshabilitado

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--16"><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-false%2C-%22m-3" 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">"Usuario Deshabilitado: Comuníquese con su administrador"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> ... <span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

---

## ❌ Empleado inactivo

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--18"><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-false%2C-%22m-4" 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">"Empleado Inactivo: Comuníquese con su administrador"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span> ... <span class="hljs-punctuation">]</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--20"><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-%22username%22%3A-%22strin-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">"username"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

### Response (general)

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk--21"><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" 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">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"array"</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--23"><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" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`curl -X POST https://midominio.com/api/get-user3 \-H <span class="hljs-string">"Content-Type: application/json"</span> \-d <span class="hljs-string">'{"username":"empleado@empresa.com"}'</span>`</td></tr></tbody></table>

---

# ✔️ Servicios ERP usados internamente

### 1. <span style="color: rgb(224, 62, 45);">**`send-query-database`**</span>

Método POST que ejecuta consultas SQL personalizadas en ERPNext.

### 2. `<span style="color: rgb(224, 62, 45);"><strong>resource/Contrato de Trabajo</strong></span>`

Consulta REST para obtener contratos de trabajo.

---

# 📝 Observaciones técnicas (reales)

- Código mezcla SQL dinámico en body con filtros JSON → validar inyección.
- Se usa <span style="color: rgb(224, 62, 45);">**`LIMIT 1`**</span> en employee, pero la consulta de contratos tiene <span style="color: rgb(224, 62, 45);">**`limit=None`**</span>.
- Campos duplicados en <span style="color: rgb(224, 62, 45);">**`sql_query`**</span> (ej. <span style="color: rgb(224, 62, 45);">**`emp.bank_ac_no`**</span> aparece 2 veces).
- Es un servicio **muy grande**, podría dividirse por responsabilidad.