2023-10-09 18:23:44 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"gitlab.com/pactual1/backend/database/notification"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func GetNotifications(c *gin.Context) {
|
2023-10-16 19:43:35 +02:00
|
|
|
// Get the Company ID and Time from query parameters
|
|
|
|
|
companyIDStr := c.DefaultQuery("company_id", "")
|
2023-10-09 18:23:44 +02:00
|
|
|
timeStr := c.DefaultQuery("time", "")
|
|
|
|
|
|
2023-10-16 19:43:35 +02:00
|
|
|
if companyIDStr == "" || timeStr == "" {
|
|
|
|
|
log.Printf("GetNotifications Error: Company ID and Time are required")
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID and Time are required"})
|
2023-10-09 18:23:44 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert string to int for UserID
|
2023-10-16 19:43:35 +02:00
|
|
|
userID, err := strconv.Atoi(companyIDStr)
|
2023-10-09 18:23:44 +02:00
|
|
|
if err != nil {
|
2023-10-16 19:43:35 +02:00
|
|
|
log.Printf("GetNotifications Error: Invalid Company ID: %v", err)
|
2023-10-09 18:23:44 +02:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid User ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timestamp, err := strconv.ParseInt(timeStr, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("GetNotifications Error: afterTime parameter ID: %v", err)
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "After time parameter ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
afterTime := time.Unix(timestamp, 0)
|
|
|
|
|
|
2023-10-16 19:43:35 +02:00
|
|
|
log.Printf("This is the Company ID: %v and Time: %v", userID, afterTime)
|
|
|
|
|
notifications, st, err := notification.GetNotificationsForCompanyID(userID, afterTime)
|
2023-10-09 18:23:44 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(st, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Respond with the notifications
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": notifications})
|
|
|
|
|
}
|