Files
broccolini-bot/db-connection.js
indifferentketchup 840b6bfcf8 simplify: prune dead code, dedup gmail send, drop neutered log stubs
- Remove no-op log stubs (logGmail, logAutomation, logSecurity, logSystem)
  and ~17 callsites; dead counters in tickets.js and gmail-poll.js go too
- Dedup three near-identical Gmail send paths into sendThreadedEmail helper
- Drop dead Mongoose fields: broccoliniTicketId, lastSyncedBroccoliniArticleId,
  renameCount, renameWindowStart, reminderSent, staffChannelId,
  unclaimedRemindersSent, lastMessageAuthorIsStaff
- Drop dead config fields and their .env.example entries
- Inline api/botClient.js (3-line wrapper, 2 callers)
- Trim unused exports across utils.js, tickets.js, configSchema.js, debugLog.js
- Fix handlers/messages.js to use isStaff() — old partial check ignored
  ADDITIONAL_STAFF_ROLES, so those members were treated as customers
- Drop unused deps p-queue + dotenv-expand; move mongodb to devDependencies

Net: -583 LOC source + -57 LOC lockfile. All 23 modules load clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 18:37:14 +00:00

86 lines
2.3 KiB
JavaScript

const mongoose = require('mongoose');
require('./models'); // Load all schemas
/**
* Connect to MongoDB with reconnection logic
* @param {string} uri - MongoDB connection URI (from process.env.MONGODB_URI)
* @param {object} options - Optional Mongoose connection options
* @returns {Promise<void>}
*/
async function connectMongoDB(uri, options = {}) {
if (!uri) {
throw new Error('MONGODB_URI is required. Add it to your .env file.');
}
const defaultOptions = {
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
...options
};
try {
await mongoose.connect(uri, defaultOptions);
console.log('✓ Connected to MongoDB');
mongoose.connection.on('error', (err) => {
console.error('MongoDB connection error:', err);
});
mongoose.connection.on('disconnected', () => {
console.warn('MongoDB disconnected. Attempting to reconnect...');
});
mongoose.connection.on('reconnected', () => {
console.log('✓ MongoDB reconnected');
});
} catch (err) {
console.error('Failed to connect to MongoDB:', err.message);
console.error('Stack:', err.stack);
throw err;
}
}
/**
* Gracefully close MongoDB connection
* @returns {Promise<void>}
*/
async function closeMongoDB() {
try {
await mongoose.connection.close();
console.log('MongoDB connection closed');
} catch (err) {
console.error('Error closing MongoDB:', err);
}
}
/**
* Retry a function on Mongoose connection errors.
* @param {Function} fn - async function to execute
* @param {object} options - { retries: 3, delayMs: 500 }
* @returns {Promise<*>}
*/
async function withRetry(fn, options = {}) {
const { retries = 3, delayMs = 500 } = options;
let lastError;
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return await fn();
} catch (err) {
lastError = err;
const isConnectionError = err.name === 'MongoNetworkError' ||
mongoose.connection.readyState !== 1;
if (!isConnectionError || attempt >= retries) throw err;
await new Promise(r => setTimeout(r, delayMs));
}
}
throw lastError;
}
module.exports = {
connectMongoDB,
closeMongoDB,
withRetry,
mongoose
};