@unifers/messenger
v1.0.0
Published
Communication module
Readme
Messenger
Communication module
Reuse a MongoDB connection
Optionally pass your existing Mongoose connection to use it for both email and SMS logs:
Messenger.init({
fromMail,
replyMail,
templates,
mongoConnection: mongoose.connection,
mailLog: { enabled: true },
smsLog: { enabled: true }
});Messenger uses the supplied connection directly, without reconnecting, changing its settings,
or closing it. Your application manages its lifecycle. MONGO_URI is unnecessary when a
connection is supplied; otherwise Messenger uses MONGO_URI as before. Direct send functions
also accept mongoConnection in their log config. The top-level connection takes priority.
Email logs
Enable MongoDB email logging with LOG_EMAIL_ENABLE=true or mailLog: { enabled: true }
in Messenger.init(). Explicit mailLog.enabled takes priority. LOG_MAIL_ENABLE
remains a fallback when LOG_EMAIL_ENABLE is unset. Writes are skipped when neither a supplied
connection nor a nonempty MONGO_URI is available.
SMS logs
Enable MongoDB SMS logging with LOG_SMS_ENABLE=true or smsLog: { enabled: true } in
Messenger.init(). Set MONGO_URI and optionally MONGO_DB_NAME using the shared MongoDB connection.
Logs are stored in smsLogs with the recipient, template, status, attempted providers,
attempt count, error, duration, dry-run flag, and timestamps.
Pass { userId, source, requestId, enabled } as the optional second argument to
Messenger.sendSms() for per-call metadata or to override logging. Direct sendSms()
calls accept those options as the second argument and an SMS log config as the third.
Writes run in the background. Without a supplied connection, missing or empty MONGO_URI skips writes; Messenger.init()
prints a skip message. Write failures call smsLog.onError(error, context) or log to
the console without failing SMS delivery. Messenger.closeSmsLog() closes the shared
MongoDB connection used by both email and SMS logs.
