render/plex cleanup

v0.1.37 v0.1.37
Vernon Keenan 2021-01-27 17:58:08 -08:00
parent 8c67d3a0c4
commit 1c63381dba
11 changed files with 117 additions and 31 deletions

View File

@ -4,40 +4,42 @@ import (
"fmt" "fmt"
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/accounts" "code.tnxs.net/taxnexus/lib/api/crm/crm_client/accounts"
"code.tnxs.net/taxnexus/lib/api/geo/geo_client/coordinate"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
) )
// GetAccount is first class retrieval function // GetAccount is first class retrieval function
func GetAccount(key string, principal *User) Account { func GetAccount(key string, principal *User) *Account {
if key == "" { if key == "" {
return Account{} return nil
} }
a, ok := accountCache.get(key) a, ok := accountCache.get(key)
if ok { if ok {
return *a return a
} }
acct, err := GetAccountByID(key, principal) acct, err := GetAccountByID(key, principal)
if err != nil { if err != nil {
return Account{} return nil
} }
return acct return acct
} }
// GetAccountByID is first class retrieval function // GetAccountByID is first class retrieval function
func GetAccountByID(recordID string, principal *User) (Account, error) { func GetAccountByID(recordID string, principal *User) (*Account, error) {
sugar.Debug("app.GetAccountByID: 📥") sugar.Debug("app.GetAccountByID: 📥")
if recordID == "" { if recordID == "" {
return Account{}, fmt.Errorf("app.getAccountByID: 💣 ⛔ key is blank") return nil, fmt.Errorf("app.getAccountByID: 💣 ⛔ key is blank")
} }
obj, ok := accountCache.get(recordID) obj, ok := accountCache.get(recordID)
if ok { if ok {
sugar.Debug("app.getAccountByID: 👍 🎯 📤") sugar.Debug("app.getAccountByID: 👍 🎯 📤")
return *obj, nil return obj, nil
} }
crmParams := accounts.NewGetAccountsParamsWithTimeout(getTimeout) crmParams := accounts.NewGetAccountsParamsWithTimeout(getTimeout)
crmParams.AccountID = &recordID crmParams.AccountID = &recordID
response, err := crmClient.Accounts.GetAccounts(crmParams, principal.Auth) response, err := crmClient.Accounts.GetAccounts(crmParams, principal.Auth)
if err != nil { if err != nil {
return Account{}, err return nil, err
} }
var newObj *Account var newObj *Account
for _, itm := range response.Payload.Data { // single iteration execution for _, itm := range response.Payload.Data { // single iteration execution
@ -45,5 +47,44 @@ func GetAccountByID(recordID string, principal *User) (Account, error) {
} }
accountCache.put(recordID, newObj) accountCache.put(recordID, newObj)
sugar.Debug("app.getAccountByID: 👍 🆕 📤") sugar.Debug("app.getAccountByID: 👍 🆕 📤")
return *newObj, nil return newObj, nil
}
// GetCoordinate is a first class retrieval function
func (obj *Account) GetCoordinate(principal *User) *Coordinate {
sugar.Debug("app.Account.getCoordinate: 📥")
if obj.CoordinateID != "" { // if CoordinateID is set, then just get it
geoParams := coordinate.NewGetCoordinatesParamsWithTimeout(getTimeout)
geoParams.CoordinateID = &obj.CoordinateID
response, err := geoClient.Coordinate.GetCoordinates(geoParams, principal.Auth)
if err != nil {
return nil
}
var obj *Coordinate
for _, itm := range response.Payload.Data { // single iteration execution
obj = UnMarshalCoordinate(itm, principal)
}
return obj
} // else get it via addresses, shipping #1, Business #2
if obj.ShippingAddress.ToString() == "" && obj.BillingAddress.ToString() == "" {
sugar.Errorf("app.Account.getCoordinate: 💣 ⛔ billing and shipping address both blank")
return nil
}
theAddress := obj.ShippingAddress.ToString()
if theAddress == "" {
theAddress = obj.BillingAddress.ToString()
}
geoParams := coordinate.NewGetCoordinatesParamsWithTimeout(getTimeout)
geoParams.Address = &theAddress
response, err := geoClient.Coordinate.GetCoordinates(geoParams, principal.Auth)
if err != nil {
sugar.Error(err)
return nil
}
var swag *geo_models.Coordinate
for _, itm := range response.Payload.Data { // single iteration execution
swag = itm
}
sugar.Debug("app.Account.getCoordinate: 👍 📤")
return UnMarshalCoordinate(swag, principal)
} }

View File

@ -14,18 +14,23 @@ type AccountActivityWrapper struct {
AdministrativeLevel string AdministrativeLevel string
BillingAddress Address BillingAddress Address
BillingContactID string BillingContactID string
BillingContact Contact
BillingPreference string BillingPreference string
BusinessAddress Address BusinessAddress Address
CannabisCustomer bool CannabisCustomer bool
CompanyID string CompanyID string
CoordinateID string CoordinateID string
Coordinate Coordinate
CustomerID string CustomerID string
CustomerPriority string CustomerPriority string
DBA string DBA string
DefaultAddress Address DefaultAddress Address
DefaultBackendID string DefaultBackendID string
DefaultBackend Backend
DefaultDeliveryContactID string DefaultDeliveryContactID string
DefaultDeliveryContact string
DefaultEndUserID string DefaultEndUserID string
DefaultEndUser Contact
Description string Description string
EIN string EIN string
Email string Email string
@ -35,6 +40,7 @@ type AccountActivityWrapper struct {
MSPCustomer bool MSPCustomer bool
Name string Name string
OrderContactID string OrderContactID string
OrderContact Contact
OrderEmail string OrderEmail string
OwnerID string OwnerID string
ParentID string ParentID string
@ -45,6 +51,7 @@ type AccountActivityWrapper struct {
Ref string Ref string
ShippingAddress Address ShippingAddress Address
ShippingContactID string ShippingContactID string
ShippingContact Contact
Site string Site string
TelecomCustomer bool TelecomCustomer bool
TenantID string TenantID string

View File

@ -22,7 +22,7 @@ func ReMarshalCompany(p interface{}) crm_models.Company {
} }
// UnMarshalCompany decodes swagger to first class object // UnMarshalCompany decodes swagger to first class object
func UnMarshalCompany(s *crm_models.Company) *Company { func UnMarshalCompany(s *crm_models.Company, principal *User) *Company {
if s.ID == "" { if s.ID == "" {
s.ID = uuid.New().String() s.ID = uuid.New().String()
} }
@ -31,6 +31,7 @@ func UnMarshalCompany(s *crm_models.Company) *Company {
dateClosed, e2 := time.Parse(dateTimeFormat, s.DateClosed) dateClosed, e2 := time.Parse(dateTimeFormat, s.DateClosed)
return &Company{ return &Company{
ID: s.ID, ID: s.ID,
BillingContact: GetContact(s.BillingContactID, principal),
AccountID: s.AccountID, AccountID: s.AccountID,
AccountNumberPrefix: s.AccountNumberPrefix, AccountNumberPrefix: s.AccountNumberPrefix,
BillingAddress: UnMarshalCrmAddress(s.BillingAddress), BillingAddress: UnMarshalCrmAddress(s.BillingAddress),
@ -45,6 +46,7 @@ func UnMarshalCompany(s *crm_models.Company) *Company {
ColorPrimary: s.ColorPrimary, ColorPrimary: s.ColorPrimary,
CreatedByID: s.CreatedByID, CreatedByID: s.CreatedByID,
CustomerSuccessID: s.CustomerSuccessID, CustomerSuccessID: s.CustomerSuccessID,
CustomerSuccess: GetContact(s.CustomerSuccessID, principal),
DefaultAddress: UnMarshalCrmAddress(s.DefaultAddress), DefaultAddress: UnMarshalCrmAddress(s.DefaultAddress),
DefaultCompany: s.DefaultCompany, DefaultCompany: s.DefaultCompany,
FontBody: s.FontBody, FontBody: s.FontBody,
@ -59,9 +61,11 @@ func UnMarshalCompany(s *crm_models.Company) *Company {
Logo: s.Logo, Logo: s.Logo,
Name: s.Name, Name: s.Name,
OwnerID: s.OwnerID, OwnerID: s.OwnerID,
Preparer: GetContact(s.PreparerID, principal),
PreparerID: s.PreparerID, PreparerID: s.PreparerID,
PricebookID: s.PricebookID, PricebookID: s.PricebookID,
TenantID: s.TenantID, TenantID: s.TenantID,
UserTechLead: GetContact(s.UserTechLeadID, principal),
UserTechLeadID: s.UserTechLeadID, UserTechLeadID: s.UserTechLeadID,
CreatedDate: sql.NullTime{ CreatedDate: sql.NullTime{
Time: createdDate, Time: createdDate,

View File

@ -46,12 +46,14 @@ type CompanyActivityWrapper struct {
// Company is a DB struct // Company is a DB struct
type Company struct { type Company struct {
ID string ID string
Account *Account
AccountID string AccountID string
AccountNumberPrefix string AccountNumberPrefix string
AdvancePeriodID string AdvancePeriodID string
BillingAddress *Address BillingAddress *Address
BillingAdvice string BillingAdvice string
BillingContactID string BillingContactID string
BillingContact *Contact
BillingEmail string BillingEmail string
BillingPhone string BillingPhone string
BillingWebsite string BillingWebsite string
@ -65,6 +67,7 @@ type Company struct {
CurrentPeriodID string CurrentPeriodID string
CurrentPeriodStatus string CurrentPeriodStatus string
CustomerSuccessID string CustomerSuccessID string
CustomerSuccess *Contact
DateClosed sql.NullTime DateClosed sql.NullTime
DefaultAddress *Address DefaultAddress *Address
DefaultCompany bool DefaultCompany bool
@ -81,8 +84,10 @@ type Company struct {
Logo string Logo string
Name string Name string
OwnerID string OwnerID string
Preparer *Contact
PreparerID string PreparerID string
PricebookID string PricebookID string
TenantID string TenantID string
UserTechLead *Contact
UserTechLeadID string UserTechLeadID string
} }

View File

@ -7,19 +7,19 @@ import (
) )
// GetContact is a first class object retrieval function // GetContact is a first class object retrieval function
func GetContact(id string, principal *User) Contact { func GetContact(id string, principal *User) *Contact {
if id == "" { if id == "" {
return Contact{} return nil
} }
c, ok := contactCache.get(id) c, ok := contactCache.get(id)
if ok { if ok {
return *c return c
} }
c, err := GetContactByID(id, principal) c, err := GetContactByID(id, principal)
if err != nil { if err != nil {
return Contact{} return nil
} }
return *c return c
} }
// GetContactByID is a first class object retrieval function // GetContactByID is a first class object retrieval function

View File

@ -7,7 +7,8 @@ import (
"code.tnxs.net/taxnexus/lib/api/geo/geo_models" "code.tnxs.net/taxnexus/lib/api/geo/geo_models"
) )
func UnMarshalSwaggerCoordinate(s *geo_models.Coordinate, principal *User) *Coordinate { // UnMarshalCoordinate converts swagger to enriched first class object
func UnMarshalCoordinate(s *geo_models.Coordinate, principal *User) *Coordinate {
taxTypes := []*TaxType{} taxTypes := []*TaxType{}
for _, itm := range s.TaxTypes { for _, itm := range s.TaxTypes {
if itm.ID != "" { if itm.ID != "" {

View File

@ -33,7 +33,6 @@ which is convenient for calls within template.
I didn't feel it was worth to publish a library just for this piece I didn't feel it was worth to publish a library just for this piece
of code, hence the snippet. Feel free to reuse as you wish. of code, hence the snippet. Feel free to reuse as you wish.
const rPattern = "#,###.##"
*/ */
import ( import (
@ -41,6 +40,8 @@ import (
"strconv" "strconv"
) )
const rPattern = "#,###.##"
var renderFloatPrecisionMultipliers = [10]float64{ var renderFloatPrecisionMultipliers = [10]float64{
1, 1,
10, 10,

View File

@ -29,6 +29,8 @@ var apiUsers map[string]User
const getTimeout = 6 * time.Minute const getTimeout = 6 * time.Minute
const postTimeout = 6 * time.Minute const postTimeout = 6 * time.Minute
const constNotFound = "not found" const constNotFound = "not found"
const percentFactor = 100
const dateFormatDB = "2006-01-02"
const dateFormat = "2006-01-02" const dateFormat = "2006-01-02"
const dateTimeFormat = "2006-01-02T15:04:05-0800" const dateTimeFormat = "2006-01-02T15:04:05-0800"
const dateTimeFormatAlt = "2006-01-02 15:04:05" const dateTimeFormatAlt = "2006-01-02 15:04:05"

View File

@ -5,6 +5,7 @@ import (
"code.tnxs.net/taxnexus/lib/api/stash/stash_models" "code.tnxs.net/taxnexus/lib/api/stash/stash_models"
) )
// StashPDFParams is a param type
type StashPDFParams struct { type StashPDFParams struct {
document *Document document *Document
account *Account account *Account
@ -12,6 +13,7 @@ type StashPDFParams struct {
principal *User principal *User
} }
// StashPDF stores a PDF in the stash database
func StashPDF(params StashPDFParams) (*Document, error) { func StashPDF(params StashPDFParams) (*Document, error) {
sugar.Debugf("render.stashPDF: 📥") sugar.Debugf("render.stashPDF: 📥")
var title string var title string
@ -31,12 +33,8 @@ func StashPDF(params StashPDFParams) (*Document, error) {
ref = "render.getTaxes" ref = "render.getTaxes"
} }
todoString := "todo" // todo #5
stashParams := stash_pdf.NewPostPdfsParamsWithTimeout(getTimeout) stashParams := stash_pdf.NewPostPdfsParamsWithTimeout(getTimeout)
stashParams.PDFRequest = &stash_models.PDFRequest{ stashParams.PDFRequest = &stash_models.PDFRequest{
Meta: &stash_models.RequestMeta{
TaxnexusAccount: &todoString,
},
Data: []*stash_models.NewPDF{ Data: []*stash_models.NewPDF{
{ {
Description: description, Description: description,

View File

@ -2,6 +2,7 @@ package app
import ( import (
"database/sql" "database/sql"
"fmt"
"time" "time"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models" "code.tnxs.net/taxnexus/lib/api/geo/geo_models"
@ -10,26 +11,49 @@ import (
// UnMarshalTaxType decodes swagger to a first class object // UnMarshalTaxType decodes swagger to a first class object
func UnMarshalTaxType(s *geo_models.TaxType) *TaxType { func UnMarshalTaxType(s *geo_models.TaxType) *TaxType {
createdDate, _ := time.Parse(dateTimeFormat, s.CreatedDate)
lastModifiedDate, _ := time.Parse(dateTimeFormat, s.LastModifiedDate)
effectiveDate, _ := time.Parse(dateFormatDB, s.EffectiveDate)
endDate, _ := time.Parse(dateFormatDB, s.EndDate)
formatted := TaxTypeFormatted{
CreatedDate: createdDate.Format(dateTimeFormat),
EffectiveDate: effectiveDate.Format(dateFormat),
EndDate: endDate.Format(dateFormat),
Fractional: fmt.Sprintf("%v", s.Fractional),
InterestRate: RenderFloat(rPattern, s.InterestRate*percentFactor) + "%",
IsMedicinal: fmt.Sprintf("%v", s.IsMedicinal),
IsRecreational: fmt.Sprintf("%v", s.IsRecreational),
LastModifiedDate: lastModifiedDate.Format(dateTimeFormat),
MarkupRate: RenderFloat(rPattern, (s.MarkupRate-1)*percentFactor) + "%",
PassThrough: fmt.Sprintf("%v", s.PassThrough),
PenaltyDays: fmt.Sprintf("%v", s.PenaltyDays),
PenaltyRate: RenderFloat(rPattern, s.PenaltyRate*percentFactor) + "%",
Rate: RenderFloat(rPattern, s.Rate*percentFactor) + "%",
}
switch {
case s.Category == "cannabis-ag":
formatted.Rate = "$" + RenderFloat(rPattern, s.Rate) + " / " + s.Units
}
if s.ID == "" { if s.ID == "" {
s.ID = uuid.New().String() s.ID = uuid.New().String()
} }
dateTime, dateErr := time.Parse(dateTimeFormat, s.CreatedDate) dateTime, dateErr := time.Parse(dateTimeFormat, s.CreatedDate)
createdDate := sql.NullTime{ createdDateSQL := sql.NullTime{
Time: dateTime, Time: dateTime,
Valid: dateErr == nil, Valid: dateErr == nil,
} }
dateTime, dateErr = time.Parse(dateTimeFormat, s.LastModifiedDate) dateTime, dateErr = time.Parse(dateTimeFormat, s.LastModifiedDate)
modifiedDate := sql.NullTime{ modifiedDateSQL := sql.NullTime{
Time: dateTime, Time: dateTime,
Valid: dateErr == nil, Valid: dateErr == nil,
} }
date, dateErr := time.Parse(dateFormat, s.EffectiveDate) date, dateErr := time.Parse(dateFormat, s.EffectiveDate)
effectiveDate := sql.NullTime{ effectiveDateSQL := sql.NullTime{
Time: date, Time: date,
Valid: dateErr == nil, Valid: dateErr == nil,
} }
date, dateErr = time.Parse(dateFormat, s.EndDate) date, dateErr = time.Parse(dateFormat, s.EndDate)
endDate := sql.NullTime{ endDateSQL := sql.NullTime{
Time: date, Time: date,
Valid: dateErr == nil, Valid: dateErr == nil,
} }
@ -46,10 +70,10 @@ func UnMarshalTaxType(s *geo_models.TaxType) *TaxType {
CompanyID: s.CompanyID, CompanyID: s.CompanyID,
ContactID: s.ContactID, ContactID: s.ContactID,
CreatedByID: s.CreatedByID, CreatedByID: s.CreatedByID,
CreatedDate: createdDate, CreatedDate: createdDateSQL,
Description: s.Description, Description: s.Description,
EffectiveDate: effectiveDate, EffectiveDate: effectiveDateSQL,
EndDate: endDate, EndDate: endDateSQL,
EnrollmentStatus: s.EnrollmentStatus, EnrollmentStatus: s.EnrollmentStatus,
FilingCity: s.FilingCity, FilingCity: s.FilingCity,
FilingCountry: s.FilingCountry, FilingCountry: s.FilingCountry,
@ -58,6 +82,7 @@ func UnMarshalTaxType(s *geo_models.TaxType) *TaxType {
FilingPostalcode: s.FilingPostalCode, FilingPostalcode: s.FilingPostalCode,
FilingState: s.FilingState, FilingState: s.FilingState,
FilingStreet: s.FilingStreet, FilingStreet: s.FilingStreet,
Formatted: formatted,
Fractional: s.Fractional, Fractional: s.Fractional,
Frequency: s.Frequency, Frequency: s.Frequency,
GeocodeString: s.GeocodeString, GeocodeString: s.GeocodeString,
@ -65,7 +90,7 @@ func UnMarshalTaxType(s *geo_models.TaxType) *TaxType {
IsMedicinal: s.IsMedicinal, IsMedicinal: s.IsMedicinal,
IsRecreational: s.IsRecreational, IsRecreational: s.IsRecreational,
LastModifiedByID: s.LastModifiedByID, LastModifiedByID: s.LastModifiedByID,
LastModifiedDate: modifiedDate, LastModifiedDate: modifiedDateSQL,
MarkupRate: s.MarkupRate, MarkupRate: s.MarkupRate,
Name: s.Name, Name: s.Name,
OwnerID: s.OwnerID, OwnerID: s.OwnerID,

View File

@ -42,7 +42,7 @@ type TaxType struct {
FilingPostalcode string FilingPostalcode string
FilingState string FilingState string
FilingStreet string FilingStreet string
Formatted taxTypeFormatted Formatted TaxTypeFormatted
Fractional bool Fractional bool
Frequency string Frequency string
GeocodeString string GeocodeString string
@ -70,7 +70,9 @@ type TaxType struct {
UnitBase float64 UnitBase float64
Units string Units string
} }
type taxTypeFormatted struct {
// TaxTypeFormatted offers formatted versions of numerical values
type TaxTypeFormatted struct {
CreatedDate string CreatedDate string
EffectiveDate string EffectiveDate string
EndDate string EndDate string