Skip to content

Commit

Permalink
search: start mapping v2 requests to Entity for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 3, 2025
1 parent d10a44e commit 4b8746f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
81 changes: 80 additions & 1 deletion internal/search/api_search.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package search

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/moov-io/base/log"
"github.com/moov-io/watchman/pkg/search"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -36,6 +40,81 @@ func (c *controller) AppendRoutes(router *mux.Router) *mux.Router {
return router
}

type searchResponse struct {
Entities []SearchedEntity[search.Value] `json:"entities"`
}

type errorResponse struct {
Error string `json:"error"`
}

func (c *controller) search(w http.ResponseWriter, r *http.Request) {
c.service.Search(r.Context())
req, err := readSearchRequest(r)
if err != nil {
err = fmt.Errorf("problem reading v2 search request: %w", err)
c.logger.Error().LogError(err)

w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(errorResponse{
Error: err.Error(),
})

return
}

entities, err := c.service.Search(r.Context(), req)
if err != nil {
c.logger.Error().LogErrorf("problem with v2 search: %v", err)

w.WriteHeader(http.StatusBadRequest)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(searchResponse{
Entities: entities,
})
}

func readSearchRequest(r *http.Request) (search.Entity[search.Value], error) {
q := r.URL.Query()

var req search.Entity[search.Value]

req.Name = strings.TrimSpace(q.Get("name"))
req.Type = search.EntityType(strings.TrimSpace(strings.ToLower(q.Get("entityType"))))
req.Source = search.SourceAPIRequest
req.SourceID = strings.TrimSpace(q.Get("requestID"))

switch req.Type {
case search.EntityPerson: // "person"

case search.EntityBusiness: // "business"

case search.EntityAircraft: // "aircraft"

case search.EntityVessel: // "vessel"

default:
return req, fmt.Errorf("unsupported entityType: %v", req.Type)
}

// Person *Person `json:"person"`
// Business *Business `json:"business"`
// Organization *Organization `json:"organization"`
// Aircraft *Aircraft `json:"aircraft"`
// Vessel *Vessel `json:"vessel"`

// CryptoAddresses []CryptoAddress `json:"cryptoAddresses"`
// TODO(adam): support multiple values? How does Go handle that?

// Addresses []Address `json:"addresses"`

// Affiliations []Affiliation `json:"affiliations"`
// SanctionsInfo *SanctionsInfo `json:"sanctionsInfo"`
// HistoricalInfo []HistoricalInfo `json:"historicalInfo"`
// Titles []string `json:"titles"`

return req, nil
}
2 changes: 2 additions & 0 deletions pkg/search/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
type SourceList string

var (
SourceAPIRequest SourceList = "api-request"

SourceEUCSL SourceList = "eu_csl"
SourceUKCSL SourceList = "uk_csl"
SourceUSCSL SourceList = "us_csl"
Expand Down

0 comments on commit 4b8746f

Please sign in to comment.