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', 'reply')
  12. }, {
  13. classMethods: {
  14. associate (models) {
  15. Notification.hasOne(models.PostNotification)
  16. Notification.belongsTo(models.User)
  17. },
  18. //Props fields: userFrom, usernameTo, post, type
  19. async createPostNotification (props) {
  20. let { PostNotification, User, Post } = sequelize.models
  21. let userTo = await User.findOne({ where: { username: props.usernameTo } })
  22. if(!userTo) return null
  23. let notification = await Notification.create({ type: props.type })
  24. let postNotification = await PostNotification.create()
  25. await postNotification.setUser(props.userFrom)
  26. await postNotification.setPost(props.post)
  27. await notification.setPostNotification(postNotification)
  28. await notification.setUser(userTo)
  29. let reloadedNotification = await notification.reload({
  30. include: [{
  31. model: PostNotification,
  32. include: [Post, { model: User, attributes: ['createdAt', 'username', 'color'] }]
  33. }]
  34. })
  35. return reloadedNotification
  36. }
  37. }
  38. })
  39. return Notification
  40. }