forked from moov-io/fed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WIREDictionary.go
389 lines (346 loc) · 12.6 KB
/
WIREDictionary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package fed
import (
"bufio"
"bytes"
"encoding/json"
"io"
"math"
"sort"
"strings"
"unicode/utf8"
"github.com/moov-io/base"
"github.com/moov-io/fed/pkg/logos"
"github.com/moov-io/fed/pkg/strcmp"
)
var (
// WIREJaroWinklerSimilarity is the search similarity percentage for strcmp.JaroWinkler for CustomerName
// (Financial Institution Name)
WIREJaroWinklerSimilarity = 0.85
// WIRELevenshteinSimilarity is the search similarity percentage for strcmp.Levenshtein for CustomerName
// (Financial Institution Name)
WIRELevenshteinSimilarity = 0.85
)
// WIREDictionary of Participant records
type WIREDictionary struct {
// Participants is a list of Participant structs
WIREParticipants []*WIREParticipant
// IndexWIRERoutingNumber creates an index of WIREParticipants keyed by WIREParticipant.RoutingNumber
IndexWIRERoutingNumber map[string]*WIREParticipant
// IndexWIRECustomerName creates an index of WIREParticipants keyed by WIREParticipant.CustomerName
IndexWIRECustomerName map[string][]*WIREParticipant
// errors holds each error encountered when attempting to parse the file
errors base.ErrorList
// validator is composed for data validation
validator
}
// NewWIREDictionary creates a WIREDictionary
func NewWIREDictionary() *WIREDictionary {
return &WIREDictionary{
IndexWIRERoutingNumber: map[string]*WIREParticipant{},
IndexWIRECustomerName: map[string][]*WIREParticipant{},
}
}
// WIREParticipant holds a FedWIRE dir routing record as defined by Fed WIRE Format
// https://frbservices.org/EPaymentsDirectory/fedwireFormat.html
type WIREParticipant struct {
// RoutingNumber The institution's routing number
RoutingNumber string `json:"routingNumber"`
// TelegraphicName is the short name of financial institution Wells Fargo
TelegraphicName string `json:"telegraphicName"`
// CustomerName (36): FEDERAL RESERVE BANK
CustomerName string `json:"customerName"`
// Location is the city and state
WIRELocation `json:"wireLocation"`
// FundsTransferStatus designates funds transfer status
// Y - Eligible
// N - Ineligible
FundsTransferStatus string `json:"fundsTransferStatus"`
// FundsSettlementOnlyStatus designates funds settlement only status
// S - Settlement-Only
FundsSettlementOnlyStatus string `json:"fundsSettlementOnlyStatus"`
// BookEntrySecuritiesTransferStatus designates book entry securities transfer status
BookEntrySecuritiesTransferStatus string `json:"bookEntrySecuritiesTransferStatus"`
// Date of last revision: YYYYMMDD, or blank
Date string `json:"date"`
// CleanName is our cleaned up value of CustomerName
CleanName string `json:"cleanName"`
// Logo from third-party provider (if enabled)
Logo *logos.Logo `json:"logo"`
}
// WIRELocation is the city and state
type WIRELocation struct {
// City
City string `json:"city"`
// State
State string `json:"state"`
}
// jsonWIREDictionary is a JSON representation of the FED Wire Participant directory
// Model generated with // https://mholt.github.io/json-to-go/
type jsonWIREDictionary struct {
FedwireParticipants struct {
Response struct {
Code int `json:"code"`
} `json:"response"`
FedwireParticipants []struct {
RoutingNumber string `json:"routingNumber"`
TelegraphicName string `json:"telegraphicName"`
CustomerName string `json:"customerName"`
CustomerState string `json:"customerState"`
CustomerCity string `json:"customerCity"`
FundsEligibility string `json:"fundsEligibility"`
FundsSettlementOnlyStatus string `json:"fundsSettlementOnlyStatus"`
SecuritiesEligibility string `json:"securitiesEligibility"`
ChangeDate string `json:"changeDate"`
} `json:"fedwireParticipants"`
} `json:"fedwireParticipants"`
}
// Read parses a single line or multiple lines of FedWIREdir text
func (f *WIREDictionary) Read(r io.Reader) error {
if f == nil {
return nil
}
bs, err := io.ReadAll(r)
if err != nil {
return err
}
// Try validating the file as JSON and if that fails read as plaintext
if json.Valid(bs) {
err = f.readJSON(bytes.NewReader(bs))
if err != nil {
return err
}
return nil
}
return f.readPlaintext(bytes.NewReader(bs))
}
func (f *WIREDictionary) readJSON(r io.Reader) error {
var wrapper jsonWIREDictionary
if err := json.NewDecoder(r).Decode(&wrapper); err != nil {
return err
}
ps := wrapper.FedwireParticipants.FedwireParticipants
for i := range ps {
p := &WIREParticipant{
RoutingNumber: ps[i].RoutingNumber,
TelegraphicName: ps[i].TelegraphicName,
CustomerName: ps[i].CustomerName,
WIRELocation: WIRELocation{
City: ps[i].CustomerCity,
State: ps[i].CustomerState,
},
FundsTransferStatus: ps[i].FundsEligibility,
FundsSettlementOnlyStatus: ps[i].FundsSettlementOnlyStatus,
BookEntrySecuritiesTransferStatus: ps[i].SecuritiesEligibility,
Date: ps[i].ChangeDate,
// Our Custom Fields
CleanName: Normalize(ps[i].CustomerName),
}
f.WIREParticipants = append(f.WIREParticipants, p)
f.IndexWIRERoutingNumber[p.RoutingNumber] = p
}
f.createIndexWIRECustomerName()
return nil
}
func (f *WIREDictionary) readPlaintext(r io.Reader) error {
if f == nil || r == nil {
return nil
}
// read each line and lift it into a ACHParticipant
s := bufio.NewScanner(r)
var line string
for s.Scan() {
line = s.Text()
if utf8.RuneCountInString(line) != WIRELineLength {
f.errors.Add(NewRecordWrongLengthErr(WIRELineLength, len(line)))
// Return with error if the record length is incorrect as this file is a FED file
return f.errors
}
if err := f.parseWIREParticipant(line); err != nil {
f.errors.Add(err)
return f.errors
}
}
f.createIndexWIRECustomerName()
return nil
}
// TODO return a parsing error if the format or file is wrong.
func (f *WIREDictionary) parseWIREParticipant(line string) error {
p := new(WIREParticipant)
//RoutingNumber (9): 011000015
p.RoutingNumber = line[:9]
// TelegraphicName (18): FED
p.TelegraphicName = strings.Trim(line[9:27], " ")
// CustomerName (36): FEDERAL RESERVE BANK
p.CustomerName = strings.Trim(line[27:63], " ")
p.WIRELocation = WIRELocation{
// State (2): GA
State: line[63:65],
// City (25): ATLANTA
City: strings.Trim(line[65:90], " "),
}
// FundsTransferStatus (1): Y or N
p.FundsTransferStatus = line[90:91]
// FundsSettlementOnlyStatus (1): " " or S - Settlement-Only
p.FundsSettlementOnlyStatus = line[91:92]
// BookEntrySecuritiesTransferStatus (1): Y or N
p.BookEntrySecuritiesTransferStatus = line[92:93]
// Date YYYYMMDD (8): 122415
p.Date = line[93:101]
// Our custom fields
p.CleanName = Normalize(p.CustomerName)
f.WIREParticipants = append(f.WIREParticipants, p)
f.IndexWIRERoutingNumber[p.RoutingNumber] = p
return nil
}
// createIndexWIRECustomerName creates an index of Financial Institutions keyed by WIREParticipant.CustomerName
func (f *WIREDictionary) createIndexWIRECustomerName() {
for _, wireP := range f.WIREParticipants {
f.IndexWIRECustomerName[wireP.CustomerName] = append(f.IndexWIRECustomerName[wireP.CustomerName], wireP)
}
}
// RoutingNumberSearchSingle returns a FEDWIRE participant based on a WIREParticipant.RoutingNumber. Routing Number
// validation is only that it exists in IndexParticipant. Expecting 9 digits, checksum needs to be included.
func (f *WIREDictionary) RoutingNumberSearchSingle(s string) *WIREParticipant {
if _, ok := f.IndexWIRERoutingNumber[s]; ok {
return f.IndexWIRERoutingNumber[s]
}
return nil
}
// FinancialInstitutionSearchSingle returns a FEDWIRE participant based on a WIREParticipant.CustomerName
func (f *WIREDictionary) FinancialInstitutionSearchSingle(s string) []*WIREParticipant {
if _, ok := f.IndexWIRECustomerName[s]; ok {
return f.IndexWIRECustomerName[s]
}
return nil
}
// RoutingNumberSearch returns FEDWIRE participants if WIREParticipant.RoutingNumber begins with prefix string s.
// The first 2 digits of the routing number are required.
// Based on https://www.frbservices.org/EPaymentsDirectory/search.html
func (f *WIREDictionary) RoutingNumberSearch(s string, limit int) ([]*WIREParticipant, error) {
s = strings.TrimSpace(s)
if utf8.RuneCountInString(s) < MinimumRoutingNumberDigits {
// The first 2 digits (characters) are required
f.errors.Add(NewRecordWrongLengthErr(2, len(s)))
return nil, f.errors
}
if utf8.RuneCountInString(s) > MaximumRoutingNumberDigits {
f.errors.Add(NewRecordWrongLengthErr(9, len(s)))
// Routing Number cannot be greater than 10 digits (characters)
return nil, f.errors
}
if err := f.isNumeric(s); err != nil {
// Routing Number is not numeric
f.errors.Add(ErrRoutingNumberNumeric)
return nil, f.errors
}
exactMatch := len(s) == 9
out := make([]*wireParticipantResult, 0)
for _, wireP := range f.WIREParticipants {
if exactMatch {
if wireP.RoutingNumber == s {
out = append(out, &wireParticipantResult{
WIREParticipant: wireP,
highestMatch: 1.0,
})
}
} else {
out = append(out, &wireParticipantResult{
WIREParticipant: wireP,
highestMatch: strcmp.JaroWinkler(wireP.RoutingNumber, s),
})
}
}
return reduceWIREResults(out, limit), nil
}
// FinancialInstitutionSearch returns a FEDWIRE participant based on a WIREParticipant.CustomerName
func (f *WIREDictionary) FinancialInstitutionSearch(s string, limit int) []*WIREParticipant {
s = strings.ToLower(s)
out := make([]*wireParticipantResult, 0)
for _, wireP := range f.WIREParticipants {
// JaroWinkler is a more accurate version of the Jaro algorithm. It works by boosting the
// score of exact matches at the beginning of the strings. By doing this, Winkler says that
// typos are less common to happen at the beginning.
jaroScore := strcmp.JaroWinkler(strings.ToLower(wireP.CleanName), s)
// Levenshtein is the "edit distance" between two strings. This is the count of operations
// (insert, delete, replace) needed for two strings to be equal.
levenScore := strcmp.Levenshtein(strings.ToLower(wireP.CleanName), s)
if jaroScore > ACHJaroWinklerSimilarity || levenScore > ACHLevenshteinSimilarity {
out = append(out, &wireParticipantResult{
WIREParticipant: wireP,
highestMatch: math.Max(jaroScore, levenScore),
})
}
}
return reduceWIREResults(out, limit)
}
// WIREParticipantRoutingNumberFilter filters WIREParticipant by Routing Number
func (f *WIREDictionary) WIREParticipantRoutingNumberFilter(wireParticipants []*WIREParticipant, s string) ([]*WIREParticipant, error) {
s = strings.TrimSpace(s)
if len(s) < MinimumRoutingNumberDigits {
// The first 2 digits (characters) are required
f.errors.Add(NewRecordWrongLengthErr(2, len(s)))
return nil, f.errors
}
nsl := make([]*WIREParticipant, 0)
for _, wireP := range wireParticipants {
if strings.HasPrefix(wireP.RoutingNumber, s) {
nsl = append(nsl, wireP)
}
}
return nsl, nil
}
// WIREParticipantStateFilter filters WIREParticipant by State.
func (f *WIREDictionary) WIREParticipantStateFilter(wireParticipants []*WIREParticipant, s string) []*WIREParticipant {
nsl := make([]*WIREParticipant, 0)
for _, wireP := range wireParticipants {
if strings.EqualFold(wireP.WIRELocation.State, s) {
nsl = append(nsl, wireP)
}
}
return nsl
}
// WIREParticipantCityFilter filters WIREParticipant by City
func (f *WIREDictionary) WIREParticipantCityFilter(wireParticipants []*WIREParticipant, s string) []*WIREParticipant {
nsl := make([]*WIREParticipant, 0)
for _, wireP := range wireParticipants {
if strings.EqualFold(wireP.WIRELocation.City, s) {
nsl = append(nsl, wireP)
}
}
return nsl
}
// StateFilter filters WIREDictionary.WIREParticipant by state
func (f *WIREDictionary) StateFilter(s string) []*WIREParticipant {
nsl := make([]*WIREParticipant, 0)
for _, wireP := range f.WIREParticipants {
if strings.EqualFold(wireP.WIRELocation.State, s) {
nsl = append(nsl, wireP)
}
}
return nsl
}
// CityFilter filters WIREDictionary.WIREParticipant by city
func (f *WIREDictionary) CityFilter(s string) []*WIREParticipant {
nsl := make([]*WIREParticipant, 0)
for _, wireP := range f.WIREParticipants {
if strings.EqualFold(wireP.WIRELocation.City, s) {
nsl = append(nsl, wireP)
}
}
return nsl
}
type wireParticipantResult struct {
*WIREParticipant
highestMatch float64
}
func reduceWIREResults(in []*wireParticipantResult, limit int) []*WIREParticipant {
sort.SliceStable(in, func(i, j int) bool { return in[i].highestMatch > in[j].highestMatch })
out := make([]*WIREParticipant, 0)
for i := 0; i < limit && i < len(in); i++ {
out = append(out, in[i].WIREParticipant)
}
return out
}