Skip to content

Commit

Permalink
Continue implementing API for scenario config around Sanction Check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Popineau committed Jan 17, 2025
1 parent 305e3ab commit d8706ca
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
28 changes: 14 additions & 14 deletions dto/scenario_iterations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cockroachdb/errors"

"github.com/checkmarble/marble-backend/models"
"github.com/checkmarble/marble-backend/utils"
)

// Read DTO
Expand Down Expand Up @@ -35,7 +36,7 @@ type ScenarioIterationBodyDto struct {
}

type SanctionCheckConfig struct {
Enabled bool `json:"enabled"`
Enabled *bool `json:"enabled"`
ForceOutcome *string `json:"force_outcome,omitempty"`
ScoreModifier *int `json:"score_modifier,omitempty"`
}
Expand All @@ -60,7 +61,7 @@ func AdaptScenarioIterationWithBodyDto(si models.ScenarioIteration) (ScenarioIte
}
if si.SanctionCheckConfig != nil {
body.SanctionCheckConfig = &SanctionCheckConfig{
Enabled: si.SanctionCheckConfig.Enabled,
Enabled: &si.SanctionCheckConfig.Enabled,
ForceOutcome: nil,
ScoreModifier: &si.SanctionCheckConfig.Outcome.ScoreModifier,
}
Expand Down Expand Up @@ -118,22 +119,21 @@ func AdaptUpdateScenarioIterationInput(input UpdateScenarioIterationBody, iterat
}

if input.Body.SanctionCheckConfig != nil {
var forcedOutcome models.Outcome
var scoreModifier int
updateScenarioIterationInput.Body.SanctionCheckConfig = &models.UpdateSanctionCheckConfigInput{
Enabled: input.Body.SanctionCheckConfig.Enabled,
Outcome: models.UpdateSanctionCheckOutcomeInput{
ForceOutcome: nil,
ScoreModifier: nil,
},
}

if input.Body.SanctionCheckConfig.ForceOutcome != nil {
forcedOutcome = models.OutcomeFrom(*input.Body.SanctionCheckConfig.ForceOutcome)
updateScenarioIterationInput.Body.SanctionCheckConfig.Outcome.ForceOutcome = utils.Ptr(models.OutcomeFrom(
*input.Body.SanctionCheckConfig.ForceOutcome))
}
if input.Body.SanctionCheckConfig.ScoreModifier != nil {
scoreModifier = *input.Body.SanctionCheckConfig.ScoreModifier
}

updateScenarioIterationInput.Body.SanctionCheckConfig = &models.SanctionCheckConfig{
Enabled: input.Body.SanctionCheckConfig.Enabled,
Outcome: models.SanctionCheckOutcome{
ForceOutcome: forcedOutcome,
ScoreModifier: scoreModifier,
},
updateScenarioIterationInput.Body.SanctionCheckConfig.Outcome.ScoreModifier =
input.Body.SanctionCheckConfig.ScoreModifier
}
}

Expand Down
12 changes: 11 additions & 1 deletion models/scenario_iterations.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type UpdateScenarioIterationInput struct {

type UpdateScenarioIterationBody struct {
TriggerConditionAstExpression *ast.Node
SanctionCheckConfig *SanctionCheckConfig
SanctionCheckConfig *UpdateSanctionCheckConfigInput
ScoreReviewThreshold *int
ScoreBlockAndReviewThreshold *int
ScoreDeclineThreshold *int
Expand All @@ -63,3 +63,13 @@ type SanctionCheckOutcome struct {
ForceOutcome Outcome
ScoreModifier int
}

type UpdateSanctionCheckConfigInput struct {
Enabled *bool
Outcome UpdateSanctionCheckOutcomeInput
}

type UpdateSanctionCheckOutcomeInput struct {
ForceOutcome *Outcome
ScoreModifier *int
}
33 changes: 23 additions & 10 deletions repositories/sanction_check_config_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ func (repo *MarbleDbRepository) GetSanctionCheckConfig(ctx context.Context, exec
}

func (repo *MarbleDbRepository) UpdateSanctionCheckConfig(ctx context.Context, exec Executor,
scenarioIterationId string, sanctionCheckConfig models.SanctionCheckConfig,
scenarioIterationId string, sanctionCheckConfig models.UpdateSanctionCheckConfigInput,
) (models.SanctionCheckConfig, error) {
var outcome *string

if sanctionCheckConfig.Outcome.ForceOutcome != models.Approve {
if sanctionCheckConfig.Outcome.ForceOutcome != nil &&
*sanctionCheckConfig.Outcome.ForceOutcome != models.Approve {
outcome = utils.Ptr(sanctionCheckConfig.Outcome.ForceOutcome.String())
}

Expand All @@ -36,14 +37,26 @@ func (repo *MarbleDbRepository) UpdateSanctionCheckConfig(ctx context.Context, e
Values(scenarioIterationId, sanctionCheckConfig.Enabled,
outcome,
sanctionCheckConfig.Outcome.ScoreModifier).
Suffix("ON CONFLICT (scenario_iteration_id) DO UPDATE").
Suffix(`SET
enabled = EXCLUDED.enabled,
forced_outcome = EXCLUDED.forced_outcome,
score_modifier = EXCLUDED.score_modifier,
updated_at = NOW()
`).
Suffix(fmt.Sprintf("RETURNING %s", strings.Join(dbmodels.SanctionCheckConfigColumnList, ",")))
Suffix("ON CONFLICT (scenario_iteration_id) DO UPDATE SET")

updateFields := make([]string, 0, 4)

if sanctionCheckConfig.Enabled != nil {
updateFields = append(updateFields, "enabled = EXCLUDED.enabled")
}
if sanctionCheckConfig.Outcome.ForceOutcome != nil {
updateFields = append(updateFields, "forced_outcome = EXCLUDED.forced_outcome")
}
if sanctionCheckConfig.Outcome.ScoreModifier != nil {
updateFields = append(updateFields, "score_modifier = EXCLUDED.score_modifier")
}

updateFields = append(updateFields, "updated_at = NOW()")

sql = sql.Suffix(strings.Join(updateFields, ","))

sql = sql.Suffix(fmt.Sprintf("RETURNING %s",
strings.Join(dbmodels.SanctionCheckConfigColumnList, ",")))

return SqlToModel(ctx, exec, sql, dbmodels.AdaptSanctionCheckConfig)
}
2 changes: 1 addition & 1 deletion usecases/sanction_check_config_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type SanctionCheckConfigRepository interface {
GetSanctionCheckConfig(ctx context.Context, exec repositories.Executor, scenarioIterationId string) (models.SanctionCheckConfig, error)
UpdateSanctionCheckConfig(ctx context.Context, exec repositories.Executor,
scenarioIterationId string, sanctionCheckConfig models.SanctionCheckConfig) (models.SanctionCheckConfig, error)
scenarioIterationId string, sanctionCheckConfig models.UpdateSanctionCheckConfigInput) (models.SanctionCheckConfig, error)
}

// TODO: Will we have a usecase for sanction checks?
12 changes: 10 additions & 2 deletions usecases/scenario_iterations_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,16 @@ func (usecase *ScenarioIterationUsecase) CreateDraftFromScenarioIteration(
ctx, tx, organizationId, createScenarioIterationInput)

if sanctionCheckConfig != nil {
if _, err := usecase.sanctionCheckConfigRepository.UpdateSanctionCheckConfig(ctx, tx,
newScenarioIteration.Id, *sanctionCheckConfig); err != nil {
newSanctionCheckConfig := models.UpdateSanctionCheckConfigInput{
Enabled: &sanctionCheckConfig.Enabled,
Outcome: models.UpdateSanctionCheckOutcomeInput{
ForceOutcome: &sanctionCheckConfig.Outcome.ForceOutcome,
ScoreModifier: &sanctionCheckConfig.Outcome.ScoreModifier,
},
}

if _, err := usecase.sanctionCheckConfigRepository.UpdateSanctionCheckConfig(
ctx, tx, scenarioIterationId, newSanctionCheckConfig); err != nil {
return models.ScenarioIteration{}, errors.Wrap(err,
"could not duplicate sanction check config for new iteration")
}
Expand Down

0 comments on commit d8706ca

Please sign in to comment.