Skip to content

Commit

Permalink
Add RegisterSignupWithParams to support policy id and account id (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
racevedoo authored Mar 26, 2024
1 parent 5eadcd1 commit 27e9c50
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/
#
.idea/
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ assessment, err := client.RegisterSignup("installation-id", &incognia.Address{
})
```

To provide additional parameters like policy id (optional) and account id (optional), use the `RegisterSignupWithParams` method:

```go
address := &incognia.Address{
AddressLine: "20 W 34th St, New York, NY 10001, United States",
StructuredAddress: &incognia.StructuredAddress{
Locale: "en-US",
CountryName: "United States of America",
CountryCode: "US",
State: "NY",
City: "New York City",
Borough: "Manhattan",
Neighborhood: "Midtown",
Street: "W 34th St.",
Number: "20",
Complements: "Floor 2",
PostalCode: "10001",
},
Coordinates: &incognia.Coordinates{
Lat: -23.561414,
Lng: -46.6558819,
},
}
assessment, err := client.RegisterSignupWithParams("installation-id", address, &incognia.Signup{
AccountID: "account-id",//use empty string if you don't have an account id
PolicyID: "policy-id",//use empty string if you don't have a policy id
})
```

### Getting a Signup

This method allows you to query the latest assessment for a given signup event, returning a `SignupAssessment`, containing the risk assessment and supporting evidence:
Expand Down
24 changes: 22 additions & 2 deletions incognia.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ type Address struct {
AddressLine string
}

type Signup struct {
AccountID string
PolicyID string
}

func New(config *IncogniaClientConfig) (*Client, error) {
if config == nil {
return nil, ErrConfigIsNil
Expand Down Expand Up @@ -169,10 +174,21 @@ func (c *Client) RegisterSignup(installationID string, address *Address) (ret *S
}
}()

return c.registerSignup(installationID, address)
return c.registerSignup(installationID, address, nil)
}

func (c *Client) RegisterSignupWithParams(installationID string, address *Address, params *Signup) (ret *SignupAssessment, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
ret = nil
}
}()

return c.registerSignup(installationID, address, params)
}

func (c *Client) registerSignup(installationID string, address *Address) (ret *SignupAssessment, err error) {
func (c *Client) registerSignup(installationID string, address *Address, params *Signup) (ret *SignupAssessment, err error) {
if installationID == "" {
return nil, ErrMissingInstallationID
}
Expand All @@ -185,6 +201,10 @@ func (c *Client) registerSignup(installationID string, address *Address) (ret *S
requestBody.StructuredAddress = address.StructuredAddress
requestBody.Coordinates = address.Coordinates
}
if params != nil {
requestBody.AccountID = params.AccountID
requestBody.PolicyID = params.PolicyID
}

requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions incognia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ var (
Lng: -46.6558819,
},
}

postSignupRequestBodyWithAllParamsFixture = &postAssessmentRequestBody{
InstallationID: installationId,
AddressLine: "address line",
StructuredAddress: &StructuredAddress{
Locale: "locale",
CountryName: "country-name",
CountryCode: "country-code",
State: "state",
City: "city",
Borough: "borough",
Neighborhood: "neighborhood",
Street: "street",
Number: "number",
Complements: "complements",
PostalCode: "postalcode",
},
Coordinates: &Coordinates{
Lat: -23.561414,
Lng: -46.6558819,
},
AccountID: "account-id",
PolicyID: "policy-id",
}
postSignupRequestBodyRequiredFieldsFixture = &postAssessmentRequestBody{
InstallationID: installationId,
}
Expand Down Expand Up @@ -436,6 +460,18 @@ func (suite *IncogniaTestSuite) TestGetSignupAssessmentErrors() {
}
}

func (suite *IncogniaTestSuite) TestSuccessRegisterSignupWithParams() {
signupServer := suite.mockPostSignupsEndpoint(token, postSignupRequestBodyWithAllParamsFixture, signupAssessmentFixture)
defer signupServer.Close()

response, err := suite.client.RegisterSignupWithParams(postSignupRequestBodyWithAllParamsFixture.InstallationID, addressFixture, &Signup{
AccountID: postSignupRequestBodyWithAllParamsFixture.AccountID,
PolicyID: postSignupRequestBodyWithAllParamsFixture.PolicyID,
})
suite.NoError(err)
suite.Equal(signupAssessmentFixture, response)
}

func (suite *IncogniaTestSuite) TestSuccessRegisterSignup() {
signupServer := suite.mockPostSignupsEndpoint(token, postSignupRequestBodyFixture, signupAssessmentFixture)
defer signupServer.Close()
Expand Down
2 changes: 2 additions & 0 deletions request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type postAssessmentRequestBody struct {
AddressLine string `json:"address_line,omitempty"`
StructuredAddress *StructuredAddress `json:"structured_address,omitempty"`
Coordinates *Coordinates `json:"address_coordinates,omitempty"`
AccountID string `json:"account_id,omitempty"`
PolicyID string `json:"policy_id,omitempty"`
}

type FeedbackType string
Expand Down

0 comments on commit 27e9c50

Please sign in to comment.