add holiday test and tweak services
This commit is contained in:
@@ -318,7 +318,15 @@ func GetAvailableHours(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Load bookings
|
||||
bookingRows, _ := db.DB.Query(r.Context(), `
|
||||
SELECT b.start_time, COALESCE(SUM(s.duration_minutes),0) as total_duration
|
||||
SELECT
|
||||
b.start_time,
|
||||
COALESCE(SUM(
|
||||
CASE
|
||||
WHEN bs.override_duration_minutes IS NOT NULL AND bs.override_duration_minutes > 0
|
||||
THEN bs.override_duration_minutes
|
||||
ELSE s.duration_minutes
|
||||
END
|
||||
), 0) AS total_duration
|
||||
FROM bookings b
|
||||
LEFT JOIN booking_services bs ON b.id = bs.booking_id
|
||||
LEFT JOIN services s ON bs.service_id = s.id
|
||||
|
||||
@@ -22,9 +22,7 @@ type Service struct {
|
||||
PatchTestDurationHours int `json:"patch_test_duration_hours"`
|
||||
MinimumAgeRequired int `json:"minimum_age_required"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CreatedBy *string `json:"created_by,omitempty"`
|
||||
UpdatedBy *string `json:"updated_by,omitempty"`
|
||||
}
|
||||
|
||||
type ServiceResponse struct {
|
||||
@@ -55,8 +53,8 @@ func ToggleService(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
query := "UPDATE services SET is_active = NOT is_active, updated_at = NOW(), updated_by = $1 WHERE id = $2"
|
||||
result, err := db.DB.Exec(r.Context(), query, r.Context().Value(mw.UserIDKey), serviceID)
|
||||
query := "UPDATE services SET is_active = NOT is_active WHERE id = $1"
|
||||
result, err := db.DB.Exec(r.Context(), query, serviceID)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to toggle service: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -112,17 +110,17 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
query := `
|
||||
INSERT INTO services (
|
||||
name, description, price, duration_minutes,
|
||||
patch_test_duration_hours, minimum_age_required, created_by, updated_by
|
||||
patch_test_duration_hours, minimum_age_required, created_by
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING
|
||||
id, name, description, price, duration_minutes, is_active,
|
||||
patch_test_duration_hours, minimum_age_required, created_at,
|
||||
updated_at, created_by, updated_by
|
||||
created_by
|
||||
`
|
||||
|
||||
var service Service
|
||||
var createdByDB, updatedByDB sql.NullString
|
||||
var createdByDB sql.NullString
|
||||
|
||||
err := db.DB.QueryRow(r.Context(),
|
||||
query,
|
||||
@@ -133,7 +131,6 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
req.PatchTestDurationHours,
|
||||
req.MinimumAgeRequired,
|
||||
createdBy,
|
||||
createdBy, // updated_by same as created_by for new records
|
||||
).Scan(
|
||||
&service.ID,
|
||||
&service.Name,
|
||||
@@ -144,9 +141,7 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
&service.PatchTestDurationHours,
|
||||
&service.MinimumAgeRequired,
|
||||
&service.CreatedAt,
|
||||
&service.UpdatedAt,
|
||||
&createdByDB,
|
||||
&updatedByDB,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -163,9 +158,6 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if createdByDB.Valid {
|
||||
service.CreatedBy = &createdByDB.String
|
||||
}
|
||||
if updatedByDB.Valid {
|
||||
service.UpdatedBy = &updatedByDB.String
|
||||
}
|
||||
|
||||
// Return created service
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -270,8 +262,7 @@ func AllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Query all services including inactive ones
|
||||
query := `
|
||||
SELECT id, name, description, price, duration_minutes, is_active,
|
||||
patch_test_duration_hours, minimum_age_required, created_at,
|
||||
updated_at, created_by, updated_by
|
||||
patch_test_duration_hours, minimum_age_required, created_at, created_by
|
||||
FROM services
|
||||
ORDER BY is_active DESC, name
|
||||
`
|
||||
@@ -287,7 +278,7 @@ func AllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
for rows.Next() {
|
||||
var service Service
|
||||
var createdBy, updatedBy sql.NullString
|
||||
var createdBy sql.NullString
|
||||
|
||||
err := rows.Scan(
|
||||
&service.ID,
|
||||
@@ -299,9 +290,7 @@ func AllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
&service.PatchTestDurationHours,
|
||||
&service.MinimumAgeRequired,
|
||||
&service.CreatedAt,
|
||||
&service.UpdatedAt,
|
||||
&createdBy,
|
||||
&updatedBy,
|
||||
)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
|
||||
@@ -312,9 +301,6 @@ func AllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if createdBy.Valid {
|
||||
service.CreatedBy = &createdBy.String
|
||||
}
|
||||
if updatedBy.Valid {
|
||||
service.UpdatedBy = &updatedBy.String
|
||||
}
|
||||
|
||||
services = append(services, service)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user