GWM : merge

This commit is contained in:
Gowtham M 2025-03-11 16:14:51 +05:30
parent 9c92cd63e3
commit f24f2af3fc
7 changed files with 85 additions and 33 deletions

118
main.go
View File

@ -517,47 +517,47 @@ func main() {
}
} else {
fmt.Printf("✅ Successfully sent message: %s\n", response)
}
// Update the pushed_notification field
var pushedNotifications []string
// Update the pushed_notification field
var pushedNotifications []string
// Get existing pushed_notification JSON field
pushedNotificationsJSON := user.GetString("pushed_notification")
if pushedNotificationsJSON != "" {
err := json.Unmarshal([]byte(pushedNotificationsJSON), &pushedNotifications)
// Get existing pushed_notification JSON field
pushedNotificationsJSON := user.GetString("pushed_notification")
if pushedNotificationsJSON != "" {
err := json.Unmarshal([]byte(pushedNotificationsJSON), &pushedNotifications)
if err != nil {
log.Printf("⚠️ Error parsing pushed_notification for user %s: %v", user.GetString("id"), err)
continue
}
}
// Append the new notification ID if not already present
alreadyExists := false
for _, existingID := range pushedNotifications {
if existingID == notificationID {
alreadyExists = true
break
}
}
if !alreadyExists {
pushedNotifications = append(pushedNotifications, notificationID)
}
// Convert back to JSON
updatedPushedNotifications, err := json.Marshal(pushedNotifications)
if err != nil {
log.Printf("⚠️ Error parsing pushed_notification for user %s: %v", user.GetString("id"), err)
log.Printf("⚠️ Error marshalling pushed_notification JSON: %v", err)
continue
}
}
// Append the new notification ID if not already present
alreadyExists := false
for _, existingID := range pushedNotifications {
if existingID == notificationID {
alreadyExists = true
break
// Update the user record in the database
user.Set("pushed_notification", string(updatedPushedNotifications))
if err := app.Dao().SaveRecord(user); err != nil {
log.Printf("❌ Error updating pushed_notification for user %s: %v", user.GetString("id"), err)
} else {
fmt.Printf("✅ Updated pushed_notification for user %s\n", user.GetString("id"))
}
}
if !alreadyExists {
pushedNotifications = append(pushedNotifications, notificationID)
}
// Convert back to JSON
updatedPushedNotifications, err := json.Marshal(pushedNotifications)
if err != nil {
log.Printf("⚠️ Error marshalling pushed_notification JSON: %v", err)
continue
}
// Update the user record in the database
user.Set("pushed_notification", string(updatedPushedNotifications))
if err := app.Dao().SaveRecord(user); err != nil {
log.Printf("❌ Error updating pushed_notification for user %s: %v", user.GetString("id"), err)
} else {
fmt.Printf("✅ Updated pushed_notification for user %s\n", user.GetString("id"))
}
}
}
@ -671,6 +671,58 @@ func main() {
})
})
e.Router.GET("/api/removeNotification", func(c echo.Context) error {
userID := c.QueryParam("user_id")
notificationID := c.QueryParam("notification_id")
if userID == "" || notificationID == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "user_id and notification_id are required"})
}
// Fetch the user by ID
user, err := app.Dao().FindRecordById("users", userID)
if err != nil {
return c.JSON(http.StatusNotFound, map[string]string{"error": "user not found"})
}
var pushedNotifications []string
// Get existing pushed_notification JSON field
pushedNotificationsJSON := user.GetString("pushed_notification")
if pushedNotificationsJSON != "" {
err := json.Unmarshal([]byte(pushedNotificationsJSON), &pushedNotifications)
if err != nil {
log.Printf("⚠️ Error parsing pushed_notification for user %s: %v", userID, err)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to parse pushed_notification"})
}
}
// Remove the notification ID if it exists
updatedNotifications := []string{}
for _, existingID := range pushedNotifications {
if existingID != notificationID {
updatedNotifications = append(updatedNotifications, existingID)
}
}
// Convert updated array back to JSON
updatedJSON, err := json.Marshal(updatedNotifications)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to update pushed_notification"})
}
// Update the user record with the new pushed_notification field
user.Set("pushed_notification", string(updatedJSON))
if err := app.Dao().SaveRecord(user); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to save updated notifications"})
}
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "success",
"pushed_notification": updatedNotifications,
})
})
e.Router.GET("/api/pushNotification", func(c echo.Context) error {
// type Request struct {

Binary file not shown.

Binary file not shown.

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.