From ca0298213e9b2c292b6eca7c0d1dc7b0847999fd Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Sat, 1 Apr 2023 16:07:26 +0200 Subject: [PATCH] Filter out nulls in tax array when viewing invoice This is for the unusual case of products without taxes, that PostgreSQL by default would generate an array with a single null value in it, but then pgx would not be able to set that null to the string variable. --- pkg/invoices.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/invoices.go b/pkg/invoices.go index c257a25..343ae74 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -336,7 +336,7 @@ func mustGetInvoice(ctx context.Context, conn *Conn, company *Company, slug stri if err := conn.QueryRow(ctx, "select array_agg(array[name, to_price(amount, $2)]) from invoice_tax_amount join tax using (tax_id) where invoice_id = $1", invoiceId, decimalDigits).Scan(&inv.Taxes); err != nil { panic(err) } - rows := conn.MustQuery(ctx, "select invoice_product.name, description, to_price(price, $2), (discount_rate * 100)::integer, quantity, to_price(subtotal, $2), to_price(total, $2), array_agg(array[tax_class.name, (tax_rate * 100)::integer::text]) from invoice_product join invoice_product_amount using (invoice_product_id) left join invoice_product_tax using (invoice_product_id) left join tax using (tax_id) left join tax_class using (tax_class_id) where invoice_id = $1 group by invoice_product.name, description, discount_rate, price, quantity, subtotal, total", invoiceId, decimalDigits) + rows := conn.MustQuery(ctx, "select invoice_product.name, description, to_price(price, $2), (discount_rate * 100)::integer, quantity, to_price(subtotal, $2), to_price(total, $2), array_agg(array[tax_class.name, (tax_rate * 100)::integer::text]) filter (where tax_rate is not null) from invoice_product join invoice_product_amount using (invoice_product_id) left join invoice_product_tax using (invoice_product_id) left join tax using (tax_id) left join tax_class using (tax_class_id) where invoice_id = $1 group by invoice_product.name, description, discount_rate, price, quantity, subtotal, total", invoiceId, decimalDigits) defer rows.Close() taxClasses := map[string]bool{} for rows.Next() {