-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antoine Popineau
committed
Jan 16, 2025
1 parent
8bc794e
commit 9352496
Showing
12 changed files
with
340 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
type OpenSanctionCheckFilter map[string][]string | ||
|
||
type OpenSanctionsQuery struct { | ||
Queries OpenSanctionCheckFilter `json:"queries"` | ||
} | ||
|
||
type SanctionCheckResult struct { | ||
Hits int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package repositories | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/checkmarble/marble-backend/models" | ||
"github.com/checkmarble/marble-backend/utils" | ||
"github.com/cockroachdb/errors" | ||
"github.com/google/uuid" | ||
) | ||
|
||
const ( | ||
// TODO: Pull this as server configuration | ||
DEV_YENTE_URL = "http://app.yente.orb.local" | ||
) | ||
|
||
type OpenSanctionsRepository struct{} | ||
|
||
type openSanctionsRequest struct { | ||
Queries map[string]openSanctionsRequestQuery `json:"queries"` | ||
} | ||
|
||
type openSanctionsRequestQuery struct { | ||
Schema string `json:"schema"` | ||
Properties models.OpenSanctionCheckFilter `json:"properties"` | ||
} | ||
|
||
type OpenSanctionsCheckResponse struct { | ||
Responses map[string]struct { | ||
Total struct { | ||
Value int `json:"value"` | ||
} `json:"total"` | ||
Results []struct { | ||
Id string `json:"id"` | ||
} `json:"results"` | ||
} `json:"responses"` | ||
} | ||
|
||
func (repo OpenSanctionsRepository) Search(ctx context.Context, cfg models.SanctionCheckConfig, | ||
query models.OpenSanctionsQuery, | ||
) (models.SanctionCheckResult, error) { | ||
req, err := repo.searchRequest(ctx, query) | ||
if err != nil { | ||
return models.SanctionCheckResult{}, err | ||
} | ||
|
||
utils.LoggerFromContext(ctx).Debug("SANCTION CHECK: sending request...") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return models.SanctionCheckResult{}, errors.Wrap(err, "could not perform sanction check") | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return models.SanctionCheckResult{}, fmt.Errorf( | ||
"sanction check API returned status %d", resp.StatusCode) | ||
} | ||
|
||
var matches OpenSanctionsCheckResponse | ||
|
||
defer resp.Body.Close() | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&matches); err != nil { | ||
return models.SanctionCheckResult{}, errors.Wrap(err, | ||
"could not parse sanction check response") | ||
} | ||
|
||
// TODO: Replace with actual processing of responses | ||
hits := 0 | ||
|
||
for _, response := range matches.Responses { | ||
hits += response.Total.Value | ||
} | ||
|
||
result := models.SanctionCheckResult{ | ||
Hits: hits, | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (OpenSanctionsRepository) searchRequest(ctx context.Context, query models.OpenSanctionsQuery) (*http.Request, error) { | ||
q := openSanctionsRequest{ | ||
Queries: make(map[string]openSanctionsRequestQuery, len(query.Queries)), | ||
} | ||
|
||
for key, value := range query.Queries { | ||
q.Queries[uuid.NewString()] = openSanctionsRequestQuery{ | ||
Schema: "Thing", | ||
Properties: map[string][]string{key: value}, | ||
} | ||
} | ||
|
||
var body bytes.Buffer | ||
|
||
if err := json.NewEncoder(&body).Encode(q); err != nil { | ||
return nil, errors.Wrap(err, "could not parse OpenSanctions response") | ||
} | ||
|
||
url := fmt.Sprintf("%s/match/sanctions", DEV_YENTE_URL) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &body) | ||
|
||
return req, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package repositories | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/checkmarble/marble-backend/models" | ||
"github.com/checkmarble/marble-backend/utils" | ||
) | ||
|
||
func (*MarbleDbRepository) InsertResults(ctx context.Context, matches models.SanctionCheckResult) (models.SanctionCheckResult, error) { | ||
utils.LoggerFromContext(ctx).Debug("SANCTION CHECK: inserting matches in database") | ||
|
||
return models.SanctionCheckResult{ | ||
Hits: matches.Hits, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.