fixed #63 all scheduled tasks are now under route /auto and will require api key in the future
Some checks failed
Tests / Run Go Tests (push) Failing after 1m32s
Some checks failed
Tests / Run Go Tests (push) Failing after 1m32s
This commit is contained in:
82
Backend/endpoints/auto-kurzarbeit.go
Normal file
82
Backend/endpoints/auto-kurzarbeit.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"arbeitszeitmessung/helper/paramParser"
|
||||
"arbeitszeitmessung/models"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func KurzarbeitFillHandler(w http.ResponseWriter, r *http.Request) {
|
||||
helper.SetCors(w)
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
fillKurzarbeit(r, w)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func fillKurzarbeit(r *http.Request, w http.ResponseWriter) {
|
||||
bookingTypeKurzarbeit, err := getKurzarbeitBookingType()
|
||||
if err != nil {
|
||||
slog.Info("Error getting BookingType Kurzarbeit %v\n", slog.Any("Error", err))
|
||||
}
|
||||
users, err := models.GetAllUsers()
|
||||
if err != nil {
|
||||
slog.Info("Error getting user list %v\n", slog.Any("Error", err))
|
||||
}
|
||||
|
||||
pp := paramParser.New(r.URL.Query())
|
||||
startDate := pp.ParseTimestampFallback("date", time.DateOnly, time.Now())
|
||||
|
||||
var kurzarbeitAdded int
|
||||
|
||||
for _, user := range users {
|
||||
days := models.GetDays(user, startDate, startDate.AddDate(0, 0, 1), false)
|
||||
if len(days) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
day := days[len(days)-1]
|
||||
if !day.IsKurzArbeit() || !day.IsWorkDay() {
|
||||
continue
|
||||
}
|
||||
if day.GetWorktimeReal(user, models.WorktimeBaseDay) >= day.GetWorktimeVirtual(user, models.WorktimeBaseDay) {
|
||||
continue
|
||||
}
|
||||
|
||||
worktimeKurzarbeit := day.GetWorktimeVirtual(user, models.WorktimeBaseDay) - day.GetWorktimeReal(user, models.WorktimeBaseDay)
|
||||
|
||||
if wDay, ok := day.(*models.WorkDay); !ok || len(wDay.Bookings) == 0 {
|
||||
continue
|
||||
}
|
||||
workday, _ := day.(*models.WorkDay)
|
||||
|
||||
lastBookingTime := workday.Bookings[len(workday.Bookings)-1].Timestamp
|
||||
kurzarbeitBegin := (*models.Booking).New(nil, user.CardUID, 0, 1, bookingTypeKurzarbeit.Id)
|
||||
kurzarbeitEnd := (*models.Booking).New(nil, user.CardUID, 0, 2, bookingTypeKurzarbeit.Id)
|
||||
kurzarbeitBegin.Timestamp = lastBookingTime.Add(time.Minute)
|
||||
kurzarbeitEnd.Timestamp = lastBookingTime.Add(worktimeKurzarbeit)
|
||||
|
||||
kurzarbeitBegin.InsertWithTimestamp()
|
||||
kurzarbeitEnd.InsertWithTimestamp()
|
||||
kurzarbeitAdded += 1
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(kurzarbeitAdded)
|
||||
}
|
||||
|
||||
func getKurzarbeitBookingType() (models.BookingType, error) {
|
||||
for _, bookingType := range models.GetBookingTypesCached() {
|
||||
if bookingType.Name == "Kurzarbeit" {
|
||||
return bookingType, nil
|
||||
}
|
||||
}
|
||||
return models.BookingType{}, errors.New("No Booking Type found")
|
||||
}
|
||||
Reference in New Issue
Block a user