Skip to content

Commit

Permalink
Merge pull request #1511 from kevinbarbour/delete-entries
Browse files Browse the repository at this point in the history
Add DeleteEntry and DeleteADVEntry to Batcher
  • Loading branch information
adamdecaf authored Nov 21, 2024
2 parents b2bfa18 + 88d0b27 commit e838056
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
11 changes: 11 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strconv"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -528,12 +529,22 @@ func (batch *Batch) AddEntry(entry *EntryDetail) {
batch.Entries = append(batch.Entries, entry)
}

// DeleteEntries deletes all Entries from the Batch where del() == true
func (batch *Batch) DeleteEntries(del func(e *EntryDetail) bool) {
batch.Entries = slices.DeleteFunc(batch.Entries, del)
}

// AddADVEntry appends an ADV EntryDetail to the Batch
func (batch *Batch) AddADVEntry(entry *ADVEntryDetail) {
batch.category = entry.Category
batch.ADVEntries = append(batch.ADVEntries, entry)
}

// DeleteADVEntries deletes all ADV Entries from the Batch where del() == true
func (batch *Batch) DeleteADVEntries(del func(e *ADVEntryDetail) bool) {
batch.ADVEntries = slices.DeleteFunc(batch.ADVEntries, del)
}

// GetADVEntries returns a slice of entry details for the batch
func (batch *Batch) GetADVEntries() []*ADVEntryDetail {
return batch.ADVEntries
Expand Down
74 changes: 74 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1697,3 +1697,77 @@ func TestBatch_calculateEntryHash(t *testing.T) {
hash := b.calculateEntryHash()
require.Equal(t, 22140797, hash)
}

func TestBatch_DeleteEntries(t *testing.T) {
b := &Batch{}
b.SetHeader(mockBatchHeader())

ed1 := mockEntryDetail()
ed1.TraceNumber = "1"
ed1.Amount = 20
ed2 := mockEntryDetail()
ed2.TraceNumber = "2"
ed2.Amount = 60
ed3 := mockEntryDetail()
ed3.TraceNumber = "3"
ed3.Amount = 100

b.AddEntry(ed1)
b.AddEntry(ed2)
b.AddEntry(ed3)

require.Equal(t, 3, len(b.Entries))

b.DeleteEntries(func(e *EntryDetail) bool {
return e.TraceNumber == "2"
})

require.Equal(t, 2, len(b.Entries))
require.Equal(t, "1", b.Entries[0].TraceNumber)
require.Equal(t, "3", b.Entries[1].TraceNumber)

b.AddEntry(ed2)

b.DeleteEntries(func(e *EntryDetail) bool {
return e.Amount >= 50
})
require.Equal(t, 1, len(b.Entries))
require.Equal(t, "1", b.Entries[0].TraceNumber)
}

func TestBatch_DeleteADVEntries(t *testing.T) {
b := &Batch{}
b.SetHeader(mockBatchHeader())

ed1 := mockADVEntryDetail()
ed1.ID = "1"
ed1.Amount = 20
ed2 := mockADVEntryDetail()
ed2.ID = "2"
ed2.Amount = 60
ed3 := mockADVEntryDetail()
ed3.ID = "3"
ed3.Amount = 100

b.AddADVEntry(ed1)
b.AddADVEntry(ed2)
b.AddADVEntry(ed3)

require.Equal(t, 3, len(b.ADVEntries))

b.DeleteADVEntries(func(e *ADVEntryDetail) bool {
return e.ID == "2"
})

require.Equal(t, 2, len(b.ADVEntries))
require.Equal(t, "1", b.ADVEntries[0].ID)
require.Equal(t, "3", b.ADVEntries[1].ID)

b.AddADVEntry(ed2)

b.DeleteADVEntries(func(e *ADVEntryDetail) bool {
return e.Amount >= 50
})
require.Equal(t, 1, len(b.ADVEntries))
require.Equal(t, "1", b.ADVEntries[0].ID)
}
2 changes: 2 additions & 0 deletions batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ type Batcher interface {
SetADVControl(*ADVBatchControl)
GetEntries() []*EntryDetail
AddEntry(*EntryDetail)
DeleteEntries(func(*EntryDetail) bool)
GetADVEntries() []*ADVEntryDetail
AddADVEntry(*ADVEntryDetail)
DeleteADVEntries(func(*ADVEntryDetail) bool)
Create() error
Validate() error
SetID(string)
Expand Down

0 comments on commit e838056

Please sign in to comment.