UNPKG

syia-mcp-utils

Version:

Global utility functions for MCP server

73 lines (72 loc) 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchCompanyImoNumbers = fetchCompanyImoNumbers; exports.shouldBypassImoFiltering = shouldBypassImoFiltering; exports.isValidImoForCompany = isValidImoForCompany; const logger_js_1 = require("./logger.js"); const mongodb_js_1 = require("./mongodb.js"); /** * Fetch IMO numbers for a specific company from MongoDB * @param companyName The name of the company to fetch IMO numbers for * @returns Array of IMO numbers as strings */ async function fetchCompanyImoNumbers(companyName) { try { logger_js_1.logger.info(`Fetching IMO numbers for company: ${companyName}`); // Get ETL Dev MongoDB client const client = await (0, mongodb_js_1.getEtlDevDataClient)(); const db = client.db((0, mongodb_js_1.getEtlDevDataDbName)()); // Query the common_group_details collection for IMO numbers belonging to the company const collection = db.collection('common_group_details'); const result = await collection.findOne({ groupName: companyName }, { projection: { imoList: 1, _id: 0 } }); logger_js_1.logger.info(`Result: ${JSON.stringify(result)}`); if (!result || !result.imoList) { logger_js_1.logger.warn(`No IMO numbers found for company: ${companyName}`); return []; } // Extract IMO numbers from the query result const imoNumbers = result.imoList; logger_js_1.logger.info(`Found ${imoNumbers.length} IMO numbers for company: ${companyName}`); return imoNumbers; } catch (error) { logger_js_1.logger.error(`Error fetching IMO numbers for company ${companyName}:`, error); throw error; } } /** * Check if IMO filtering should be bypassed for admin companies * @param companyName - Name of the company to check * @returns True if filtering should be bypassed */ function shouldBypassImoFiltering(companyName) { const adminCompanies = ['admin', 'administrator', 'superadmin', 'syia']; return adminCompanies.some(admin => companyName.toLowerCase().includes(admin.toLowerCase())); } /** * Validate if an IMO number is valid for the current company * @param imoNumber - IMO number to validate (string or number) * @param companyName - Company name to fetch IMO numbers for * @returns Promise that resolves to boolean indicating if IMO is valid */ async function isValidImoForCompany(imoNumber, companyName) { try { // Convert to string for consistent comparison const imoStr = String(imoNumber); // Basic IMO number validation (7 digits) if (!/^\d{7}$/.test(imoStr)) { return false; } // Check if IMO is in company's authorized list const companyImos = await fetchCompanyImoNumbers(companyName); if (companyImos.length === 0) { // If no company IMOs are set, allow all valid IMO numbers return true; } return companyImos.includes(imoStr); } catch (error) { logger_js_1.logger.error('Error validating IMO number:', error); return false; } }