const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command

exports.main = async event => {
  const { OPENID } = cloud.getWXContext()
  if (!event.conversationId) throw new Error('conversationId is required')

  await db.collection('conversations').doc(event.conversationId).update({
    data: {
      unreadOpenids: _.pull(OPENID),
      [`unreadCounts.${OPENID}`]: 0
    }
  })

  return { unreadCount: 0 }
}
