-
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 476816f
Showing
12 changed files
with
308 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,28 @@ | ||
package models | ||
|
||
type SanctionCheckQuery struct { | ||
String string | ||
} | ||
|
||
type SanctionCheckResponse struct { | ||
Total struct { | ||
Value int `json:"value"` | ||
} `json:"total"` | ||
} | ||
|
||
type SanctionCheckResult struct { | ||
Hits int | ||
} | ||
|
||
func (in SanctionCheckResult) process() SanctionCheckResult { | ||
// TODO; process matches and merge them. | ||
return in | ||
} | ||
|
||
func AdaptSanctionCheckResult(resp SanctionCheckResponse) SanctionCheckResult { | ||
result := SanctionCheckResult{ | ||
Hits: resp.Total.Value, | ||
} | ||
|
||
return result.process() | ||
} |
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,62 @@ | ||
package repositories | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/checkmarble/marble-backend/models" | ||
"github.com/checkmarble/marble-backend/utils" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
const ( | ||
// TODO: Pull this as server configuration | ||
DEV_YENTE_URL = "http://app.yente.orb.local" | ||
) | ||
|
||
type OpenSanctionsRepository struct{} | ||
|
||
func (repo OpenSanctionsRepository) Search(ctx context.Context, cfg models.SanctionCheckConfig, | ||
query models.SanctionCheckQuery, | ||
) (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 sanctionCheckResp models.SanctionCheckResponse | ||
|
||
defer resp.Body.Close() | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&sanctionCheckResp); err != nil { | ||
return models.SanctionCheckResult{}, errors.Wrap(err, | ||
"could not parse sanction check response") | ||
} | ||
|
||
return models.AdaptSanctionCheckResult(sanctionCheckResp), nil | ||
} | ||
|
||
func (OpenSanctionsRepository) searchRequest(ctx context.Context, query models.SanctionCheckQuery) (*http.Request, error) { | ||
q := url.Values{} | ||
q.Set("q", query.String) | ||
|
||
url := fmt.Sprintf("%s/search/sanctions?%s", DEV_YENTE_URL, q.Encode()) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
|
||
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
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,33 @@ | ||
package usecases | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/checkmarble/marble-backend/models" | ||
) | ||
|
||
type SanctionCheckProvider interface { | ||
Search(context.Context, models.SanctionCheckConfig, models.SanctionCheckQuery) (models.SanctionCheckResult, error) | ||
} | ||
|
||
type SanctionCheckRepository interface { | ||
InsertResults(context.Context, models.SanctionCheckResult) (models.SanctionCheckResult, error) | ||
} | ||
|
||
type SanctionCheckUsecase struct { | ||
openSanctionsProvider SanctionCheckProvider | ||
repository SanctionCheckRepository | ||
} | ||
|
||
func (uc SanctionCheckUsecase) Execute(ctx context.Context, cfg models.SanctionCheckConfig, | ||
query models.SanctionCheckQuery, | ||
) (models.SanctionCheckResult, error) { | ||
matches, err := uc.openSanctionsProvider.Search(ctx, cfg, query) | ||
if err != nil { | ||
return models.SanctionCheckResult{}, err | ||
} | ||
|
||
result, err := uc.repository.InsertResults(ctx, matches) | ||
|
||
return result, err | ||
} |
Oops, something went wrong.