Skip to content

Commit

Permalink
Merge pull request #209 from moov-io/fix-return-batch-header
Browse files Browse the repository at this point in the history
fix: use RDFI in batch header for returns/corrections/etc
  • Loading branch information
adamdecaf committed Feb 23, 2024
2 parents cb47a01 + b0d8d41 commit bb90e79
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 24 deletions.
8 changes: 6 additions & 2 deletions pkg/response/file_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (ft *FileTransfomer) Transform(file *ach.File) error {
}

// Get the appropriate ach.Batch object to update
batch, err := outBatches.getOutBatch(processAction.Delay, entry.Category, *file.Batches[i].GetHeader(), i)
batch, err := outBatches.getOutBatch(processAction.Delay, entry.Category, file.Header, *file.Batches[i].GetHeader(), i)
if err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func (outFiles outFiles) getOutFile(delay *time.Duration, file *ach.File, opts *

type outBatches map[*time.Duration]map[bool]*ach.Batcher

func (outBatches outBatches) getOutBatch(delay *time.Duration, category string, bh ach.BatchHeader, i int) (*ach.Batcher, error) {
func (outBatches outBatches) getOutBatch(delay *time.Duration, category string, fh ach.FileHeader, bh ach.BatchHeader, i int) (*ach.Batcher, error) {
var batchesByCategory = outBatches[delay]
if batchesByCategory == nil {
batchesByCategory = make(map[bool]*ach.Batcher)
Expand All @@ -172,6 +172,10 @@ func (outBatches outBatches) getOutBatch(delay *time.Duration, category string,
if category == ach.CategoryNOC {
bh.StandardEntryClassCode = ach.COR
}

// We need to flip the Origin / Destination values when setting up the out batch
bh.ODFIIdentification = fh.ImmediateDestination

batch, err := ach.NewBatch(&bh)
if err != nil {
return nil, fmt.Errorf("transform batch[%d] problem creating Batch: %v", i, err)
Expand Down
102 changes: 80 additions & 22 deletions pkg/response/file_transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"

"github.com/moov-io/ach"
"github.com/moov-io/ach-test-harness/internal/achx"
"github.com/moov-io/ach-test-harness/pkg/service"
"github.com/moov-io/base/log"

Expand Down Expand Up @@ -162,6 +164,10 @@ func TestFileTransformer_CopyOnlyAndCopyOnly_SingleBatch(t *testing.T) {
read, _ := ach.ReadFile(filepath.Join(recondir, fds[0].Name())) // ignore the error b/c this file has no header or control record
require.Equal(t, achIn.Batches, read.Batches)

trace1 := achIn.Batches[0].GetEntries()[0].TraceNumber
trace2 := read.Batches[0].GetEntries()[0].TraceNumber
require.Equal(t, trace1, trace2)

// verify the timestamp on the file is in the past
fInfo, err := fds[0].Info()
require.NoError(t, err)
Expand Down Expand Up @@ -251,6 +257,8 @@ func TestFileTransformer_ReturnOnly(t *testing.T) {
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)
require.Equal(t, "22147578", entries[0].Addenda99.OriginalDFI)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the past
fInfo, err := fds[0].Info()
require.NoError(t, err)
Expand Down Expand Up @@ -297,6 +305,8 @@ func TestFileTransformer_CorrectionOnly(t *testing.T) {
require.Equal(t, "C01", entries[0].Addenda98.ChangeCode)
require.Equal(t, "10200002", entries[0].Addenda98.OriginalDFI)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the past
fInfo, err := fds[0].Info()
require.NoError(t, err)
Expand Down Expand Up @@ -333,8 +343,12 @@ func TestFileTransformer_ReturnOnlyAndCopyOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "R03", found.Batches[0].GetEntries()[0].Addenda99.ReturnCode)

entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the past
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -382,8 +396,11 @@ func TestFileTransformer_CorrectionOnlyAndCopyOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "C01", found.Batches[0].GetEntries()[0].Addenda98.ChangeCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "C01", entries[0].Addenda98.ChangeCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the past
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -430,8 +447,11 @@ func TestFileTransformer_DelayReturnOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "R03", found.Batches[0].GetEntries()[0].Addenda99.ReturnCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -468,8 +488,11 @@ func TestFileTransformer_DelayCorrectionOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "C01", found.Batches[0].GetEntries()[0].Addenda98.ChangeCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "C01", entries[0].Addenda98.ChangeCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -507,8 +530,11 @@ func TestFileTransformer_DelayReturnOnlyAndCopyOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "R03", found.Batches[0].GetEntries()[0].Addenda99.ReturnCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -556,8 +582,11 @@ func TestFileTransformer_DelayCorrectionOnlyAndCopyOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "C01", found.Batches[0].GetEntries()[0].Addenda98.ChangeCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "C01", entries[0].Addenda98.ChangeCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -604,8 +633,11 @@ func TestFileTransformer_CopyAndDelayReturn(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "R03", found.Batches[0].GetEntries()[0].Addenda99.ReturnCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -650,8 +682,11 @@ func TestFileTransformer_CopyAndDelayCorrection(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "C01", found.Batches[0].GetEntries()[0].Addenda98.ChangeCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "C01", entries[0].Addenda98.ChangeCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -696,8 +731,11 @@ func TestFileTransformer_CopyAndDelayReturnAndCopyOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "R03", found.Batches[0].GetEntries()[0].Addenda99.ReturnCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -742,8 +780,11 @@ func TestFileTransformer_CopyAndDelayCorrectionAndCopyOnly(t *testing.T) {
found, err := ach.ReadFile(filepath.Join(retdir, fds[0].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "C01", found.Batches[0].GetEntries()[0].Addenda98.ChangeCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "C01", entries[0].Addenda98.ChangeCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -857,8 +898,11 @@ func TestFileTransformer_DelayCorrectionOnlyAndDelayReturnOnly_differentDelay(t
found, err = ach.ReadFile(filepath.Join(retdir, fds[1].Name()))
require.NoError(t, err)
require.Len(t, found.Batches, 1)
require.Len(t, found.Batches[0].GetEntries(), 1)
require.Equal(t, "R03", found.Batches[0].GetEntries()[0].Addenda99.ReturnCode)
entries := found.Batches[0].GetEntries()
require.Len(t, entries, 1)
require.Equal(t, "R03", entries[0].Addenda99.ReturnCode)

hasPrefix(t, entries[0].TraceNumber, achx.ABA8(achIn.Header.ImmediateDestination))

// verify the timestamp on the file is in the future
fInfo, err := fds[0].Info()
Expand Down Expand Up @@ -899,3 +943,17 @@ func testFileTransformer(t *testing.T, resp ...service.Response) (*FileTransfome

return NewFileTransformer(logger, cfg, responses, w), dir
}

func hasPrefix(t *testing.T, s, prefix string) {
t.Helper()

if !strings.HasPrefix(s, prefix) {
t.Errorf("%q does not contain %q", s, prefix)
}
}

func TestHasPrefix(t *testing.T) {
hasPrefix(t, "abc", "a")
hasPrefix(t, "abc", "ab")
hasPrefix(t, "abc", "abc")
}

0 comments on commit bb90e79

Please sign in to comment.