176 lines
5.1 KiB
JavaScript
176 lines
5.1 KiB
JavaScript
const dotenv = require("dotenv");
|
|
const path = require("path");
|
|
const { outputProgress, formatElapsedTime } = require('./metrics/utils/progress');
|
|
const { setupConnections, closeConnections } = require('./import/utils');
|
|
const importCategories = require('./import/categories');
|
|
const { importProducts } = require('./import/products');
|
|
const importOrders = require('./import/orders');
|
|
const importPurchaseOrders = require('./import/purchase-orders');
|
|
|
|
dotenv.config({ path: path.join(__dirname, "../.env") });
|
|
|
|
// Constants to control which imports run
|
|
const IMPORT_CATEGORIES = true;
|
|
const IMPORT_PRODUCTS = true;
|
|
const IMPORT_ORDERS = true;
|
|
const IMPORT_PURCHASE_ORDERS = true;
|
|
|
|
// SSH configuration
|
|
const sshConfig = {
|
|
ssh: {
|
|
host: process.env.PROD_SSH_HOST,
|
|
port: process.env.PROD_SSH_PORT || 22,
|
|
username: process.env.PROD_SSH_USER,
|
|
privateKey: process.env.PROD_SSH_KEY_PATH
|
|
? require("fs").readFileSync(process.env.PROD_SSH_KEY_PATH)
|
|
: undefined,
|
|
},
|
|
// Production database configuration
|
|
prodDbConfig: {
|
|
host: process.env.PROD_DB_HOST || "localhost",
|
|
user: process.env.PROD_DB_USER,
|
|
password: process.env.PROD_DB_PASSWORD,
|
|
database: process.env.PROD_DB_NAME,
|
|
port: process.env.PROD_DB_PORT || 3306,
|
|
},
|
|
// Local database configuration
|
|
localDbConfig: {
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME,
|
|
multipleStatements: true,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
namedPlaceholders: true,
|
|
}
|
|
};
|
|
|
|
let isImportCancelled = false;
|
|
|
|
// Add cancel function
|
|
function cancelImport() {
|
|
isImportCancelled = true;
|
|
outputProgress({
|
|
status: 'cancelled',
|
|
operation: 'Import process',
|
|
message: 'Import cancelled by user',
|
|
current: 0,
|
|
total: 0,
|
|
elapsed: null,
|
|
remaining: null,
|
|
rate: 0
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
const startTime = Date.now();
|
|
let connections;
|
|
|
|
try {
|
|
// Initial progress update
|
|
outputProgress({
|
|
status: "running",
|
|
operation: "Import process",
|
|
message: "Initializing SSH tunnel...",
|
|
current: 0,
|
|
total: 4, // Total number of major steps
|
|
elapsed: formatElapsedTime((Date.now() - startTime) / 1000)
|
|
});
|
|
|
|
connections = await setupConnections(sshConfig);
|
|
const { prodConnection, localConnection } = connections;
|
|
|
|
if (isImportCancelled) throw new Error("Import cancelled");
|
|
|
|
const results = {
|
|
categories: null,
|
|
products: null,
|
|
orders: null,
|
|
purchaseOrders: null
|
|
};
|
|
|
|
// Run each import based on constants
|
|
if (IMPORT_CATEGORIES) {
|
|
results.categories = await importCategories(prodConnection, localConnection);
|
|
if (isImportCancelled) throw new Error("Import cancelled");
|
|
currentStep++;
|
|
}
|
|
|
|
if (IMPORT_PRODUCTS) {
|
|
results.products = await importProducts(prodConnection, localConnection);
|
|
if (isImportCancelled) throw new Error("Import cancelled");
|
|
currentStep++;
|
|
}
|
|
|
|
if (IMPORT_ORDERS) {
|
|
results.orders = await importOrders(prodConnection, localConnection);
|
|
if (isImportCancelled) throw new Error("Import cancelled");
|
|
currentStep++;
|
|
}
|
|
|
|
if (IMPORT_PURCHASE_ORDERS) {
|
|
results.purchaseOrders = await importPurchaseOrders(prodConnection, localConnection);
|
|
if (isImportCancelled) throw new Error("Import cancelled");
|
|
currentStep++;
|
|
}
|
|
|
|
const endTime = Date.now();
|
|
outputProgress({
|
|
status: "complete",
|
|
operation: "Import process",
|
|
message: "All imports completed successfully",
|
|
current: 4,
|
|
total: 4,
|
|
elapsed: formatElapsedTime((endTime - startTime) / 1000),
|
|
timing: {
|
|
start_time: new Date(startTime).toISOString(),
|
|
end_time: new Date(endTime).toISOString(),
|
|
elapsed_time: formatElapsedTime((endTime - startTime) / 1000),
|
|
elapsed_seconds: Math.round((endTime - startTime) / 1000)
|
|
},
|
|
results
|
|
});
|
|
|
|
return results;
|
|
} catch (error) {
|
|
const endTime = Date.now();
|
|
console.error("Error during import process:", error);
|
|
outputProgress({
|
|
status: error.message === "Import cancelled" ? "cancelled" : "error",
|
|
operation: "Import process",
|
|
message: error.message === "Import cancelled" ? "Import cancelled by user" : "Import failed",
|
|
error: error.message,
|
|
current: 0,
|
|
total: 4,
|
|
elapsed: formatElapsedTime((endTime - startTime) / 1000),
|
|
timing: {
|
|
start_time: new Date(startTime).toISOString(),
|
|
end_time: new Date(endTime).toISOString(),
|
|
elapsed_time: formatElapsedTime((endTime - startTime) / 1000),
|
|
elapsed_seconds: Math.round((endTime - startTime) / 1000)
|
|
}
|
|
});
|
|
throw error;
|
|
} finally {
|
|
if (connections) {
|
|
await closeConnections(connections);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run the import only if this is the main module
|
|
if (require.main === module) {
|
|
main().catch((error) => {
|
|
console.error("Unhandled error in main process:", error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
// Export the functions needed by the route
|
|
module.exports = {
|
|
main,
|
|
cancelImport,
|
|
};
|