2023-10-27 14:04:43 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package booking
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-18 20:05:30 +00:00
|
|
|
"golang.org/x/text/language"
|
2023-10-27 14:04:43 +00:00
|
|
|
"net/http"
|
2024-01-18 20:05:30 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-10-27 14:04:43 +00:00
|
|
|
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/auth"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
|
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AdminHandler struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAdminHandler() *AdminHandler {
|
|
|
|
return &AdminHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var head string
|
|
|
|
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
|
|
|
|
|
|
|
switch head {
|
|
|
|
case "":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2024-01-18 20:05:30 +00:00
|
|
|
serveBookingIndex(w, r, user, company, conn)
|
|
|
|
default:
|
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet)
|
|
|
|
}
|
2023-10-27 14:04:43 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-18 20:05:30 +00:00
|
|
|
func serveBookingIndex(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
|
|
|
bookings, err := collectBookingEntries(r.Context(), conn, user.Locale.Language)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
page := bookingIndex(bookings)
|
|
|
|
page.MustRender(w, r, user, company)
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectBookingEntries(ctx context.Context, conn *database.Conn, lang language.Tag) ([]*bookingEntry, error) {
|
|
|
|
rows, err := conn.Query(ctx, `
|
|
|
|
select left(slug::text, 10)
|
|
|
|
, '/admin/booking/' || slug
|
2024-04-19 19:29:36 +00:00
|
|
|
, lower(stay)
|
|
|
|
, upper(stay)
|
2024-01-18 20:05:30 +00:00
|
|
|
, holder_name
|
|
|
|
, booking.booking_status
|
|
|
|
, coalesce(i18n.name, status.name)
|
|
|
|
from booking
|
|
|
|
join booking_status as status using (booking_status)
|
|
|
|
left join booking_status_i18n as i18n on status.booking_status = i18n.booking_status and i18n.lang_tag = $1
|
2024-04-19 19:29:36 +00:00
|
|
|
order by stay desc
|
2024-01-18 20:05:30 +00:00
|
|
|
`, lang)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var entries []*bookingEntry
|
|
|
|
for rows.Next() {
|
|
|
|
entry := &bookingEntry{}
|
|
|
|
if err = rows.Scan(&entry.Reference, &entry.URL, &entry.ArrivalDate, &entry.DepartureDate, &entry.HolderName, &entry.Status, &entry.StatusLabel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type bookingEntry struct {
|
|
|
|
Reference string
|
|
|
|
URL string
|
|
|
|
ArrivalDate time.Time
|
|
|
|
DepartureDate time.Time
|
|
|
|
HolderName string
|
|
|
|
Status string
|
|
|
|
StatusLabel string
|
|
|
|
}
|
|
|
|
|
|
|
|
type bookingIndex []*bookingEntry
|
|
|
|
|
|
|
|
func (page bookingIndex) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
|
|
|
switch r.URL.Query().Get("format") {
|
|
|
|
case "ods":
|
|
|
|
columns := []string{
|
|
|
|
"Reference",
|
|
|
|
"Arrival Date",
|
|
|
|
"Departure Date",
|
|
|
|
"Holder Name",
|
|
|
|
"Status",
|
|
|
|
}
|
|
|
|
ods, err := writeTableOds(page, columns, user.Locale, func(sb *strings.Builder, entry *bookingEntry) error {
|
|
|
|
if err := writeCellString(sb, entry.Reference); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writeCellDate(sb, entry.ArrivalDate)
|
|
|
|
writeCellDate(sb, entry.DepartureDate)
|
|
|
|
if err := writeCellString(sb, entry.HolderName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeCellString(sb, entry.StatusLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
mustWriteOdsResponse(w, ods, user.Locale.Pgettext("bookings.ods", "filename"))
|
|
|
|
default:
|
|
|
|
template.MustRenderAdmin(w, r, user, company, "booking/index.gohtml", page)
|
|
|
|
}
|
|
|
|
}
|