forked from moov-io/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
135 lines (120 loc) · 4.04 KB
/
search.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
// Copyright 2019 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"context"
"errors"
"fmt"
"strings"
moov "github.com/cardonator/ofac/client"
"github.com/antihax/optional"
)
// searchByName will attempt OFAC searches for the provided name and then load the SDN metadata
// associated to the company/organization or individual.
func searchByName(ctx context.Context, api *moov.APIClient, name string) (*moov.Sdn, error) {
search, resp, err := api.OFACApi.Search(ctx, &moov.SearchOpts{
Name: optional.NewString(name),
Limit: optional.NewInt32(2),
})
if err != nil {
return nil, fmt.Errorf("searchByName: %v", err)
}
defer resp.Body.Close()
if len(search.SDNs) == 0 {
return nil, fmt.Errorf("searchByName: found no SDNs for %q", name)
}
// Find Customer or company
if strings.EqualFold(search.SDNs[0].SdnType, "individual") {
if err := getCustomer(ctx, api, search.SDNs[0].EntityID); err != nil {
return nil, err
}
} else {
if err := getCompany(ctx, api, search.SDNs[0].EntityID); err != nil {
return nil, err
}
}
return &search.SDNs[0], nil
}
// searchByAltName will attempt OFAC searches and retrieval of all alt names associated to the first result
// for the provided altName and error if none are found.
func searchByAltName(ctx context.Context, api *moov.APIClient, alt string) error {
search, resp, err := api.OFACApi.Search(ctx, &moov.SearchOpts{
AltName: optional.NewString(alt),
Limit: optional.NewInt32(2),
})
if err != nil {
return fmt.Errorf("searchByAltName: %v", err)
}
defer resp.Body.Close()
if len(search.AltNames) == 0 {
return fmt.Errorf("searchByAltName: found no AltNames for %q", alt)
}
return getSDNAltNames(ctx, api, search.AltNames[0].EntityID)
}
// searchByAddress will attempt OFAC searches and retrieval of all addresses associated to the first result
// for the provided address and error if none are found.
func searchByAddress(ctx context.Context, api *moov.APIClient, address string) error {
search, resp, err := api.OFACApi.Search(ctx, &moov.SearchOpts{
Address: optional.NewString(address),
Limit: optional.NewInt32(2),
})
if err != nil {
return fmt.Errorf("searchByAddress: %v", err)
}
defer resp.Body.Close()
if len(search.Addresses) == 0 {
return fmt.Errorf("searchByAddress: found no Addresses for %q", address)
}
return getSDNAddresses(ctx, api, search.Addresses[0].EntityID)
}
func getSDNAddresses(ctx context.Context, api *moov.APIClient, id string) error {
addr, resp, err := api.OFACApi.GetSDNAddresses(ctx, id, nil)
if err != nil {
return fmt.Errorf("loadAddresses: %v", err)
}
defer resp.Body.Close()
if len(addr) == 0 {
return errors.New("loadAddresses: no Addresses found")
}
if addr[0].EntityID != id {
return fmt.Errorf("loadAddresses: wrong Address: expected %s but got %s", id, addr[0].EntityID)
}
return nil
}
func getSDNAltNames(ctx context.Context, api *moov.APIClient, id string) error {
alt, resp, err := api.OFACApi.GetSDNAltNames(ctx, id, nil)
if err != nil {
return fmt.Errorf("loadAltNames: %v", err)
}
defer resp.Body.Close()
if len(alt) == 0 {
return errors.New("loadAltNames: no AltNames found")
}
if alt[0].EntityID != id {
return fmt.Errorf("loadAltNames: wrong AltName: expected %s but got %s", id, alt[0].EntityID)
}
return nil
}
func getCustomer(ctx context.Context, api *moov.APIClient, id string) error {
cust, resp, err := api.OFACApi.GetOFACCustomer(ctx, id, nil)
if err != nil {
return fmt.Errorf("loadCustomer: %v", err)
}
defer resp.Body.Close()
if cust.Id != id {
return fmt.Errorf("loadCustomer: wrong Customer: expected %s but got %s", id, cust.Id)
}
return nil
}
func getCompany(ctx context.Context, api *moov.APIClient, id string) error {
company, resp, err := api.OFACApi.GetOFACCompany(ctx, id, nil)
if err != nil {
return fmt.Errorf("loadCompany: %v", err)
}
defer resp.Body.Close()
if company.Id != id {
return fmt.Errorf("loadCompany: wrong Company: expected %s but got %s", id, company.Id)
}
return nil
}