small fixes and refactoring

This commit is contained in:
2025-04-10 09:11:17 +02:00
parent e60f19ee27
commit 19251eefed
10 changed files with 34 additions and 15 deletions

View File

@@ -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)

View File

@@ -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)
}
}
}

View File

@@ -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

View File

@@ -69,6 +69,5 @@ func checkPassword(r *http.Request) bool {
return false
}
}
log.Println(authHeaders)
return authToken == authHeaders[_authStart:]
}

View File

@@ -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:

View File

@@ -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))

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

2
db.sql
View File

@@ -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