From 356d0a08921ed9ba4b26fc1eddadb71f7f6f10b2 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Sun, 19 Mar 2023 23:10:01 +0100 Subject: [PATCH] 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. --- pkg/invoices.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/invoices.go b/pkg/invoices.go index ed95288..6878312 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -56,7 +56,7 @@ func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company , invoice_number , contact.business_name , contact.slug - , array_agg(tag.name::text) + , array_agg(coalesce(tag.name::text, '')) , invoice.invoice_status , isi18n.name , to_price(total, decimal_digits) @@ -583,7 +583,7 @@ func (form *invoiceForm) MustFillFromDatabase(ctx context.Context, conn *Conn, s , invoice_date , notes , payment_method_id - , string_agg(tag.name, ', ') + , string_agg(tag.name, ',') from invoice left join invoice_tag using (invoice_id) 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 { 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 {