purchase-mcp-server
Version:
Purchase and budget management server handling requisitions, purchase orders, expenses, budgets, and vendor management with ERP access for data extraction
90 lines • 3.45 kB
JavaScript
/**
* IMO Management Script
*
* This script allows adding, updating, or viewing IMO numbers for companies in the database.
*
* Usage:
* npm run manage-imos -- --company "Company Name" --action list
* npm run manage-imos -- --company "Company Name" --action add --imos "1234567,7654321"
*/
import { logger } from '../utils/logger.js';
import { initializeConnections, closeConnections } from '../utils/mongodb.js';
import { fetchCompanyImoNumbers, addCompanyWithImoNumbers } from '../utils/imoUtils.js';
// Parse command line arguments
function parseArgs() {
const args = process.argv.slice(2);
const parsedArgs = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg.startsWith('--') && i + 1 < args.length) {
const key = arg.slice(2);
parsedArgs[key] = args[++i];
}
}
return parsedArgs;
}
async function main() {
try {
const args = parseArgs();
const companyName = args.company;
const action = args.action || 'list';
if (!companyName) {
console.error('Error: Company name is required. Use --company "Company Name"');
process.exit(1);
}
// Set the COMPANY_NAME environment variable for config
process.env.COMPANY_NAME = companyName;
// Initialize MongoDB connections
await initializeConnections();
logger.info('MongoDB connections initialized');
if (action === 'list') {
// List IMO numbers for the company
const imoNumbers = await fetchCompanyImoNumbers(companyName);
if (imoNumbers.length === 0) {
console.log(`No IMO numbers found for company: ${companyName}`);
}
else {
console.log(`IMO numbers for company ${companyName}:`);
console.log(imoNumbers.join(', '));
console.log(`Total: ${imoNumbers.length} IMO numbers`);
}
}
else if (action === 'add' || action === 'update') {
// Add or update IMO numbers for the company
const imoString = args.imos;
if (!imoString) {
console.error('Error: IMO numbers are required. Use --imos "1234567,7654321"');
process.exit(1);
}
const imoNumbers = imoString.split(',').map(imo => imo.trim());
if (imoNumbers.length === 0) {
console.error('Error: No valid IMO numbers provided');
process.exit(1);
}
const success = await addCompanyWithImoNumbers(companyName, imoNumbers);
if (success) {
console.log(`Successfully ${action === 'add' ? 'added' : 'updated'} ${imoNumbers.length} IMO numbers for company ${companyName}`);
}
else {
console.error(`Failed to ${action} IMO numbers for company ${companyName}`);
process.exit(1);
}
}
else {
console.error(`Error: Unknown action "${action}". Use "list", "add", or "update"`);
process.exit(1);
}
}
catch (error) {
console.error('Error:', error);
process.exit(1);
}
finally {
// Close MongoDB connections
await closeConnections();
logger.info('MongoDB connections closed');
}
}
main();
//# sourceMappingURL=manage-imos.js.map