Skip to content

Commit

Permalink
Merge pull request moov-io#383 from moov-io/Add-Addenda02.TerminalSta…
Browse files Browse the repository at this point in the history
…te-validation

Add Addenda02 Terminal State Validation
  • Loading branch information
bkmoovio authored Nov 27, 2018
2 parents 6fee36a + b22855c commit 5c2ba64
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
ADDITIONS

- Support ADV, TRC, TRX, XCK StandardEntryClassCode (SEC types)
- Transaction Codes match Service Class Codes
- Add Addenda02.TerminalState validation for BatchPOS and BatchSHR (POS and SEC Standard Entry Class Codes)

## v0.4.0 (Released 2018-11-06)

Expand Down
12 changes: 6 additions & 6 deletions batchMTE.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package ach

import (
"fmt"

"github.com/moov-io/ach/internal/usabbrev"
)

Expand Down Expand Up @@ -43,11 +42,6 @@ func (batch *BatchMTE) Validate() error {
msg := fmt.Sprintf(msgBatchAmountNonZero, entry.Amount, "MTE")
return &BatchError{BatchNumber: batch.Header.BatchNumber, FieldName: "Amount", Msg: msg}
}

if !usabbrev.Valid(entry.Addenda02.TerminalState) {
msg := fmt.Sprintf("%q is not a valid US state or territory", entry.Addenda02.TerminalState)
return &BatchError{BatchNumber: batch.Header.BatchNumber, FieldName: "TerminalState", Msg: msg}
}
// Verify the TransactionCode is valid for a ServiceClassCode
if err := batch.ValidTranCodeForServiceClassCode(entry); err != nil {
return err
Expand All @@ -56,6 +50,12 @@ func (batch *BatchMTE) Validate() error {
if err := batch.addendaFieldInclusion(entry); err != nil {
return err
}
if entry.Category == CategoryForward {
if !usabbrev.Valid(entry.Addenda02.TerminalState) {
msg := fmt.Sprintf("%q is not a valid US state or territory", entry.Addenda02.TerminalState)
return &BatchError{BatchNumber: batch.Header.BatchNumber, FieldName: "TerminalState", Msg: msg}
}
}
}
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions batchPOS.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ach

import (
"fmt"
"github.com/moov-io/ach/internal/usabbrev"
)

// BatchPOS holds the BatchHeader and BatchControl and all EntryDetail for POS Entries.
Expand Down Expand Up @@ -73,6 +74,12 @@ func (batch *BatchPOS) Validate() error {
if err := batch.addendaFieldInclusion(entry); err != nil {
return err
}
if entry.Category == CategoryForward {
if !usabbrev.Valid(entry.Addenda02.TerminalState) {
msg := fmt.Sprintf("%q is not a valid US state or territory", entry.Addenda02.TerminalState)
return &BatchError{BatchNumber: batch.Header.BatchNumber, FieldName: "TerminalState", Msg: msg}
}
}
}
return nil
}
Expand Down
16 changes: 10 additions & 6 deletions batchPOS_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func BenchmarkBatchPOSAddendaCount(b *testing.B) {
func testBatchPOSAddendaCountZero(t testing.TB) {
mockBatch := NewBatchPOS(mockBatchPOSHeader())
mockBatch.AddEntry(mockPOSEntryDetail())
mockAddenda02 := mockAddenda02()
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02
mockBatch.Entries[0].AddendaRecordIndicator = 1
if err := mockBatch.Create(); err != nil {
if e, ok := err.(*BatchError); ok {
if e.FieldName != "AddendaCount" {
Expand Down Expand Up @@ -548,16 +551,17 @@ func TestBatchPOSCategoryReturnAddenda99(t *testing.T) {
}
}

// TestBatchPOSCategoryReturnValid validates CategoryReturn does not return an error if Addenda99 is not defined
func TestBatchPOSCategoryReturnValid(t *testing.T) {
// TestBatchPOSTerminalState validates TerminalState returns an error if invalid from usabbrev
func TestBatchPOSTerminalState(t *testing.T) {
mockBatch := NewBatchPOS(mockBatchPOSHeader())
mockBatch.AddEntry(mockPOSEntryDetail())
mockBatch.GetEntries()[0].Addenda99 = mockAddenda99()
mockBatch.GetEntries()[0].Category = CategoryReturn
mockBatch.GetEntries()[0].AddendaRecordIndicator = 1
mockAddenda02 := mockAddenda02()
mockAddenda02.TerminalState = "YY"
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02
mockBatch.Entries[0].AddendaRecordIndicator = 1
if err := mockBatch.Create(); err != nil {
if e, ok := err.(*BatchError); ok {
if e.FieldName != "Addenda99" {
if e.FieldName != "TerminalState" {
t.Errorf("%T: %s", err, err)
}
} else {
Expand Down
7 changes: 7 additions & 0 deletions batchSHR.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ach

import (
"fmt"
"github.com/moov-io/ach/internal/usabbrev"
)

// BatchSHR holds the BatchHeader and BatchControl and all EntryDetail for SHR Entries.
Expand Down Expand Up @@ -77,6 +78,12 @@ func (batch *BatchSHR) Validate() error {
if err := batch.addendaFieldInclusion(entry); err != nil {
return err
}
if entry.Category == CategoryForward {
if !usabbrev.Valid(entry.Addenda02.TerminalState) {
msg := fmt.Sprintf("%q is not a valid US state or territory", entry.Addenda02.TerminalState)
return &BatchError{BatchNumber: batch.Header.BatchNumber, FieldName: "TerminalState", Msg: msg}
}
}
}
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions batchSHR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ func BenchmarkBatchSHRAddendaCount(b *testing.B) {
func testBatchSHRAddendaCountZero(t testing.TB) {
mockBatch := NewBatchSHR(mockBatchSHRHeader())
mockBatch.AddEntry(mockSHREntryDetail())
mockAddenda02 := mockAddenda02()
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02
mockBatch.Entries[0].AddendaRecordIndicator = 1
if err := mockBatch.Create(); err != nil {
if e, ok := err.(*BatchError); ok {
if e.FieldName != "AddendaCount" {
Expand Down Expand Up @@ -626,3 +629,22 @@ func TestBatchSHRAddendum99Category(t *testing.T) {
}
}
}

// TestBatchSHRTerminalState validates TerminalState returns an error if invalid from usabbrev
func TestBatchSHRTerminalState(t *testing.T) {
mockBatch := NewBatchSHR(mockBatchSHRHeader())
mockBatch.AddEntry(mockSHREntryDetail())
mockAddenda02 := mockAddenda02()
mockAddenda02.TerminalState = "YY"
mockBatch.GetEntries()[0].Addenda02 = mockAddenda02
mockBatch.Entries[0].AddendaRecordIndicator = 1
if err := mockBatch.Create(); err != nil {
if e, ok := err.(*BatchError); ok {
if e.FieldName != "TerminalState" {
t.Errorf("%T: %s", err, err)
}
} else {
t.Errorf("%T: %s", err, err)
}
}
}

0 comments on commit 5c2ba64

Please sign in to comment.