-
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
Showing
13 changed files
with
384 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,20 @@ | ||
package models | ||
|
||
type OpenSanctionCheckFilter map[string][]string | ||
|
||
type OpenSanctionsQuery struct { | ||
Queries OpenSanctionCheckFilter `json:"queries"` | ||
} | ||
|
||
type SanctionCheckResult struct { | ||
Partial bool | ||
Count int | ||
Matches []SanctionCheckResultMatch | ||
} | ||
|
||
type SanctionCheckResultMatch struct { | ||
Id string | ||
Schema string | ||
Datasets []string | ||
Names []string | ||
} |
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,58 @@ | ||
package httpmodels | ||
|
||
import ( | ||
"maps" | ||
"slices" | ||
|
||
"github.com/checkmarble/marble-backend/models" | ||
) | ||
|
||
type HTTPOpenSanctionsResult struct { | ||
Responses map[string]struct { | ||
Total struct { | ||
Value int `json:"value"` | ||
} `json:"total"` | ||
Limit int `json:"limit"` | ||
Results []struct { | ||
Id string `json:"id"` | ||
Schema string `json:"schema"` | ||
Datasets []string `json:"datasets"` | ||
Properties struct { | ||
Name []string `json:"name"` | ||
} `json:"properties"` | ||
} `json:"results"` | ||
} `json:"responses"` | ||
} | ||
|
||
func AdaptOpenSanctionsResult(result HTTPOpenSanctionsResult) (models.SanctionCheckResult, error) { | ||
// TODO: Replace with actual processing of responses | ||
partial := false | ||
matches := make(map[string]models.SanctionCheckResultMatch) | ||
|
||
for _, resp := range result.Responses { | ||
if resp.Total.Value != resp.Limit { | ||
partial = true | ||
} | ||
|
||
for _, match := range resp.Results { | ||
if _, ok := matches[match.Id]; !ok { | ||
entity := models.SanctionCheckResultMatch{ | ||
Id: match.Id, | ||
Schema: match.Schema, | ||
Datasets: match.Datasets, | ||
Names: match.Properties.Name, | ||
} | ||
|
||
matches[match.Id] = entity | ||
} | ||
} | ||
} | ||
|
||
output := models.SanctionCheckResult{ | ||
Partial: partial, | ||
Count: len(matches), | ||
Matches: slices.Collect(maps.Values(matches)), | ||
} | ||
|
||
return output, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package repositories | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/checkmarble/marble-backend/models" | ||
"github.com/checkmarble/marble-backend/repositories/httpmodels" | ||
"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"` | ||
} | ||
|
||
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 httpmodels.HTTPOpenSanctionsResult | ||
|
||
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") | ||
} | ||
|
||
return httpmodels.AdaptOpenSanctionsResult(matches) | ||
} | ||
|
||
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,14 @@ | ||
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 matches, 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
Oops, something went wrong.