Skip to content

Commit cb7b20d

Browse files
committed
fix: fix error when downloading invoice
1 parent 301625e commit cb7b20d

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

api/pkg/handlers/user_handler.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handlers
22

33
import (
44
"fmt"
5+
"io"
56

67
"github.com/NdoleStudio/httpsms/pkg/requests"
78
"github.com/NdoleStudio/httpsms/pkg/validators"
@@ -333,7 +334,7 @@ func (h *UserHandler) subscriptionInvoice(c fiber.Ctx) error {
333334
return h.responseUnprocessableEntity(c, errors, "validation errors while generating payment invoice")
334335
}
335336

336-
data, err := h.service.GenerateReceipt(ctx, request.UserInvoiceGenerateParams(h.userIDFomContext(c)))
337+
reader, err := h.service.GenerateReceipt(ctx, request.UserInvoiceGenerateParams(h.userIDFomContext(c)))
337338
if err != nil {
338339
msg := fmt.Sprintf("cannot generate receipt for invoice ID [%s] and user [%s]", request.SubscriptionInvoiceID, h.userFromContext(c))
339340
ctxLogger.Error(stacktrace.Propagate(err, msg))
@@ -343,5 +344,12 @@ func (h *UserHandler) subscriptionInvoice(c fiber.Ctx) error {
343344
c.Set(fiber.HeaderContentType, "application/pdf")
344345
c.Set(fiber.HeaderContentDisposition, fmt.Sprintf("attachment; filename=\"httpsms.com - %s.pdf\"", request.SubscriptionInvoiceID))
345346

346-
return c.SendStream(data)
347+
data, err := io.ReadAll(reader)
348+
if err != nil {
349+
msg := fmt.Sprintf("cannot read invoice data with ID [%s] for user with ID [%s]", request.SubscriptionInvoiceID, h.userIDFomContext(c))
350+
ctxLogger.Error(stacktrace.Propagate(err, msg))
351+
return h.responseInternalServerError(c)
352+
}
353+
354+
return c.Send(data)
347355
}

0 commit comments

Comments
 (0)