Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
package pkg
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
)
const (
ContextCompanyKey = "numerus-company"
)
type Company struct {
Id int
Slug string
}
func CompanyHandler ( next http . Handler ) http . Handler {
return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
slug := r . URL . Path
if idx := strings . IndexByte ( slug , '/' ) ; idx >= 0 {
slug = slug [ : idx ]
}
conn := getConn ( r )
company := & Company {
Slug : slug ,
}
err := conn . QueryRow ( r . Context ( ) , "select company_id from company where slug = $1" , slug ) . Scan ( & company . Id )
if err != nil {
http . NotFound ( w , r )
return
}
ctx := context . WithValue ( r . Context ( ) , ContextCompanyKey , company )
r = r . WithContext ( ctx )
// Same as StripPrefix
p := strings . TrimPrefix ( r . URL . Path , slug )
rp := strings . TrimPrefix ( r . URL . RawPath , slug )
if len ( p ) < len ( r . URL . Path ) && ( r . URL . RawPath == "" || len ( rp ) < len ( r . URL . RawPath ) ) {
r2 := new ( http . Request )
* r2 = * r
r2 . URL = new ( url . URL )
* r2 . URL = * r . URL
if p == "" {
r2 . URL . Path = "/"
} else {
r2 . URL . Path = p
}
r2 . URL . RawPath = rp
next . ServeHTTP ( w , r2 )
} else {
http . NotFound ( w , r )
}
} )
}
func getCompany ( r * http . Request ) * Company {
company := r . Context ( ) . Value ( ContextCompanyKey )
if company == nil {
return nil
}
return company . ( * Company )
}
2023-01-28 11:24:52 +00:00
type CurrencyOption struct {
Code string
Symbol string
}
2023-01-27 20:30:14 +00:00
type CountryOption struct {
Code string
Name string
}
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
type TaxDetailsPage struct {
2023-01-27 00:08:03 +00:00
Title string
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
BusinessName string
2023-01-27 00:08:03 +00:00
VATIN string
TradeName string
Phone string
Email string
Web string
Address string
City string
Province string
PostalCode string
2023-01-27 20:30:14 +00:00
CountryCode string
Countries [ ] CountryOption
2023-01-28 11:24:52 +00:00
CurrencyCode string
Currencies [ ] CurrencyOption
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
}
func CompanyTaxDetailsHandler ( ) http . Handler {
return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
locale := getLocale ( r )
page := & TaxDetailsPage {
Title : pgettext ( "title" , "Tax Details" , locale ) ,
}
company := mustGetCompany ( r )
conn := getConn ( r )
2023-01-27 00:08:03 +00:00
if r . Method == "POST" {
r . ParseForm ( )
page . BusinessName = r . FormValue ( "business_name" )
2023-01-27 20:30:14 +00:00
page . CountryCode = r . FormValue ( "country" )
page . VATIN = page . CountryCode + r . FormValue ( "vatin" )
2023-01-27 00:08:03 +00:00
page . TradeName = r . FormValue ( "trade_name" )
page . Phone = r . FormValue ( "phone" )
page . Email = r . FormValue ( "email" )
page . Web = r . FormValue ( "web" )
page . Address = r . FormValue ( "address" )
page . City = r . FormValue ( "city" )
page . Province = r . FormValue ( "province" )
page . PostalCode = r . FormValue ( "postal_code" )
2023-01-28 11:24:52 +00:00
page . CurrencyCode = r . FormValue ( "currency" )
conn . MustExec ( r . Context ( ) , "update company set business_name = $1, vatin = $2, trade_name = $3, phone = parse_packed_phone_number($4, $11), email = $5, web = $6, address = $7, city = $8, province = $9, postal_code = $10, country_code = $11, currency_code = $12 where company_id = $13" , page . BusinessName , page . VATIN , page . TradeName , page . Phone , page . Email , page . Web , page . Address , page . City , page . Province , page . PostalCode , page . CountryCode , page . CurrencyCode , company . Id )
2023-01-27 00:08:03 +00:00
http . Redirect ( w , r , "/company/" + company . Slug + "/tax-details" , http . StatusSeeOther )
} else {
2023-01-28 11:24:52 +00:00
err := conn . QueryRow ( r . Context ( ) , "select business_name, substr(vatin::text, 3), trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code from company where company_id = $1" , company . Id ) . Scan ( & page . BusinessName , & page . VATIN , & page . TradeName , & page . Phone , & page . Email , & page . Web , & page . Address , & page . City , & page . Province , & page . PostalCode , & page . CountryCode , & page . CurrencyCode )
2023-01-27 00:08:03 +00:00
if err != nil {
panic ( err )
}
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
}
2023-01-27 20:30:14 +00:00
page . Countries = mustGetCountryOptions ( r . Context ( ) , conn , locale )
2023-01-28 11:24:52 +00:00
page . Currencies = mustGetCurrencyOptions ( r . Context ( ) , conn )
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
mustRenderAppTemplate ( w , r , "tax-details.html" , page )
2023-01-27 00:08:03 +00:00
} )
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
}
func mustGetCompany ( r * http . Request ) * Company {
company := getCompany ( r )
if company == nil {
panic ( errors . New ( "company: required but not found" ) )
}
2023-01-27 00:08:03 +00:00
return company
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
}
2023-01-27 20:30:14 +00:00
func mustGetCountryOptions ( ctx context . Context , conn * Conn , locale * Locale ) [ ] CountryOption {
2023-01-28 11:24:52 +00:00
rows , err := conn . Query ( ctx , "select country.country_code, coalesce(i18n.name, country.name) as l10n_name from country left join country_i18n as i18n on country.country_code = i18n.country_code and i18n.lang_tag = $1 order by l10n_name" , locale . Language )
2023-01-27 20:30:14 +00:00
if err != nil {
panic ( err )
}
defer rows . Close ( )
var countries [ ] CountryOption
for rows . Next ( ) {
var country CountryOption
err = rows . Scan ( & country . Code , & country . Name )
if err != nil {
panic ( err )
}
countries = append ( countries , country )
}
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return countries
}
2023-01-28 11:24:52 +00:00
func mustGetCurrencyOptions ( ctx context . Context , conn * Conn ) [ ] CurrencyOption {
rows , err := conn . Query ( ctx , "select currency_code, currency_symbol from currency order by currency_code" )
if err != nil {
panic ( err )
}
defer rows . Close ( )
var currencies [ ] CurrencyOption
for rows . Next ( ) {
var currency CurrencyOption
err = rows . Scan ( & currency . Code , & currency . Symbol )
if err != nil {
panic ( err )
}
currencies = append ( currencies , currency )
}
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return currencies
}