Add Calls tab

This commit is contained in:
lowcarbdev
2025-11-15 15:10:12 -07:00
parent 848bf84341
commit 98901957f2
5 changed files with 400 additions and 3 deletions
+43 -1
View File
@@ -1,6 +1,5 @@
package internal
import (
"database/sql"
"fmt"
@@ -613,6 +612,49 @@ func GetCallLogs(userDB *sql.DB, number string, startDate, endDate *time.Time) (
return calls, nil
}
func GetAllCalls(userDB *sql.DB, startDate, endDate *time.Time, limit, offset int) ([]CallLog, error) {
query := `
SELECT id, address, duration, date, type,
COALESCE(presentation, 0), COALESCE(subscription_id, ''), COALESCE(contact_name, '')
FROM messages
WHERE record_type = 3 -- 3 = call
`
args := []interface{}{}
if startDate != nil {
query += " AND date >= ?"
args = append(args, startDate.Unix())
}
if endDate != nil {
query += " AND date <= ?"
args = append(args, endDate.Unix())
}
query += " ORDER BY date ASC LIMIT ? OFFSET ?"
args = append(args, limit, offset)
rows, err := userDB.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
calls := []CallLog{}
for rows.Next() {
var c CallLog
var dateUnix int64
err := rows.Scan(&c.ID, &c.Number, &c.Duration, &dateUnix, &c.Type,
&c.Presentation, &c.SubscriptionID, &c.ContactName)
if err != nil {
return nil, err
}
c.Date = time.Unix(dateUnix, 0)
calls = append(calls, c)
}
return calls, nil
}
func GetActivity(userDB *sql.DB, startDate, endDate *time.Time, limit, offset int) ([]ActivityItem, error) {
return GetActivityByAddress(userDB, "", startDate, endDate, limit, offset)
}
+52 -1
View File
@@ -1,6 +1,5 @@
package internal
import (
"database/sql"
"fmt"
@@ -249,6 +248,58 @@ func HandleActivity(c echo.Context) error {
return c.JSON(http.StatusOK, activities)
}
func HandleCalls(c echo.Context) error {
userDB, err := getUserDB(c)
if err != nil {
slog.Error("Error getting user database", "error", err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": "Failed to get user database",
})
}
var startDate, endDate *time.Time
if startStr := c.QueryParam("start"); startStr != "" {
t, err := time.Parse(time.RFC3339, startStr)
if err == nil {
startDate = &t
}
}
if endStr := c.QueryParam("end"); endStr != "" {
t, err := time.Parse(time.RFC3339, endStr)
if err == nil {
endDate = &t
}
}
// Parse pagination parameters
limit := 50 // default limit
offset := 0 // default offset
if limitStr := c.QueryParam("limit"); limitStr != "" {
if val, err := strconv.Atoi(limitStr); err == nil {
limit = val
}
}
if offsetStr := c.QueryParam("offset"); offsetStr != "" {
if val, err := strconv.Atoi(offsetStr); err == nil {
offset = val
}
}
calls, err := GetAllCalls(userDB, startDate, endDate, limit, offset)
if err != nil {
slog.Error("Error getting calls", "error", err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": "Failed to get calls",
})
}
return c.JSON(http.StatusOK, calls)
}
func HandleDateRange(c echo.Context) error {
userDB, err := getUserDB(c)
if err != nil {