Notification.js 1007 B

123456789101112131415161718192021222324252627282930313233343536
  1. module.exports = (sequelize, DataTypes) => {
  2. let Notification = sequelize.define('Notification', {
  3. interacted: {
  4. type: DataTypes.BOOLEAN,
  5. defaultValue: false
  6. },
  7. type: DataTypes.ENUM('mention', 'thread update')
  8. }, {
  9. classMethods: {
  10. associate (models) {
  11. Notification.hasOne(models.MentionNotification)
  12. Notification.belongsTo(models.User)
  13. },
  14. //Props fields: user, post, mention
  15. async createMention (props) {
  16. let { MentionNotification, User } = sequelize.models
  17. let user = await User.findOne({ where: { username: props.mention } })
  18. if(!user) return null
  19. let notification = await Notification.create({ type: 'mention' })
  20. let mentionNotification = await MentionNotification.create()
  21. await mentionNotification.addUser(props.user)
  22. await mentionNotification.addPost(props.post)
  23. await notification.addMentionNotification(mentionNotification)
  24. await notification.addUser(user)
  25. return notification
  26. }
  27. }
  28. })
  29. return Notification
  30. }