Refactor notification generation for assigned user
This commit is contained in:
parent
b08a180d99
commit
99453d0869
1 changed files with 67 additions and 102 deletions
|
@ -28,10 +28,11 @@ func NewNotificationPlanner(nr *nRepo.NotificationRepository, cr *cRepo.CircleRe
|
||||||
func (n *NotificationPlanner) GenerateNotifications(c context.Context, chore *chModel.Chore) bool {
|
func (n *NotificationPlanner) GenerateNotifications(c context.Context, chore *chModel.Chore) bool {
|
||||||
log := logging.FromContext(c)
|
log := logging.FromContext(c)
|
||||||
circleMembers, err := n.cRepo.GetCircleUsers(c, chore.CircleID)
|
circleMembers, err := n.cRepo.GetCircleUsers(c, chore.CircleID)
|
||||||
assignees := make([]*cModel.UserCircleDetail, 0)
|
var assignedUser *cModel.UserCircleDetail
|
||||||
for _, member := range circleMembers {
|
for _, member := range circleMembers {
|
||||||
if member.UserID == chore.AssignedTo {
|
if member.UserID == chore.AssignedTo {
|
||||||
assignees = append(assignees, member)
|
assignedUser = member
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,13 +55,13 @@ func (n *NotificationPlanner) GenerateNotifications(c context.Context, chore *ch
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if mt.DueDate {
|
if mt.DueDate {
|
||||||
notifications = append(notifications, generateDueNotifications(chore, assignees)...)
|
notifications = append(notifications, generateDueNotifications(chore, assignedUser))
|
||||||
}
|
}
|
||||||
if mt.PreDue {
|
if mt.PreDue {
|
||||||
notifications = append(notifications, generatePreDueNotifications(chore, assignees)...)
|
notifications = append(notifications, generatePreDueNotifications(chore, assignedUser))
|
||||||
}
|
}
|
||||||
if mt.Nagging {
|
if mt.Nagging {
|
||||||
notifications = append(notifications, generateOverdueNotifications(chore, assignees)...)
|
notifications = append(notifications, generateOverdueNotifications(chore, assignedUser)...)
|
||||||
}
|
}
|
||||||
if mt.CircleGroup {
|
if mt.CircleGroup {
|
||||||
notifications = append(notifications, generateCircleGroupNotifications(chore, mt)...)
|
notifications = append(notifications, generateCircleGroupNotifications(chore, mt)...)
|
||||||
|
@ -70,118 +71,82 @@ func (n *NotificationPlanner) GenerateNotifications(c context.Context, chore *ch
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDueNotifications(chore *chModel.Chore, users []*cModel.UserCircleDetail) []*nModel.Notification {
|
func generateDueNotifications(chore *chModel.Chore, assignedUser *cModel.UserCircleDetail) *nModel.Notification {
|
||||||
notifications := make([]*nModel.Notification, 0)
|
|
||||||
|
|
||||||
var assignee *cModel.UserCircleDetail
|
|
||||||
for _, user := range users {
|
|
||||||
if user.ID == chore.AssignedTo {
|
|
||||||
assignee = user
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, user := range users {
|
|
||||||
notification := &nModel.Notification{
|
notification := &nModel.Notification{
|
||||||
ChoreID: chore.ID,
|
ChoreID: chore.ID,
|
||||||
IsSent: false,
|
IsSent: false,
|
||||||
ScheduledFor: *chore.NextDueDate,
|
ScheduledFor: *chore.NextDueDate,
|
||||||
CreatedAt: time.Now().UTC(),
|
CreatedAt: time.Now().UTC(),
|
||||||
TypeID: user.NotificationType,
|
TypeID: assignedUser.NotificationType,
|
||||||
|
|
||||||
UserID: user.UserID,
|
UserID: assignedUser.UserID,
|
||||||
TargetID: user.TargetID,
|
TargetID: assignedUser.TargetID,
|
||||||
Text: fmt.Sprintf("📅 Reminder: *%s* is due today and assigned to %s.", chore.Name, assignee.DisplayName),
|
Text: fmt.Sprintf("📅 Reminder: *%s* is due today and assigned to %s.", chore.Name, assignedUser.DisplayName),
|
||||||
RawEvent: map[string]interface{}{
|
RawEvent: map[string]interface{}{
|
||||||
"id": chore.ID,
|
"id": chore.ID,
|
||||||
"name": chore.Name,
|
"name": chore.Name,
|
||||||
"due_date": chore.NextDueDate,
|
"due_date": chore.NextDueDate,
|
||||||
"assignee": assignee.DisplayName,
|
"assignee": assignedUser.DisplayName,
|
||||||
"assignee_username": assignee.Username,
|
"assignee_username": assignedUser.Username,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if notification.IsValid() {
|
|
||||||
notifications = append(notifications, notification)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return notifications
|
return notification
|
||||||
}
|
}
|
||||||
|
|
||||||
func generatePreDueNotifications(chore *chModel.Chore, users []*cModel.UserCircleDetail) []*nModel.Notification {
|
func generatePreDueNotifications(chore *chModel.Chore, assignedUser *cModel.UserCircleDetail) *nModel.Notification {
|
||||||
var assignee *cModel.UserCircleDetail
|
|
||||||
for _, user := range users {
|
|
||||||
if user.ID == chore.AssignedTo {
|
|
||||||
assignee = user
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
notifications := make([]*nModel.Notification, 0)
|
|
||||||
for _, user := range users {
|
|
||||||
notification := &nModel.Notification{
|
notification := &nModel.Notification{
|
||||||
ChoreID: chore.ID,
|
ChoreID: chore.ID,
|
||||||
IsSent: false,
|
IsSent: false,
|
||||||
ScheduledFor: *chore.NextDueDate,
|
ScheduledFor: *chore.NextDueDate,
|
||||||
CreatedAt: time.Now().UTC().Add(-time.Hour * 3),
|
CreatedAt: time.Now().UTC().Add(-time.Hour * 3),
|
||||||
TypeID: user.NotificationType,
|
TypeID: assignedUser.NotificationType,
|
||||||
UserID: user.UserID,
|
UserID: assignedUser.UserID,
|
||||||
CircleID: user.CircleID,
|
CircleID: assignedUser.CircleID,
|
||||||
TargetID: user.TargetID,
|
TargetID: assignedUser.TargetID,
|
||||||
|
|
||||||
Text: fmt.Sprintf("📢 Heads up! *%s* is due soon (on %s) and assigned to %s.", chore.Name, chore.NextDueDate.Format("January 2nd"), assignee.DisplayName),
|
Text: fmt.Sprintf("📢 Heads up! *%s* is due soon (on %s) and assigned to %s.", chore.Name, chore.NextDueDate.Format("January 2nd"), assignedUser.DisplayName),
|
||||||
|
|
||||||
RawEvent: map[string]interface{}{
|
RawEvent: map[string]interface{}{
|
||||||
"id": chore.ID,
|
"id": chore.ID,
|
||||||
"name": chore.Name,
|
"name": chore.Name,
|
||||||
"due_date": chore.NextDueDate,
|
"due_date": chore.NextDueDate,
|
||||||
"assignee": assignee.DisplayName,
|
"assignee": assignedUser.DisplayName,
|
||||||
"assignee_username": assignee.Username,
|
"assignee_username": assignedUser.Username,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if notification.IsValid() {
|
|
||||||
notifications = append(notifications, notification)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
return notification
|
||||||
return notifications
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateOverdueNotifications(chore *chModel.Chore, users []*cModel.UserCircleDetail) []*nModel.Notification {
|
func generateOverdueNotifications(chore *chModel.Chore, assignedUser *cModel.UserCircleDetail) []*nModel.Notification {
|
||||||
var assignee *cModel.UserCircleDetail
|
var notifications []*nModel.Notification
|
||||||
for _, user := range users {
|
|
||||||
if user.ID == chore.AssignedTo {
|
|
||||||
assignee = user
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
notifications := make([]*nModel.Notification, 0)
|
|
||||||
for _, hours := range []int{24, 48, 72} {
|
for _, hours := range []int{24, 48, 72} {
|
||||||
scheduleTime := chore.NextDueDate.Add(time.Hour * time.Duration(hours))
|
scheduleTime := chore.NextDueDate.Add(time.Hour * time.Duration(hours))
|
||||||
for _, user := range users {
|
|
||||||
notification := &nModel.Notification{
|
notification := &nModel.Notification{
|
||||||
ChoreID: chore.ID,
|
ChoreID: chore.ID,
|
||||||
IsSent: false,
|
IsSent: false,
|
||||||
ScheduledFor: scheduleTime,
|
ScheduledFor: scheduleTime,
|
||||||
CreatedAt: time.Now().UTC(),
|
CreatedAt: time.Now().UTC(),
|
||||||
TypeID: user.NotificationType,
|
TypeID: assignedUser.NotificationType,
|
||||||
UserID: user.UserID,
|
UserID: assignedUser.UserID,
|
||||||
CircleID: user.CircleID,
|
CircleID: assignedUser.CircleID,
|
||||||
TargetID: fmt.Sprint(user.TargetID),
|
TargetID: fmt.Sprint(assignedUser.TargetID),
|
||||||
Text: fmt.Sprintf("🚨 *%s* is now %d hours overdue. Please complete it as soon as possible. (Assigned to %s)", chore.Name, hours, assignee.DisplayName),
|
Text: fmt.Sprintf("🚨 *%s* is now %d hours overdue. Please complete it as soon as possible. (Assigned to %s)", chore.Name, hours, assignedUser.DisplayName),
|
||||||
RawEvent: map[string]interface{}{
|
RawEvent: map[string]interface{}{
|
||||||
"id": chore.ID,
|
"id": chore.ID,
|
||||||
"type": EventTypeOverdue,
|
"type": EventTypeOverdue,
|
||||||
"name": chore.Name,
|
"name": chore.Name,
|
||||||
"due_date": chore.NextDueDate,
|
"due_date": chore.NextDueDate,
|
||||||
"assignee": assignee.DisplayName,
|
"assignee": assignedUser.DisplayName,
|
||||||
"assignee_username": assignee.Username,
|
"assignee_username": assignedUser.Username,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if notification.IsValid() {
|
|
||||||
notifications = append(notifications, notification)
|
notifications = append(notifications, notification)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return notifications
|
return notifications
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue