From 19251eefed0ace2d523a7dad516d3876007fb776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Tr=C3=B6ger?= Date: Thu, 10 Apr 2025 09:11:17 +0200 Subject: [PATCH] small fixes and refactoring --- Backend/database.go | 2 +- Backend/endpoints/auto_logout.go | 3 ++- Backend/endpoints/time.go | 2 +- Backend/endpoints/time_create.go | 1 - Backend/endpoints/user.go | 2 +- Backend/main.go | 13 ++++++++++++- Backend/models/booking.go | 14 +++++++++++--- Backend/models/user.go | 8 ++++---- DB/initdb/01_create_tables.sql | 2 +- db.sql | 2 +- 10 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Backend/database.go b/Backend/database.go index 1112aa6..f814974 100644 --- a/Backend/database.go +++ b/Backend/database.go @@ -10,7 +10,7 @@ import ( func OpenDatabase() (*sql.DB, error) { dbHost := helper.GetEnv("POSTGRES_HOST", "localhost") dbName := helper.GetEnv("POSTGRES_DB", "arbeitszeitmessung") - dbUser := helper.GetEnv("POSTGRES_API_USER", "arbeit_zeit") + dbUser := helper.GetEnv("POSTGRES_API_USER", "api_nutzer") dbPassword := helper.GetEnv("POSTGRES_API_PASS", "password") connStr := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=disable&TimeZone=Europe/Berlin", dbUser, dbPassword, dbHost, dbName) diff --git a/Backend/endpoints/auto_logout.go b/Backend/endpoints/auto_logout.go index 9457d3e..b27f75f 100644 --- a/Backend/endpoints/auto_logout.go +++ b/Backend/endpoints/auto_logout.go @@ -29,8 +29,9 @@ func autoLogout(w http.ResponseWriter) { err = user.Logout() if err != nil { fmt.Printf("Error logging out user %v\n", err) - } + }else { logged_out_users = append(logged_out_users, user) + } } } diff --git a/Backend/endpoints/time.go b/Backend/endpoints/time.go index 4573249..abe2c89 100644 --- a/Backend/endpoints/time.go +++ b/Backend/endpoints/time.go @@ -100,7 +100,7 @@ func updateBooking(w http.ResponseWriter, r *http.Request) { log.Println("Error getting booking!", err) continue } - parsedTime, err := time.ParseInLocation("15:04", possibleBooking[0], time.Local) + parsedTime, err := time.ParseInLocation("15:04", possibleBooking[0], booking.Timestamp.Location()) if err != nil { log.Println("Error parsing time!", err) continue diff --git a/Backend/endpoints/time_create.go b/Backend/endpoints/time_create.go index e1f4d58..ddeb0a4 100644 --- a/Backend/endpoints/time_create.go +++ b/Backend/endpoints/time_create.go @@ -69,6 +69,5 @@ func checkPassword(r *http.Request) bool { return false } } - log.Println(authHeaders) return authToken == authHeaders[_authStart:] } diff --git a/Backend/endpoints/user.go b/Backend/endpoints/user.go index b225f39..8681022 100644 --- a/Backend/endpoints/user.go +++ b/Backend/endpoints/user.go @@ -34,7 +34,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { } } -func UserHandler(w http.ResponseWriter, r *http.Request) { +func UserSettingsHandler(w http.ResponseWriter, r *http.Request) { helper.RequiresLogin(Session, w, r) switch r.Method { case http.MethodGet: diff --git a/Backend/main.go b/Backend/main.go index e999972..9077586 100644 --- a/Backend/main.go +++ b/Backend/main.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "net/http" + "os" "time" "github.com/joho/godotenv" @@ -21,6 +22,16 @@ func main() { if err != nil { log.Println("No .env file found in directory!") } + if(helper.GetEnv("GO_ENV", "production") == "debug") { + log.Println("Debug mode enabled") + log.Println("Environment Variables") + envs := os.Environ() + for _, e := range envs { + fmt.Println(e) + } + } + + models.DB, err = OpenDatabase() if err != nil { @@ -38,7 +49,7 @@ func main() { server.Handle("/time", ParamsMiddleware(endpoints.TimeHandler)) server.HandleFunc("/logout", endpoints.LogoutHandler) server.HandleFunc("/user/login", endpoints.LoginHandler) - server.HandleFunc("/user", endpoints.UserHandler) + server.HandleFunc("/user/settings", endpoints.UserSettingsHandler) server.HandleFunc("/team", endpoints.TeamHandler) server.HandleFunc("/team/presence", endpoints.TeamHandler) server.Handle("/", http.RedirectHandler("/time", http.StatusPermanentRedirect)) diff --git a/Backend/models/booking.go b/Backend/models/booking.go index 9c84e8c..fd3e1ff 100644 --- a/Backend/models/booking.go +++ b/Backend/models/booking.go @@ -1,6 +1,7 @@ package models import ( + "arbeitszeitmessung/helper" "database/sql" "fmt" "log" @@ -161,16 +162,23 @@ func (b Booking) Save() { } func (b *Booking) GetBookingType() string { + debug := (helper.GetEnv("GO_ENV", "production") == "debug") switch b.CheckInOut { case 1: //manuelle Änderung return "kommen" case 3: + if(debug){ return "kommen manuell" + } + return "kommen" case 2: //manuelle Änderung return "gehen" case 4: + if(debug){ return "gehen manuell" - case 255: + } + return "gehen" + case 254: return "abgemeldet" default: return "Buchungs Typ unbekannt" @@ -221,11 +229,11 @@ func (b *Booking) UpdateTime(newTime time.Time) { // 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) + newBooking.Timestamp = time.Date(b.Timestamp.Year(), b.Timestamp.Month(), b.Timestamp.Day(), hour, minute, 0, 0, b.Timestamp.Location()) if b.CheckInOut < 3 { newBooking.CheckInOut = b.CheckInOut + 2 } - if b.CheckInOut == 255 { + if b.CheckInOut == 254 { newBooking.CheckInOut = 4 } b.Update(newBooking) diff --git a/Backend/models/user.go b/Backend/models/user.go index 94fd3d0..1e0155f 100644 --- a/Backend/models/user.go +++ b/Backend/models/user.go @@ -68,7 +68,7 @@ func (u *User) GetAll() ([]User, error) { // Returns true if there is a booking 1 for today -> meaning the user is at work // Returns false if there is no booking today or the user is already booked out of the system func (u *User) CheckAnwesenheit() bool { - qStr, err := DB.Prepare((`SELECT check_in_out FROM anwesenheit WHERE card_uid = $1 AND "timestamp" >= now()::date + interval '1h' ORDER BY "timestamp" DESC`)) + qStr, err := DB.Prepare((`SELECT check_in_out FROM anwesenheit WHERE card_uid = $1 AND "timestamp" >= now()::date + interval '1h' ORDER BY "timestamp" DESC LIMIT 1;`)) if err != nil { fmt.Printf("Error preparing query statement %v\n", err) return false @@ -79,12 +79,12 @@ func (u *User) CheckAnwesenheit() bool { if err != nil { return false } - return check_in_out == 1 + return check_in_out%2 == 1 } -// Creates a new booking for the user -> check_in_out will be 255 for automatic check out +// Creates a new booking for the user -> check_in_out will be 254 for automatic check out func (u *User) Logout() error { - booking := (*Booking).New(nil, u.CardUID, 0, 255) + booking := (*Booking).New(nil, u.CardUID, 0, 254) err := booking.Insert() if err != nil { fmt.Printf("Error inserting booking %v\n", err) diff --git a/DB/initdb/01_create_tables.sql b/DB/initdb/01_create_tables.sql index 60a91e6..82cb49a 100644 --- a/DB/initdb/01_create_tables.sql +++ b/DB/initdb/01_create_tables.sql @@ -75,7 +75,7 @@ CREATE TABLE "abwesenheiten" ( "counter_id" bigserial PRIMARY KEY, "card_uid" varchar(255), "abwesenheit_typ" int2, - "datum" timestamptz(6) DEFAULT NOW()::DATE, + "datum" timestamptz(6) DEFAULT NOW()::DATE ); -- Adds crypto extension diff --git a/db.sql b/db.sql index 05e4244..0e59269 100644 --- a/db.sql +++ b/db.sql @@ -6,7 +6,7 @@ CREATE TABLE "public"."anwesenheit" ( "check_in_out" int2, "geraet_id" int2 ); -COMMENT ON COLUMN "public"."anwesenheit"."check_in_out" IS '1=Check In 2=Check Out 255=Automatic Check Out'; +COMMENT ON COLUMN "public"."anwesenheit"."check_in_out" IS '1=Check In 2=Check Out 254=Automatic Check Out'; COMMENT ON COLUMN "public"."anwesenheit"."geraet_id" IS 'ID des Lesegerätes'; -- @block create table personaldaten