survey-mcp-server
Version:
Survey management server handling survey creation, response collection, analysis, and reporting with database access for data management
83 lines • 3.43 kB
JavaScript
import { MongoDBClient } from './mongodb.js';
import { logger } from './logger.js';
// Global storage for company IMO numbers
let companyImoNumbers = [];
// Fetch IMO numbers for a specific company from MongoDB
export async function fetchCompanyImoNumbers(companyName) {
try {
const client = MongoDBClient.getInstance();
const db = await client.getDatabase('devSyiaApi'); // Using devSyiaApi connection
const collection = db.collection('common_group_details');
const result = await collection.findOne({ groupName: companyName }, { projection: { imo: 1, imoList: 1, groupImoList: 1, _id: 0 } });
if (!result) {
logger.warn(`No IMO numbers found for company: ${companyName}`);
companyImoNumbers = [];
return [];
}
// Combine IMO numbers from all three fields
const imoNumbers = [];
// Add single IMO if present
if (result.imo) {
imoNumbers.push(result.imo);
}
// Add imoList array if present
if (result.imoList && Array.isArray(result.imoList)) {
imoNumbers.push(...result.imoList);
}
// Add groupImoList array if present (optional field)
if (result.groupImoList && Array.isArray(result.groupImoList)) {
imoNumbers.push(...result.groupImoList);
}
// Convert to strings and cache
companyImoNumbers = imoNumbers.map(imo => String(imo));
logger.info(`Fetched ${companyImoNumbers.length} IMO numbers for company: ${companyName}`);
return companyImoNumbers;
}
catch (error) {
logger.error('Error fetching company IMO numbers:', error);
companyImoNumbers = [];
return [];
}
}
// Get cached IMO numbers for the current company
export function getCompanyImoNumbers() {
return companyImoNumbers;
}
// Validate if an IMO number is valid for the current company
export function isValidImoForCompany(imoNumber) {
const companyImos = getCompanyImoNumbers();
if (companyImos.length === 0) {
return true; // If no IMO restrictions, allow all
}
const imoNum = Number(imoNumber);
const companyImosNum = companyImos.map(imo => Number(imo));
return companyImosNum.includes(imoNum);
}
// Check if company should bypass IMO filtering (admin access)
export function shouldBypassImoFiltering(companyName) {
const bypassCompanies = ['Synergy', 'Admin'];
const shouldBypass = bypassCompanies.includes(companyName);
if (shouldBypass) {
logger.info(`Company ${companyName} has admin access - bypassing IMO filtering`);
}
return shouldBypass;
}
// Standardized error response for invalid IMO
export function createImoErrorResponse(requestedImo, availableImos) {
return {
type: "text",
text: `Error: IMO number ${requestedImo} is not associated with the current company. Please provide a valid IMO number. Available IMO numbers: ${availableImos.slice(0, 10).join(", ")}${availableImos.length > 10 ? '...' : ''}`,
title: "Invalid IMO Number",
format: "json"
};
}
// System error response
export function createSystemErrorResponse(error) {
return {
type: "text",
text: `System error occurred: ${error.message}. Please contact administrator if this persists.`,
title: "System Error",
format: "json"
};
}
//# sourceMappingURL=imoUtils.js.map