survey-mcp-server
Version:
63 lines (62 loc) • 2.23 kB
JavaScript
import { MongoClient, ObjectId } from "mongodb";
export async function getUserDetails(identifier) {
try {
const mongoUri = process.env.USERS_MONGO_URI || "";
const dbName = process.env.USERS_DB_NAME || "";
const mongoClient = await MongoClient.connect(mongoUri);
const db = mongoClient.db(dbName);
const collection = db.collection("users");
const query = { _id: new ObjectId(identifier) };
const projection = { _id: 0, firstName: 1, lastName: 1, email: 1, phone: 1 };
const result = await collection.findOne(query, { projection });
await mongoClient.close();
return result || { error: "User Not Found" };
}
catch (error) {
return { error: String(error) };
}
}
export async function getVesselManagers(imo) {
try {
const mongoUri = process.env.FLEET_DISTRIBUTION_MONGO_URI || "";
const dbName = process.env.FLEET_DISTRIBUTION_DB_NAME || "";
const mongoClient = await MongoClient.connect(mongoUri);
const db = mongoClient.db(dbName);
const collection = db.collection("fleet_distributions_overviews");
const result = await collection.findOne({ imo: parseInt(imo) });
await mongoClient.close();
if (result) {
const clean = (value) => {
return (value === null || value === undefined ||
(typeof value === 'number' && isNaN(value))) ? "" : String(value);
};
return {
TS: clean(result.technicalSuperintendent),
TM: clean(result.fleetManager),
TA: clean(result.technicalExecutive),
MM: clean(result.marineManager),
MS: clean(result.marineSuperintendent),
};
}
else {
return {
TS: "",
TM: "",
TA: "",
MM: "",
MS: "",
error: `No vessel found for IMO: ${imo}`
};
}
}
catch (error) {
return {
TS: "",
TM: "",
TA: "",
MM: "",
MS: "",
error: String(error)
};
}
}