first commit

This commit is contained in:
kvilches1410
2026-03-31 13:10:10 -03:00
parent 4183bb8313
commit 1928661734
4 changed files with 448 additions and 89 deletions
+108
View File
@@ -0,0 +1,108 @@
/**
* config.js — Configuración centralizada del Dashboard JIRA
*
* Carga y valida todas las variables de entorno.
* Uso: import { config } from './config.js'
*/
// Función helper para obtener variable o lanzar error
function getEnv(key, defaultValue = null) {
const value = process.env[key] || defaultValue;
if (!value) {
throw new Error(`❌ Variable de entorno requerida no configurada: ${key}`);
}
return value;
}
// Función para convertir string a número
function getEnvNumber(key, defaultValue) {
const value = process.env[key] || defaultValue;
const num = parseInt(value, 10);
if (isNaN(num)) {
throw new Error(`❌ ${key} debe ser un número válido, recibió: ${value}`);
}
return num;
}
// Validar que las credenciales estén presentes
function validateCredentials() {
const email = process.env.JIRA_EMAIL;
const token = process.env.JIRA_TOKEN;
if (!email) {
throw new Error('❌ Falta JIRA_EMAIL en variables de entorno');
}
if (!token) {
throw new Error('❌ Falta JIRA_TOKEN en variables de entorno');
}
// Validar formato de email
if (!email.includes('@')) {
throw new Error(`❌ JIRA_EMAIL parece inválido: ${email}`);
}
// Validar que el token no esté vacío
if (token.length < 10) {
throw new Error('❌ JIRA_TOKEN parece muy corto, verifica que sea válido');
}
}
// Construir JQL dinámicamente reemplazando variables
function buildJQL() {
const baseJQL = getEnv('JIRA_JQL',
'project = ${PROJECT} AND ${FIELD_SP_ESTIMATE} is not EMPTY ORDER BY updated DESC'
);
return baseJQL
.replace('${PROJECT}', getEnv('JIRA_PROJECT', 'LABS'))
.replace('${FIELD_SP_ESTIMATE}', getEnv('JIRA_FIELD_SP_ESTIMATE', 'customfield_10016'));
}
// Exportar configuración
export const config = {
// ── JIRA Host ──────────────────────────────────────────────────────
jira: {
host: getEnv('JIRA_HOST', 'invera.atlassian.net'),
email: getEnv('JIRA_EMAIL'),
token: getEnv('JIRA_TOKEN'),
project: getEnv('JIRA_PROJECT', 'LABS'),
},
// ── Custom Fields (IDs) ────────────────────────────────────────────
fields: {
spEstimate: getEnv('JIRA_FIELD_SP_ESTIMATE', 'customfield_10016'),
spFinal: getEnv('JIRA_FIELD_SP_FINAL', 'customfield_11019'),
developer: getEnv('JIRA_FIELD_DEVELOPER', 'customfield_10622'),
},
// ── JQL Query ──────────────────────────────────────────────────────
jql: buildJQL(),
// ── API Settings ───────────────────────────────────────────────────
api: {
maxResults: getEnvNumber('MAX_RESULTS', 500),
cacheMsec: getEnvNumber('CACHE_MS', 86400000), // 24 horas por defecto
},
// ── Environment ────────────────────────────────────────────────────
env: {
nodeEnv: getEnv('NODE_ENV', 'development'),
isProduction: getEnv('NODE_ENV', 'development') === 'production',
isDevelopment: getEnv('NODE_ENV', 'development') === 'development',
},
};
// ── Validación al cargar ───────────────────────────────────────────
try {
validateCredentials();
console.log('✅ Configuración cargada correctamente');
console.log(` Host JIRA: ${config.jira.host}`);
console.log(` Proyecto: ${config.jira.project}`);
console.log(` Max Results: ${config.api.maxResults}`);
console.log(` Cache: ${config.api.cacheMsec / 1000 / 60}min`);
} catch (error) {
console.error(error.message);
process.exit(1);
}
export default config;