Notification.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, Post } = 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. let reloadedNotification = await notification.reload({
  30. include: [{
  31. model: MentionNotification,
  32. include: [Post, { model: User, attributes: ['createdAt', 'username', 'color'] }]
  33. }]
  34. })
  35. return reloadedNotification
  36. }
  37. }
  38. })
  39. return Notification
  40. }