name: b6_collect_afterInsert_notification
listenTo: b6_collect
when:
  - afterInsert
isEnabled: true
handler: |-
  // global: {_:lodash, moment, validator, filters}
  // ctx
  // objects
  // services

  const doc = ctx.params.doc;
  const userId = ctx.params.userId;

  const to = doc.owner;

  const table = await objects.b6_tables.findOne(doc.table, { fields: ['label'] });
  const fromUser = await ctx.getUser(userId, doc.space);
  const notificationTitle = `${fromUser.name} 给您下发了收集表`;
  let notificationDoc = {
      name: table.label,
      body: notificationTitle,
      related_to: {
          o: "b6_collect",
          ids: [doc._id]
      },
      related_name: table.label + "-" + doc._id,
      from: userId,
      space: doc.space
  };

  ctx.broker.call('notifications.add', {
      message: notificationDoc,
      from: userId,
      to: to
  })
locked: false
