Skip to content

Commit

Permalink
Store enrichment status and prevent enriching twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Feb 26, 2025
1 parent f4cab5a commit cb77aa3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions dto/sanction_check_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type SanctionCheckMatchDto struct {
Datasets []string `json:"datasets"`
UniqueCounterpartyIdentifier *string `json:"unique_counterparty_identifier"`
Payload json.RawMessage `json:"payload"`
Enriched bool `json:"enriched"`
Comments []SanctionCheckMatchCommentDto `json:"comments"`
}

Expand All @@ -99,6 +100,7 @@ func AdaptSanctionCheckMatchDto(m models.SanctionCheckMatch) SanctionCheckMatchD
QueryIds: m.QueryIds,
Datasets: make([]string, 0),
Payload: m.Payload,
Enriched: m.Enriched,
UniqueCounterpartyIdentifier: m.UniqueCounterpartyIdentifier,
Comments: pure_utils.Map(m.Comments, AdaptSanctionCheckMatchCommentDto),
}
Expand Down
1 change: 1 addition & 0 deletions models/sanction_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ type SanctionCheckMatch struct {
QueryIds []string
UniqueCounterpartyIdentifier *string
Payload []byte
Enriched bool
ReviewedBy *string
Comments []SanctionCheckMatchComment
}
Expand Down
2 changes: 2 additions & 0 deletions repositories/dbmodels/db_sanction_check_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DBSanctionCheckMatch struct {
QueryIds []string `db:"query_ids"`
CounterpartyId *string `db:"counterparty_id"`
Payload json.RawMessage `db:"payload"`
Enriched bool `db:"enriched"`
ReviewedBy *string `db:"reviewed_by"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Expand All @@ -37,6 +38,7 @@ func AdaptSanctionCheckMatch(dto DBSanctionCheckMatch) (models.SanctionCheckMatc
QueryIds: dto.QueryIds,
UniqueCounterpartyIdentifier: dto.CounterpartyId,
Payload: dto.Payload,
Enriched: dto.Enriched,
}

return match, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up

alter table sanction_check_matches
add column enriched bool default false;

-- +goose Down

alter table sanction_check_matches
drop column enriched;
1 change: 1 addition & 0 deletions repositories/sanction_check_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (*MarbleDbRepository) UpdateSanctionCheckMatchPayload(ctx context.Context,
sql := NewQueryBuilder().
Update(dbmodels.TABLE_SANCTION_CHECK_MATCHES).
Set("payload", newPayload).
Set("enriched", true).
Set("updated_at", "NOW()").
Where(squirrel.Eq{"id": match.Id}).Suffix(fmt.Sprintf("RETURNING %s",
strings.Join(dbmodels.SelectSanctionCheckMatchesColumn, ",")))
Expand Down
5 changes: 5 additions & 0 deletions usecases/sanction_check_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ func (uc SanctionCheckUsecase) EnrichMatch(ctx context.Context, matchId string)
return models.SanctionCheckMatch{}, err
}

if match.Enriched {
return models.SanctionCheckMatch{}, errors.Wrap(models.ConflictError,
"this sanction check match was already enriched")
}

newPayload, err := uc.openSanctionsProvider.EnrichMatch(ctx, match)
if err != nil {
return models.SanctionCheckMatch{}, err
Expand Down
10 changes: 5 additions & 5 deletions usecases/sanction_check_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestListSanctionChecksOnDecision(t *testing.T) {
exec.Mock.ExpectQuery(escapeSql(`
SELECT
sc.id, sc.decision_id, sc.status, sc.search_input, sc.search_datasets, sc.match_threshold, sc.match_limit, sc.is_manual, sc.requested_by, sc.is_partial, sc.is_archived, sc.initial_has_matches, sc.whitelisted_entities, sc.error_codes, sc.created_at, sc.updated_at,
ARRAY_AGG(ROW(scm.id,scm.sanction_check_id,scm.opensanction_entity_id,scm.status,scm.query_ids,scm.counterparty_id,scm.payload,scm.reviewed_by,scm.created_at,scm.updated_at) ORDER BY array_position(.+, scm.status), scm.payload->>'score' DESC) FILTER (WHERE scm.id IS NOT NULL) AS matches
ARRAY_AGG(ROW(scm.id,scm.sanction_check_id,scm.opensanction_entity_id,scm.status,scm.query_ids,scm.counterparty_id,scm.payload,scm.enriched,scm.reviewed_by,scm.created_at,scm.updated_at) ORDER BY array_position(.+, scm.status), scm.payload->>'score' DESC) FILTER (WHERE scm.id IS NOT NULL) AS matches
FROM sanction_checks AS sc
LEFT JOIN sanction_check_matches AS scm ON sc.id = scm.sanction_check_id
WHERE sc.decision_id = $1 AND sc.is_archived = $2
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestUpdateMatchStatus(t *testing.T) {
}))

exec.Mock.
ExpectQuery(`SELECT id, sanction_check_id, opensanction_entity_id, status, query_ids, counterparty_id, payload, reviewed_by, created_at, updated_at FROM sanction_check_matches WHERE id = \$1`).
ExpectQuery(`SELECT id, sanction_check_id, opensanction_entity_id, status, query_ids, counterparty_id, payload, enriched, reviewed_by, created_at, updated_at FROM sanction_check_matches WHERE id = \$1`).
WithArgs("matchid").
WillReturnRows(pgxmock.NewRows(dbmodels.SelectSanctionCheckMatchesColumn).
AddRow(mockScmRow...),
Expand All @@ -157,20 +157,20 @@ func TestUpdateMatchStatus(t *testing.T) {
WillReturnRows(pgxmock.NewRows(dbmodels.SelectSanctionChecksColumn).
AddRow(mockScRow...),
)
exec.Mock.ExpectQuery(`SELECT id, sanction_check_id, opensanction_entity_id, status, query_ids, counterparty_id, payload, reviewed_by, created_at, updated_at FROM sanction_check_matches WHERE sanction_check_id = \$1`).
exec.Mock.ExpectQuery(`SELECT id, sanction_check_id, opensanction_entity_id, status, query_ids, counterparty_id, payload, enriched, reviewed_by, created_at, updated_at FROM sanction_check_matches WHERE sanction_check_id = \$1`).
WithArgs("sanction_check_id").
WillReturnRows(pgxmock.NewRows(dbmodels.SelectSanctionCheckMatchesColumn).
AddRow(mockScmRow...).
AddRows(mockOtherScmRows...),
)
exec.Mock.ExpectQuery(`UPDATE sanction_check_matches SET reviewed_by = \$1, status = \$2, updated_at = \$3 WHERE id = \$4 RETURNING id,sanction_check_id,opensanction_entity_id,status,query_ids,counterparty_id,payload,reviewed_by,created_at,updated_at`).
exec.Mock.ExpectQuery(`UPDATE sanction_check_matches SET reviewed_by = \$1, status = \$2, updated_at = \$3 WHERE id = \$4 RETURNING id,sanction_check_id,opensanction_entity_id,status,query_ids,counterparty_id,payload,enriched,reviewed_by,created_at,updated_at`).
WithArgs(models.UserId(""), models.SanctionMatchStatusConfirmedHit, "NOW()", "matchid").
WillReturnRows(pgxmock.NewRows(dbmodels.SelectSanctionCheckMatchesColumn).
AddRow(mockScmRow...),
)

for i := range 3 {
exec.Mock.ExpectQuery(`UPDATE sanction_check_matches SET reviewed_by = \$1, status = \$2, updated_at = \$3 WHERE id = \$4 RETURNING id,sanction_check_id,opensanction_entity_id,status,query_ids,counterparty_id,payload,reviewed_by,created_at,updated_at`).
exec.Mock.ExpectQuery(`UPDATE sanction_check_matches SET reviewed_by = \$1, status = \$2, updated_at = \$3 WHERE id = \$4 RETURNING id,sanction_check_id,opensanction_entity_id,status,query_ids,counterparty_id,payload,enriched,reviewed_by,created_at,updated_at`).
WithArgs(models.UserId(""), models.SanctionMatchStatusSkipped, "NOW()", mockOtherScms[i].Id).
WillReturnRows(pgxmock.NewRows(dbmodels.SelectSanctionCheckMatchesColumn).
AddRow(mockOtherScmRows[i]...),
Expand Down

0 comments on commit cb77aa3

Please sign in to comment.