Allow users update their tax details

This commit is contained in:
jordi fita mas 2023-01-27 01:08:03 +01:00
parent 798289bc8e
commit 57d5137913
4 changed files with 38 additions and 20 deletions

View File

@ -29,8 +29,8 @@ create table company (
created_at timestamptz not null default current_timestamp created_at timestamptz not null default current_timestamp
); );
grant select on table company to invoicer; grant select, update on table company to invoicer;
grant select on table company to admin; grant select, update on table company to admin;
commit; commit;

View File

@ -66,18 +66,18 @@ func getCompany(r *http.Request) *Company {
} }
type TaxDetailsPage struct { type TaxDetailsPage struct {
Title string Title string
BusinessName string BusinessName string
VATIN string VATIN string
TradeName string TradeName string
Phone string Phone string
Email string Email string
Web string Web string
Address string Address string
City string City string
Province string Province string
PostalCode string PostalCode string
Country string Country string
} }
func CompanyTaxDetailsHandler() http.Handler { func CompanyTaxDetailsHandler() http.Handler {
@ -88,12 +88,29 @@ func CompanyTaxDetailsHandler() http.Handler {
} }
company := mustGetCompany(r) company := mustGetCompany(r)
conn := getConn(r) conn := getConn(r)
err := conn.QueryRow(r.Context(), "select business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country 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.Country); if r.Method == "POST" {
if err != nil { r.ParseForm()
panic(err) page.BusinessName = r.FormValue("business_name")
page.VATIN = r.FormValue("vatin")
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")
page.Country = r.FormValue("country")
conn.MustExec(r.Context(), "update company set business_name = $1, vatin = $2, trade_name = $3, phone = $4, email = $5, web = $6, address = $7, city = $8, province = $9, postal_code = $10, country = $11 where company_id = $12", page.BusinessName, page.VATIN, page.TradeName, page.Phone, page.Email, page.Web, page.Address, page.City, page.Province, page.PostalCode, page.Country, company.Id)
http.Redirect(w, r, "/company/"+company.Slug+"/tax-details", http.StatusSeeOther)
} else {
err := conn.QueryRow(r.Context(), "select business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country 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.Country)
if err != nil {
panic(err)
}
} }
mustRenderAppTemplate(w, r, "tax-details.html", page) mustRenderAppTemplate(w, r, "tax-details.html", page)
}); })
} }
func mustGetCompany(r *http.Request) *Company { func mustGetCompany(r *http.Request) *Company {
@ -101,5 +118,5 @@ func mustGetCompany(r *http.Request) *Company {
if company == nil { if company == nil {
panic(errors.New("company: required but not found")) panic(errors.New("company: required but not found"))
} }
return company; return company
} }

View File

@ -12,8 +12,8 @@ set search_path to numerus, auth, public;
select has_table('company'); select has_table('company');
select has_pk('company'); select has_pk('company');
select table_privs_are('company', 'guest', array []::text[]); select table_privs_are('company', 'guest', array []::text[]);
select table_privs_are('company', 'invoicer', array ['SELECT']); select table_privs_are('company', 'invoicer', array ['SELECT', 'UPDATE']);
select table_privs_are('company', 'admin', array ['SELECT']); select table_privs_are('company', 'admin', array ['SELECT', 'UPDATE']);
select table_privs_are('company', 'authenticator', array []::text[]); select table_privs_are('company', 'authenticator', array []::text[]);
select has_column('company', 'company_id'); select has_column('company', 'company_id');

View File

@ -46,6 +46,7 @@
<input type="text" name="country" id="country" required="required" value="{{ .Country }}" placeholder="{{( pgettext "Country" "input" )}}"> <input type="text" name="country" id="country" required="required" value="{{ .Country }}" placeholder="{{( pgettext "Country" "input" )}}">
<label for="country">{{( pgettext "Country" "input" )}}</label> <label for="country">{{( pgettext "Country" "input" )}}</label>
</div> </div>
<button type="submit">{{( pgettext "Save changes" "action" )}}</button>
</form> </form>
</section> </section>
{{- end }} {{- end }}