Notification.js 1.0 KB

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