Pre-fill all guests with the holder’s address

Most will be families living at the same address.  And, if they are not,
it is far easier to replace the incorrect address with the actual,
rather than write the same address to all family members under the same
household.
This commit is contained in:
jordi fita mas 2024-04-29 17:49:38 +02:00
parent f71ad2cc65
commit 7edf3a3ed1
1 changed files with 13 additions and 10 deletions

View File

@ -96,7 +96,7 @@ func (f *checkInForm) FillFromDatabase(ctx context.Context, conn *database.Conn,
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
guest := newGuestFormWithOptions(documentTypes, sexes, countries, nil) guest := newGuestFormWithOptions(documentTypes, sexes, countries, "", nil)
if err := guest.FillFromRow(rows); err != nil { if err := guest.FillFromRow(rows); err != nil {
return err return err
} }
@ -104,14 +104,15 @@ func (f *checkInForm) FillFromDatabase(ctx context.Context, conn *database.Conn,
} }
if len(f.Guests) == 0 { if len(f.Guests) == 0 {
var numberGuests int var numberGuests int
var address string
var country []string var country []string
row := conn.QueryRow(ctx, "select number_adults + number_teenagers, array[coalesce(country_code, '')] from booking where slug = $1", f.Slug) row := conn.QueryRow(ctx, "select number_adults + number_teenagers, coalesce(address, ''), array[coalesce(country_code, '')] from booking where slug = $1", f.Slug)
if err = row.Scan(&numberGuests, &country); err != nil { if err = row.Scan(&numberGuests, &address, &country); err != nil {
return err return err
} }
guests := make([]*guestForm, 0, numberGuests) guests := make([]*guestForm, 0, numberGuests)
for i := 0; i < numberGuests; i++ { for i := 0; i < numberGuests; i++ {
guests = append(guests, newGuestFormWithOptions(documentTypes, sexes, countries, country)) guests = append(guests, newGuestFormWithOptions(documentTypes, sexes, countries, address, country))
} }
f.Guests = guests f.Guests = guests
} }
@ -131,13 +132,13 @@ func (f *checkInForm) Parse(r *http.Request, user *auth.User, conn *database.Con
sexes := mustGetSexOptions(r.Context(), conn, user.Locale) sexes := mustGetSexOptions(r.Context(), conn, user.Locale)
countries := form.MustGetCountryOptions(r.Context(), conn, user.Locale) countries := form.MustGetCountryOptions(r.Context(), conn, user.Locale)
guest := newGuestFormWithOptions(documentTypes, sexes, countries, nil) guest := newGuestFormWithOptions(documentTypes, sexes, countries, "", nil)
count := guest.count(r) count := guest.count(r)
f.Guests = make([]*guestForm, 0, count) f.Guests = make([]*guestForm, 0, count)
guest.FillValueIndex(r, 0) guest.FillValueIndex(r, 0)
f.Guests = append(f.Guests, guest) f.Guests = append(f.Guests, guest)
for i := 1; i < count; i++ { for i := 1; i < count; i++ {
guest = newGuestFormWithOptions(documentTypes, sexes, countries, nil) guest = newGuestFormWithOptions(documentTypes, sexes, countries, "", nil)
guest.FillValueIndex(r, i) guest.FillValueIndex(r, i)
f.Guests = append(f.Guests, guest) f.Guests = append(f.Guests, guest)
} }
@ -180,15 +181,16 @@ func newGuestForm(ctx context.Context, conn *database.Conn, l *locale.Locale, sl
sexes := mustGetSexOptions(ctx, conn, l) sexes := mustGetSexOptions(ctx, conn, l)
countries := form.MustGetCountryOptions(ctx, conn, l) countries := form.MustGetCountryOptions(ctx, conn, l)
var address string
var country []string var country []string
row := conn.QueryRow(ctx, "select array[coalesce(country_code, '')] from booking where slug = $1", slug) row := conn.QueryRow(ctx, "select coalesce(address, ''), array[coalesce(country_code, '')] from booking where slug = $1", slug)
if err := row.Scan(&country); err != nil { if err := row.Scan(&address, &country); err != nil {
return nil, err return nil, err
} }
return newGuestFormWithOptions(documentTypes, sexes, countries, country), nil return newGuestFormWithOptions(documentTypes, sexes, countries, address, country), nil
} }
func newGuestFormWithOptions(documentTypes []*form.Option, sexes []*form.Option, countries []*form.Option, selectedCountry []string) *guestForm { func newGuestFormWithOptions(documentTypes []*form.Option, sexes []*form.Option, countries []*form.Option, address string, selectedCountry []string) *guestForm {
return &guestForm{ return &guestForm{
IDDocumentType: &form.Select{ IDDocumentType: &form.Select{
Name: "id_document_type", Name: "id_document_type",
@ -223,6 +225,7 @@ func newGuestFormWithOptions(documentTypes []*form.Option, sexes []*form.Option,
}, },
Address: &form.Input{ Address: &form.Input{
Name: "address", Name: "address",
Val: address,
}, },
Phone: &form.Input{ Phone: &form.Input{
Name: "phone", Name: "phone",