94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Seed script para configurar tenant con credenciales de WooCommerce.
|
|
* Lee las credenciales de variables de entorno (no hardcodeadas).
|
|
*
|
|
* Variables requeridas:
|
|
* - DATABASE_URL: conexión a PostgreSQL
|
|
* - APP_ENCRYPTION_KEY: clave para encriptar credenciales
|
|
* - WOO_CONSUMER_KEY: consumer key de WooCommerce
|
|
* - WOO_CONSUMER_SECRET: consumer secret de WooCommerce
|
|
* - WOO_BASE_URL: URL base de WooCommerce (opcional, default: https://piaf.floda.dev/wp-json/wc/v3)
|
|
*/
|
|
|
|
import pg from "pg";
|
|
|
|
const TENANT_ID = "eb71b9a7-9ccf-430e-9b25-951a0c589c0f";
|
|
|
|
async function seed() {
|
|
const {
|
|
DATABASE_URL,
|
|
APP_ENCRYPTION_KEY,
|
|
WOO_CONSUMER_KEY,
|
|
WOO_CONSUMER_SECRET,
|
|
WOO_BASE_URL = "https://piaf.floda.dev/wp-json/wc/v3",
|
|
} = process.env;
|
|
|
|
// Validar variables requeridas
|
|
if (!DATABASE_URL) {
|
|
console.log("[seed] DATABASE_URL no configurada, saltando seed");
|
|
return;
|
|
}
|
|
|
|
if (!APP_ENCRYPTION_KEY || !WOO_CONSUMER_KEY || !WOO_CONSUMER_SECRET) {
|
|
console.log("[seed] Variables de WooCommerce no configuradas, saltando seed de ecommerce config");
|
|
console.log("[seed] Para configurar, definir: APP_ENCRYPTION_KEY, WOO_CONSUMER_KEY, WOO_CONSUMER_SECRET");
|
|
return;
|
|
}
|
|
|
|
const pool = new pg.Pool({ connectionString: DATABASE_URL });
|
|
|
|
try {
|
|
// Verificar si ya existe la config
|
|
const check = await pool.query(
|
|
"SELECT 1 FROM tenant_ecommerce_config WHERE tenant_id = $1",
|
|
[TENANT_ID]
|
|
);
|
|
|
|
if (check.rows.length > 0) {
|
|
console.log("[seed] tenant_ecommerce_config ya existe, saltando");
|
|
return;
|
|
}
|
|
|
|
// Configurar encryption key para la sesión
|
|
await pool.query("SELECT set_config('app.encryption_key', $1, false)", [
|
|
APP_ENCRYPTION_KEY,
|
|
]);
|
|
|
|
// Insertar config de WooCommerce
|
|
await pool.query(
|
|
`INSERT INTO tenant_ecommerce_config (
|
|
tenant_id,
|
|
provider,
|
|
base_url,
|
|
credential_ref,
|
|
api_version,
|
|
timeout_ms,
|
|
enabled,
|
|
enc_consumer_key,
|
|
enc_consumer_secret
|
|
) VALUES (
|
|
$1::uuid,
|
|
'woo',
|
|
$2,
|
|
'secret://woo/piaf',
|
|
'wc/v3',
|
|
8000,
|
|
true,
|
|
pgp_sym_encrypt($3, current_setting('app.encryption_key')),
|
|
pgp_sym_encrypt($4, current_setting('app.encryption_key'))
|
|
)`,
|
|
[TENANT_ID, WOO_BASE_URL, WOO_CONSUMER_KEY, WOO_CONSUMER_SECRET]
|
|
);
|
|
|
|
console.log("[seed] tenant_ecommerce_config creada exitosamente");
|
|
} catch (err) {
|
|
console.error("[seed] Error:", err.message);
|
|
// No fallar el startup si el seed falla (puede ser que las tablas no existan aún)
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
seed();
|