2024-09-01 08:38:09 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-09-01 08:38:09 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-10-14 14:23:51 +00:00
|
|
|
"strconv"
|
2024-09-01 08:38:09 +00:00
|
|
|
)
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func listCheckInRecord(c *fiber.Ctx) error {
|
2024-09-01 08:38:09 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-09-01 08:38:09 +00:00
|
|
|
|
|
|
|
var count int64
|
|
|
|
if err := database.C.
|
2024-11-27 13:57:10 +00:00
|
|
|
Model(&models.CheckInRecord{}).
|
2024-09-01 08:38:09 +00:00
|
|
|
Where("account_id = ?", user.ID).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
var records []models.CheckInRecord
|
2024-09-01 08:38:09 +00:00
|
|
|
if err := database.C.
|
|
|
|
Where("account_id = ?", user.ID).
|
|
|
|
Limit(take).Offset(offset).
|
|
|
|
Order("created_at DESC").
|
|
|
|
Find(&records).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": records,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func listOtherUserCheckInRecord(c *fiber.Ctx) error {
|
2024-09-21 14:50:01 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
alias := c.Params("alias")
|
|
|
|
|
|
|
|
var account models.Account
|
|
|
|
if err := database.C.
|
|
|
|
Where(&models.Account{Name: alias}).
|
|
|
|
First(&account).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
if err := database.C.
|
2024-11-27 13:57:10 +00:00
|
|
|
Model(&models.CheckInRecord{}).
|
2024-09-21 14:50:01 +00:00
|
|
|
Where("account_id = ?", account.ID).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
var records []models.CheckInRecord
|
2024-09-21 14:50:01 +00:00
|
|
|
if err := database.C.
|
|
|
|
Where("account_id = ?", account.ID).
|
|
|
|
Limit(take).Offset(offset).
|
|
|
|
Order("created_at DESC").
|
|
|
|
Find(&records).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": records,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func getTodayCheckIn(c *fiber.Ctx) error {
|
2024-09-02 12:07:19 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-09-02 12:07:19 +00:00
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
if record, err := services.GetTodayCheckIn(user); err != nil {
|
2024-09-02 12:07:19 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
func doCheckIn(c *fiber.Ctx) error {
|
2024-09-01 08:38:09 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-09-01 08:38:09 +00:00
|
|
|
|
2024-11-27 13:57:10 +00:00
|
|
|
if record, err := services.CheckIn(user); err != nil {
|
2024-09-01 08:38:09 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-11-29 17:01:54 +00:00
|
|
|
services.AddEvent(user.ID, "checkIn", strconv.Itoa(int(record.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-09-01 08:38:09 +00:00
|
|
|
return c.JSON(record)
|
|
|
|
}
|
|
|
|
}
|