add methods for invoices

This commit is contained in:
diPhantxm
2023-03-23 21:45:02 +03:00
parent 021a577bf9
commit 6b921ad101
4 changed files with 256 additions and 3 deletions

View File

@@ -147,9 +147,9 @@
- [ ] Mark messages as read
## Invoices
- [ ] Create or edit proforma invoice link
- [ ] Get a proforma invoice link
- [ ] Delete the proforma invoice link
- [x] Create or edit proforma invoice link
- [x] Get a proforma invoice link
- [x] Delete the proforma invoice link
## Reports
- [x] Report details

97
ozon/invoices.go Normal file
View File

@@ -0,0 +1,97 @@
package ozon
import (
"net/http"
core "github.com/diphantxm/ozon-api-client"
)
type Invoices struct {
client *core.Client
}
type CreateUpdateProformaLinkParams struct {
// Shipment number
PostingNumber string `json:"posting_number"`
// Proforma invoice link
URL string `json:"url"`
}
type CreateUpdateProformaLinkResponse struct {
core.CommonResponse
// Method result
Result bool `json:"result"`
}
// Create or edit proforma invoice link for VAT refund to Turkey sellers
func (c Invoices) CreateUpdate(params *CreateUpdateProformaLinkParams) (*CreateUpdateProformaLinkResponse, error) {
url := "/v1/invoice/create-or-update"
resp := &CreateUpdateProformaLinkResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type GetProformaLinkParams struct {
// Shipment number
PostingNumber string `json:"posting_number"`
}
type GetProformaLinkResponse struct {
core.CommonResponse
// Method result
Result struct {
// Proforma invoice link
FileURL string `json:"file_url"`
} `json:"result"`
}
// Get a proforma invoice link
func (c Invoices) Get(params *GetProformaLinkParams) (*GetProformaLinkResponse, error) {
url := "/v1/invoice/get"
resp := &GetProformaLinkResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type DeleteProformaLinkParams struct {
// Shipment number
PostingNumber string `json:"posting_number"`
}
type DeleteProformaLinkResponse struct {
core.CommonResponse
// Method result
Result bool `json:"result"`
}
func (c Invoices) Delete(params *DeleteProformaLinkParams) (*DeleteProformaLinkResponse, error) {
url := "/v1/invoice/delete"
resp := &DeleteProformaLinkResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}

149
ozon/invoices_test.go Normal file
View File

@@ -0,0 +1,149 @@
package ozon
import (
"net/http"
"testing"
core "github.com/diphantxm/ozon-api-client"
)
func TestCreateUpdateProformaLink(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *CreateUpdateProformaLinkParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&CreateUpdateProformaLinkParams{
PostingNumber: "posting number",
URL: "link",
},
`{
"result": true
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&CreateUpdateProformaLinkParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Invoices().CreateUpdate(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestGetProformaLink(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *GetProformaLinkParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&GetProformaLinkParams{
PostingNumber: "posting number",
},
`{
"result": {
"file_url": "string"
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&GetProformaLinkParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Invoices().Get(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestDeleteProformaLink(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *DeleteProformaLinkParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&DeleteProformaLinkParams{
PostingNumber: "posting number",
},
`{
"result": true
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&DeleteProformaLinkParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Invoices().Delete(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}

View File

@@ -26,6 +26,7 @@ type Client struct {
cancellations *Cancellations
categories *Categories
polygons *Polygons
invoices *Invoices
}
func (c Client) Analytics() *Analytics {
@@ -80,6 +81,10 @@ func (c Client) Polygons() *Polygons {
return c.polygons
}
func (c Client) Invoices() *Invoices {
return c.invoices
}
func NewClient(clientId, apiKey string) *Client {
coreClient := core.NewClient(DefaultAPIBaseUrl, map[string]string{
"Client-Id": clientId,
@@ -101,6 +106,7 @@ func NewClient(clientId, apiKey string) *Client {
cancellations: &Cancellations{client: coreClient},
categories: &Categories{client: coreClient},
polygons: &Polygons{client: coreClient},
invoices: &Invoices{client: coreClient},
}
}
@@ -122,5 +128,6 @@ func NewMockClient(handler http.HandlerFunc) *Client {
cancellations: &Cancellations{client: coreClient},
categories: &Categories{client: coreClient},
polygons: &Polygons{client: coreClient},
invoices: &Invoices{client: coreClient},
}
}