Compare commits
8 Commits
db98eecd87
...
0.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| dafcd95428 | |||
| 23cb312644 | |||
| 4dbd1ced0d | |||
| 27bac5ff84 | |||
| 54104d5a0e | |||
| 0ddca8b9ce | |||
| d68a19790e | |||
| 478fd53d4f |
@@ -4,28 +4,114 @@ import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"arbeitszeitmessung/models"
|
||||
"arbeitszeitmessung/templates"
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TeamHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var user models.User
|
||||
var err error
|
||||
if helper.GetEnv("GO_ENV", "production") == "debug" {
|
||||
user, err = (*models.User).GetByPersonalNummer(nil, 123)
|
||||
} else {
|
||||
if !Session.Exists(r.Context(), "user") {
|
||||
log.Println("No user in session storage!")
|
||||
http.Error(w, "Not logged in!", http.StatusForbidden)
|
||||
helper.RequiresLogin(Session, w, r)
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
submitReport(w, r)
|
||||
break
|
||||
case http.MethodGet:
|
||||
showWeeks(w, r)
|
||||
break
|
||||
default:
|
||||
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
|
||||
break
|
||||
}
|
||||
// user, err := (*models.User).GetUserFromSession(nil, Session, r.Context())
|
||||
// if err != nil {
|
||||
// log.Println("No user found with the given personal number!")
|
||||
// http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||
// return
|
||||
// }
|
||||
// var userWorkDays []models.WorkDay
|
||||
// userWorkDays = (*models.WorkDay).GetWorkDays(nil, user.CardUID, time.Date(2025, time.February, 24, 0, 0, 0, 0, time.Local), time.Date(2025, time.February, 24+7, 0, 0, 0, 0, time.Local))
|
||||
// log.Println("User:", user)
|
||||
// teamMembers, err := user.GetTeamMembers()
|
||||
// getWeeksTillNow(time.Now().AddDate(0, 0, -14))
|
||||
// templates.TeamPage(teamMembers, userWorkDays).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func submitReport(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Println("Error parsing form", err)
|
||||
return
|
||||
}
|
||||
user, err = (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
|
||||
userPN, _ := strconv.Atoi(r.FormValue("user"))
|
||||
_weekTs := r.FormValue("week")
|
||||
weekTs, err := time.Parse(time.DateOnly, _weekTs)
|
||||
user, err := (*models.User).GetByPersonalNummer(nil, userPN)
|
||||
workWeek := (*models.WorkWeek).GetWeek(nil, user, weekTs, false)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Could not get user!")
|
||||
return
|
||||
}
|
||||
|
||||
switch r.FormValue("method") {
|
||||
case "send":
|
||||
err = workWeek.Send()
|
||||
break
|
||||
case "accept":
|
||||
err = workWeek.Accept()
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
if errors.Is(err, models.ErrRunningWeek) {
|
||||
showWeeks(w, r.WithContext(context.WithValue(r.Context(), "error", true)))
|
||||
}
|
||||
showWeeks(w, r)
|
||||
}
|
||||
|
||||
func showWeeks(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := (*models.User).GetUserFromSession(nil, Session, r.Context())
|
||||
if err != nil {
|
||||
log.Println("No user found with the given personal number!")
|
||||
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
var workWeeks []models.WorkWeek
|
||||
teamMembers, err := user.GetTeamMembers()
|
||||
templates.TeamPage(teamMembers).Render(r.Context(), w)
|
||||
for _, member := range teamMembers {
|
||||
weeks := (*models.WorkWeek).GetSendWeeks(nil, member)
|
||||
workWeeks = append(workWeeks, weeks...)
|
||||
}
|
||||
lastSub := user.GetLastSubmission()
|
||||
log.Println(lastSub)
|
||||
userWeek := (*models.WorkWeek).GetWeek(nil, user, lastSub, true)
|
||||
// isRunningWeek := time.Since(lastSub) < 24*5*time.Hour //the last submission is this week and cannot be send yet
|
||||
templates.TeamPage(workWeeks, userWeek).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func getWeeksTillNow(lastWeek time.Time) []time.Time {
|
||||
var weeks []time.Time
|
||||
if lastWeek.After(time.Now()) {
|
||||
log.Println("Timestamp is after today, no weeks till now!")
|
||||
return weeks
|
||||
}
|
||||
if lastWeek.Weekday() != time.Monday {
|
||||
if lastWeek.Weekday() == time.Sunday {
|
||||
lastWeek = lastWeek.AddDate(0, 0, -6)
|
||||
} else {
|
||||
lastWeek = lastWeek.AddDate(0, 0, -int(lastWeek.Weekday()-1))
|
||||
}
|
||||
}
|
||||
if time.Since(lastWeek) < 24*5*time.Hour {
|
||||
log.Println("Timestamp in running week, cannot split!")
|
||||
}
|
||||
|
||||
for t := lastWeek; t.Before(time.Now()); t = t.Add(7 * 24 * time.Hour) {
|
||||
weeks = append(weeks, t)
|
||||
}
|
||||
log.Println(weeks)
|
||||
return weeks
|
||||
}
|
||||
|
||||
@@ -14,17 +14,22 @@ import (
|
||||
|
||||
// Frontend relevant backend functionality -> not used by the arduino devices
|
||||
func TimeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
helper.RequiresLogin(Session, w, r)
|
||||
helper.SetCors(w)
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
case http.MethodGet:
|
||||
getBookings(w, r)
|
||||
case "POST":
|
||||
break
|
||||
case http.MethodPost:
|
||||
updateBooking(w, r)
|
||||
case "OPTIONS":
|
||||
break
|
||||
case http.MethodOptions:
|
||||
// just support options header for non GET Requests from SWAGGER
|
||||
w.WriteHeader(http.StatusOK)
|
||||
break
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,21 +47,10 @@ func parseTimestamp(r *http.Request, get_key string, fallback string) (time.Time
|
||||
|
||||
// Returns bookings from DB with similar card uid -> checks for card uid in http query params
|
||||
func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
var user models.User
|
||||
var err error
|
||||
if helper.GetEnv("GO_ENV", "production") == "debug" {
|
||||
user, err = (*models.User).GetByPersonalNummer(nil, 123)
|
||||
} else {
|
||||
if !Session.Exists(r.Context(), "user") {
|
||||
log.Println("No user in session storage!")
|
||||
http.Error(w, "Not logged in!", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
user, err = (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
|
||||
}
|
||||
user, err := (*models.User).GetUserFromSession(nil, Session, r.Context())
|
||||
if err != nil {
|
||||
log.Println("No user found with the given personal number!")
|
||||
http.Error(w, "No user found", http.StatusNotFound)
|
||||
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -81,6 +75,12 @@ func getBookings(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Accept") == "application/json" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(bookings)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), "user", user)
|
||||
templates.TimePage(bookings).Render(ctx, w)
|
||||
}
|
||||
|
||||
@@ -16,13 +16,17 @@ func TimeCreateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodPut:
|
||||
createBooking(w, r)
|
||||
break
|
||||
case http.MethodGet:
|
||||
createBooking(w, r)
|
||||
break
|
||||
case http.MethodOptions:
|
||||
// just support options header for non GET Requests from SWAGGER
|
||||
w.WriteHeader(http.StatusOK)
|
||||
break
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,5 +62,13 @@ func checkPassword(r *http.Request) bool {
|
||||
authToken := helper.GetEnv("API_TOKEN", "dont_access")
|
||||
authHeaders := r.Header.Get("Authorization")
|
||||
_authStart := len("Bearer ")
|
||||
if len(authHeaders) <= _authStart {
|
||||
authHeaders = r.URL.Query().Get("api_key")
|
||||
_authStart = 0
|
||||
if len(authHeaders) <= _authStart {
|
||||
return false
|
||||
}
|
||||
}
|
||||
log.Println(authHeaders)
|
||||
return authToken == authHeaders[_authStart:]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"arbeitszeitmessung/models"
|
||||
"arbeitszeitmessung/templates"
|
||||
"log"
|
||||
@@ -18,27 +19,26 @@ func CreateSessionManager(lifetime time.Duration) *scs.SessionManager {
|
||||
Session.Lifetime = lifetime
|
||||
return Session
|
||||
}
|
||||
|
||||
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
showLoginForm(w, r, false)
|
||||
showLoginPage(w, r, false)
|
||||
break
|
||||
case http.MethodPost:
|
||||
loginUser(w, r)
|
||||
break
|
||||
default:
|
||||
showLoginForm(w, r, false)
|
||||
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func UserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !Session.Exists(r.Context(), "user") {
|
||||
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||
}
|
||||
helper.RequiresLogin(Session, w, r)
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
showPWForm(w, r, 0)
|
||||
showUserPage(w, r, 0)
|
||||
break
|
||||
case http.MethodPost:
|
||||
changePassword(w, r)
|
||||
@@ -49,7 +49,7 @@ func UserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func showLoginForm(w http.ResponseWriter, r *http.Request, failed bool) {
|
||||
func showLoginPage(w http.ResponseWriter, r *http.Request, failed bool) {
|
||||
templates.LoginPage(failed).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
@@ -85,10 +85,10 @@ func loginUser(w http.ResponseWriter, r *http.Request) {
|
||||
Session.Put(r.Context(), "user", user.PersonalNummer)
|
||||
http.Redirect(w, r, "/time", http.StatusSeeOther) //with this browser always uses GET
|
||||
} else {
|
||||
showLoginForm(w, r, true)
|
||||
showLoginPage(w, r, true)
|
||||
return
|
||||
}
|
||||
showLoginForm(w, r, false)
|
||||
showLoginPage(w, r, false)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -103,26 +103,26 @@ func changePassword(w http.ResponseWriter, r *http.Request) {
|
||||
password := r.FormValue("password")
|
||||
newPassword := r.FormValue("new_password")
|
||||
if password == "" || newPassword == "" || newPassword != r.FormValue("new_password_repeat") {
|
||||
showPWForm(w, r, http.StatusBadRequest)
|
||||
showUserPage(w, r, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user, err := (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
|
||||
if err != nil {
|
||||
log.Println("Error getting user!", err)
|
||||
showPWForm(w, r, http.StatusBadRequest)
|
||||
showUserPage(w, r, http.StatusBadRequest)
|
||||
}
|
||||
auth, err := user.ChangePass(password, newPassword)
|
||||
if err != nil {
|
||||
log.Println("Error when changing password!", err)
|
||||
}
|
||||
if auth {
|
||||
showPWForm(w, r, http.StatusOK)
|
||||
showUserPage(w, r, http.StatusOK)
|
||||
return
|
||||
}
|
||||
showPWForm(w, r, http.StatusUnauthorized)
|
||||
showUserPage(w, r, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
func showPWForm(w http.ResponseWriter, r *http.Request, status int) {
|
||||
func showUserPage(w http.ResponseWriter, r *http.Request, status int) {
|
||||
templates.UserPage(status).Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3,15 +3,28 @@ package helper
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
)
|
||||
|
||||
// setting cors, important for later frontend use
|
||||
//
|
||||
// in DEBUG == "true" everything is set to "*" so that no cors errors will be happen
|
||||
func SetCors(w http.ResponseWriter) {
|
||||
if os.Getenv("DEBUG") == "true" {
|
||||
if os.Getenv("NO_CORS") == "true" {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||
// log.Println("Setting cors to *")
|
||||
}
|
||||
}
|
||||
|
||||
func RequiresLogin(session *scs.SessionManager, w http.ResponseWriter, r *http.Request) {
|
||||
if GetEnv("GO_ENV", "production") == "debug" {
|
||||
return
|
||||
}
|
||||
if session.Exists(r.Context(), "user") {
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ import (
|
||||
"arbeitszeitmessung/endpoints"
|
||||
"arbeitszeitmessung/helper"
|
||||
"arbeitszeitmessung/models"
|
||||
"arbeitszeitmessung/templates"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
@@ -40,6 +42,7 @@ func main() {
|
||||
server.HandleFunc("/user/login", endpoints.LoginHandler)
|
||||
server.HandleFunc("/user", endpoints.UserHandler)
|
||||
server.HandleFunc("/team", endpoints.TeamHandler)
|
||||
server.Handle("/", templ.Handler(templates.NavPage()))
|
||||
server.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
|
||||
serverSessionMiddleware := endpoints.Session.LoadAndSave(server)
|
||||
|
||||
@@ -119,7 +119,7 @@ func (b *Booking) GetBookingsByCardID(card_uid string, tsFrom time.Time, tsTo ti
|
||||
func (b *Booking) GetBookingsGrouped(card_uid string, tsFrom time.Time, tsTo time.Time) ([]WorkDay, error) {
|
||||
var grouped = make(map[string][]Booking)
|
||||
bookings, err := b.GetBookingsByCardID(card_uid, tsFrom, tsTo)
|
||||
if (err != nil){
|
||||
if err != nil {
|
||||
log.Println("Failed to get bookings", err)
|
||||
return []WorkDay{}, nil
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func (b *Booking) Update(nb Booking) {
|
||||
if b.GeraetID != nb.GeraetID && nb.GeraetID != 0 {
|
||||
b.GeraetID = nb.GeraetID
|
||||
}
|
||||
if(b.Timestamp != nb.Timestamp){
|
||||
if b.Timestamp != nb.Timestamp {
|
||||
b.Timestamp = nb.Timestamp
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,7 @@ func checkLastBooking(b Booking) bool {
|
||||
log.Println("Error checking last booking: ", err)
|
||||
return false
|
||||
}
|
||||
if int16(check_in_out) == b.CheckInOut {
|
||||
if int16(check_in_out)%2 == b.CheckInOut%2 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -211,17 +211,17 @@ func checkLastBooking(b Booking) bool {
|
||||
|
||||
func (b *Booking) UpdateTime(newTime time.Time) {
|
||||
hour, minute, _ := newTime.Clock()
|
||||
if(hour == b.Timestamp.Hour() && minute == b.Timestamp.Minute()){
|
||||
if hour == b.Timestamp.Hour() && minute == b.Timestamp.Minute() {
|
||||
return
|
||||
}
|
||||
// TODO: add check for time overlap
|
||||
|
||||
var newBooking Booking
|
||||
newBooking.Timestamp = time.Date(b.Timestamp.Year(), b.Timestamp.Month(), b.Timestamp.Day(), hour, minute, 0, 0, time.Local)
|
||||
if(b.CheckInOut < 3){
|
||||
if b.CheckInOut < 3 {
|
||||
newBooking.CheckInOut = b.CheckInOut + 2
|
||||
}
|
||||
if(b.CheckInOut == 255){
|
||||
if b.CheckInOut == 255 {
|
||||
newBooking.CheckInOut = 4
|
||||
}
|
||||
b.Update(newBooking)
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/helper"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -14,6 +20,25 @@ type User struct {
|
||||
Arbeitszeit float32 `json:"arbeitszeit"`
|
||||
}
|
||||
|
||||
func (u *User) GetUserFromSession(Session *scs.SessionManager, ctx context.Context) (User, error) {
|
||||
var user User
|
||||
var err error
|
||||
if helper.GetEnv("GO_ENV", "production") == "debug" {
|
||||
user, err = (*User).GetByPersonalNummer(nil, 123)
|
||||
} else {
|
||||
if !Session.Exists(ctx, "user") {
|
||||
log.Println("No user in session storage!")
|
||||
return user, errors.New("No user in session storage!")
|
||||
}
|
||||
user, err = (*User).GetByPersonalNummer(nil, Session.GetInt(ctx, "user"))
|
||||
}
|
||||
if err != nil {
|
||||
log.Println("Cannot get user from session!")
|
||||
return user, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (u *User) GetAll() ([]User, error) {
|
||||
qStr, err := DB.Prepare((`SELECT card_uid, vorname, nachname FROM personal_daten;`))
|
||||
var users []User
|
||||
@@ -119,7 +144,26 @@ func (u *User) ChangePass(password, newPassword string) (bool, error) {
|
||||
|
||||
func (u *User) GetTeamMembers() ([]User, error) {
|
||||
var teamMembers []User
|
||||
teamMembers = append(teamMembers, *u)
|
||||
qStr, err := DB.Prepare(`SELECT personal_nummer, card_uid, vorname, nachname, arbeitszeit_per_tag FROM personal_daten WHERE vorgesetzter_pers_nr = $1`)
|
||||
if err != nil {
|
||||
return teamMembers, err
|
||||
}
|
||||
defer qStr.Close()
|
||||
rows, err := qStr.Query(u.PersonalNummer)
|
||||
if err != nil {
|
||||
log.Println("Error getting rows!")
|
||||
return teamMembers, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
user, err := parseUser(rows)
|
||||
if err != nil {
|
||||
log.Println("Error parsing user!")
|
||||
return teamMembers, err
|
||||
}
|
||||
teamMembers = append(teamMembers, user)
|
||||
}
|
||||
|
||||
return teamMembers, nil
|
||||
}
|
||||
|
||||
@@ -140,3 +184,48 @@ func (u *User) GetNextWeek() WorkWeek {
|
||||
return week
|
||||
|
||||
}
|
||||
|
||||
func parseUser(rows *sql.Rows) (User, error) {
|
||||
var user User
|
||||
if err := rows.Scan(&user.PersonalNummer, &user.CardUID, &user.Vorname, &user.Name, &user.Arbeitszeit); err != nil {
|
||||
log.Println("Error scanning row!", err)
|
||||
return user, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// returns the start of the week, the last submission was made, submission == first booking or last send booking_report to team leader
|
||||
func (u *User) GetLastSubmission() time.Time {
|
||||
var lastSub time.Time
|
||||
qStr, err := DB.Prepare(`
|
||||
SELECT COALESCE(
|
||||
(SELECT woche_start + INTERVAL '1 week' FROM wochen_report WHERE personal_nummer = $1 ORDER BY woche_start DESC LIMIT 1),
|
||||
(SELECT timestamp FROM anwesenheit WHERE card_uid = $2 ORDER BY timestamp LIMIT 1)
|
||||
) AS letzte_buchung;
|
||||
`)
|
||||
if err != nil {
|
||||
log.Println("Error preparing statement!", err)
|
||||
return lastSub
|
||||
}
|
||||
err = qStr.QueryRow(u.PersonalNummer, u.CardUID).Scan(&lastSub)
|
||||
if err != nil {
|
||||
log.Println("Error executing query!", err)
|
||||
return lastSub
|
||||
}
|
||||
log.Println("From DB: ", lastSub)
|
||||
lastSub = getMonday(lastSub)
|
||||
lastSub = lastSub.Round(24 * time.Hour)
|
||||
log.Println("After truncate: ", lastSub)
|
||||
return lastSub
|
||||
}
|
||||
|
||||
func getMonday(ts time.Time) time.Time {
|
||||
if ts.Weekday() != time.Monday {
|
||||
if ts.Weekday() == time.Sunday {
|
||||
ts = ts.AddDate(0, 0, -6)
|
||||
} else {
|
||||
ts = ts.AddDate(0, 0, -int(ts.Weekday()-1))
|
||||
}
|
||||
}
|
||||
return ts
|
||||
}
|
||||
|
||||
@@ -2,18 +2,113 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WorkDay struct {
|
||||
Day time.Time
|
||||
Bookings []Booking
|
||||
Day time.Time `json:"day"`
|
||||
Bookings []Booking `json:"bookings"`
|
||||
workTime time.Duration
|
||||
pauseTime time.Duration
|
||||
}
|
||||
|
||||
func (d *WorkDay) GetWorkDays(card_uid string, tsFrom, tsTo time.Time) []WorkDay {
|
||||
var workDays []WorkDay
|
||||
var workSec, pauseSec float64
|
||||
qStr, err := DB.Prepare(`
|
||||
WITH ordered_bookings AS (
|
||||
SELECT
|
||||
timestamp::DATE AS work_date, -- Extract date for grouping
|
||||
timestamp,
|
||||
check_in_out,
|
||||
LAG(timestamp) OVER (
|
||||
PARTITION BY card_uid, timestamp::DATE -- Reset for each day
|
||||
ORDER BY timestamp
|
||||
) AS prev_timestamp,
|
||||
LAG(check_in_out) OVER (
|
||||
PARTITION BY card_uid, timestamp::DATE
|
||||
ORDER BY timestamp
|
||||
) AS prev_check
|
||||
FROM anwesenheit
|
||||
WHERE card_uid = $1 -- Replace with actual card_uid
|
||||
AND timestamp::DATE >= $2 -- Set date range
|
||||
AND timestamp::DATE <= $3
|
||||
)
|
||||
SELECT
|
||||
work_date,
|
||||
|
||||
-- Total work time per day
|
||||
COALESCE(
|
||||
EXTRACT(EPOCH FROM SUM(
|
||||
CASE
|
||||
WHEN prev_check IN (1, 3) AND check_in_out IN (2, 4, 254)
|
||||
THEN timestamp - prev_timestamp
|
||||
ELSE INTERVAL '0'
|
||||
END
|
||||
)), 0
|
||||
) AS total_work,
|
||||
|
||||
-- Extract total pause time in seconds
|
||||
COALESCE(
|
||||
EXTRACT(EPOCH FROM SUM(
|
||||
CASE
|
||||
WHEN prev_check IN (2, 4, 254) AND check_in_out IN (1, 3)
|
||||
THEN timestamp - prev_timestamp
|
||||
ELSE INTERVAL '0'
|
||||
END
|
||||
)), 0
|
||||
) AS total_pause
|
||||
|
||||
FROM ordered_bookings
|
||||
GROUP BY work_date
|
||||
ORDER BY work_date;`)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Error preparing SQL statement", err)
|
||||
return workDays
|
||||
}
|
||||
|
||||
defer qStr.Close()
|
||||
rows, err := qStr.Query(card_uid, tsFrom, tsTo)
|
||||
if err != nil {
|
||||
log.Println("Error getting rows!")
|
||||
return workDays
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var workDay WorkDay
|
||||
if err := rows.Scan(&workDay.Day, &workSec, &pauseSec); err != nil {
|
||||
log.Println("Error scanning row!", err)
|
||||
return workDays
|
||||
}
|
||||
workDay.workTime = time.Duration(workSec * float64(time.Second))
|
||||
workDay.pauseTime = time.Duration(pauseSec * float64(time.Second))
|
||||
workDay.calcPauseTime()
|
||||
workDays = append(workDays, workDay)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return workDays
|
||||
}
|
||||
return workDays
|
||||
}
|
||||
|
||||
func (d *WorkDay) calcPauseTime() {
|
||||
if d.workTime > 6*time.Hour && d.pauseTime < 45*time.Minute {
|
||||
if d.workTime < 9*time.Hour && d.pauseTime < 30*time.Minute {
|
||||
diff := 30*time.Minute - d.pauseTime
|
||||
d.workTime -= diff
|
||||
d.pauseTime += diff
|
||||
} else if d.pauseTime < 45*time.Minute {
|
||||
diff := 45*time.Minute - d.pauseTime
|
||||
d.workTime -= diff
|
||||
d.pauseTime += diff
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the duration someone worked that day
|
||||
func (d *WorkDay) GetWorkTime() time.Duration {
|
||||
func (d *WorkDay) GetWorkTime() {
|
||||
var workTime, pauseTime time.Duration
|
||||
var lastBooking Booking
|
||||
for _, booking := range d.Bookings {
|
||||
@@ -30,26 +125,15 @@ func (d *WorkDay) GetWorkTime() time.Duration {
|
||||
if d.Day.Day() == time.Now().Day() && len(d.Bookings)%2 == 1 {
|
||||
workTime += time.Since(lastBooking.Timestamp.Local())
|
||||
}
|
||||
|
||||
if workTime > 6*time.Hour && pauseTime < 45*time.Minute {
|
||||
if workTime < 9*time.Hour && pauseTime < 30*time.Minute {
|
||||
diff := 30*time.Minute - pauseTime
|
||||
workTime -= diff
|
||||
pauseTime += diff
|
||||
} else if pauseTime < 45*time.Minute {
|
||||
diff := 45*time.Minute - pauseTime
|
||||
workTime -= diff
|
||||
pauseTime += diff
|
||||
}
|
||||
}
|
||||
d.workTime = workTime
|
||||
d.pauseTime = pauseTime
|
||||
return workTime
|
||||
|
||||
d.calcPauseTime()
|
||||
}
|
||||
|
||||
func formatDuration(d time.Duration) string {
|
||||
hours := int(d.Hours())
|
||||
minutes := int(d.Minutes()) % 60
|
||||
hours := int(d.Abs().Hours())
|
||||
minutes := int(d.Abs().Minutes()) % 60
|
||||
switch {
|
||||
case hours > 0:
|
||||
return fmt.Sprintf("%dh %dmin", hours, minutes)
|
||||
|
||||
@@ -1,5 +1,107 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WorkWeek struct {
|
||||
Id int
|
||||
WorkDays []WorkDay
|
||||
User User
|
||||
WeekStart time.Time
|
||||
WorkHours time.Duration
|
||||
}
|
||||
|
||||
func (w *WorkWeek) GetWeek(user User, tsMonday time.Time, populateDays bool) WorkWeek {
|
||||
var week WorkWeek
|
||||
if populateDays {
|
||||
week.WorkDays = (*WorkDay).GetWorkDays(nil, user.CardUID, tsMonday, tsMonday.Add(7*24*time.Hour))
|
||||
week.WorkHours = aggregateWorkTime(week.WorkDays)
|
||||
}
|
||||
week.User = user
|
||||
week.WeekStart = tsMonday
|
||||
return week
|
||||
}
|
||||
|
||||
func (w *WorkWeek) GetWorkHourString() string {
|
||||
return formatDuration(w.WorkHours)
|
||||
}
|
||||
|
||||
func aggregateWorkTime(days []WorkDay) time.Duration {
|
||||
var workTime time.Duration
|
||||
for _, day := range days {
|
||||
workTime += day.workTime
|
||||
}
|
||||
return workTime
|
||||
}
|
||||
|
||||
func (w *WorkWeek) GetSendWeeks(user User) []WorkWeek {
|
||||
var weeks []WorkWeek
|
||||
qStr, err := DB.Prepare(`SELECT id, woche_start::DATE FROM wochen_report WHERE bestaetigt = FALSE AND personal_nummer = $1;`)
|
||||
if err != nil {
|
||||
log.Println("Error preparing SQL statement", err)
|
||||
return weeks
|
||||
}
|
||||
defer qStr.Close()
|
||||
|
||||
rows, err := qStr.Query(user.PersonalNummer)
|
||||
if err != nil {
|
||||
log.Println("Error querining db!", err)
|
||||
return weeks
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var week WorkWeek
|
||||
week.User = user
|
||||
if err := rows.Scan(&week.Id, &week.WeekStart); err != nil {
|
||||
log.Println("Error scanning row!", err)
|
||||
return weeks
|
||||
}
|
||||
week.WorkDays = (*WorkDay).GetWorkDays(nil, user.CardUID, week.WeekStart, week.WeekStart.Add(7*24*time.Hour))
|
||||
week.WorkHours = aggregateWorkTime(week.WorkDays)
|
||||
weeks = append(weeks, week)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return weeks
|
||||
}
|
||||
|
||||
return weeks
|
||||
}
|
||||
|
||||
var ErrRunningWeek = errors.New("Week is in running week")
|
||||
|
||||
// creates a new entry in the woche_report table with the given workweek
|
||||
func (w *WorkWeek) Send() error {
|
||||
if time.Since(w.WeekStart) < 5*24*time.Hour {
|
||||
log.Println("Cannot send week, because it's the running week!")
|
||||
return ErrRunningWeek
|
||||
}
|
||||
qStr, err := DB.Prepare(`INSERT INTO wochen_report (personal_nummer, woche_start) VALUES ($1, $2);`)
|
||||
if err != nil {
|
||||
log.Println("Error preparing SQL statement", err)
|
||||
return err
|
||||
}
|
||||
_, err = qStr.Exec(w.User.PersonalNummer, w.WeekStart)
|
||||
if err != nil {
|
||||
log.Println("Error executing query!", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (w *WorkWeek) Accept() error {
|
||||
qStr, err := DB.Prepare(`UPDATE "wochen_report" SET bestaetigt = TRUE WHERE personal_nummer = $1 AND woche_start = $2;`)
|
||||
if err != nil {
|
||||
log.Println("Error preparing SQL statement", err)
|
||||
return err
|
||||
}
|
||||
_, err = qStr.Exec(w.User.PersonalNummer, w.WeekStart)
|
||||
if err != nil {
|
||||
log.Println("Error executing query!", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,11 @@
|
||||
package templates
|
||||
|
||||
import "arbeitszeitmessung/models"
|
||||
import (
|
||||
"arbeitszeitmessung/models"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
templ Base() {
|
||||
<!DOCTYPE html>
|
||||
@@ -66,20 +71,47 @@ templ UserPage(status int) {
|
||||
</div>
|
||||
}
|
||||
|
||||
templ TeamPage(teamMembers []models.User) {
|
||||
templ TeamPage(weeks []models.WorkWeek, userWeek models.WorkWeek) {
|
||||
{{
|
||||
year, kw := userWeek.WeekStart.ISOWeek()
|
||||
}}
|
||||
@Base()
|
||||
@headerComponent()
|
||||
<div class="grid-main divide-y-1">
|
||||
<div class="grid-sub divide-x-1 bg-neutral-300">
|
||||
<div class="grid-cell uppercase">Max Mustermann</div>
|
||||
<div class="grid-cell col-span-3"></div>
|
||||
<div class="grid-cell">
|
||||
<div class="grid-cell font-bold uppercase">{ fmt.Sprintf("%s %s", userWeek.User.Vorname, userWeek.User.Name) }</div>
|
||||
<div class="grid-cell col-span-3 flex flex-col gap-2">
|
||||
for _, day := range userWeek.WorkDays {
|
||||
@weekDayComponent(userWeek.User, day)
|
||||
}
|
||||
</div>
|
||||
<form class="grid-cell flex flex-col gap-2" method="post">
|
||||
<div>
|
||||
<p class="text-sm"><span class="">Woche:</span> { fmt.Sprintf("%02d-%d", kw, year) }</p>
|
||||
<p class="text-sm">an Vorgesetzten senden</p>
|
||||
</div>
|
||||
<input type="hidden" name="method" value="send"/>
|
||||
<input type="hidden" name="user" value={ strconv.Itoa(userWeek.User.PersonalNummer) }/>
|
||||
<input type="hidden" name="week" value={ userWeek.WeekStart.Format(time.DateOnly) }/>
|
||||
// if failed {
|
||||
// <p>Fehlgeschlagen</p>
|
||||
// }
|
||||
<button type="submit" class="w-full cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-800 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50">Senden</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
for _, user := range teamMembers {
|
||||
@employeComponent(user)
|
||||
for _, week := range weeks {
|
||||
@employeComponent(week)
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ NavPage() {
|
||||
@Base()
|
||||
<div class="w-full h-[100vh] flex flex-col justify-center items-center">
|
||||
<div class="flex flex-col justify-between w-full md:w-1/2 py-2">
|
||||
<a class="text-xl hover:text-accent transition-colors1" href="/time">Zeitverwaltung</a>
|
||||
<a class="text-xl hover:text-accent transition-colors1" href="/team">Mitarbeiter</a>
|
||||
<a class="text-xl hover:text-accent transition-colors1" href="/user">Nutzer</a>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -8,7 +8,12 @@ package templates
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import "arbeitszeitmessung/models"
|
||||
import (
|
||||
"arbeitszeitmessung/models"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Base() templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
@@ -195,7 +200,7 @@ func UserPage(status int) templ.Component {
|
||||
})
|
||||
}
|
||||
|
||||
func TeamPage(teamMembers []models.User) templ.Component {
|
||||
func TeamPage(weeks []models.WorkWeek, userWeek models.WorkWeek) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
@@ -216,6 +221,8 @@ func TeamPage(teamMembers []models.User) templ.Component {
|
||||
templ_7745c5c3_Var5 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
|
||||
year, kw := userWeek.WeekStart.ISOWeek()
|
||||
templ_7745c5c3_Err = Base().Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
@@ -224,17 +231,112 @@ func TeamPage(teamMembers []models.User) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<div class=\"grid-main divide-y-1\"><div class=\"grid-sub divide-x-1 bg-neutral-300\"><div class=\"grid-cell uppercase\">Max Mustermann</div><div class=\"grid-cell col-span-3\"></div><div class=\"grid-cell\"><p class=\"text-sm\">an Vorgesetzten senden</p><button type=\"submit\" class=\"w-full cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-800 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50\">Senden</button></div></div>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<div class=\"grid-main divide-y-1\"><div class=\"grid-sub divide-x-1 bg-neutral-300\"><div class=\"grid-cell font-bold uppercase\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
for _, user := range teamMembers {
|
||||
templ_7745c5c3_Err = employeComponent(user).Render(ctx, templ_7745c5c3_Buffer)
|
||||
var templ_7745c5c3_Var6 string
|
||||
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%s %s", userWeek.User.Vorname, userWeek.User.Name))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/pages.templ`, Line: 82, Col: 111}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "</div><div class=\"grid-cell col-span-3 flex flex-col gap-2\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
for _, day := range userWeek.WorkDays {
|
||||
templ_7745c5c3_Err = weekDayComponent(userWeek.User, day).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "</div>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "</div><form class=\"grid-cell flex flex-col gap-2\" method=\"post\"><div><p class=\"text-sm\"><span class=\"\">Woche:</span> ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var7 string
|
||||
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%02d-%d", kw, year))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/pages.templ`, Line: 90, Col: 87}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "</p><p class=\"text-sm\">an Vorgesetzten senden</p></div><input type=\"hidden\" name=\"method\" value=\"send\"> <input type=\"hidden\" name=\"user\" value=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var8 string
|
||||
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(userWeek.User.PersonalNummer))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/pages.templ`, Line: 94, Col: 87}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "\"> <input type=\"hidden\" name=\"week\" value=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var9 string
|
||||
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(userWeek.WeekStart.Format(time.DateOnly))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/pages.templ`, Line: 95, Col: 85}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "\"><button type=\"submit\" class=\"w-full cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-800 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50\">Senden</button></form></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
for _, week := range weeks {
|
||||
templ_7745c5c3_Err = employeComponent(week).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "</div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func NavPage() templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var10 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var10 == nil {
|
||||
templ_7745c5c3_Var10 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = Base().Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "<div class=\"w-full h-[100vh] flex flex-col justify-center items-center\"><div class=\"flex flex-col justify-between w-full md:w-1/2 py-2\"><a class=\"text-xl hover:text-accent transition-colors1\" href=\"/time\">Zeitverwaltung</a> <a class=\"text-xl hover:text-accent transition-colors1\" href=\"/team\">Mitarbeiter</a> <a class=\"text-xl hover:text-accent transition-colors1\" href=\"/user\">Nutzer</a></div></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@ package templates
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/models"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
templ weekDayComponent(day models.WorkDay) {
|
||||
templ weekDayComponent(user models.User, day models.WorkDay) {
|
||||
{{ work, pause := day.GetWorkTimeString() }}
|
||||
<div class="flex flex-row gap-2">
|
||||
@timeGaugeComponent(92, false, false)
|
||||
@timeGaugeComponent(day.GetWorkDayProgress(user), false, false)
|
||||
<div class="flex flex-col">
|
||||
<p class=""><span class="font-bold uppercase hidden md:inline">{ day.Day.Format("Mon") }:</span> { day.Day.Format("02.01.2006") }</p>
|
||||
<div class="flex flex-row gap-2">
|
||||
@@ -19,25 +21,29 @@ templ weekDayComponent(day models.WorkDay) {
|
||||
</div>
|
||||
}
|
||||
|
||||
templ employeComponent(user models.User) {
|
||||
templ employeComponent(week models.WorkWeek) {
|
||||
{{
|
||||
workWeek := user.GetWeek(time.Now().AddDate(0, 0, -2))
|
||||
year, kw := week.WeekStart.ISOWeek()
|
||||
}}
|
||||
<div class="grid-sub divide-x-1">
|
||||
<div class="grid-cell">
|
||||
<p class="font-bold uppercase">{ user.Vorname } { user.Name }</p>
|
||||
<p class="font-bold uppercase">{ week.User.Vorname } { week.User.Name }</p>
|
||||
<p class="text-sm">Arbeitszeit</p>
|
||||
<p class="text-accent">40h 12min</p>
|
||||
<p class="text-accent">{ week.GetWorkHourString() }</p>
|
||||
</div>
|
||||
<div class="grid-cell col-span-3 flex flex-col gap-2">
|
||||
for _, day := range workWeek.WorkDays {
|
||||
@weekDayComponent(day)
|
||||
for _, day := range week.WorkDays {
|
||||
@weekDayComponent(week.User, day)
|
||||
}
|
||||
</div>
|
||||
<div class="grid-cell flex flex-col justify-end">
|
||||
<form class="grid-cell flex flex-col justify-between gap-2" method="post">
|
||||
<p class="text-sm"><span class="">Woche:</span> { fmt.Sprintf("%02d-%d", kw, year) }</p>
|
||||
<input type="hidden" name="method" value="accept"/>
|
||||
<input type="hidden" name="user" value={ strconv.Itoa(week.User.PersonalNummer) }/>
|
||||
<input type="hidden" name="week" value={ week.WeekStart.Format(time.DateOnly) }/>
|
||||
<button type="submit" class="w-full bg-neutral-100 cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-900 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50">
|
||||
<p class="">Bestätigen</p>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -10,10 +10,12 @@ import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import (
|
||||
"arbeitszeitmessung/models"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
func weekDayComponent(user models.User, day models.WorkDay) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
@@ -39,7 +41,7 @@ func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = timeGaugeComponent(92, false, false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
templ_7745c5c3_Err = timeGaugeComponent(day.GetWorkDayProgress(user), false, false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@@ -50,7 +52,7 @@ func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(day.Day.Format("Mon"))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 13, Col: 89}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 15, Col: 89}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@@ -63,7 +65,7 @@ func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(day.Day.Format("02.01.2006"))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 13, Col: 130}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 15, Col: 130}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@@ -76,7 +78,7 @@ func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
var templ_7745c5c3_Var4 string
|
||||
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(work)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 15, Col: 36}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 17, Col: 36}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@@ -89,7 +91,7 @@ func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
var templ_7745c5c3_Var5 string
|
||||
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(pause)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 16, Col: 42}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 18, Col: 42}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@@ -103,7 +105,7 @@ func weekDayComponent(day models.WorkDay) templ.Component {
|
||||
})
|
||||
}
|
||||
|
||||
func employeComponent(user models.User) templ.Component {
|
||||
func employeComponent(week models.WorkWeek) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
@@ -125,15 +127,15 @@ func employeComponent(user models.User) templ.Component {
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
|
||||
workWeek := user.GetWeek(time.Now().AddDate(0, 0, -2))
|
||||
year, kw := week.WeekStart.ISOWeek()
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<div class=\"grid-sub divide-x-1\"><div class=\"grid-cell\"><p class=\"font-bold uppercase\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var7 string
|
||||
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(user.Vorname)
|
||||
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(week.User.Vorname)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 28, Col: 48}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 30, Col: 53}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@@ -144,25 +146,77 @@ func employeComponent(user models.User) templ.Component {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var8 string
|
||||
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(user.Name)
|
||||
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(week.User.Name)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 28, Col: 62}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 30, Col: 72}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "</p><p class=\"text-sm\">Arbeitszeit</p><p class=\"text-accent\">40h 12min</p></div><div class=\"grid-cell col-span-3 flex flex-col gap-2\">")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "</p><p class=\"text-sm\">Arbeitszeit</p><p class=\"text-accent\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
for _, day := range workWeek.WorkDays {
|
||||
templ_7745c5c3_Err = weekDayComponent(day).Render(ctx, templ_7745c5c3_Buffer)
|
||||
var templ_7745c5c3_Var9 string
|
||||
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(week.GetWorkHourString())
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 32, Col: 52}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "</p></div><div class=\"grid-cell col-span-3 flex flex-col gap-2\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
for _, day := range week.WorkDays {
|
||||
templ_7745c5c3_Err = weekDayComponent(week.User, day).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "</div><div class=\"grid-cell flex flex-col justify-end\"><button type=\"submit\" class=\"w-full bg-neutral-100 cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-900 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50\"><p class=\"\">Bestätigen</p></button></div></div>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "</div><form class=\"grid-cell flex flex-col justify-between gap-2\" method=\"post\"><p class=\"text-sm\"><span class=\"\">Woche:</span> ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var10 string
|
||||
templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%02d-%d", kw, year))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 40, Col: 85}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "</p><input type=\"hidden\" name=\"method\" value=\"accept\"> <input type=\"hidden\" name=\"user\" value=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var11 string
|
||||
templ_7745c5c3_Var11, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(week.User.PersonalNummer))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 42, Col: 82}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var11))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "\"> <input type=\"hidden\" name=\"week\" value=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var12 string
|
||||
templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(week.WeekStart.Format(time.DateOnly))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templates/teamComponents.templ`, Line: 43, Col: 80}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "\"> <button type=\"submit\" class=\"w-full bg-neutral-100 cursor-pointer rounded-md text-neutral-800 p-2 md:px-4 border text-center text-sm hover:text-white transition-colors border-neutral-900 focus:bg-neutral-700 active:bg-neutral-700 hover:bg-neutral-700 disabled:pointer-events-none disabled:opacity-50\"><p class=\"\">Bestätigen</p></button></form></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
||||
@@ -61,13 +61,14 @@ EXECUTE FUNCTION update_zuletzt_geandert();
|
||||
|
||||
-- audittabelle für arbeitsstunden bestätigung
|
||||
|
||||
-- DROP TABLE IF EXISTS "buchung_wochen";
|
||||
-- CREATE TABLE "buchung_wochen" (
|
||||
-- "personal_nummer" int4,
|
||||
-- "woche_start" date,
|
||||
-- "buchungen" []bigserial,
|
||||
-- "bestaetigt" bool DEFAULT FALSE,
|
||||
-- );
|
||||
DROP TABLE IF EXISTS "wochen_report";
|
||||
CREATE TABLE "wochen_report" (
|
||||
"id" serial PRIMARY KEY,
|
||||
"personal_nummer" int4,
|
||||
"woche_start" date,
|
||||
"bestaetigt" bool DEFAULT FALSE,
|
||||
UNIQUE ("personal_nummer", "woche_start")
|
||||
);
|
||||
|
||||
-- Adds crypto extension
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
|
||||
CREATE USER $POSTGRES_API_USER WITH ENCRYPTED PASSWORD '$POSTGRES_API_PASSWORD';
|
||||
GRANT CONNECT ON DATABASE $POSTGRES_DB TO $POSTGRES_API_USER;
|
||||
GRANT USAGE ON SCHEMA public TO $POSTGRES_API_USER;
|
||||
GRANT SELECT, INSERT, UPDATE ON anwesenheit, personal_daten, user_password TO $POSTGRES_API_USER;
|
||||
GRANT SELECT, INSERT, UPDATE ON anwesenheit, personal_daten, user_password, wochen_report TO $POSTGRES_API_USER;
|
||||
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO $POSTGRES_API_USER;
|
||||
EOSQL
|
||||
|
||||
|
||||
@@ -11,7 +11,12 @@
|
||||
},
|
||||
"servers": [
|
||||
{
|
||||
"url": "http://localhost:8000"
|
||||
"url": "http://localhost:8000",
|
||||
"description": "Docker Server"
|
||||
},
|
||||
{
|
||||
"url": "http://localhost:8080",
|
||||
"description": "Local Development"
|
||||
}
|
||||
],
|
||||
"tags": [
|
||||
@@ -22,55 +27,11 @@
|
||||
],
|
||||
"paths": {
|
||||
"/time": {
|
||||
"put": {
|
||||
"tags": ["booking"],
|
||||
"summary": "Update a existing booking",
|
||||
"description": "Update an existing booking by Id",
|
||||
"operationId": "updateBooking",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "counter_id",
|
||||
"in": "query",
|
||||
"description": "Booking ID to update",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"description": "Update an existent booking in the db. Not all values have to be updated",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Booking"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Booking Updated",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Booking"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Invalid ID supplied"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Error"
|
||||
}
|
||||
}
|
||||
},
|
||||
"get": {
|
||||
"tags": ["booking"],
|
||||
"summary": "Gets all the bookings limited",
|
||||
"tags": [
|
||||
"booking"
|
||||
],
|
||||
"summary": "Gets all the bookings from one card_uid",
|
||||
"description": "Returns all the bookings optionally filtered with cardID",
|
||||
"operationId": "getBooking",
|
||||
"parameters": [
|
||||
@@ -78,10 +39,30 @@
|
||||
"name": "card_uid",
|
||||
"in": "query",
|
||||
"description": "CardID to filter for",
|
||||
"required": false,
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "time_from",
|
||||
"in": "query",
|
||||
"description": "Timestamp since when all bookings are shown (default=1 month ago)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"example": "2025-02-28"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "time_to",
|
||||
"in": "query",
|
||||
"description": "Timestamp till when all bookings are shown (default=today)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"example": "2025-02-28"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
@@ -92,7 +73,51 @@
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Booking"
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"day": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"bookings": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"counter_id": {
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"example": 100
|
||||
},
|
||||
"card_uid": {
|
||||
"type": "string",
|
||||
"example": "test_card"
|
||||
},
|
||||
"geraet_id": {
|
||||
"type": "string",
|
||||
"example": "test_reader"
|
||||
},
|
||||
"check_in_out": {
|
||||
"type": "integer",
|
||||
"example": 1,
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
},
|
||||
"timestamp": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"example": "2024-09-05T08:51:12.670Z"
|
||||
}
|
||||
},
|
||||
"xml": {
|
||||
"name": "booking"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,11 +131,21 @@
|
||||
},
|
||||
"/time/new": {
|
||||
"put": {
|
||||
"tags": ["booking"],
|
||||
"tags": [
|
||||
"booking"
|
||||
],
|
||||
"summary": "Create new Booking",
|
||||
"description": "Creates a new booking with the supplied parameters",
|
||||
"operationId": "pcreateBooking",
|
||||
"parameters": [
|
||||
{
|
||||
"in": "header",
|
||||
"name": "Authorization",
|
||||
"description": "Predefined API Key to authorize access",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "card_uid",
|
||||
"in": "query",
|
||||
@@ -136,7 +171,11 @@
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"enum": [1, 2, 255]
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
@@ -146,7 +185,39 @@
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Booking"
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"counter_id": {
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"example": 100
|
||||
},
|
||||
"card_uid": {
|
||||
"type": "string",
|
||||
"example": "test_card"
|
||||
},
|
||||
"geraet_id": {
|
||||
"type": "string",
|
||||
"example": "test_reader"
|
||||
},
|
||||
"check_in_out": {
|
||||
"type": "integer",
|
||||
"example": 1,
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
},
|
||||
"timestamp": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"example": "2024-09-05T08:51:12.670Z"
|
||||
}
|
||||
},
|
||||
"xml": {
|
||||
"name": "booking"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,11 +228,30 @@
|
||||
}
|
||||
},
|
||||
"get": {
|
||||
"tags": ["booking"],
|
||||
"tags": [
|
||||
"booking"
|
||||
],
|
||||
"summary": "Create new Booking",
|
||||
"description": "Creates a new booking with the supplied parameters",
|
||||
"operationId": "gcreateBooking",
|
||||
"parameters": [
|
||||
{
|
||||
"in": "header",
|
||||
"name": "Authorization",
|
||||
"description": "Predefined API Key to authorize access",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"in": "query",
|
||||
"name": "api_key",
|
||||
"description": "Predefined API Key to authorize access",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "card_uid",
|
||||
"in": "query",
|
||||
@@ -187,7 +277,11 @@
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"enum": [1, 2, 255]
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
@@ -197,10 +291,45 @@
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Booking"
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"counter_id": {
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"example": 100
|
||||
},
|
||||
"card_uid": {
|
||||
"type": "string",
|
||||
"example": "test_card"
|
||||
},
|
||||
"geraet_id": {
|
||||
"type": "string",
|
||||
"example": "test_reader"
|
||||
},
|
||||
"check_in_out": {
|
||||
"type": "integer",
|
||||
"example": 1,
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
},
|
||||
"timestamp": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"example": "2024-09-05T08:51:12.670Z"
|
||||
}
|
||||
},
|
||||
"xml": {
|
||||
"name": "booking"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "none or wrong api key provided!"
|
||||
},
|
||||
"409": {
|
||||
"description": "Same booking type as last booking"
|
||||
@@ -210,7 +339,9 @@
|
||||
},
|
||||
"/logout": {
|
||||
"get": {
|
||||
"tags": ["booking"],
|
||||
"tags": [
|
||||
"booking"
|
||||
],
|
||||
"summary": "Logs out all logged in users",
|
||||
"description": "With this call all actively logged in users (last booking today has check_in_out=1) will be logged out automaticly (check_in_out=255)",
|
||||
"operationId": "autoLogout",
|
||||
@@ -222,7 +353,26 @@
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/User"
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"card_uid": {
|
||||
"type": "string",
|
||||
"example": "test_card"
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"example": "Mustermann"
|
||||
},
|
||||
"vorname": {
|
||||
"type": "string",
|
||||
"example": "Max"
|
||||
},
|
||||
"hauptbeschäftigungsort": {
|
||||
"type": "integer",
|
||||
"format": "int8",
|
||||
"example": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -234,6 +384,53 @@
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"BookingGrouped": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"day": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"bookings": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"counter_id": {
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"example": 100
|
||||
},
|
||||
"card_uid": {
|
||||
"type": "string",
|
||||
"example": "test_card"
|
||||
},
|
||||
"geraet_id": {
|
||||
"type": "string",
|
||||
"example": "test_reader"
|
||||
},
|
||||
"check_in_out": {
|
||||
"type": "integer",
|
||||
"example": 1,
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
},
|
||||
"timestamp": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"example": "2024-09-05T08:51:12.670Z"
|
||||
}
|
||||
},
|
||||
"xml": {
|
||||
"name": "booking"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Booking": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -253,7 +450,11 @@
|
||||
"check_in_out": {
|
||||
"type": "integer",
|
||||
"example": 1,
|
||||
"enum": [1, 2, 255]
|
||||
"enum": [
|
||||
1,
|
||||
2,
|
||||
255
|
||||
]
|
||||
},
|
||||
"timestamp": {
|
||||
"type": "string",
|
||||
|
||||
@@ -1,74 +1,58 @@
|
||||
openapi: 3.0.3
|
||||
info:
|
||||
title: Arbeitszeitmessung - OpenAPI 3.0
|
||||
description: 'This demos the API for the Arbeitszeitmessung Project '
|
||||
description: "This demos the API for the Arbeitszeitmessung Project "
|
||||
version: 0.1.0
|
||||
externalDocs:
|
||||
description: Git-Repository
|
||||
url: https://git.letsstein.de/tom/arbeitszeitmessung
|
||||
servers:
|
||||
- url: http://localhost:8000
|
||||
description: Docker Server
|
||||
- url: http://localhost:8080
|
||||
description: Local Development
|
||||
tags:
|
||||
- name: booking
|
||||
description: all Bookings
|
||||
paths:
|
||||
/time:
|
||||
put:
|
||||
tags:
|
||||
- booking
|
||||
summary: Update a existing booking
|
||||
description: Update an existing booking by Id
|
||||
operationId: updateBooking
|
||||
parameters:
|
||||
- name: counter_id
|
||||
in: query
|
||||
description: Booking ID to update
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
requestBody:
|
||||
description: >-
|
||||
Update an existent booking in the db. Not all values have to be
|
||||
updated
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Booking'
|
||||
required: true
|
||||
responses:
|
||||
'200':
|
||||
description: Booking Updated
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Booking'
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'500':
|
||||
description: Server Error
|
||||
get:
|
||||
tags:
|
||||
- booking
|
||||
summary: Gets all the bookings limited
|
||||
summary: Gets all the bookings from one card_uid
|
||||
description: Returns all the bookings optionally filtered with cardID
|
||||
operationId: getBooking
|
||||
parameters:
|
||||
- name: card_uid
|
||||
in: query
|
||||
description: CardID to filter for
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
- name: time_from
|
||||
in: query
|
||||
description: Timestamp since when all bookings are shown (default=1 month ago)
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
example: "2025-02-28"
|
||||
- name: time_to
|
||||
in: query
|
||||
description: Timestamp till when all bookings are shown (default=today)
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
example: "2025-02-28"
|
||||
responses:
|
||||
'200':
|
||||
"200":
|
||||
description: Successful operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Booking'
|
||||
'400':
|
||||
$ref: "#/components/schemas/BookingGrouped"
|
||||
"400":
|
||||
description: Invalid cardID
|
||||
/time/new:
|
||||
put:
|
||||
@@ -78,6 +62,11 @@ paths:
|
||||
description: Creates a new booking with the supplied parameters
|
||||
operationId: pcreateBooking
|
||||
parameters:
|
||||
- in: header
|
||||
name: Authorization
|
||||
description: Predefined API Key to authorize access
|
||||
schema:
|
||||
type: string
|
||||
- name: card_uid
|
||||
in: query
|
||||
description: id of the RFID card scanned
|
||||
@@ -101,13 +90,13 @@ paths:
|
||||
- 2
|
||||
- 255
|
||||
responses:
|
||||
'200':
|
||||
"200":
|
||||
description: successfully created booking
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Booking'
|
||||
'409':
|
||||
$ref: "#/components/schemas/Booking"
|
||||
"409":
|
||||
description: Same booking type as last booking
|
||||
get:
|
||||
tags:
|
||||
@@ -116,6 +105,17 @@ paths:
|
||||
description: Creates a new booking with the supplied parameters
|
||||
operationId: gcreateBooking
|
||||
parameters:
|
||||
- in: header
|
||||
name: Authorization
|
||||
description: Predefined API Key to authorize access
|
||||
schema:
|
||||
type: string
|
||||
- in: query
|
||||
name: api_key
|
||||
description: Predefined API Key to authorize access
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
- name: card_uid
|
||||
in: query
|
||||
description: id of the RFID card scanned
|
||||
@@ -139,13 +139,15 @@ paths:
|
||||
- 2
|
||||
- 255
|
||||
responses:
|
||||
'200':
|
||||
"200":
|
||||
description: successfully created booking
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Booking'
|
||||
'409':
|
||||
$ref: "#/components/schemas/Booking"
|
||||
"401":
|
||||
description: none or wrong api key provided!
|
||||
"409":
|
||||
description: Same booking type as last booking
|
||||
/logout:
|
||||
get:
|
||||
@@ -155,16 +157,26 @@ paths:
|
||||
description: With this call all actively logged in users (last booking today has check_in_out=1) will be logged out automaticly (check_in_out=255)
|
||||
operationId: autoLogout
|
||||
responses:
|
||||
'200':
|
||||
"200":
|
||||
description: Succesful
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/User'
|
||||
$ref: "#/components/schemas/User"
|
||||
components:
|
||||
schemas:
|
||||
BookingGrouped:
|
||||
type: object
|
||||
properties:
|
||||
day:
|
||||
type: string
|
||||
format: date
|
||||
bookings:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/Booking"
|
||||
Booking:
|
||||
type: object
|
||||
properties:
|
||||
@@ -188,7 +200,7 @@ components:
|
||||
timestamp:
|
||||
type: string
|
||||
format: date-time
|
||||
example: '2024-09-05T08:51:12.670Z'
|
||||
example: "2024-09-05T08:51:12.670Z"
|
||||
xml:
|
||||
name: booking
|
||||
User:
|
||||
|
||||
@@ -20,16 +20,16 @@ services:
|
||||
- 8001:8080
|
||||
backend:
|
||||
build: ../Backend
|
||||
image: git.letsstein.de/tom/arbeitszeit-backend
|
||||
image: git.letsstein.de/tom/arbeitszeit-backend:0.1.1
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
POSTGRES_HOST: db
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
EXPOSED_PORT: ${EXPOSED_PORT}
|
||||
DEBUG: true
|
||||
NO_CORS: true
|
||||
ports:
|
||||
- 8000:8080
|
||||
- ${EXPOSED_PORT}:8080
|
||||
depends_on:
|
||||
- db
|
||||
swagger:
|
||||
|
||||
@@ -13,6 +13,8 @@ services:
|
||||
volumes:
|
||||
- ${POSTGRES_PATH}:/var/lib/postgresql/data
|
||||
- ${POSTGRES_PATH}/initdb:/docker-entrypoint-initdb.d
|
||||
ports:
|
||||
- 5432:5432
|
||||
|
||||
backend:
|
||||
image: git.letsstein.de/tom/arbeitszeit-backend
|
||||
@@ -26,3 +28,4 @@ services:
|
||||
- ${EXPOSED_PORT}:8080
|
||||
depends_on:
|
||||
- db
|
||||
restart: unless-stopped
|
||||
|
||||
84
db.sql
84
db.sql
@@ -62,3 +62,87 @@ VALUES (
|
||||
-- @block select
|
||||
SELECT *
|
||||
FROM personal_daten;
|
||||
|
||||
-- @block work and pause time
|
||||
WITH ordered_bookings AS (
|
||||
SELECT
|
||||
timestamp,
|
||||
check_in_out,
|
||||
LAG(timestamp) OVER (PARTITION BY card_uid ORDER BY timestamp) AS prev_timestamp,
|
||||
LAG(check_in_out) OVER (PARTITION BY card_uid ORDER BY timestamp) AS prev_check
|
||||
FROM anwesenheit
|
||||
WHERE card_uid = 'acde-edca' -- Replace with actual card_uid
|
||||
AND timestamp::DATE = '2025-02-23' -- Replace with actual date
|
||||
)
|
||||
SELECT
|
||||
-- Total work time: Duration between check-in (1,3) and check-out (2,4,254)
|
||||
COALESCE(
|
||||
SUM(
|
||||
CASE
|
||||
WHEN prev_check IN (1, 3) AND check_in_out IN (2, 4, 254)
|
||||
THEN timestamp - prev_timestamp
|
||||
ELSE INTERVAL '0'
|
||||
END
|
||||
), INTERVAL '0'
|
||||
) AS total_work,
|
||||
|
||||
-- Total pause time: Duration between check-out (2,4,254) and next check-in (1,3)
|
||||
COALESCE(
|
||||
SUM(
|
||||
CASE
|
||||
WHEN prev_check IN (2, 4, 254) AND check_in_out IN (1, 3)
|
||||
THEN timestamp - prev_timestamp
|
||||
ELSE INTERVAL '0'
|
||||
END
|
||||
), INTERVAL '0'
|
||||
) AS total_pause
|
||||
|
||||
FROM ordered_bookings;
|
||||
|
||||
-- @block work and pause time multi day
|
||||
WITH ordered_bookings AS (
|
||||
SELECT
|
||||
timestamp::DATE AS work_date, -- Extract date for grouping
|
||||
timestamp,
|
||||
check_in_out,
|
||||
LAG(timestamp) OVER (
|
||||
PARTITION BY card_uid, timestamp::DATE -- Reset for each day
|
||||
ORDER BY timestamp
|
||||
) AS prev_timestamp,
|
||||
LAG(check_in_out) OVER (
|
||||
PARTITION BY card_uid, timestamp::DATE
|
||||
ORDER BY timestamp
|
||||
) AS prev_check
|
||||
FROM anwesenheit
|
||||
WHERE card_uid = $1 -- Replace with actual card_uid
|
||||
AND timestamp::DATE >= $2 -- Set date range
|
||||
AND timestamp::DATE < $3
|
||||
)
|
||||
SELECT
|
||||
work_date,
|
||||
|
||||
-- Total work time per day
|
||||
COALESCE(
|
||||
EXTRACT(EPOCH FROM SUM(
|
||||
CASE
|
||||
WHEN prev_check IN (1, 3) AND check_in_out IN (2, 4, 254)
|
||||
THEN timestamp - prev_timestamp
|
||||
ELSE INTERVAL '0'
|
||||
END
|
||||
)), 0
|
||||
) AS total_work,
|
||||
|
||||
-- Extract total pause time in seconds
|
||||
COALESCE(
|
||||
EXTRACT(EPOCH FROM SUM(
|
||||
CASE
|
||||
WHEN prev_check IN (2, 4, 254) AND check_in_out IN (1, 3)
|
||||
THEN timestamp - prev_timestamp
|
||||
ELSE INTERVAL '0'
|
||||
END
|
||||
)), 0
|
||||
) AS total_pause
|
||||
|
||||
FROM ordered_bookings
|
||||
GROUP BY work_date
|
||||
ORDER BY work_date;
|
||||
|
||||
Reference in New Issue
Block a user