Subscribe a user to a task when they are assigned to it

This commit is contained in:
kolaente 2021-02-17 20:14:38 +01:00
parent aaeb89db4f
commit 0bd27ddeb7
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -42,6 +42,7 @@ func RegisterListeners() {
events.RegisterListener((&TaskAssigneeCreatedEvent{}).Name(), &SendTaskAssignedNotification{}) events.RegisterListener((&TaskAssigneeCreatedEvent{}).Name(), &SendTaskAssignedNotification{})
events.RegisterListener((&TaskDeletedEvent{}).Name(), &SendTaskDeletedNotification{}) events.RegisterListener((&TaskDeletedEvent{}).Name(), &SendTaskDeletedNotification{})
events.RegisterListener((&ListCreatedEvent{}).Name(), &SendListCreatedNotification{}) events.RegisterListener((&ListCreatedEvent{}).Name(), &SendListCreatedNotification{})
events.RegisterListener((&TaskAssigneeCreatedEvent{}).Name(), &SubscribeAssigneeToTask{})
} }
////// //////
@ -212,6 +213,39 @@ func (s *SendTaskDeletedNotification) Handle(payload message.Payload) (err error
return nil return nil
} }
type SubscribeAssigneeToTask struct {
}
// Name defines the name for the SubscribeAssigneeToTask listener
func (s *SubscribeAssigneeToTask) Name() string {
return "subscribe.assignee.to.task"
}
// Handle is executed when the event SubscribeAssigneeToTask listens on is fired
func (s *SubscribeAssigneeToTask) Handle(payload message.Payload) (err error) {
event := &TaskAssigneeCreatedEvent{}
err = json.Unmarshal(payload, event)
if err != nil {
return err
}
sub := &Subscription{
UserID: event.Assignee.ID,
EntityType: SubscriptionEntityTask,
EntityID: event.Task.ID,
}
sess := db.NewSession()
defer sess.Close()
err = sub.Create(sess, event.Assignee)
if err != nil && !IsErrSubscriptionAlreadyExists(err) {
return err
}
return sess.Commit()
}
/////// ///////
// List Event Listeners // List Event Listeners