Skip to content

Commit

Permalink
add request to notify for a single athlete
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Dec 5, 2024
1 parent a162273 commit 46e19a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
45 changes: 45 additions & 0 deletions controller/notification_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/swimresults/user-service/dto"
"github.com/swimresults/user-service/service"
"go.mongodb.org/mongo-driver/bson/primitive"
"io"
"net/http"
)
Expand All @@ -13,6 +14,7 @@ func notificationController() {
router.POST("/notification/test/:device", sendTestNotification)
router.POST("/notification/:device", sendNotification)
router.POST("/notification/meet/:meeting", sendNotificationForMeeting)
router.POST("/notification/meet/:meeting/athlete/:athlete", sendNotificationForMeetingAndAthlete)

router.POST("/notification/broadcast/:channel", sendBroadcast)
router.POST("/notification/broadcast/meeting/:meeting", sendMeetingBroadcast)
Expand Down Expand Up @@ -72,6 +74,11 @@ func sendNotificationForMeeting(c *gin.Context) {

meeting := c.Param("meeting")

if meeting == "" {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "no meeting given"})
return
}

var request dto.MeetingNotificationRequestDto
if err := c.BindJSON(&request); err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
Expand All @@ -91,6 +98,44 @@ func sendNotificationForMeeting(c *gin.Context) {
})
}

func sendNotificationForMeetingAndAthlete(c *gin.Context) {

if failIfNotRoot(c) {
return
}

meeting := c.Param("meeting")

if meeting == "" {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "no meeting given"})
return
}

athleteId, convErr := primitive.ObjectIDFromHex(c.Param("athlete"))
if convErr != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given athlete was not of type ObjectID"})
return
}

var request dto.MeetingNotificationRequestDto
if err := c.BindJSON(&request); err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

users, notificationUsers, success, err := service.SendPushNotificationForMeetingAndAthletes(meeting, []primitive.ObjectID{athleteId}, request)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, dto.NotificationsResponseDto{
UserCount: users,
NotificationUserCount: notificationUsers,
SuccessCount: success,
})
}

func sendBroadcast(c *gin.Context) {

if failIfNotRoot(c) {
Expand Down
26 changes: 15 additions & 11 deletions service/notification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,12 @@ func SendTestPushNotification(receiver string) error {
return nil
}

func SendPushNotificationForMeeting(meetingId string, request dto.MeetingNotificationRequestDto) (int, int, int, error) {
func SendPushNotificationForMeetingAndAthletes(meetingId string, athleteIds []primitive.ObjectID, request dto.MeetingNotificationRequestDto) (int, int, int, error) {
meeting, err := GetMeetingById(meetingId)
if err != nil {
return 0, 0, 0, err
}

athletes, err := ac.GetAthletesByMeeting(meetingId)
if err != nil {
return 0, 0, 0, err
}

var athleteIds []primitive.ObjectID
for _, athlete := range athletes {
athleteIds = append(athleteIds, athlete.Identifier)
}

var users []model.User
var err1 error
if request.MessageType == "athlete" {
Expand Down Expand Up @@ -129,6 +119,20 @@ func SendPushNotificationForMeeting(meetingId string, request dto.MeetingNotific
return len(users), len(notificationUsers), success, nil
}

func SendPushNotificationForMeeting(meetingId string, request dto.MeetingNotificationRequestDto) (int, int, int, error) {
athletes, err := ac.GetAthletesByMeeting(meetingId)
if err != nil {
return 0, 0, 0, err
}

var athleteIds []primitive.ObjectID
for _, athlete := range athletes {
athleteIds = append(athleteIds, athlete.Identifier)
}

return SendPushNotificationForMeetingAndAthletes(meetingId, athleteIds, request)
}

func SendPushNotification(receiver string, title string, subtitle string, message string, interruptionLevel string) (string, string, int, error) {
token := apns.GetToken()

Expand Down

0 comments on commit 46e19a6

Please sign in to comment.