Explorar o código

Add class method to add mention notification

sbkwgh %!s(int64=8) %!d(string=hai) anos
pai
achega
5bc67bcc20
Modificáronse 1 ficheiros con 18 adicións e 0 borrados
  1. 18 0
      models/Notification.js

+ 18 - 0
models/Notification.js

@@ -10,6 +10,24 @@ module.exports = (sequelize, DataTypes) => {
 			associate (models) {
 				Notification.hasOne(models.MentionNotification)
 				Notification.belongsTo(models.User)
+			},
+			//Props fields: user, post, mention
+			async createMention (props) {
+				let { MentionNotification, User } = sequelize.models
+
+				let user = await User.findOne({ where: { username: props.mention } })
+				if(!user) return null
+				
+				let notification = await Notification.create({ type: 'mention' })
+				let mentionNotification = await MentionNotification.create()
+
+				await mentionNotification.addUser(props.user)
+				await mentionNotification.addPost(props.post)
+
+				await notification.addMentionNotification(mentionNotification)
+				await notification.addUser(user)
+
+				return notification
 			}
 		}
 	})