feat: redesigned DB schema with users, work_types, days, events

This commit is contained in:
2026-06-24 00:36:16 +03:30
parent 8616ed0867
commit 2970b7d3fc
6 changed files with 904 additions and 242 deletions

View File

@@ -9,7 +9,25 @@ import (
"worktimeBot/internal/db"
)
func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.Month) ([]byte, error) {
type themeColors struct {
headerFill string
border string
totalFill string
}
var themes = map[string]themeColors{
"ocean": {headerFill: "4472C4", border: "D9D9D9", totalFill: "D9E2F3"},
"beach": {headerFill: "C87D3C", border: "E8D5B7", totalFill: "FFF0E0"},
"rose": {headerFill: "B86C80", border: "F0D0E0", totalFill: "FCE8F0"},
"catppuccin": {headerFill: "B48DED", border: "D9D0E8", totalFill: "F0E8FC"},
}
func GenerateMonthlyReport(store *db.Store, userID int64, timezone, accent string, year int, month time.Month) ([]byte, error) {
theme, ok := themes[accent]
if !ok {
theme = themes["ocean"]
}
f := excelize.NewFile()
defer f.Close()
@@ -23,12 +41,12 @@ func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.M
titleStyle, _ := f.NewStyle(&excelize.Style{
Font: &excelize.Font{Bold: true, Size: 14, Color: "FFFFFF"},
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"4472C4"}},
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{theme.headerFill}},
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
})
headerStyle, _ := f.NewStyle(&excelize.Style{
Font: &excelize.Font{Bold: true, Color: "FFFFFF"},
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"4472C4"}},
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{theme.headerFill}},
Border: []excelize.Border{
{Type: "left", Color: "FFFFFF", Style: 1},
{Type: "right", Color: "FFFFFF", Style: 1},
@@ -39,32 +57,32 @@ func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.M
})
dataStyle, _ := f.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "D9D9D9", Style: 1},
{Type: "right", Color: "D9D9D9", Style: 1},
{Type: "top", Color: "D9D9D9", Style: 1},
{Type: "bottom", Color: "D9D9D9", Style: 1},
{Type: "left", Color: theme.border, Style: 1},
{Type: "right", Color: theme.border, Style: 1},
{Type: "top", Color: theme.border, Style: 1},
{Type: "bottom", Color: theme.border, Style: 1},
},
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
})
totalStyle, _ := f.NewStyle(&excelize.Style{
Font: &excelize.Font{Bold: true, Size: 11},
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"D9E2F3"}},
Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{theme.totalFill}},
Border: []excelize.Border{
{Type: "left", Color: "D9D9D9", Style: 1},
{Type: "right", Color: "D9D9D9", Style: 1},
{Type: "top", Color: "D9D9D9", Style: 1},
{Type: "bottom", Color: "D9D9D9", Style: 2},
{Type: "left", Color: theme.border, Style: 1},
{Type: "right", Color: theme.border, Style: 1},
{Type: "top", Color: theme.border, Style: 1},
{Type: "bottom", Color: theme.border, Style: 2},
},
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
})
monthName := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC).Format("January 2006")
f.SetCellValue(sheet, "A1", fmt.Sprintf("Work Time Report %s", monthName))
f.MergeCell(sheet, "A1", "E1")
f.SetCellStyle(sheet, "A1", "E1", titleStyle)
f.SetCellValue(sheet, "A1", fmt.Sprintf("Work Time Report - %s", monthName))
f.MergeCell(sheet, "A1", "F1")
f.SetCellStyle(sheet, "A1", "F1", titleStyle)
f.SetRowHeight(sheet, 1, 30)
headers := []string{"Date", "Clock In", "Clock Out", "Work", "Break"}
headers := []string{"Date", "Clock In", "Clock Out", "Type", "Work", "Break"}
for i, h := range headers {
cell, _ := excelize.CoordinatesToCellName(i+1, 2)
f.SetCellValue(sheet, cell, h)
@@ -75,33 +93,51 @@ func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.M
f.SetColWidth(sheet, "A", "A", 14)
f.SetColWidth(sheet, "B", "B", 10)
f.SetColWidth(sheet, "C", "C", 10)
f.SetColWidth(sheet, "D", "D", 10)
f.SetColWidth(sheet, "D", "D", 12)
f.SetColWidth(sheet, "E", "E", 10)
f.SetColWidth(sheet, "F", "F", 10)
loc, err := time.LoadLocation(timezone)
if err != nil {
loc = time.UTC
}
row := 3
loc := time.Now().Location()
firstDay := time.Date(year, month, 1, 0, 0, 0, 0, loc)
lastDay := firstDay.AddDate(0, 1, -1)
userDays, err := store.GetUserDaysInRange(userID, firstDay.Format("2006-01-02"), lastDay.Format("2006-01-02"))
if err != nil {
return nil, err
}
dayMap := make(map[string]*db.Day)
for i := range userDays {
dayMap[userDays[i].Date] = &userDays[i]
}
var totalWork, totalBreak int64
for d := firstDay; !d.After(lastDay); d = d.AddDate(0, 0, 1) {
dateStr := d.Format("2006-01-02")
isOff, err := store.IsDayOff(chatID, dateStr)
if err != nil {
return nil, err
day, exists := dayMap[dateStr]
if !exists {
continue
}
if isOff {
if day.IsDayOff {
continue
}
events, err := store.EventsForDay(chatID, dateStr)
events, err := store.EventsForDayByDayID(day.ID)
if err != nil {
return nil, err
}
totals := ComputeDailyTotals(events)
if len(events) == 0 {
continue
}
totals := ComputeDailyTotals(events, day.MinBreakThreshold)
totalWork += totals.TotalSeconds
totalBreak += totals.BreakSeconds
@@ -111,20 +147,25 @@ func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.M
if len(events) > 0 {
cellIn, _ := excelize.CoordinatesToCellName(2, row)
cellOut, _ := excelize.CoordinatesToCellName(3, row)
f.SetCellValue(sheet, cellIn, time.Unix(events[0].OccurredAt, 0).Format("15:04"))
f.SetCellValue(sheet, cellIn, time.Unix(events[0].OccurredAt, 0).In(loc).Format("15:04"))
f.SetCellStyle(sheet, cellIn, cellIn, dataStyle)
if events[len(events)-1].EventType == "out" {
f.SetCellValue(sheet, cellOut, time.Unix(events[len(events)-1].OccurredAt, 0).Format("15:04"))
}
}
if lastEvent := events[len(events)-1]; lastEvent.EventType == "out" {
cellOut, _ := excelize.CoordinatesToCellName(3, row)
f.SetCellValue(sheet, cellOut, time.Unix(lastEvent.OccurredAt, 0).In(loc).Format("15:04"))
f.SetCellStyle(sheet, cellOut, cellOut, dataStyle)
}
cellWork, _ := excelize.CoordinatesToCellName(4, row)
wtLabel := workTypeLabel(store, day, events)
cellType, _ := excelize.CoordinatesToCellName(4, row)
f.SetCellValue(sheet, cellType, wtLabel)
f.SetCellStyle(sheet, cellType, cellType, dataStyle)
cellWork, _ := excelize.CoordinatesToCellName(5, row)
f.SetCellValue(sheet, cellWork, fmtHHMM(totals.TotalSeconds))
f.SetCellStyle(sheet, cellWork, cellWork, dataStyle)
cellBreak, _ := excelize.CoordinatesToCellName(5, row)
cellBreak, _ := excelize.CoordinatesToCellName(6, row)
f.SetCellValue(sheet, cellBreak, fmtHHMM(totals.BreakSeconds))
f.SetCellStyle(sheet, cellBreak, cellBreak, dataStyle)
@@ -135,10 +176,10 @@ func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.M
f.SetCellValue(sheet, fmt.Sprintf("A%d", row), "Total")
f.SetCellStyle(sheet, fmt.Sprintf("A%d", row), fmt.Sprintf("A%d", row), totalStyle)
f.SetCellValue(sheet, fmt.Sprintf("D%d", row), fmtDDHHMM(totalWork))
f.SetCellStyle(sheet, fmt.Sprintf("D%d", row), fmt.Sprintf("D%d", row), totalStyle)
f.SetCellValue(sheet, fmt.Sprintf("E%d", row), fmtDDHHMM(totalBreak))
f.SetCellValue(sheet, fmt.Sprintf("E%d", row), fmtDDHHMM(totalWork))
f.SetCellStyle(sheet, fmt.Sprintf("E%d", row), fmt.Sprintf("E%d", row), totalStyle)
f.SetCellValue(sheet, fmt.Sprintf("F%d", row), fmtDDHHMM(totalBreak))
f.SetCellStyle(sheet, fmt.Sprintf("F%d", row), fmt.Sprintf("F%d", row), totalStyle)
buf, err := f.WriteToBuffer()
if err != nil {
@@ -147,6 +188,31 @@ func GenerateMonthlyReport(store *db.Store, chatID int64, year int, month time.M
return buf.Bytes(), nil
}
func workTypeLabel(store *db.Store, day *db.Day, events []db.Event) string {
wtSeen := make(map[int64]bool)
for _, e := range events {
if e.WorkTypeID != nil {
wtSeen[*e.WorkTypeID] = true
}
}
if len(wtSeen) == 0 {
wt, err := store.GetWorkType(day.CurrentWorkTypeID)
if err == nil {
return wt.Name
}
return ""
}
if len(wtSeen) == 1 {
for id := range wtSeen {
wt, err := store.GetWorkType(id)
if err == nil {
return wt.Name
}
}
}
return "mixed"
}
func fmtHHMM(seconds int64) string {
hours := seconds / 3600
mins := (seconds % 3600) / 60

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log/slog"
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -45,12 +46,9 @@ func (h *Handler) handleActionCallback(chatID int64, msgID int, callbackID strin
h.Bot.Send(edit)
}
// --- Entry points ---
func (h *Handler) HandleMessage(update tgbotapi.Update) {
msg := update.Message
if len(h.AllowedUsers) > 0 && !h.AllowedUsers[msg.Chat.ID] {
slog.Warn("unauthorized message", "chat_id", msg.Chat.ID, "user", msg.From.UserName, "text", msg.Text)
return
}
slog.Info("message", "chat_id", msg.Chat.ID, "text", msg.Text)
@@ -61,14 +59,18 @@ func (h *Handler) HandleMessage(update tgbotapi.Update) {
h.handleActionMsg(msg, h.clockIn)
case "/clockout", "Clock Out":
h.handleActionMsg(msg, h.clockOut)
case "/remote":
h.handleActionMsg(msg, h.toggleRemote)
case "/worktype":
h.handleWorkTypeMsg(msg)
case "/report":
h.handleActionMsg(msg, h.report)
case "/export":
h.handleExport(msg)
case "/dayoff":
h.handleActionMsg(msg, h.dayOff)
case "/reporttoggle":
h.handleActionMsg(msg, h.toggleReport)
case "/setreporttime":
h.handleSetReportTime(msg)
default:
h.sendText(msg.Chat.ID, "Unknown command")
}
@@ -77,20 +79,20 @@ func (h *Handler) HandleMessage(update tgbotapi.Update) {
func (h *Handler) HandleCallback(update tgbotapi.Update) {
cb := update.CallbackQuery
if len(h.AllowedUsers) > 0 && !h.AllowedUsers[cb.Message.Chat.ID] {
slog.Warn("unauthorized callback", "chat_id", cb.Message.Chat.ID, "data", cb.Data)
h.Bot.Request(tgbotapi.NewCallback(cb.ID, "Unauthorized"))
return
}
slog.Info("callback", "chat_id", cb.Message.Chat.ID, "data", cb.Data)
chatID := cb.Message.Chat.ID
msgID := cb.Message.MessageID
switch cb.Data {
case "clockin":
h.handleActionCallback(chatID, msgID, cb.ID, h.clockIn)
case "clockout":
h.handleActionCallback(chatID, msgID, cb.ID, h.clockOut)
case "remote":
h.handleActionCallback(chatID, msgID, cb.ID, h.toggleRemote)
case "worktype":
h.workTypeCallback(chatID, msgID, cb.ID)
case "report":
h.handleActionCallback(chatID, msgID, cb.ID, h.report)
case "export":
@@ -99,46 +101,67 @@ func (h *Handler) HandleCallback(update tgbotapi.Update) {
h.handleActionCallback(chatID, msgID, cb.ID, h.dayOff)
case "back_menu":
h.backToMenu(chatID, msgID, cb.ID)
default:
if len(cb.Data) > 9 && cb.Data[:9] == "worktype_" {
h.selectWorkType(chatID, msgID, cb.ID, cb.Data[9:])
}
}
}
// --- Actions (shared by msg + callback) ---
func (h *Handler) getOrCreateUser(chatID int64) (*db.User, error) {
return h.DB.GetOrCreateUser(chatID)
}
func (h *Handler) getUserToday(chatID int64) (*db.User, *db.Day, error) {
user, err := h.getOrCreateUser(chatID)
if err != nil {
return nil, nil, err
}
loc, err := time.LoadLocation(user.Timezone)
if err != nil {
loc = time.UTC
}
today := time.Now().In(loc).Format("2006-01-02")
day, err := h.DB.GetOrCreateDay(user.ID, today)
if err != nil {
return nil, nil, err
}
return user, day, nil
}
func (h *Handler) clockIn(chatID int64) (string, error) {
today := time.Now().Format("2006-01-02")
isOff, err := h.DB.IsDayOff(chatID, today)
user, day, err := h.getUserToday(chatID)
if err != nil {
return "", err
}
if isOff {
if day.IsDayOff {
return "", errors.New("Today is a day off. Use /dayoff to remove it first.")
}
last, err := h.DB.GetLastEvent(chatID)
last, err := h.DB.GetLastEvent(user.ID)
if err != nil && err != sql.ErrNoRows {
return "", err
}
if last != nil && last.EventType == "in" {
return "", errors.New("Already clocked in")
}
if _, err := h.DB.GetSetting(chatID, "remote_flag"); err != nil {
h.DB.SetSetting(chatID, "remote_flag", "onsite")
}
if err := h.DB.CreateEvent(chatID, "in", time.Now().Unix(), ""); err != nil {
loc, _ := time.LoadLocation(user.Timezone)
now := time.Now().In(loc)
wt := day.CurrentWorkTypeID
if err := h.DB.CreateEvent(user.ID, day.ID, "in", &wt, now.Unix(), ""); err != nil {
return "", err
}
return "Clocked in at " + time.Now().Format("15:04"), nil
return fmt.Sprintf("Clocked in at %s", now.Format("15:04")), nil
}
func (h *Handler) clockOut(chatID int64) (string, error) {
today := time.Now().Format("2006-01-02")
isOff, err := h.DB.IsDayOff(chatID, today)
user, day, err := h.getUserToday(chatID)
if err != nil {
return "", err
}
if isOff {
if day.IsDayOff {
return "", errors.New("Today is a day off. Use /dayoff to remove it first.")
}
last, err := h.DB.GetLastEvent(chatID)
last, err := h.DB.GetLastEvent(user.ID)
if err != nil {
return "", err
}
@@ -148,58 +171,100 @@ func (h *Handler) clockOut(chatID int64) (string, error) {
if last.EventType == "out" {
return "", errors.New("Already clocked out")
}
if err := h.DB.CreateEvent(chatID, "out", time.Now().Unix(), ""); err != nil {
loc, _ := time.LoadLocation(user.Timezone)
now := time.Now().In(loc)
if err := h.DB.CreateEvent(user.ID, day.ID, "out", nil, now.Unix(), ""); err != nil {
return "", err
}
return "Clocked out at " + time.Now().Format("15:04"), nil
}
func (h *Handler) toggleRemote(chatID int64) (string, error) {
current, err := h.DB.GetSetting(chatID, "remote_flag")
if err != nil {
current = "onsite"
}
var newMode, text string
if current == "remote" {
newMode = "onsite"
text = "Onsite mode enabled"
} else {
newMode = "remote"
text = "Remote mode enabled"
}
if err := h.DB.SetSetting(chatID, "remote_flag", newMode); err != nil {
return "", err
}
return text, nil
return fmt.Sprintf("Clocked out at %s", now.Format("15:04")), nil
}
func (h *Handler) dayOff(chatID int64) (string, error) {
today := time.Now().Format("2006-01-02")
isOff, err := h.DB.IsDayOff(chatID, today)
user, day, err := h.getUserToday(chatID)
if err != nil {
return "", err
}
if isOff {
if err := h.DB.RemoveDayOff(chatID, today); err != nil {
if day.IsDayOff {
if err := h.DB.RemoveDayOff(user.ID, day.Date); err != nil {
return "", err
}
return "Day off removed", nil
}
if err := h.DB.SetDayOff(chatID, today, ""); err != nil {
if err := h.DB.SetDayOff(user.ID, day.Date, ""); err != nil {
return "", err
}
return "Today marked as day off", nil
}
func (h *Handler) report(chatID int64) (string, error) {
return h.buildDailyReport(chatID, time.Now()), nil
user, day, err := h.getUserToday(chatID)
if err != nil {
return "", err
}
events, err := h.DB.EventsForDayByDayID(day.ID)
if err != nil {
return "", err
}
return h.buildDailyReport(user, day, events), nil
}
// --- /start ---
func (h *Handler) toggleReport(chatID int64) (string, error) {
user, err := h.getOrCreateUser(chatID)
if err != nil {
return "", err
}
user.ReportEnabled = !user.ReportEnabled
if err := h.DB.UpdateUser(user); err != nil {
return "", err
}
if user.ReportEnabled {
return "Daily report enabled", nil
}
return "Daily report disabled", nil
}
func (h *Handler) handleSetReportTime(msg *tgbotapi.Message) {
arg := msg.CommandArguments()
if arg == "" {
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
h.sendText(msg.Chat.ID, "Error loading profile")
return
}
h.sendText(msg.Chat.ID, fmt.Sprintf("Current report time: %s\nUsage: /setreporttime HH:MM (24-hour)", user.ReportTime))
return
}
if len(arg) != 5 || arg[2] != ':' {
h.sendText(msg.Chat.ID, "Invalid format. Usage: /setreporttime HH:MM (24-hour)")
return
}
hours, err1 := strconv.Atoi(arg[:2])
mins, err2 := strconv.Atoi(arg[3:])
if err1 != nil || err2 != nil || hours < 0 || hours > 23 || mins < 0 || mins > 59 {
h.sendText(msg.Chat.ID, "Invalid time. Usage: /setreporttime HH:MM (24-hour)")
return
}
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
h.sendText(msg.Chat.ID, "Error updating report time")
return
}
user.ReportTime = arg
if err := h.DB.UpdateUser(user); err != nil {
h.sendText(msg.Chat.ID, "Error updating report time")
return
}
h.sendText(msg.Chat.ID, fmt.Sprintf("Report time set to %s", arg))
}
func (h *Handler) handleStart(msg *tgbotapi.Message) {
h.DB.SetSetting(0, "report_chat_id", fmt.Sprintf("%d", msg.Chat.ID))
slog.Info("start", "chat_id", msg.Chat.ID)
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
slog.Error("start: create user", "error", err)
h.sendText(msg.Chat.ID, "Error setting up your profile")
return
}
slog.Info("start", "user_id", user.ID, "chat_id", msg.Chat.ID)
reply := tgbotapi.NewMessage(msg.Chat.ID, "We hope you have a happy day working.")
reply.ReplyMarkup = quickKeyboard()
h.Bot.Send(reply)
@@ -208,18 +273,98 @@ func (h *Handler) handleStart(msg *tgbotapi.Message) {
h.Bot.Send(menu)
}
// --- Export (different flow — sends a file) ---
func (h *Handler) handleWorkTypeMsg(msg *tgbotapi.Message) {
user, day, err := h.getUserToday(msg.Chat.ID)
if err != nil {
h.sendText(msg.Chat.ID, "Error loading work types")
return
}
text, kb := h.buildWorkTypeKeyboard(user, day)
reply := tgbotapi.NewMessage(msg.Chat.ID, text)
reply.ReplyMarkup = &kb
h.Bot.Send(reply)
}
func (h *Handler) workTypeCallback(chatID int64, msgID int, callbackID string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
user, day, err := h.getUserToday(chatID)
if err != nil {
edit := tgbotapi.NewEditMessageText(chatID, msgID, "Error loading work types")
kb := backKeyboard()
edit.ReplyMarkup = &kb
h.Bot.Send(edit)
return
}
text, kb := h.buildWorkTypeKeyboard(user, day)
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
edit.ReplyMarkup = &kb
h.Bot.Send(edit)
}
func (h *Handler) buildWorkTypeKeyboard(user *db.User, day *db.Day) (string, tgbotapi.InlineKeyboardMarkup) {
wts, err := h.DB.GetWorkTypes()
if err != nil || len(wts) == 0 {
return "No work types available.", backKeyboard()
}
text := "Select work type:"
var rows [][]tgbotapi.InlineKeyboardButton
for _, wt := range wts {
label := wt.Name
if wt.ID == day.CurrentWorkTypeID {
label = "> " + wt.Name
}
callbackData := fmt.Sprintf("worktype_%d", wt.ID)
row := tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(label, callbackData),
)
rows = append(rows, row)
}
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Back to Menu", "back_menu"),
))
kb := tgbotapi.NewInlineKeyboardMarkup(rows...)
return text, kb
}
func (h *Handler) selectWorkType(chatID int64, msgID int, callbackID, idStr string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
_, day, err := h.getUserToday(chatID)
if err != nil {
return
}
var wtID int64
if _, err := fmt.Sscanf(idStr, "%d", &wtID); err != nil {
return
}
wt, err := h.DB.GetWorkType(wtID)
if err != nil {
return
}
day.CurrentWorkTypeID = wtID
if err := h.DB.UpdateDay(day); err != nil {
return
}
text := fmt.Sprintf("Work type set to: %s", wt.Name)
edit := tgbotapi.NewEditMessageText(chatID, msgID, text)
kb := backKeyboard()
edit.ReplyMarkup = &kb
h.Bot.Send(edit)
}
func (h *Handler) handleExport(msg *tgbotapi.Message) {
user, err := h.getOrCreateUser(msg.Chat.ID)
if err != nil {
h.sendText(msg.Chat.ID, "Error loading profile")
return
}
now := time.Now()
slog.Info("export", "chat_id", msg.Chat.ID, "year", now.Year(), "month", now.Month())
data, err := GenerateMonthlyReport(h.DB, msg.Chat.ID, now.Year(), now.Month())
slog.Info("export", "user_id", user.ID, "year", now.Year(), "month", now.Month())
data, err := GenerateMonthlyReport(h.DB, user.ID, user.Timezone, user.ExportAccent, now.Year(), now.Month())
if err != nil {
slog.Error("generate report", "error", err)
h.sendText(msg.Chat.ID, "Error generating report")
return
}
slog.Info("report generated", "bytes", len(data))
doc := tgbotapi.NewDocument(msg.Chat.ID, tgbotapi.FileBytes{
Name: fmt.Sprintf("worktime_%s.xlsx", now.Format("2006_01")),
Bytes: data,
@@ -232,9 +377,17 @@ func (h *Handler) handleExport(msg *tgbotapi.Message) {
func (h *Handler) exportCallback(chatID int64, msgID int, callbackID string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
user, err := h.getOrCreateUser(chatID)
if err != nil {
edit := tgbotapi.NewEditMessageText(chatID, msgID, "Error loading profile")
kb := backKeyboard()
edit.ReplyMarkup = &kb
h.Bot.Send(edit)
return
}
now := time.Now()
slog.Info("export", "chat_id", chatID, "year", now.Year(), "month", now.Month())
data, err := GenerateMonthlyReport(h.DB, chatID, now.Year(), now.Month())
slog.Info("export", "user_id", user.ID, "year", now.Year(), "month", now.Month())
data, err := GenerateMonthlyReport(h.DB, user.ID, user.Timezone, user.ExportAccent, now.Year(), now.Month())
if err != nil {
slog.Error("generate report", "error", err)
edit := tgbotapi.NewEditMessageText(chatID, msgID, "Error generating report")
@@ -257,8 +410,6 @@ func (h *Handler) exportCallback(chatID int64, msgID int, callbackID string) {
}
}
// --- Back to menu ---
func (h *Handler) backToMenu(chatID int64, msgID int, callbackID string) {
defer h.Bot.Request(tgbotapi.NewCallback(callbackID, ""))
edit := tgbotapi.NewEditMessageText(chatID, msgID, "Welcome. Choose an action:")
@@ -267,52 +418,39 @@ func (h *Handler) backToMenu(chatID int64, msgID int, callbackID string) {
h.Bot.Send(edit)
}
// --- Daily report builder ---
func (h *Handler) buildDailyReport(chatID int64, now time.Time) string {
dateStr := now.Format("2006-01-02")
isOff, _ := h.DB.IsDayOff(chatID, dateStr)
events, err := h.DB.EventsForDay(chatID, dateStr)
func (h *Handler) buildDailyReport(user *db.User, day *db.Day, events []db.Event) string {
loc, err := time.LoadLocation(user.Timezone)
if err != nil {
return "Error fetching events."
loc = time.UTC
}
if len(events) == 0 {
if isOff {
return fmt.Sprintf("%s Day Off", dateStr)
if day.IsDayOff {
return fmt.Sprintf("%s - Day Off", day.Date)
}
return fmt.Sprintf("%s No events recorded.", dateStr)
return fmt.Sprintf("%s - No events recorded.", day.Date)
}
mode := "Onsite"
current, err := h.DB.GetSetting(chatID, "remote_flag")
if err == nil && current == "remote" {
mode = "Remote"
}
totals := ComputeDailyTotals(events)
if totals.PairCount == 0 {
t := time.Unix(events[0].OccurredAt, 0).Format("15:04")
return fmt.Sprintf("%s\nMode: %s\nClocked in at %s\n\nStill working — no clock-out yet.", dateStr, mode, t)
}
lines := fmt.Sprintf("Report for %s\nMode: %s", dateStr, mode)
if isOff {
totals := ComputeDailyTotals(events, day.MinBreakThreshold)
lines := fmt.Sprintf("Report for %s", day.Date)
if day.IsDayOff {
lines += "\nDay Off: Yes"
}
lines += "\n\n"
for _, e := range events {
t := time.Unix(e.OccurredAt, 0).Format("15:04")
t := time.Unix(e.OccurredAt, 0).In(loc).Format("15:04")
label := "IN"
action := "Clock In"
if e.EventType == "out" {
label = "OUT"
action = "Clock Out"
}
lines += fmt.Sprintf("%s %s — %s\n", label, t, action)
wt := ""
if e.WorkTypeID != nil {
if wtObj, err := h.DB.GetWorkType(*e.WorkTypeID); err == nil {
wt = " [" + wtObj.Name + "]"
}
}
lines += fmt.Sprintf("%s %s%s\n", label, t, wt)
}
workStr := formatDuration(totals.TotalSeconds)
@@ -322,17 +460,36 @@ func (h *Handler) buildDailyReport(chatID int64, now time.Time) string {
return lines
}
// --- Scheduled report ---
func (h *Handler) SendDailyReport(userID int64, chatID int64) {
user, err := h.DB.GetUserByChatID(chatID)
if err != nil {
slog.Error("daily report: user not found", "user_id", userID)
return
}
loc, err := time.LoadLocation(user.Timezone)
if err != nil {
loc = time.UTC
}
now := time.Now().In(loc)
today := now.Format("2006-01-02")
func (h *Handler) SendDailyReport(chatID int64) {
text := h.buildDailyReport(chatID, time.Now())
day, err := h.DB.GetOrCreateDay(user.ID, today)
if err != nil {
slog.Error("daily report: get day", "error", err)
return
}
events, err := h.DB.EventsForDayByDayID(day.ID)
if err != nil {
slog.Error("daily report: get events", "error", err)
return
}
text := h.buildDailyReport(user, day, events)
reply := tgbotapi.NewMessage(chatID, text)
reply.ReplyMarkup = quickKeyboard()
h.Bot.Send(reply)
h.DB.MarkReportSent(user.ID, today)
}
// --- Keyboards ---
func quickKeyboard() tgbotapi.ReplyKeyboardMarkup {
return tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
@@ -349,12 +506,12 @@ func mainKeyboard() tgbotapi.InlineKeyboardMarkup {
tgbotapi.NewInlineKeyboardButtonData("Clock Out", "clockout"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Report", "report"),
tgbotapi.NewInlineKeyboardButtonData("Export", "export"),
tgbotapi.NewInlineKeyboardButtonData("Work Type", "worktype"),
tgbotapi.NewInlineKeyboardButtonData("Day Off", "dayoff"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Toggle Remote/Onsite", "remote"),
tgbotapi.NewInlineKeyboardButtonData("Day Off", "dayoff"),
tgbotapi.NewInlineKeyboardButtonData("Report", "report"),
tgbotapi.NewInlineKeyboardButtonData("Export", "export"),
),
)
}
@@ -367,8 +524,6 @@ func backKeyboard() tgbotapi.InlineKeyboardMarkup {
)
}
// --- Helpers ---
func formatDuration(seconds int64) string {
hours := seconds / 3600
mins := (seconds % 3600) / 60

View File

@@ -20,7 +20,7 @@ type DailyTotals struct {
PairCount int
}
func ComputeDailyTotals(events []db.Event) DailyTotals {
func ComputeDailyTotals(events []db.Event, minBreakThreshold int64) DailyTotals {
if len(events) == 0 {
return DailyTotals{}
}
@@ -43,9 +43,13 @@ func ComputeDailyTotals(events []db.Event) DailyTotals {
workStart = e.OccurredAt
state = StateWorking
case StateOnBreak:
breakTotal += e.OccurredAt - breakStart
breakStart = 0
workStart = e.OccurredAt
breakDuration := e.OccurredAt - breakStart
if minBreakThreshold > 0 && breakDuration <= minBreakThreshold {
workStart = breakStart
} else {
breakTotal += breakDuration
workStart = e.OccurredAt
}
state = StateWorking
}
case "out":