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

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

exports.main = async event => {
  const { OPENID } = cloud.getWXContext()
  let ownerId = event.ownerId
  if (!ownerId) {
    const user = await db.collection('users').where({ openid: OPENID }).limit(1).get()
    ownerId = user.data[0]?._id
  }

  const res = ownerId ? await db.collection('pets').where({ ownerId }).limit(20).get() : { data: [] }
  return { list: res.data }
}

