Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
comments 4 dayz
Browse files Browse the repository at this point in the history
  • Loading branch information
j-Rosenthal committed Aug 29, 2023
1 parent ce72bd3 commit 886151f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
44 changes: 42 additions & 2 deletions pkg/trdm/get_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func NewGetTable(physicalName string, securityToken string, privateKey *rsa.Priv
},
}
}

// Fetch Transportation Accounting Codes from DB and return the list of records if the updated_at field is < the returned LastTableUpdate updated time.
// Ex:
//
// LastTableUpdate : 2023-08-30 15:24:13.19931
// updated_at: 2023-08-29 15:24:13.19931
//
// Because updated_at is before LastTableUpdate the DB will return records that match this case.
//
// returns []models.TransportationAccountingCode, error
func FetchTACRecordsByTime(appcontext appcontext.AppContext, time string) ([]models.TransportationAccountingCode, error) {
var tacCodes []models.TransportationAccountingCode
err := appcontext.DB().Select("*").Where("updated_at < $1", time).All(&tacCodes)
Expand All @@ -131,17 +141,33 @@ func FetchTACRecordsByTime(appcontext appcontext.AppContext, time string) ([]mod
return tacCodes, nil
}

// Fetch Line Of Accounting records from DB and return the list of records if the updated_at field is < the returned LastTableUpdate updated time.
// Ex:
//
// LastTableUpdate : 2023-08-30 15:24:13.19931
// updated_at: 2023-08-29 15:24:13.19931
//
// Because updated_at is before LastTableUpdate the DB will return records that match this case.
//
// returns []models.LineOfAccounting, error
func FetchLOARecordsByTime(appcontext appcontext.AppContext, time string) ([]models.LineOfAccounting, error) {
var loa []models.LineOfAccounting
err := appcontext.DB().Select("*").Where("updated_at < $1", time).All(&loa)

if err != nil {
return loa, errors.Wrap(err, "Fetch line items query failed")
}

return loa, nil
}

// Determines if SOAP call is needed to be made
// If the DB does not return any records we do not need make a call to GetTable and update our local mapping
// - appCtx: Application Context
// - physicalName: Table Name (Will be either TAC or LOA)
// - lastUpdate: Returned date time from LastTableUpdate Soap Request
//
// returns error
func (d *GetTableRequestElement) GetTable(appCtx appcontext.AppContext, physicalName string, lastUpdate string) error {

switch physicalName {
case lineOfAccounting:
loaRecords, loaFetchErr := FetchLOARecordsByTime(appCtx, lastUpdate)
Expand Down Expand Up @@ -170,6 +196,12 @@ func (d *GetTableRequestElement) GetTable(appCtx appcontext.AppContext, physical
return nil
}

// Sets up SOAP parameters. Signed SOAP header and SOAP body
// Calls getTableSoap call within Exponential backoff function. If soap call errors out, it will wait 1 hour before making another call and then at maximum 5 calls after the second failure.
//
// appCtx - application context
// physicalName - table name (TAC or LOA)
// returns error
func setupSoapCall(d *GetTableRequestElement, appCtx appcontext.AppContext, physicalName string) error {
gosoap.SetCustomEnvelope("soapenv", map[string]string{
"xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
Expand Down Expand Up @@ -209,6 +241,9 @@ func setupSoapCall(d *GetTableRequestElement, appCtx appcontext.AppContext, phys
return nil
}

// Makes SOAP call to GetTable webservice and unmarshalls response.
//
// returns error
func getTableSoapCall(d *GetTableRequestElement, params gosoap.Params, appCtx appcontext.AppContext, physicalName string) error {
response, err := d.soapClient.Call("ProcessRequest", params)
if err != nil {
Expand All @@ -231,6 +266,9 @@ func getTableSoapCall(d *GetTableRequestElement, params gosoap.Params, appCtx ap
return nil
}

// Parses pipedelimited file attachment from GetTable webservice and saves records to database
//
// returns error
func parseGetTableResponse(appcontext appcontext.AppContext, response *gosoap.Response, physicalName string) error {
reader := bytes.NewReader(response.Payload)
switch physicalName {
Expand Down Expand Up @@ -258,6 +296,7 @@ func parseGetTableResponse(appcontext appcontext.AppContext, response *gosoap.Re
return nil
}

// Saves TAC Code slice to DB and updates records
func saveTacCodes(appcontext appcontext.AppContext, tacCodes []models.TransportationAccountingCode) error {
saveErr := appcontext.DB().Update(tacCodes)
if saveErr != nil {
Expand All @@ -266,6 +305,7 @@ func saveTacCodes(appcontext appcontext.AppContext, tacCodes []models.Transporta
return nil
}

// Saves LOA Code slice to DB and updates records
func saveLoaCodes(appcontext appcontext.AppContext, loa []models.LineOfAccounting) error {
saveErr := appcontext.DB().Update(loa)
if saveErr != nil {
Expand Down
19 changes: 15 additions & 4 deletions pkg/trdm/last_table_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const successfulStatusCode = "Successful"
type GetLastTableUpdater interface {
GetLastTableUpdate(appCtx appcontext.AppContext, physicalName string) error
}

type GetLastTableUpdateRequestElement struct {
XMLName string `xml:"ret:getLastTableUpdateReqElement"`
PhysicalName string `xml:"ret:physicalName"`
Expand Down Expand Up @@ -77,6 +78,11 @@ func NewTRDMGetLastTableUpdate(physicalName string, securityToken string, privat

}

// Sets up GetLastTableUpdate Soap Request
// - appCtx - Application Context.
// - physicalName - TableName
// - Generates custom soap envelope, soap body, soap header.
// - Returns Error
func (d *GetLastTableUpdateRequestElement) GetLastTableUpdate(appCtx appcontext.AppContext, physicalName string) error {

gosoap.SetCustomEnvelope("soapenv", map[string]string{
Expand All @@ -99,14 +105,19 @@ func (d *GetLastTableUpdateRequestElement) GetLastTableUpdate(appCtx appcontext.
"header": signedHeader,
"body": marshaledBody,
}
err := lastTableUpdateSoapCall(d, newParams, appCtx, physicalName)
err := lastTableUpdateSoapCall(d, newParams, appCtx)
if err != nil {
return fmt.Errorf("Request error: %s", err.Error())
}
return nil
}

func lastTableUpdateSoapCall(d *GetLastTableUpdateRequestElement, params gosoap.Params, appCtx appcontext.AppContext, physicalName string) error {
// Makes Soap Request for Last Table Update. If successful call GetTable to update TRDM table.
// - *GetLastTableUpdateRequestElement - request elements
// - params - Created SOAP element (Header and Body)
// - appCtx - Application context
// - returns error
func lastTableUpdateSoapCall(d *GetLastTableUpdateRequestElement, params gosoap.Params, appCtx appcontext.AppContext) error {
res, err := d.soapClient.Call("ProcessRequest", params)
if err != nil {
return fmt.Errorf("call error: %s", err.Error())
Expand All @@ -120,8 +131,8 @@ func lastTableUpdateSoapCall(d *GetLastTableUpdateRequestElement, params gosoap.
}

if r.Status.StatusCode == successfulStatusCode {
getTable := NewGetTable(physicalName, d.securityToken, d.privateKey, d.soapClient)
getTableErr := getTable.GetTable(appCtx, physicalName, r.LastUpdate)
getTable := NewGetTable(d.PhysicalName, d.securityToken, d.privateKey, d.soapClient)
getTableErr := getTable.GetTable(appCtx, d.PhysicalName, r.LastUpdate)
if getTableErr != nil {
return fmt.Errorf("getTable error: %s", getTableErr.Error())
}
Expand Down

0 comments on commit 886151f

Please sign in to comment.