GWM : merge
This commit is contained in:
parent
9c92cd63e3
commit
f24f2af3fc
118
main.go
118
main.go
@ -517,47 +517,47 @@ func main() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("✅ Successfully sent message: %s\n", response)
|
fmt.Printf("✅ Successfully sent message: %s\n", response)
|
||||||
}
|
|
||||||
|
|
||||||
// Update the pushed_notification field
|
// Update the pushed_notification field
|
||||||
var pushedNotifications []string
|
var pushedNotifications []string
|
||||||
|
|
||||||
// Get existing pushed_notification JSON field
|
// Get existing pushed_notification JSON field
|
||||||
pushedNotificationsJSON := user.GetString("pushed_notification")
|
pushedNotificationsJSON := user.GetString("pushed_notification")
|
||||||
if pushedNotificationsJSON != "" {
|
if pushedNotificationsJSON != "" {
|
||||||
err := json.Unmarshal([]byte(pushedNotificationsJSON), &pushedNotifications)
|
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 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Append the new notification ID if not already present
|
// Update the user record in the database
|
||||||
alreadyExists := false
|
user.Set("pushed_notification", string(updatedPushedNotifications))
|
||||||
for _, existingID := range pushedNotifications {
|
if err := app.Dao().SaveRecord(user); err != nil {
|
||||||
if existingID == notificationID {
|
log.Printf("❌ Error updating pushed_notification for user %s: %v", user.GetString("id"), err)
|
||||||
alreadyExists = true
|
} else {
|
||||||
break
|
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 {
|
e.Router.GET("/api/pushNotification", func(c echo.Context) error {
|
||||||
|
|
||||||
// type Request struct {
|
// type Request struct {
|
||||||
|
|||||||
BIN
pb_data/data.db
BIN
pb_data/data.db
Binary file not shown.
Binary file not shown.
BIN
pb_data/logs.db
BIN
pb_data/logs.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user