Handle case of no tag given for invoice

In that case, strings.Split() return an array with a single empty string
element, that does not pass the domain check for tag_name in the
database.

And an invoice with no tags would get an array of a single NULL in
array_agg, so i had to convert it to an empty string in order for it
to work as expected.
This commit is contained in:
jordi fita mas 2023-03-19 23:10:01 +01:00
parent 041017adc3
commit 356d0a0892
1 changed files with 7 additions and 3 deletions

View File

@ -56,7 +56,7 @@ func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company
, invoice_number , invoice_number
, contact.business_name , contact.business_name
, contact.slug , contact.slug
, array_agg(tag.name::text) , array_agg(coalesce(tag.name::text, ''))
, invoice.invoice_status , invoice.invoice_status
, isi18n.name , isi18n.name
, to_price(total, decimal_digits) , to_price(total, decimal_digits)
@ -583,7 +583,7 @@ func (form *invoiceForm) MustFillFromDatabase(ctx context.Context, conn *Conn, s
, invoice_date , invoice_date
, notes , notes
, payment_method_id , payment_method_id
, string_agg(tag.name, ', ') , string_agg(tag.name, ',')
from invoice from invoice
left join invoice_tag using (invoice_id) left join invoice_tag using (invoice_id)
left join tag using(tag_id) where slug = $1 left join tag using(tag_id) where slug = $1
@ -609,7 +609,11 @@ func mustGetTaxOptions(ctx context.Context, conn *Conn, company *Company) []*Sel
func (form *invoiceForm) SplitTags() []string { func (form *invoiceForm) SplitTags() []string {
reg := regexp.MustCompile("[^a-z0-9-]+") reg := regexp.MustCompile("[^a-z0-9-]+")
return strings.Split(reg.ReplaceAllString(form.Tags.Val, " "), " ") tags := strings.Split(reg.ReplaceAllString(form.Tags.Val, ","), ",")
if len(tags) == 1 && len(tags[0]) == 0 {
return []string{}
}
return tags
} }
type invoiceProductForm struct { type invoiceProductForm struct {