2023-01-29 14:14:31 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-03 11:30:56 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-02-01 13:34:40 +00:00
|
|
|
"html/template"
|
2023-01-29 14:14:31 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ContactEntry struct {
|
2023-02-03 12:29:10 +00:00
|
|
|
Slug string
|
2023-01-29 14:14:31 +00:00
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
Phone string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContactsIndexPage struct {
|
|
|
|
Contacts []*ContactEntry
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:30:56 +00:00
|
|
|
func IndexContacts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
conn := getConn(r)
|
2023-02-04 09:43:42 +00:00
|
|
|
company := mustGetCompany(r)
|
2023-02-03 11:30:56 +00:00
|
|
|
page := &ContactsIndexPage{
|
|
|
|
Contacts: mustGetContactEntries(r.Context(), conn, company),
|
|
|
|
}
|
2023-02-04 09:48:03 +00:00
|
|
|
mustRenderAppTemplate(w, r, "contacts/index.gohtml", page)
|
2023-02-03 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:29:10 +00:00
|
|
|
func GetContactForm(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
2023-02-03 11:30:56 +00:00
|
|
|
locale := getLocale(r)
|
|
|
|
conn := getConn(r)
|
|
|
|
form := newContactForm(r.Context(), conn, locale)
|
2023-02-03 12:29:10 +00:00
|
|
|
slug := params[0].Value
|
|
|
|
if slug == "new" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
mustRenderNewContactForm(w, r, form)
|
|
|
|
return
|
|
|
|
}
|
2023-02-14 11:46:11 +00:00
|
|
|
if notFoundErrorOrPanic(conn.QueryRow(r.Context(), "select business_name, substr(vatin::text, 3), trade_name, phone, email, web, address, city, province, postal_code, country_code from contact where slug = $1", slug).Scan(form.BusinessName, form.VATIN, form.TradeName, form.Phone, form.Email, form.Web, form.Address, form.City, form.Province, form.PostalCode, form.Country)) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2023-02-03 12:29:10 +00:00
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
mustRenderEditContactForm(w, r, form)
|
2023-02-03 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:29:10 +00:00
|
|
|
func mustRenderNewContactForm(w http.ResponseWriter, r *http.Request, form *contactForm) {
|
2023-02-04 09:48:03 +00:00
|
|
|
mustRenderAppTemplate(w, r, "contacts/new.gohtml", form)
|
2023-02-03 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:29:10 +00:00
|
|
|
func mustRenderEditContactForm(w http.ResponseWriter, r *http.Request, form *contactForm) {
|
2023-02-04 09:48:03 +00:00
|
|
|
mustRenderAppTemplate(w, r, "contacts/edit.gohtml", form)
|
2023-02-03 12:29:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 11:30:56 +00:00
|
|
|
func HandleAddContact(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
conn := getConn(r)
|
|
|
|
locale := getLocale(r)
|
|
|
|
form := newContactForm(r.Context(), conn, locale)
|
|
|
|
if err := form.Parse(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCsrfTokenValid(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !form.Validate(r.Context(), conn) {
|
2023-02-03 12:29:10 +00:00
|
|
|
mustRenderNewContactForm(w, r, form)
|
2023-02-03 11:30:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-04 09:43:42 +00:00
|
|
|
company := mustGetCompany(r)
|
2023-02-03 12:29:10 +00:00
|
|
|
conn.MustExec(r.Context(), "insert into contact (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code) values ($1, $2, ($12 || $3)::vatin, $4, parse_packed_phone_number($5, $12), $6, $7, $8, $9, $10, $11, $12)", company.Id, form.BusinessName, form.VATIN, form.TradeName, form.Phone, form.Email, form.Web, form.Address, form.City, form.Province, form.PostalCode, form.Country)
|
2023-02-04 09:43:42 +00:00
|
|
|
http.Redirect(w, r, companyURI(company, "/contacts"), http.StatusSeeOther)
|
2023-01-29 14:14:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:29:10 +00:00
|
|
|
func HandleUpdateContact(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
|
|
conn := getConn(r)
|
|
|
|
locale := getLocale(r)
|
|
|
|
form := newContactForm(r.Context(), conn, locale)
|
|
|
|
if err := form.Parse(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := verifyCsrfTokenValid(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !form.Validate(r.Context(), conn) {
|
|
|
|
mustRenderEditContactForm(w, r, form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
slug := conn.MustGetText(r.Context(), "", "update contact set business_name = $1, vatin = ($11 || $2)::vatin, 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 where slug = $12 returning slug", form.BusinessName, form.VATIN, form.TradeName, form.Phone, form.Email, form.Web, form.Address, form.City, form.Province, form.PostalCode, form.Country, params[0].Value)
|
|
|
|
if slug == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
2023-02-04 09:43:42 +00:00
|
|
|
http.Redirect(w, r, companyURI(mustGetCompany(r), "/contacts/"+slug), http.StatusSeeOther)
|
2023-02-03 12:29:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 14:14:31 +00:00
|
|
|
func mustGetContactEntries(ctx context.Context, conn *Conn, company *Company) []*ContactEntry {
|
2023-02-03 12:29:10 +00:00
|
|
|
rows, err := conn.Query(ctx, "select slug, business_name, email, phone from contact where company_id = $1 order by business_name", company.Id)
|
2023-01-29 14:14:31 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var entries []*ContactEntry
|
|
|
|
for rows.Next() {
|
|
|
|
entry := &ContactEntry{}
|
2023-02-03 12:29:10 +00:00
|
|
|
err = rows.Scan(&entry.Slug, &entry.Name, &entry.Email, &entry.Phone)
|
2023-01-29 14:14:31 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
if rows.Err() != nil {
|
|
|
|
panic(rows.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:34:40 +00:00
|
|
|
type contactForm struct {
|
|
|
|
locale *Locale
|
|
|
|
BusinessName *InputField
|
|
|
|
VATIN *InputField
|
|
|
|
TradeName *InputField
|
|
|
|
Phone *InputField
|
|
|
|
Email *InputField
|
|
|
|
Web *InputField
|
|
|
|
Address *InputField
|
|
|
|
City *InputField
|
|
|
|
Province *InputField
|
|
|
|
PostalCode *InputField
|
|
|
|
Country *SelectField
|
|
|
|
}
|
|
|
|
|
|
|
|
func newContactForm(ctx context.Context, conn *Conn, locale *Locale) *contactForm {
|
|
|
|
return &contactForm{
|
|
|
|
locale: locale,
|
|
|
|
BusinessName: &InputField{
|
|
|
|
Name: "business_name",
|
|
|
|
Label: pgettext("input", "Business name", locale),
|
|
|
|
Type: "text",
|
|
|
|
Required: true,
|
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="organization"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
VATIN: &InputField{
|
|
|
|
Name: "vatin",
|
|
|
|
Label: pgettext("input", "VAT number", locale),
|
|
|
|
Type: "text",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
TradeName: &InputField{
|
|
|
|
Name: "trade_name",
|
|
|
|
Label: pgettext("input", "Trade name", locale),
|
|
|
|
Type: "text",
|
|
|
|
},
|
|
|
|
Phone: &InputField{
|
|
|
|
Name: "phone",
|
|
|
|
Label: pgettext("input", "Phone", locale),
|
|
|
|
Type: "tel",
|
|
|
|
Required: true,
|
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="tel"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Email: &InputField{
|
|
|
|
Name: "email",
|
|
|
|
Label: pgettext("input", "Email", locale),
|
|
|
|
Type: "email",
|
|
|
|
Required: true,
|
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="email"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Web: &InputField{
|
|
|
|
Name: "web",
|
|
|
|
Label: pgettext("input", "Web", locale),
|
|
|
|
Type: "url",
|
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="url"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Address: &InputField{
|
|
|
|
Name: "address",
|
|
|
|
Label: pgettext("input", "Address", locale),
|
|
|
|
Type: "text",
|
|
|
|
Required: true,
|
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="address-line1"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
City: &InputField{
|
|
|
|
Name: "city",
|
|
|
|
Label: pgettext("input", "City", locale),
|
|
|
|
Type: "text",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
Province: &InputField{
|
|
|
|
Name: "province",
|
|
|
|
Label: pgettext("input", "Province", locale),
|
|
|
|
Type: "text",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
PostalCode: &InputField{
|
|
|
|
Name: "postal_code",
|
|
|
|
Label: pgettext("input", "Postal code", locale),
|
|
|
|
Type: "text",
|
|
|
|
Required: true,
|
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="postal-code"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Country: &SelectField{
|
|
|
|
Name: "country",
|
2023-02-08 12:47:36 +00:00
|
|
|
Label: pgettext("input", "Country", locale),
|
2023-02-01 13:34:40 +00:00
|
|
|
Options: mustGetCountryOptions(ctx, conn, locale),
|
2023-02-05 13:06:33 +00:00
|
|
|
Required: true,
|
2023-02-08 12:47:36 +00:00
|
|
|
Selected: []string{"ES"},
|
2023-02-01 13:34:40 +00:00
|
|
|
Attributes: []template.HTMLAttr{
|
|
|
|
`autocomplete="country"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (form *contactForm) Parse(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
form.BusinessName.FillValue(r)
|
|
|
|
form.VATIN.FillValue(r)
|
|
|
|
form.TradeName.FillValue(r)
|
|
|
|
form.Phone.FillValue(r)
|
|
|
|
form.Email.FillValue(r)
|
|
|
|
form.Web.FillValue(r)
|
|
|
|
form.Address.FillValue(r)
|
|
|
|
form.City.FillValue(r)
|
|
|
|
form.Province.FillValue(r)
|
|
|
|
form.PostalCode.FillValue(r)
|
|
|
|
form.Country.FillValue(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (form *contactForm) Validate(ctx context.Context, conn *Conn) bool {
|
|
|
|
validator := newFormValidator()
|
2023-02-08 12:47:36 +00:00
|
|
|
|
|
|
|
country := ""
|
|
|
|
if validator.CheckValidSelectOption(form.Country, gettext("Selected country is not valid.", form.locale)) {
|
|
|
|
country = form.Country.Selected[0]
|
|
|
|
}
|
|
|
|
|
2023-02-01 13:34:40 +00:00
|
|
|
validator.CheckRequiredInput(form.BusinessName, gettext("Business name can not be empty.", form.locale))
|
|
|
|
if validator.CheckRequiredInput(form.VATIN, gettext("VAT number can not be empty.", form.locale)) {
|
2023-02-08 12:47:36 +00:00
|
|
|
validator.CheckValidVATINInput(form.VATIN, country, gettext("This value is not a valid VAT number.", form.locale))
|
2023-02-01 13:34:40 +00:00
|
|
|
}
|
|
|
|
if validator.CheckRequiredInput(form.Phone, gettext("Phone can not be empty.", form.locale)) {
|
2023-02-08 12:47:36 +00:00
|
|
|
validator.CheckValidPhoneInput(form.Phone, country, gettext("This value is not a valid phone number.", form.locale))
|
2023-02-01 13:34:40 +00:00
|
|
|
}
|
|
|
|
if validator.CheckRequiredInput(form.Email, gettext("Email can not be empty.", form.locale)) {
|
|
|
|
validator.CheckValidEmailInput(form.Email, gettext("This value is not a valid email. It should be like name@domain.com.", form.locale))
|
|
|
|
}
|
|
|
|
if form.Web.Val != "" {
|
2023-02-12 20:01:20 +00:00
|
|
|
validator.CheckValidURL(form.Web, gettext("This value is not a valid web address. It should be like https://domain.com/.", form.locale))
|
2023-02-01 13:34:40 +00:00
|
|
|
}
|
|
|
|
validator.CheckRequiredInput(form.Address, gettext("Address can not be empty.", form.locale))
|
|
|
|
validator.CheckRequiredInput(form.City, gettext("City can not be empty.", form.locale))
|
|
|
|
validator.CheckRequiredInput(form.Province, gettext("Province can not be empty.", form.locale))
|
|
|
|
if validator.CheckRequiredInput(form.PostalCode, gettext("Postal code can not be empty.", form.locale)) {
|
2023-02-08 12:47:36 +00:00
|
|
|
validator.CheckValidPostalCode(ctx, conn, form.PostalCode, country, gettext("This value is not a valid postal code.", form.locale))
|
2023-02-01 13:34:40 +00:00
|
|
|
}
|
|
|
|
return validator.AllOK()
|
2023-01-29 14:14:31 +00:00
|
|
|
}
|