Skip to content

Commit

Permalink
feat: add support for expression in request body when fetching metada… (
Browse files Browse the repository at this point in the history
#170)

* feat: add support for expression in request body when fetching metadata sources for appeal

* add a condition in http mock call

---------

Co-authored-by: anjali.agarwal <[email protected]>
  • Loading branch information
anjali9791 and anjaliagg9791 authored Aug 13, 2024
1 parent 6fbebbe commit 521ab2d
Show file tree
Hide file tree
Showing 2 changed files with 836 additions and 15 deletions.
46 changes: 31 additions & 15 deletions core/appeal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,25 +1554,20 @@ func (s *Service) populateAppealMetadata(ctx context.Context, a *domain.Appeal,
if cfg.URL == "" {
return fmt.Errorf("URL cannot be empty for http type")
}
if strings.Contains(cfg.URL, "$appeal") {
appealMap, err := a.ToMap()
if err != nil {
return fmt.Errorf("error converting appeal to map: %w", err)
}
params := map[string]interface{}{"appeal": appealMap}
url, err := evaluator.Expression(cfg.URL).EvaluateWithVars(params)
if err != nil {
return fmt.Errorf("error evaluating URL expression: %w", err)
}
urlStr, ok := url.(string)
if !ok {
return fmt.Errorf("URL expression must evaluate to a string")
}
cfg.URL = urlStr

var err error
cfg.URL, err = evaluateExpressionWithAppeal(a, cfg.URL)
if err != nil {
return err
}

cfg.Body, err = evaluateExpressionWithAppeal(a, cfg.Body)
if err != nil {
return err
}
clientCreator := &http.HttpClientCreatorStruct{}
metadataCl, err := http.NewHTTPClient(&cfg.HTTPClientConfig, clientCreator)

if err != nil {
return fmt.Errorf("key: %s, %w", key, err)
}
Expand Down Expand Up @@ -1605,6 +1600,7 @@ func (s *Service) populateAppealMetadata(ctx context.Context, a *domain.Appeal,
"response": responseMap,
"appeal": a,
}

value, err := metadata.EvaluateValue(params)
if err != nil {
return fmt.Errorf("error parsing value: %w", err)
Expand Down Expand Up @@ -1785,3 +1781,23 @@ func (s *Service) prepareGrant(ctx context.Context, appeal *domain.Appeal) (newG
func (s *Service) GetAppealsTotalCount(ctx context.Context, filters *domain.ListAppealsFilter) (int64, error) {
return s.repo.GetAppealsTotalCount(ctx, filters)
}

func evaluateExpressionWithAppeal(a *domain.Appeal, expression string) (string, error) {
if expression != "" && strings.Contains(expression, "$appeal") {
appealMap, err := a.ToMap()
if err != nil {
return "", fmt.Errorf("error converting appeal to map: %w", err)
}
params := map[string]interface{}{"appeal": appealMap}
evaluated, err := evaluator.Expression(expression).EvaluateWithVars(params)
if err != nil {
return "", fmt.Errorf("error evaluating expression %w", err)
}
evaluatedStr, ok := evaluated.(string)
if !ok {
return "", fmt.Errorf("expression must evaluate to a string")
}
return evaluatedStr, nil
}
return expression, nil
}
Loading

0 comments on commit 521ab2d

Please sign in to comment.