lib/app/stash-services.go

70 lines
2.0 KiB
Go
Raw Normal View History

2021-01-27 23:11:19 +00:00
package app
import (
"code.tnxs.net/taxnexus/lib/api/stash/stash_client/stash_pdf"
"code.tnxs.net/taxnexus/lib/api/stash/stash_models"
)
2021-01-28 01:58:08 +00:00
// StashPDFParams is a param type
2021-01-27 23:11:19 +00:00
type StashPDFParams struct {
2021-01-28 02:14:06 +00:00
Document *Document
Account *Account
ObjectType string
Principal *User
2021-01-27 23:11:19 +00:00
}
2021-01-28 01:58:08 +00:00
// StashPDF stores a PDF in the stash database
2021-01-28 02:16:58 +00:00
func StashPDF(params StashPDFParams) (*Document, error) {
2021-01-27 23:11:19 +00:00
sugar.Debugf("render.stashPDF: 📥")
var title string
var fileName string
var description string
var ref string
2021-01-28 02:14:06 +00:00
switch params.ObjectType {
2021-01-27 23:11:19 +00:00
case "account":
2021-01-28 02:14:06 +00:00
title = "Taxnexus Report for " + params.Account.Name
fileName = "Taxnexus Report for " + params.Account.Name + ".pdf"
2021-01-27 23:11:19 +00:00
description = "Account Report generated by render"
ref = "render.getAccounts"
case "tax_summary":
2021-01-28 02:14:06 +00:00
title = "Taxnexus Tax Report for " + params.Account.Name
fileName = params.Account.Name + " Tax Report.pdf"
2021-01-27 23:11:19 +00:00
description = "CDTFA Quarterly District Sales and Use Tax for "
ref = "render.getTaxes"
}
stashParams := stash_pdf.NewPostPdfsParamsWithTimeout(getTimeout)
stashParams.PDFRequest = &stash_models.PDFRequest{
Data: []*stash_models.NewPDF{
{
Description: description,
Filename: fileName,
2021-01-28 02:14:06 +00:00
HTML: params.Document.HTML,
ObjectType: params.Document.SagaType,
2021-01-28 02:16:58 +00:00
OwnerID: params.Principal.ID,
2021-01-28 02:14:06 +00:00
ParentID: params.Document.ParentID,
2021-01-27 23:11:19 +00:00
Ref: ref,
Title: title,
},
},
}
2021-01-28 02:14:06 +00:00
response, err := stashClient.StashPdf.PostPdfs(stashParams, params.Principal.Auth)
2021-01-27 23:11:19 +00:00
if err != nil {
sugar.Errorf("render.stashPDF: 💣 ⛔ post PDF to stash error %w", err)
return nil, err
}
var newObj *stash_models.Document
for _, itm := range response.Payload.Data { // single iteration execution
newObj = itm
}
sugar.Debugf("render.stashPDF: 👍 📤")
return &Document{
ID: newObj.ID,
2021-01-28 02:14:06 +00:00
Filename: params.Account.Name + fileName,
SagaType: params.Document.SagaType,
ParentID: params.Document.ParentID,
Title: title + params.Account.Name,
2021-01-27 23:11:19 +00:00
URI: newObj.URI,
}, nil
}