56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
)
|
|
|
|
type Company struct {
|
|
ID int
|
|
CurrencySymbol string
|
|
DecimalDigits int
|
|
Slug string
|
|
}
|
|
|
|
func QueryMainCompany(ctx context.Context, conn *database.Conn) (*Company, error) {
|
|
slug, err := conn.GetText(ctx, "select slug::text from company order by company_id limit 1")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return QueryBySlug(ctx, conn, slug)
|
|
}
|
|
|
|
func QueryBySlug(ctx context.Context, conn *database.Conn, slug string) (*Company, error) {
|
|
company := &Company{
|
|
Slug: slug,
|
|
}
|
|
if err := conn.QueryRow(ctx, `
|
|
select company_id
|
|
, currency_symbol
|
|
, decimal_digits
|
|
from company
|
|
join currency using (currency_code)
|
|
where slug = $1
|
|
`, company.Slug).Scan(
|
|
&company.ID,
|
|
&company.CurrencySymbol,
|
|
&company.DecimalDigits,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
return company, nil
|
|
}
|
|
|
|
func (c *Company) URL() string {
|
|
if c == nil {
|
|
return ""
|
|
}
|
|
return "/company/" + c.Slug
|
|
}
|