Repository: VictorAvelar/mollie-api-go
Pull request #160 (2021-10-31 19:38:48)

add organization partner status action
<!--- Provide a general summary of your changes in the Title above -->

## Description

Adds endpoint to check the current organization partner status.

The usage of this endpoint requires a valid access token.

## Motivation and context

Follow up to complete #156 

## How has this been tested?

Unit test coverage added.

## Types of changes

What types of changes does your code introduce? Put an `x` in all the boxes that apply:
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

Go over all the following points, and put an `x` in all the boxes that apply.

Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our continuous integration server to make sure your [tests and code style pass](https://help.github.com/articles/about-required-status-checks/).

- [x] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document.
- [x] My pull request addresses exactly one patch/feature.
- [x] I have created a branch for this patch/feature.
- [x] Each individual commit in the pull request is meaningful.
- [x] I have added tests to cover my changes.
- [x] If my change requires a change to the documentation, I have updated it accordingly.

If you're unsure about any of these, don't hesitate to ask. We're here to help!



Patch:
diff --git a/docs/README.md b/docs/README.md
index 29d1200..1fb81cc 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -1848,6 +1848,35 @@ type OrganizationLinks struct {
 OrganizationLinks describes all the possible links to be returned with a
 organization object.
 
+#### type OrganizationPartnerLinks
+
+```go
+type OrganizationPartnerLinks struct {
+	Self          *URL `json:"self,omitempty"`
+	Documentation *URL `json:"documentation,omitempty"`
+	SignUpLink    *URL `json:"signuplink,omitempty"`
+}
+```
+
+OrganizationPartnerLinks is an object with several URL objects relevant to the
+partner resource.
+
+#### type OrganizationPartnerStatus
+
+```go
+type OrganizationPartnerStatus struct {
+	IsCommissionPartner            bool                     `json:"isCommissionPartner,omitempty"`
+	PartnerContractUpdateAvailable bool                     `json:"partnerContractUpdate_available,omitempty"`
+	Resource                       string                   `json:"resource,omitempty"`
+	PartnerType                    PartnerType              `json:"partnerType,omitempty"`
+	UserAgentTokens                []*UserAgentToken        `json:"userAgentTokens,omitempty"`
+	PartnerContractSignedAt        *time.Time               `json:"partnerContractSignedAt,omitempty"`
+	Links                          OrganizationPartnerLinks `json:"_links,omitempty"`
+}
+```
+
+OrganizationPartnerStatus response descriptor.
+
 #### type OrganizationsService
 
 ```go
@@ -1872,6 +1901,17 @@ func (os *OrganizationsService) GetCurrent() (o *Organization, err error)
 
 GetCurrent retrieve the currently authenticated organization
 
+#### func (\*OrganizationsService) GetPartnerStatus
+
+```go
+func (os *OrganizationsService) GetPartnerStatus() (ops *OrganizationPartnerStatus, err error)
+```
+
+GetPartnerStatus retrieves details about the partner status of the currently
+authenticated organization.
+
+See: https://docs.mollie.com/reference/v2/organizations-api/get-partner
+
 #### type PaginationLinks
 
 ```go
@@ -1954,6 +1994,24 @@ Get retrieves all clients.
 
 See: https://docs.mollie.com/reference/v2/partners-api/list-clients
 
+#### type PartnerType
+
+```go
+type PartnerType string
+```
+
+PartnerType alias for organization partner types.
+
+```go
+const (
+	PartnerTypeOauth      PartnerType = "oauth"
+	PartnerTypeSignUpLink PartnerType = "signuplink"
+	PartnerTypeUserAgent  PartnerType = "useragent"
+)
+```
+
+Available partner types.
+
 #### type Payment
 
 ```go
@@ -3491,3 +3549,15 @@ type UsedGiftCard struct {
 ```
 
 UsedGiftCard describes a used gift card.
+
+#### type UserAgentToken
+
+```go
+type UserAgentToken struct {
+	Token    string
+	StartsAt *time.Time
+	EndsAt   *time.Time
+}
+```
+
+UserAgentToken are time limited valid access tokens.
diff --git a/mollie/organizations.go b/mollie/organizations.go
index fcb17b4..417a27a 100644
--- a/mollie/organizations.go
+++ b/mollie/organizations.go
@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"net/http"
+	"time"
 )
 
 // Organization describes an organization detail
@@ -35,6 +36,42 @@ type OrganizationLinks struct {
 	Documentation *URL `json:"documentation,omitempty"`
 }
 
+// PartnerType alias for organization partner types.
+type PartnerType string
+
+// Available partner types.
+const (
+	PartnerTypeOauth      PartnerType = "oauth"
+	PartnerTypeSignUpLink PartnerType = "signuplink"
+	PartnerTypeUserAgent  PartnerType = "useragent"
+)
+
+// UserAgentToken are time limited valid access tokens.
+type UserAgentToken struct {
+	Token    string
+	StartsAt *time.Time
+	EndsAt   *time.Time
+}
+
+// OrganizationPartnerLinks is an object with several URL objects
+// relevant to the partner resource.
+type OrganizationPartnerLinks struct {
+	Self          *URL `json:"self,omitempty"`
+	Documentation *URL `json:"documentation,omitempty"`
+	SignUpLink    *URL `json:"signuplink,omitempty"`
+}
+
+// OrganizationPartnerStatus response descriptor.
+type OrganizationPartnerStatus struct {
+	IsCommissionPartner            bool                     `json:"isCommissionPartner,omitempty"`
+	PartnerContractUpdateAvailable bool                     `json:"partnerContractUpdate_available,omitempty"`
+	Resource                       string                   `json:"resource,omitempty"`
+	PartnerType                    PartnerType              `json:"partnerType,omitempty"`
+	UserAgentTokens                []*UserAgentToken        `json:"userAgentTokens,omitempty"`
+	PartnerContractSignedAt        *time.Time               `json:"partnerContractSignedAt,omitempty"`
+	Links                          OrganizationPartnerLinks `json:"_links,omitempty"`
+}
+
 // OrganizationsService instance operates over organization resources
 type OrganizationsService service
 
@@ -48,6 +85,28 @@ func (os *OrganizationsService) GetCurrent() (o *Organization, err error) {
 	return os.get("v2/organizations/me")
 }
 
+// GetPartnerStatus retrieves details about the partner status
+// of the currently authenticated organization.
+//
+// See: https://docs.mollie.com/reference/v2/organizations-api/get-partner
+func (os *OrganizationsService) GetPartnerStatus() (ops *OrganizationPartnerStatus, err error) {
+	req, err := os.client.NewAPIRequest(http.MethodGet, "v2/organizations/me/partner", nil)
+	if err != nil {
+		return
+	}
+
+	res, err := os.client.Do(req)
+	if err != nil {
+		return
+	}
+
+	if err = json.Unmarshal(res.content, &ops); err != nil {
+		return
+	}
+
+	return
+}
+
 func (os *OrganizationsService) get(uri string) (o *Organization, err error) {
 	req, err := os.client.NewAPIRequest(http.MethodGet, uri, nil)
 	if err != nil {


Test patch:
diff --git a/mollie/organizations_test.go b/mollie/organizations_test.go
index 17fe677..c2a5a5d 100644
--- a/mollie/organizations_test.go
+++ b/mollie/organizations_test.go
@@ -61,6 +61,32 @@ func TestOrganizationsService_GetCurrent(t *testing.T) {
 	}
 }
 
+func TestOrganizationsService_GetPartnerStatus(t *testing.T) {
+	setup()
+	defer teardown()
+	_ = tClient.WithAuthenticationValue("access_token")
+	tMux.HandleFunc("/v2/organizations/me/partner", func(w http.ResponseWriter, r *http.Request) {
+		testHeader(t, r, AuthHeader, "Bearer access_token")
+		testMethod(t, r, "GET")
+		if _, ok := r.Header[AuthHeader]; !ok {
+			w.WriteHeader(http.StatusUnauthorized)
+		}
+
+		w.WriteHeader(http.StatusOK)
+		_, _ = w.Write([]byte(testdata.GetPartnerStatusResponse))
+	})
+
+	res, err := tClient.Organizations.GetPartnerStatus()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if res.PartnerType != PartnerTypeSignUpLink {
+		t.Errorf("mismatching info. got %v, want %v", res.PartnerType, PartnerTypeSignUpLink)
+	}
+
+}
+
 func TestOrganizationsService_HttpRequestErrors(t *testing.T) {
 	setup()
 	defer teardown()
@@ -68,8 +94,9 @@ func TestOrganizationsService_HttpRequestErrors(t *testing.T) {
 
 	_, gerr := tClient.Organizations.Get("org_12345678")
 	_, gcerr := tClient.Organizations.GetCurrent()
+	_, gpserr := tClient.Organizations.GetPartnerStatus()
 
-	tests := []error{gerr, gcerr}
+	tests := []error{gerr, gcerr, gpserr}
 
 	for _, tt := range tests {
 		if tt == nil {
@@ -87,8 +114,9 @@ func TestOrganizationsService_NewAPIRequestErrors(t *testing.T) {
 
 	_, gerr := tClient.Organizations.Get("org_12345678")
 	_, gcerr := tClient.Organizations.GetCurrent()
+	_, gpserr := tClient.Organizations.GetPartnerStatus()
 
-	tests := []error{gerr, gcerr}
+	tests := []error{gerr, gcerr, gpserr}
 
 	for _, tt := range tests {
 		if tt != errBadBaseURL {
@@ -104,8 +132,9 @@ func TestOrganizationsService_EncodingResponseErrors(t *testing.T) {
 
 	_, gerr := tClient.Organizations.Get("org_12345678")
 	_, gcerr := tClient.Organizations.GetCurrent()
+	_, gpserr := tClient.Organizations.GetPartnerStatus()
 
-	tests := []error{gerr, gcerr}
+	tests := []error{gerr, gcerr, gpserr}
 
 	for _, tt := range tests {
 		if tt == nil {
diff --git a/testdata/organizations.go b/testdata/organizations.go
index e8a9746..4266d3d 100644
--- a/testdata/organizations.go
+++ b/testdata/organizations.go
@@ -79,3 +79,23 @@ const GetCurrentOrganizationResponse = `{
          }
      }
  }`
+
+const GetPartnerStatusResponse = `{
+    "resource": "partner",
+    "partnerType": "signuplink",
+    "partnerContractSignedAt": "2018-03-20T13:13:37+00:00",
+    "_links": {
+        "self": {
+            "href": "https://api.mollie.com/v2/organizations/me/partner",
+            "type": "application/hal+json"
+        },
+        "documentation": {
+            "href": "https://docs.mollie.com/reference/v2/partners-api/get-partner",
+            "type": "text/html"
+        },
+        "signuplink": {
+            "href": "https://www.mollie.com/dashboard/signup/myCode?lang=en",
+            "type": "text/html"
+        }
+    }
+}`