const { getRedisClient } = require("./client")

const NOTIFICATIONS_CHANNEL = "notifications"

function createRedisClient() {
  return getRedisClient()
}

async function publishNotification(payload) {
  if (!payload) return 0
  const redis = createRedisClient()
  try {
    const message = JSON.stringify(payload)
    return await redis.publish(NOTIFICATIONS_CHANNEL, message)
  } catch {
    return 0
  } finally {}
}

module.exports = { publishNotification, NOTIFICATIONS_CHANNEL }
