Enhance notification system with in-app delivery support
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Added `DeliveryChannelInApp` to support in-app notifications. - Introduced `EventTypeInApp` constant for identifying in-app notifications. - Updated `AdminNotifications` and `CustomerNotifications` handlers to set the event type to `EventTypeInApp`. - Modified the `GetByUserID` repository method to handle filtering for in-app notifications, allowing for more flexible retrieval options. - Updated the `persistNotification` method to include in-app as a delivery method. This update improves the notification system by enabling in-app notifications, enhancing user engagement and notification management capabilities.
This commit is contained in:
@@ -25,8 +25,12 @@ type DeliveryChannel string
|
|||||||
const (
|
const (
|
||||||
DeliveryChannelPush DeliveryChannel = "push"
|
DeliveryChannelPush DeliveryChannel = "push"
|
||||||
DeliveryChannelEmail DeliveryChannel = "email"
|
DeliveryChannelEmail DeliveryChannel = "email"
|
||||||
|
DeliveryChannelInApp DeliveryChannel = "in_app"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EventTypeInApp identifies notifications stored for the in-app notification center.
|
||||||
|
const EventTypeInApp = "in_app"
|
||||||
|
|
||||||
// DeliveryStatus represents the delivery status of notification
|
// DeliveryStatus represents the delivery status of notification
|
||||||
type NotificationStatus string
|
type NotificationStatus string
|
||||||
|
|
||||||
|
|||||||
@@ -147,6 +147,8 @@ func (h *NotificationHandler) AdminNotifications(c echo.Context) error {
|
|||||||
|
|
||||||
form.Recipient = []string{userID}
|
form.Recipient = []string{userID}
|
||||||
form.Status = "sent"
|
form.Status = "sent"
|
||||||
|
form.EventType = EventTypeInApp
|
||||||
|
form.Method = ""
|
||||||
|
|
||||||
pagination, err := response.NewPagination(c)
|
pagination, err := response.NewPagination(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -265,6 +267,8 @@ func (h *NotificationHandler) CustomerNotifications(c echo.Context) error {
|
|||||||
|
|
||||||
form.Recipient = []string{userID}
|
form.Recipient = []string{userID}
|
||||||
form.Status = "sent"
|
form.Status = "sent"
|
||||||
|
form.EventType = EventTypeInApp
|
||||||
|
form.Method = ""
|
||||||
|
|
||||||
pagination, err := response.NewPagination(c)
|
pagination, err := response.NewPagination(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -119,13 +119,16 @@ func (r *notificationRepository) GetByID(ctx context.Context, id string) (*Notif
|
|||||||
// GetByUserID retrieves notifications with optional user filters
|
// GetByUserID retrieves notifications with optional user filters
|
||||||
func (r *notificationRepository) GetByUserID(ctx context.Context, userIDs []string, search, status string, seen *bool, priority, eventType, notificationType string, pagination *response.Pagination) (*orm.PaginatedResult[Notification], error) {
|
func (r *notificationRepository) GetByUserID(ctx context.Context, userIDs []string, search, status string, seen *bool, priority, eventType, notificationType string, pagination *response.Pagination) (*orm.PaginatedResult[Notification], error) {
|
||||||
filter := bson.M{}
|
filter := bson.M{}
|
||||||
|
andConditions := bson.A{}
|
||||||
|
|
||||||
if q := strings.TrimSpace(search); q != "" {
|
if q := strings.TrimSpace(search); q != "" {
|
||||||
pattern := regexp.QuoteMeta(q)
|
pattern := regexp.QuoteMeta(q)
|
||||||
filter["$or"] = bson.A{
|
andConditions = append(andConditions, bson.M{
|
||||||
bson.M{"title": bson.M{"$regex": pattern, "$options": "i"}},
|
"$or": bson.A{
|
||||||
bson.M{"message": bson.M{"$regex": pattern, "$options": "i"}},
|
bson.M{"title": bson.M{"$regex": pattern, "$options": "i"}},
|
||||||
}
|
bson.M{"message": bson.M{"$regex": pattern, "$options": "i"}},
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(userIDs) == 1 {
|
if len(userIDs) == 1 {
|
||||||
@@ -147,13 +150,31 @@ func (r *notificationRepository) GetByUserID(ctx context.Context, userIDs []stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
if eventType != "" {
|
if eventType != "" {
|
||||||
filter["event_type"] = eventType
|
if eventType == EventTypeInApp {
|
||||||
|
andConditions = append(andConditions, bson.M{
|
||||||
|
"$or": bson.A{
|
||||||
|
bson.M{"event_type": EventTypeInApp},
|
||||||
|
bson.M{"event_type": ""},
|
||||||
|
bson.M{"event_type": bson.M{"$exists": false}},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
filter["event_type"] = eventType
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if notificationType != "" {
|
if notificationType != "" {
|
||||||
filter["type"] = notificationType
|
filter["type"] = notificationType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(andConditions) == 1 {
|
||||||
|
for key, value := range andConditions[0].(bson.M) {
|
||||||
|
filter[key] = value
|
||||||
|
}
|
||||||
|
} else if len(andConditions) > 1 {
|
||||||
|
filter["$and"] = andConditions
|
||||||
|
}
|
||||||
|
|
||||||
mongoPagination, err := orm.BuildListPagination(
|
mongoPagination, err := orm.BuildListPagination(
|
||||||
pagination.Limit,
|
pagination.Limit,
|
||||||
pagination.Offset,
|
pagination.Offset,
|
||||||
|
|||||||
@@ -149,7 +149,9 @@ func (s *notificationService) sendToRecipient(ctx context.Context, recipient not
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *notificationService) persistNotification(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string) error {
|
func (s *notificationService) persistNotification(ctx context.Context, recipient notificationRecipient, req *NotificationRequest, link, image string) error {
|
||||||
methods := make(map[string]string)
|
methods := map[string]string{
|
||||||
|
"in_app": "true",
|
||||||
|
}
|
||||||
if slices.Contains(req.Channels, DeliveryChannelEmail) && recipient.Email != "" {
|
if slices.Contains(req.Channels, DeliveryChannelEmail) && recipient.Email != "" {
|
||||||
methods["email"] = recipient.Email
|
methods["email"] = recipient.Email
|
||||||
}
|
}
|
||||||
@@ -170,6 +172,7 @@ func (s *notificationService) persistNotification(ctx context.Context, recipient
|
|||||||
Image: image,
|
Image: image,
|
||||||
Priority: string(req.Priority),
|
Priority: string(req.Priority),
|
||||||
Type: string(req.Type),
|
Type: string(req.Type),
|
||||||
|
EventType: EventTypeInApp,
|
||||||
Status: status,
|
Status: status,
|
||||||
Methods: methods,
|
Methods: methods,
|
||||||
Metadata: map[string]any{
|
Metadata: map[string]any{
|
||||||
|
|||||||
Reference in New Issue
Block a user