syia-mcp-utils
Version:
Global utility functions for MCP server
1,076 lines (1,075 loc) • 44.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchQADetails = fetchQADetails;
exports.fetchQADetailsAndCreateResponse = fetchQADetailsAndCreateResponse;
exports.getComponentData = getComponentData;
exports.addComponentData = addComponentData;
exports.getVesselQnASnapshot = getVesselQnASnapshot;
exports.getVesselQnASnapshotHandler = getVesselQnASnapshotHandler;
exports.getDataLink = getDataLink;
exports.insertDataLinkToMongoDBGeneric = insertDataLinkToMongoDBGeneric;
exports.insertDataLinkToMongoDB = insertDataLinkToMongoDB;
exports.insertPmsDataLinkToMongodb = insertPmsDataLinkToMongodb;
exports.getArtifact = getArtifact;
exports.getListOfArtifacts = getListOfArtifacts;
exports.convertUnixDates = convertUnixDates;
exports.convertToCSV = convertToCSV;
exports.processTypesenseResults = processTypesenseResults;
exports.processTypesenseExportResults = processTypesenseExportResults;
exports.formatTypesenseResults = formatTypesenseResults;
exports.getFleetImoByName = getFleetImoByName;
exports.getVesselImoListFromFleet = getVesselImoListFromFleet;
exports.updateTypesenseFilterWithCompanyImos = updateTypesenseFilterWithCompanyImos;
exports.updateTypesenseFilterWithCompanyImosPms = updateTypesenseFilterWithCompanyImosPms;
exports.updateTypesenseFilterWithCompanyImosDefect = updateTypesenseFilterWithCompanyImosDefect;
exports.updateMongoFilterWithCompanyImos = updateMongoFilterWithCompanyImos;
exports.updateMongoAggregationWithCompanyImos = updateMongoAggregationWithCompanyImos;
exports.updateSearchParamsWithCompanyImos = updateSearchParamsWithCompanyImos;
exports.isVesselAuthorizedForCompany = isVesselAuthorizedForCompany;
exports.getAuthorizedImoNumbers = getAuthorizedImoNumbers;
exports.logImoFilteringActivity = logImoFilteringActivity;
exports.exportDefectsForImoList = exportDefectsForImoList;
exports.exportPurchasesForImoList = exportPurchasesForImoList;
exports.exportBudgetsForImoList = exportBudgetsForImoList;
exports.exportExpensesForImoList = exportExpensesForImoList;
exports.exportSurveysForImoList = exportSurveysForImoList;
exports.getDatabaseInstance = getDatabaseInstance;
exports.getPmsDatabase = getPmsDatabase;
exports.getPmsEtlDatabase = getPmsEtlDatabase;
exports.getPmsEngineDataDatabase = getPmsEngineDataDatabase;
exports.getDefectDatabase = getDefectDatabase;
exports.getDefectSecondaryDatabase = getDefectSecondaryDatabase;
const mongodb_js_1 = require("./mongodb.js");
const mongodb_js_2 = require("./mongodb.js");
const logger_js_1 = require("./logger.js");
const mongodb_js_3 = require("./mongodb.js");
const config_1 = require("./config");
const imoUtils_js_1 = require("./imoUtils.js");
const typesense_js_1 = require("./typesense.js");
const mongodb_1 = require("mongodb");
async function fetchQADetails(imo, qaId) {
try {
const client = await (0, mongodb_js_1.getEtlDevClient)();
const db = client.db((0, mongodb_js_2.getEtlDevDbName)());
const vesselinfos = db.collection('vesselinfos');
const query = {
'imo': parseInt(imo),
'questionNo': qaId
};
const projection = {
'_id': 0,
'imo': 1,
'vesselName': 1,
'refreshDate': 1,
'answer': 1
};
const mongoResult = await vesselinfos.findOne(query, { projection });
let res = mongoResult ? {
imo: mongoResult.imo,
vesselName: mongoResult.vesselName,
refreshDate: mongoResult.refreshDate,
answer: mongoResult.answer
} : {
imo: parseInt(imo),
vesselName: null,
refreshDate: null,
answer: null
};
// Format refresh date if it exists
if (res.refreshDate && new Date(res.refreshDate).toString() !== 'Invalid Date') {
res.refreshDate = new Date(res.refreshDate).toLocaleDateString('en-US', {
day: 'numeric',
month: 'short',
year: 'numeric'
});
}
// Process answer with component data if it exists
if (res.answer) {
res.answer = await addComponentData(res.answer, imo);
}
// Get vessel QnA snapshot link
try {
res.Artifactlink = await getVesselQnASnapshot(imo, qaId.toString());
}
catch (error) {
res.Artifactlink = null;
}
return res;
}
catch (error) {
logger_js_1.logger.error('Error fetching QA details:', error);
throw new Error(`Error fetching QA details: ${error.message}`);
}
}
async function fetchQADetailsAndCreateResponse(imo, questionNo, functionName, linkHeader, session_id = "testing") {
if (!imo) {
throw new Error("IMO is required");
}
try {
// Fetch QA details
const result = await fetchQADetails(imo, questionNo);
const link = result.Artifactlink;
const vesselName = result.vesselName;
// Insert data link to MongoDB
await insertDataLinkToMongoDB(link, linkHeader, session_id, imo, vesselName);
// Get artifact data
const artifactData = await getArtifact(functionName, link);
// Create content responses
const content = {
type: "text",
text: JSON.stringify(result, null, 2)
};
const artifact = {
type: "text",
text: JSON.stringify(artifactData, null, 2)
};
return [content, artifact];
}
catch (error) {
logger_js_1.logger.error(`Error in ${functionName}:`, error);
throw new Error(`Error in ${functionName}: ${error.message}`);
}
}
async function getComponentData(componentId) {
const match = componentId.match(/^(\d+)_(\d+)_(\d+)$/);
if (!match) {
return `⚠️ Invalid component_id format: ${componentId}`;
}
const [, componentNumber, questionNumber, imo] = match;
const componentNo = `${componentNumber}_${questionNumber}_${imo}`;
try {
const client = await (0, mongodb_js_1.getEtlDevClient)();
const db = client.db((0, mongodb_js_2.getEtlDevDbName)());
const collection = db.collection('vesselinfocomponents');
const doc = await collection.findOne({ componentNo });
if (!doc) {
return `⚠️ No component found for ID: ${componentId}`;
}
if (!doc.data) {
return "No data found in the table component";
}
// Extract headers excluding lineitem
const headers = doc.data.headers
.filter((h) => h.name !== "lineitem")
.map((h) => h.name);
const rows = doc.data.body;
// Build markdown table
let md = "| " + headers.join(" | ") + " |\n";
md += "| " + headers.map(() => "---").join(" | ") + " |\n";
for (const row of rows) {
const formattedRow = row
.filter((cell) => !cell.lineitem) // Exclude lineitem
.map((cell) => {
if (cell.value && cell.link) {
return `[${cell.value}](${cell.link})`;
}
else if (cell.status && cell.color) {
return cell.status;
}
return String(cell);
});
md += "| " + formattedRow.join(" | ") + " |\n";
}
return md;
}
catch (error) {
logger_js_1.logger.error('Error getting component data:', error);
throw new Error(`Error getting component data: ${error.message}`);
}
}
async function addComponentData(answer, imo) {
const pattern = /httpsdev\.syia\.ai\/chat\/ag-grid-table\?component=(\d+_\d+)/g;
const matches = Array.from(answer.matchAll(pattern));
let result = answer;
for (const match of matches) {
const component = match[1];
try {
const replacement = await getComponentData(`${component}_${imo}`);
result = result.replace(match[0], replacement);
}
catch (error) {
logger_js_1.logger.error('Error replacing component data:', error);
}
}
return result;
}
async function getVesselQnASnapshot(imo, questionNo) {
try {
const config = (0, config_1.getConfig)();
const raw_snapshotUrl = config.snapshotUrl;
// API endpoint
const snapshotUrl = `${raw_snapshotUrl}/${imo}/${questionNo}`;
const raw_jwtToken = config.jwtToken;
// Authentication token
const jwtToken = `Bearer ${raw_jwtToken}`;
// Headers for the request
const headers = {
"Authorization": jwtToken
};
logger_js_1.logger.info(`Fetching vessel QnA snapshot for IMO: ${imo}, Question: ${questionNo}`);
const response = await fetch(snapshotUrl, {
method: 'GET',
headers
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Return resultData if it exists, otherwise return the full response
if (data && typeof data === 'object' && "resultData" in data) {
return data.resultData;
}
return data;
}
catch (error) {
logger_js_1.logger.error(`Error fetching vessel QnA snapshot for IMO ${imo}, Question ${questionNo}:`, error);
return null;
}
}
async function getVesselQnASnapshotHandler(arguments_) {
const { imo, questionNo } = arguments_;
if (!imo || !questionNo) {
throw new Error("Both IMO and questionNo are required");
}
try {
const result = await getVesselQnASnapshot(imo, questionNo);
if (!result) {
return [{
type: "text",
text: `No QnA snapshot data found for vessel IMO: ${imo}, Question: ${questionNo}`
}];
}
return [{
type: "text",
text: JSON.stringify(result, null, 2),
title: `Vessel QnA Snapshot - IMO: ${imo}, Question: ${questionNo}`,
format: "json"
}];
}
catch (error) {
logger_js_1.logger.error(`Error getting vessel QnA snapshot:`, error);
throw new Error(`Error getting vessel QnA snapshot: ${error.message}`);
}
}
async function getDataLink(data) {
try {
const config = (0, config_1.getConfig)();
const raw_url = config.snapshotUrl;
const url = raw_url;
const raw_jwtToken = config.jwtToken;
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${raw_jwtToken}`
};
const payload = {
data
};
// Log URL and token (complete values)
logger_js_1.logger.info(`Making request to URL: ${url}`);
logger_js_1.logger.info(`Using JWT token: ${raw_jwtToken}`);
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(payload)
});
// Log response status and headers
logger_js_1.logger.info(`Response status: ${response.status} ${response.statusText}`);
logger_js_1.logger.info(`Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
if (!response.ok) {
logger_js_1.logger.error(`HTTP error! status: ${response.status}, statusText: ${response.statusText}`);
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
// Log complete response data
logger_js_1.logger.info(`Complete response data: ${JSON.stringify(result)}`);
if (result.status === "OK") {
logger_js_1.logger.info(`Successfully got data link: ${result.resultData}`);
return result.resultData;
}
else {
logger_js_1.logger.error(`Failed to get data link: Invalid response status - ${result.status}`);
throw new Error('Failed to get data link: Invalid response status');
}
}
catch (error) {
logger_js_1.logger.error('Error getting data link:', error);
throw new Error(`Error getting data link: ${error.message}`);
}
}
/**
* Generic function to insert data links into MongoDB
*/
async function insertDataLinkToMongoDBGeneric(options) {
try {
const mongoClient = await (0, mongodb_js_3.getMongoClient)();
const db = mongoClient.db((0, config_1.getConfig)().dbName);
const collection = db.collection(options.collectionName);
if (options.mode === 'general') {
await collection.insertOne({
link: options.dataLink,
type: options.type,
sessionId: options.sessionId,
imo: options.imo,
vesselName: options.vesselName,
createdAt: new Date()
});
}
else if (options.mode === 'pms') {
const linkData = {
link: options.dataLink,
linkHeader: options.linkHeader || ''
};
const sessionExists = await collection.findOne({ sessionId: options.sessionId });
if (sessionExists) {
await collection.updateOne({ sessionId: options.sessionId }, {
$push: { links: { $each: [linkData] } },
$set: { datetime: new Date().toISOString() }
});
}
else {
const newEntry = {
sessionId: options.sessionId,
imo: options.imo ?? '',
vesselName: options.vesselName ?? '',
links: [linkData],
datetime: new Date().toISOString()
};
await collection.insertOne(newEntry);
}
}
else {
throw new Error(`Unsupported insert mode: ${options.mode}`);
}
}
catch (error) {
logger_js_1.logger.error(`Error inserting data link to MongoDB [${options.mode}]:`, error);
throw new Error(`Error inserting data link to MongoDB: ${error.message}`);
}
}
async function insertDataLinkToMongoDB(link, type, sessionId, imo, vesselName) {
return insertDataLinkToMongoDBGeneric({
mode: 'general',
collectionName: 'data_links',
dataLink: link,
sessionId,
imo,
vesselName,
type
});
}
async function insertPmsDataLinkToMongodb(dataLink, linkHeader, sessionId, imo, vesselName) {
return insertDataLinkToMongoDBGeneric({
mode: 'pms',
collectionName: 'casefile_data',
dataLink,
linkHeader,
sessionId,
imo,
vesselName
});
}
async function getArtifact(toolName, link) {
try {
const timestamp = Math.floor(Date.now() / 1000);
const artifact = {
id: `msg_browser_${Math.random().toString(36).substring(2, 8)}`,
parentTaskId: `task_${toolName}_${Math.random().toString(36).substring(2, 8)}`,
timestamp,
agent: {
id: "agent_siya_browser",
name: "SIYA",
type: "qna"
},
messageType: "action",
action: {
tool: "browser",
operation: "browsing",
params: {
url: link,
pageTitle: `Tool response for ${toolName}`,
visual: {
icon: "browser",
color: "#2D8CFF"
},
stream: {
type: "vnc",
streamId: "stream_browser_1",
target: "browser"
}
}
},
content: `Viewed page: ${toolName}`,
artifacts: [
{
id: `artifact_webpage_${Date.now()}_${Math.floor(Math.random() * 1000)}`,
type: "browser_view",
content: {
url: link,
title: toolName,
screenshot: "",
textContent: `Observed output of cmd \`${toolName}\` executed:`,
extractedInfo: {}
},
metadata: {
domainName: "example.com",
visitTimestamp: Date.now(),
category: "web_page"
}
}
],
status: "completed"
};
return artifact;
}
catch (error) {
logger_js_1.logger.error('Error getting artifact:', error);
throw new Error(`Error getting artifact: ${error.message}`);
}
}
async function getListOfArtifacts(toolName, linkData) {
try {
const artifacts = [];
for (const link of linkData) {
if (link.url) {
const artifactData = await getArtifact(toolName, link.url);
artifacts.push({
type: "text",
text: JSON.stringify(artifactData, null, 2)
});
}
}
return artifacts;
}
catch (error) {
logger_js_1.logger.error("Error getting list of artifacts:", error);
throw new Error(`Error getting list of artifacts: ${error.message}`);
}
}
function convertUnixDates(document) {
logger_js_1.logger.debug(`Starting Unix date conversion for document with ${Object.keys(document).length} fields`);
const result = { ...document };
const dateFields = [
'purchaseRequisitionDate',
'purchaseOrderIssuedDate',
'orderReadinessDate',
'date',
'poDate',
'expenseDate',
"inspectionTargetDate",
"reportDate",
"closingDate",
"targetDate",
"nextDueDate",
"extendedDate"
];
logger_js_1.logger.debug(`Checking ${dateFields.length} potential date fields for Unix timestamp conversion`);
let convertedCount = 0;
for (const field of dateFields) {
const value = result[field];
if (typeof value === "number" && Number.isFinite(value)) {
const originalValue = value;
result[field] = new Date(value * 1000).toISOString();
logger_js_1.logger.debug(`Converted field '${field}' from Unix timestamp ${originalValue} to ISO date: ${result[field]}`);
convertedCount++;
}
}
logger_js_1.logger.debug(`Unix date conversion completed - ${convertedCount} fields converted out of ${dateFields.length} checked fields`);
return result;
}
/**
* Convert data to CSV format
* @param data - Array of objects to convert
* @returns CSV string
*/
function convertToCSV(data) {
if (!data.length)
return "";
const headers = Object.keys(data[0]);
const escapeCSV = (value) => {
const str = value != null ? String(value) : "";
const needsEscaping = /[",\n]/.test(str);
const escaped = str.replace(/"/g, '""'); // escape double quotes
return needsEscaping ? `"${escaped}"` : escaped;
};
const rows = data.map(doc => headers.map(header => escapeCSV(doc[header])).join(','));
return [headers.join(','), ...rows].join('\n');
}
async function processTypesenseResults(searchResult, toolName, title, session_id = "testing", linkHeader, artifactTitle) {
logger_js_1.logger.info(`Starting processTypesenseResults for tool: ${toolName}, session: ${session_id}`);
try {
// Log input validation
logger_js_1.logger.info(`Validating search results for ${toolName} - hits count: ${searchResult?.hits?.length || 0}`);
if (!searchResult || !searchResult.hits || searchResult.hits.length === 0) {
logger_js_1.logger.warn(`No search results found for ${toolName}`);
return [{
type: "text",
text: "No records found for the specified criteria.",
title: "No Results Found",
format: "json"
}];
}
logger_js_1.logger.info(`Processing ${searchResult.hits.length} hits for ${toolName}`);
// Process search results into the standard format
const hits = searchResult.hits || [];
logger_js_1.logger.info(`Starting document processing for ${hits.length} hits in ${toolName}`);
const documents = await Promise.all(hits.map(async (hit, index) => {
if (!hit.document) {
logger_js_1.logger.warn(`Hit ${index} is missing document property in ${toolName}`);
return {};
}
// Create a shallow copy of the document
const document = { ...hit.document };
// Remove embedding field to reduce response size
if (document.embedding) {
logger_js_1.logger.debug(`Removing embedding field from document ${index} in ${toolName}`);
delete document.embedding;
}
// Convert Unix timestamps to readable dates
logger_js_1.logger.debug(`Converting Unix dates for document ${index} in ${toolName}`);
return await convertUnixDates(document);
}));
logger_js_1.logger.info(`Successfully processed ${documents.length} documents for ${toolName}`);
// Get data link
logger_js_1.logger.info(`Generating data link for ${toolName}`);
const dataLink = await getDataLink(documents);
logger_js_1.logger.info(`Data link generated successfully for ${toolName}: ${dataLink.substring(0, 50)}...`);
// Get vessel name and IMO from hits
let vesselName = null;
let imo = null;
logger_js_1.logger.info(`Extracting vessel information from search results for ${toolName}`);
try {
vesselName = searchResult.hits[0]?.document?.vesselName;
imo = searchResult.hits[0]?.document?.imo;
logger_js_1.logger.info(`Vessel info extracted for ${toolName} - Name: ${vesselName}, IMO: ${imo}`);
}
catch (error) {
logger_js_1.logger.warn(`Could not get vessel name or IMO from hits in ${toolName}:`, error);
}
// Insert the data link to mongodb collection
logger_js_1.logger.info(`Inserting data link to MongoDB for ${toolName}, session: ${session_id}`);
await insertDataLinkToMongoDB(dataLink, linkHeader, session_id, imo || "", vesselName || "");
logger_js_1.logger.info(`Data link successfully inserted to MongoDB for ${toolName}`);
// Format results in the standard structure
logger_js_1.logger.info(`Formatting results for ${toolName}`);
const formattedResults = {
found: searchResult.found || 0,
out_of: searchResult.out_of || 0,
page: searchResult.page || 1,
hits: documents,
artifactLink: dataLink
};
logger_js_1.logger.info(`Results formatted successfully for ${toolName} - found: ${formattedResults.found}, out_of: ${formattedResults.out_of}`);
// Get artifact data
logger_js_1.logger.info(`Retrieving artifact data for ${toolName}`);
const artifactData = await getArtifact(toolName, dataLink);
logger_js_1.logger.info(`Artifact data retrieved successfully for ${toolName}`);
// Create content response
logger_js_1.logger.info(`Creating content response for ${toolName}`);
const content = {
type: "text",
text: JSON.stringify(formattedResults, null, 2),
title,
format: "json"
};
// Create artifact response
logger_js_1.logger.info(`Creating artifact response for ${toolName}`);
const artifact = {
type: "text",
text: JSON.stringify(artifactData, null, 2),
title: artifactTitle || title,
format: "json"
};
logger_js_1.logger.info(`processTypesenseResults completed successfully for ${toolName}`);
return [content, artifact];
}
catch (error) {
logger_js_1.logger.error(`Error processing Typesense results for ${toolName}:`, error);
return [{
type: "text",
text: `Error processing results: ${error.message}`,
title: "Error",
format: "json"
}];
}
}
async function processTypesenseExportResults(documents, toolName, title, artifactTitle, session_id, linkHeader, imo, vesselName) {
try {
// Process documents
const processedDocuments = await Promise.all(documents.map(async (doc) => {
const document = { ...doc };
// Remove embedding field to reduce response size
if (document.embedding) {
delete document.embedding;
}
// Convert any Unix timestamps to readable dates
return await convertUnixDates(document);
}));
// Get data link
const dataLink = await getDataLink(processedDocuments);
// Insert the data link to mongodb collection
await insertDataLinkToMongoDB(dataLink, linkHeader, session_id, imo || "", vesselName || "");
// Format results in the standard structure
const formattedResults = {
found: processedDocuments.length,
out_of: processedDocuments.length,
page: 1,
hits: processedDocuments,
artifactLink: dataLink
};
// Get artifact data
const artifactData = await getArtifact(toolName, dataLink);
// Create content response
const content = {
type: "text",
text: JSON.stringify(formattedResults, null, 2),
title,
format: "json"
};
// Create artifact response
const artifact = {
type: "text",
text: JSON.stringify(artifactData, null, 2),
title: artifactTitle,
format: "json"
};
return [content, artifact];
}
catch (error) {
logger_js_1.logger.error(`Error processing Typesense export results for ${toolName}:`, error);
return [{
type: "text",
text: `Error processing results: ${error.message}`,
title: "Error",
format: "json"
}];
}
}
async function formatTypesenseResults(searchResult, toolName, title, dataLink, artifactTitle) {
try {
// Process search results into the standard format
const hits = searchResult.hits || [];
const documents = await Promise.all(hits.map(async (hit) => {
if (!hit.document) {
logger_js_1.logger.warn(`Hit is missing document property in ${toolName}`);
return {};
}
// Create a shallow copy of the document
const document = { ...hit.document };
// Remove embedding field to reduce response size
if (document.embedding) {
delete document.embedding;
}
// Convert Unix timestamps to readable dates
return await convertUnixDates(document);
}));
// Format results in the standard structure
const formattedResults = {
found: searchResult.found || 0,
out_of: searchResult.out_of || 0,
page: searchResult.page || 1,
hits: documents
};
// Get artifact data
const artifactData = await getArtifact(toolName, dataLink);
const testing_json = {
found: searchResult.found || 0,
out_of: searchResult.out_of || 0,
page: searchResult.page || 1,
hits: "testing"
};
// Create content response
const content = {
type: "text",
text: JSON.stringify(testing_json, null, 2),
title,
format: "json"
};
// Create artifact response
const artifact = {
type: "text",
text: JSON.stringify(artifactData, null, 2),
title: artifactTitle || title,
format: "json"
};
return [content, artifact];
}
catch (error) {
logger_js_1.logger.error(`Error formatting Typesense results for ${toolName}:`, error);
return [{
type: "text",
text: `Error formatting results: ${error.message}`,
title: "Error",
format: "json"
}];
}
}
/**
* Step 1: Query Typesense fleet-details collection to get fleet IMO by name
* @param fleetName - Name of the fleet (e.g., "SMPL DRY")
* @returns Promise<number | null> - Fleet IMO number or null if not found
*/
async function getFleetImoByName(fleetName) {
try {
const client = (0, typesense_js_1.getTypesenseClient)();
const searchResult = await client.collections('fleet-details').documents().search({
q: fleetName,
query_by: 'name',
per_page: 1
});
if (searchResult.hits && searchResult.hits.length > 0) {
const fleetDoc = searchResult.hits[0].document;
return fleetDoc.imo || null;
}
return null;
}
catch (error) {
logger_js_1.logger.error(`Error fetching fleet IMO for ${fleetName}:`, error);
return null;
}
}
/**
* Step 2: Query MongoDB common_group_details collection to get vessel IMO list
* @param fleetImo - IMO number of the fleet
* @returns Promise<number[]> - Array of vessel IMO numbers
*/
async function getVesselImoListFromFleet(fleetImo) {
const mongoUri = (0, config_1.getConfig)().mongodbEtlDevDataUri || "";
const dbName = (0, config_1.getConfig)().mongodbEtlDevDataDbName || "";
if (!mongoUri || !dbName) {
throw new Error('ETL database URI and name are required for fleet operations');
}
const client = new mongodb_1.MongoClient(mongoUri);
try {
await client.connect();
const db = client.db(dbName);
const collection = db.collection('common_group_details');
const fleetDoc = await collection.findOne({ imo: fleetImo });
if (fleetDoc && fleetDoc.imoList && Array.isArray(fleetDoc.imoList)) {
return fleetDoc.imoList;
}
return [];
}
catch (error) {
logger_js_1.logger.error(`Error fetching vessel IMO list for fleet ${fleetImo}:`, error);
throw error;
}
finally {
await client.close();
}
}
async function updateTypesenseFilterWithCompanyImosGeneric(filter, options) {
const { bypassForSynergy = false, bypassForAdminCompanies = false, loggerTag = "" } = options || {};
const companyName = (0, config_1.getConfig)().companyName;
if (!companyName) {
logger_js_1.logger.warn(`[${loggerTag}] Company name is missing in config.`);
return filter;
}
if (bypassForSynergy && companyName === "Synergy") {
return filter;
}
if (bypassForAdminCompanies && (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName)) {
logger_js_1.logger.debug(`[${loggerTag}] Skipping Typesense IMO filtering for admin company: ${companyName}`);
return filter;
}
const companyImos = await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
if (companyImos.length === 0) {
logger_js_1.logger.warn(`[${loggerTag}] No company IMO numbers configured. Skipping Typesense IMO filtering.`);
return filter;
}
const imoFilter = `imo:[${companyImos.join(",")}]`;
if (filter && filter.trim()) {
const combinedFilter = `${filter} && ${imoFilter}`;
logger_js_1.logger.debug(`[${loggerTag}] Applied Typesense IMO filter: ${combinedFilter}`);
return combinedFilter;
}
else {
logger_js_1.logger.debug(`[${loggerTag}] Applied Typesense IMO filter: ${imoFilter}`);
return imoFilter;
}
}
// Specific wrappers
async function updateTypesenseFilterWithCompanyImos(filter) {
return updateTypesenseFilterWithCompanyImosGeneric(filter, {
bypassForSynergy: true,
loggerTag: "General"
});
}
async function updateTypesenseFilterWithCompanyImosPms(filter) {
return updateTypesenseFilterWithCompanyImosGeneric(filter, {
bypassForAdminCompanies: true,
loggerTag: "PMS"
});
}
async function updateTypesenseFilterWithCompanyImosDefect(filter) {
return updateTypesenseFilterWithCompanyImosGeneric(filter, {
bypassForAdminCompanies: true,
loggerTag: "Defect"
});
}
/**
* Update MongoDB filter with company IMO numbers for filtering (PMS version)
* @param filter - Existing MongoDB filter object
* @returns Updated filter object with IMO restrictions
*/
async function updateMongoFilterWithCompanyImos(filter) {
const companyName = (0, config_1.getConfig)().companyName;
// Skip filtering for admin companies
if (!companyName || (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName)) {
logger_js_1.logger.debug(`Skipping MongoDB IMO filtering for admin company: ${companyName}`);
return filter;
}
const companyImos = await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
// If no IMO numbers configured, return original filter
if (companyImos.length === 0) {
logger_js_1.logger.warn('No company IMO numbers configured. Skipping MongoDB IMO filtering.');
return filter;
}
// Create a copy of the filter to avoid modifying the original
const updatedFilter = { ...filter };
// Convert IMO numbers to integers for MongoDB query
const imoNumbers = companyImos.map((imo) => Number(imo));
// Add IMO restriction to the filter
updatedFilter.imo = { $in: imoNumbers };
logger_js_1.logger.debug(`Applied MongoDB IMO filter: ${JSON.stringify(updatedFilter)}`);
return updatedFilter;
}
/**
* Update MongoDB aggregation pipeline with company IMO numbers for filtering (PMS version)
* @param pipeline - Existing MongoDB aggregation pipeline
* @returns Updated pipeline with IMO restrictions
*/
async function updateMongoAggregationWithCompanyImos(pipeline) {
const companyName = (0, config_1.getConfig)().companyName;
// Skip filtering for admin companies
if (!companyName || (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName)) {
logger_js_1.logger.debug(`Skipping MongoDB aggregation IMO filtering for admin company: ${companyName}`);
return pipeline;
}
const companyImos = await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
// If no IMO numbers configured, return original pipeline
if (companyImos.length === 0) {
logger_js_1.logger.warn('No company IMO numbers configured. Skipping MongoDB aggregation IMO filtering.');
return pipeline;
}
// Convert IMO numbers to integers for MongoDB query
const imoNumbers = companyImos.map((imo) => Number(imo));
// Create IMO match stage
const imoMatchStage = {
$match: {
imo: { $in: imoNumbers }
}
};
// Add IMO filter as the first stage in the pipeline
const updatedPipeline = [imoMatchStage, ...pipeline];
logger_js_1.logger.debug(`Applied MongoDB aggregation IMO filter: ${JSON.stringify(imoMatchStage)}`);
return updatedPipeline;
}
/**
* Update search query parameters with company IMO filtering (PMS version)
* @param searchParams - Search parameters object
* @returns Updated search parameters with IMO restrictions
*/
async function updateSearchParamsWithCompanyImos(searchParams) {
const companyName = (0, config_1.getConfig)().companyName;
// Skip filtering for admin companies
if (!companyName || (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName)) {
logger_js_1.logger.debug(`Skipping search params IMO filtering for admin company: ${companyName}`);
return searchParams;
}
const companyImos = await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
// If no IMO numbers configured, return original params
if (companyImos.length === 0) {
logger_js_1.logger.warn('No company IMO numbers configured. Skipping search params IMO filtering.');
return searchParams;
}
// Create a copy of the search params
const updatedParams = { ...searchParams };
// Update filter_by parameter for Typesense
if (updatedParams.filter_by) {
updatedParams.filter_by = updateTypesenseFilterWithCompanyImosPms(updatedParams.filter_by);
}
else {
updatedParams.filter_by = updateTypesenseFilterWithCompanyImosPms('');
}
logger_js_1.logger.debug(`Updated search params with IMO filtering: ${JSON.stringify(updatedParams)}`);
return updatedParams;
}
/**
* Check if a vessel IMO is authorized for the current company (PMS version)
* @param imo - IMO number to check
* @returns True if authorized, false otherwise
*/
async function isVesselAuthorizedForCompany(imo) {
const companyName = (0, config_1.getConfig)().companyName;
// Allow access for admin companies
if (!companyName || (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName)) {
return true;
}
const companyImos = await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
// If no IMO numbers configured, deny access
if (companyImos.length === 0) {
return false;
}
const imoNumber = Number(imo);
const companyImoNumbers = companyImos.map((imo) => Number(imo));
return companyImoNumbers.includes(imoNumber);
}
/**
* Get authorized IMO numbers for the current company (PMS version)
* @returns Array of authorized IMO numbers
*/
async function getAuthorizedImoNumbers() {
const companyName = (0, config_1.getConfig)().companyName;
// For admin companies, return empty array (no restrictions)
if (!companyName || (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName)) {
return [];
}
return await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
}
/**
* Log IMO filtering activity for monitoring (PMS version)
* @param action - Action being performed
* @param details - Additional details about the filtering
*/
async function logImoFilteringActivity(action, details = {}) {
const companyName = (0, config_1.getConfig)().companyName;
const companyImos = await (0, imoUtils_js_1.fetchCompanyImoNumbers)(companyName);
logger_js_1.logger.info(`IMO filtering activity: ${action}`, {
companyName,
companyImoCount: companyImos.length,
isAdminCompany: companyName ? (0, imoUtils_js_1.shouldBypassImoFiltering)(companyName) : false,
...details
});
}
/**
* Generic function to export Typesense data for a given collection and IMO list
* @param collectionName - Name of the Typesense collection
* @param imoList - Array of IMO numbers
* @param startDate - Optional start date filter (ISO string)
* @param endDate - Optional end date filter (ISO string)
* @param dateField - Name of the date field to filter on
* @param excludeFields - Fields to exclude in the export
* @param timestampFields - Fields to convert from UNIX timestamp to ISO string
* @returns Array of parsed and processed documents
*/
async function exportDataForImoListGeneric(collectionName, imoList, startDate, endDate, dateField, excludeFields = "", timestampFields = []) {
try {
const client = (0, typesense_js_1.getTypesenseClient)();
const collection = client.collections(collectionName);
const dateToTs = (dateStr) => {
return Math.floor(new Date(dateStr).getTime() / 1000);
};
const filterParts = [`imo:[${imoList.join(',')}]`];
if (startDate && dateField) {
filterParts.push(`${dateField}:>=${dateToTs(startDate)}`);
}
if (endDate && dateField) {
filterParts.push(`${dateField}:<=${dateToTs(endDate)}`);
}
const filterBy = filterParts.join(" && ");
const query = {
filter_by: filterBy,
exclude_fields: excludeFields
};
const exportResult = await collection.documents().export(query);
let exportData;
if (typeof exportResult === 'string') {
exportData = exportResult;
}
else if (exportResult && typeof exportResult === 'object' && 'buffer' in exportResult) {
exportData = new TextDecoder().decode(exportResult);
}
else {
exportData = String(exportResult);
}
const documents = exportData
.split('\n')
.filter(line => line.trim())
.map(line => JSON.parse(line));
// Convert UNIX timestamps to readable date strings
for (const doc of documents) {
for (const field of timestampFields) {
if (field in doc && typeof doc[field] === 'number') {
try {
doc[field] = new Date(doc[field] * 1000).toISOString().replace('T', ' ').substring(0, 19);
}
catch (err) {
// Leave original value on error
}
}
}
}
return documents;
}
catch (error) {
logger_js_1.logger.error(`Error exporting data from '${collectionName}' collection:`, error);
return [];
}
}
async function exportDefectsForImoList(imoList, startDate, endDate) {
return exportDataForImoListGeneric('defect', imoList, startDate, endDate, 'reportDate', "_id,docId,fleetId,vesselId,fleetManagerId,technicalSuperintendentId,id", [
'inspectionTargetDate',
'reportDate',
'closingDate',
'targetDate',
'nextDueDate',
'extendedDate'
]);
}
async function exportPurchasesForImoList(imoList, startDate, endDate) {
return exportDataForImoListGeneric('purchase', imoList, startDate, endDate, 'purchaseRequisitionDate', "embedding", [
'purchaseRequisitionDate',
'purchaseOrderIssuedDate',
'orderReadinessDate'
]);
}
async function exportBudgetsForImoList(imoList, startDate, endDate) {
return exportDataForImoListGeneric('budget', imoList, startDate, endDate, 'date', "embedding", ['date']);
}
async function exportExpensesForImoList(imoList, startDate, endDate) {
return exportDataForImoListGeneric('expense', imoList, startDate, endDate, 'expenseDate', "embedding", [
'expenseDate',
'poDate'
]);
}
async function exportSurveysForImoList(imoList, startDate, endDate) {
return exportDataForImoListGeneric('survey', imoList, startDate, endDate, 'surveyDate', "embedding", ['surveyDate']);
}
/**
* Get MongoDB database instance by context
* @param context - Determines the client and database name to use
* @returns MongoDB Database instance
*/
async function getDatabaseInstance(context) {
if (context === 'pms-etl') {
const client = await (0, mongodb_js_1.getEtlDevClient)();
return client.db((0, mongodb_js_2.getEtlDevDbName)());
}
const client = await (0, mongodb_js_3.getMongoClient)();
const config = (0, config_1.getConfig)();
switch (context) {
case 'pms':
case 'defect':
return client.db(config.dbName);
case 'pms-engine':
case 'defect-secondary':
return client.db(config.secondaryDbName || config.dbName);
default:
throw new Error(`Unsupported database context: ${context}`);
}
}
async function getPmsDatabase() {
return getDatabaseInstance('pms');
}
async function getPmsEtlDatabase() {
return getDatabaseInstance('pms-etl');
}
async function getPmsEngineDataDatabase() {
return getDatabaseInstance('pms-engine');
}
async function getDefectDatabase() {
return getDatabaseInstance('defect');
}
async function getDefectSecondaryDatabase() {
return getDatabaseInstance('defect-secondary');
}