Skip to content

Commit

Permalink
second edge case for #19
Browse files Browse the repository at this point in the history
  • Loading branch information
toudi committed Jan 14, 2024
1 parent 2954179 commit dbed687
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions internal/sei/api/session/interactive/interactive_download_pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func DownloadPDFFromAPI(
// httpSession := session.HTTPSession()
var seiRefNo string
var downloadAll = args.Invoice == "*"
var pdfExists bool

for _, invoice := range r.Invoices {
pdfExists = false
// * means to download all invoices.
seiRefNo = invoice.SEIReferenceNumber
if !downloadAll {
Expand All @@ -57,13 +59,17 @@ func DownloadPDFFromAPI(
continue
}
if _, err = os.Stat(path.Join(args.Output, seiRefNo+".pdf")); err == nil {
// PDF was already downloaded
if !downloadAll {
// PDF was already downloaded.
// make sure that if SaveXML was requested that we do not exit early
pdfExists = true
if !downloadAll && !args.SaveXml {
// this is only a single file download mode therefore
// nothing left to be done, we can safely return.
return nil
}
continue
if !args.SaveXml {
continue
}
}

var invoiceXMLReader io.Reader
Expand Down Expand Up @@ -102,17 +108,18 @@ func DownloadPDFFromAPI(
}
}

err = httpSession.DownloadPDFFromSourceXML(
fmt.Sprintf(pdf.DownloadInvoicePDF, seiRefNo),
seiRefNo+".xml",
invoiceXMLReader,
path.Join(args.Output, seiRefNo+".pdf"),
)

if err != nil {
return fmt.Errorf("unable to download PDF: %v", err)
if !pdfExists {
// dowload the PDF only if it wasn't already downloaded
err = httpSession.DownloadPDFFromSourceXML(
fmt.Sprintf(pdf.DownloadInvoicePDF, seiRefNo),
seiRefNo+".xml",
invoiceXMLReader,
path.Join(args.Output, seiRefNo+".pdf"),
)
if err != nil {
return fmt.Errorf("unable to download PDF: %v", err)
}
}

}

return nil
Expand Down

0 comments on commit dbed687

Please sign in to comment.