Move the locales parameter inside Company struct.

I realized that locales should be company-dependent: we could have two
companies that show pages in a different subset of the application
locales.  It is not the case now, because despite being a “multicompany
application”, it is intended for a single customer, but still makes
sense to include it in Company, even if the subset is the same set as
the application’s.
This commit is contained in:
jordi fita mas 2023-08-07 10:51:57 +02:00
parent 9a8ef8ce9f
commit e59e6a2a42
3 changed files with 13 additions and 10 deletions

View File

@ -76,7 +76,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
panic(err)
}
company, err := auth.CompanyByHost(r.Context(), conn, r.Host)
company, err := auth.CompanyByHost(r.Context(), conn, r.Host, h.locales)
if database.ErrorIsNotFound(err) {
http.NotFound(w, r)
return
@ -116,7 +116,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
user.Locale = urlLocale
h.public.Handler(user, company, h.locales, conn).ServeHTTP(w, r)
h.public.Handler(user, company, conn).ServeHTTP(w, r)
}
}
}

View File

@ -12,7 +12,6 @@ import (
"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/locale"
"dev.tandem.ws/tandem/camper/pkg/template"
)
@ -22,14 +21,14 @@ func newPublicHandler() *publicHandler {
return &publicHandler{}
}
func (h *publicHandler) Handler(user *auth.User, company *auth.Company, locales locale.Locales, conn *database.Conn) http.Handler {
func (h *publicHandler) 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 "":
page := newHomePage()
page.MustRender(w, r, user, company, locales)
page.MustRender(w, r, user, company)
default:
http.NotFound(w, r)
}
@ -55,11 +54,11 @@ func newPublicPage(template string) *PublicPage {
}
}
func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, locales locale.Locales) {
func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
schema := httplib.Protocol(r)
authority := httplib.Host(r)
_, path := httplib.ShiftPath(r.RequestURI)
for _, l := range locales {
for _, l := range company.Locales {
p.LocalizedAlternates = append(p.LocalizedAlternates, &LocalizedAlternate{
Lang: l.Language.String(),
Endonym: l.Endonym,

View File

@ -9,14 +9,18 @@ import (
"context"
"dev.tandem.ws/tandem/camper/pkg/database"
"dev.tandem.ws/tandem/camper/pkg/locale"
)
type Company struct {
ID int
Locales locale.Locales
}
func CompanyByHost(ctx context.Context, conn *database.Conn, host string) (*Company, error) {
company := &Company{}
func CompanyByHost(ctx context.Context, conn *database.Conn, host string, allLocales locale.Locales) (*Company, error) {
company := &Company{
Locales: allLocales,
}
if err := conn.QueryRow(ctx, `
select company_id
from company_host