From 73a1c9513718fc1e7034a288a024eb01edd8a9fd Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Tue, 20 Oct 2020 05:46:58 -0400 Subject: [PATCH] [MM-28218] API hookup for updating company info and address in MM app (#15974) --- api4/cloud.go | 80 ++++++++++++++++++++++++++++++++++++++++++++ einterfaces/cloud.go | 2 ++ model/client4.go | 30 +++++++++++++++++ model/cloud.go | 27 +++++++++------ 4 files changed, 128 insertions(+), 11 deletions(-) diff --git a/api4/cloud.go b/api4/cloud.go index 8b96a55be0..86001d2232 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -22,7 +22,11 @@ func (api *API) InitCloud() { api.BaseRoutes.Cloud.Handle("/payment/confirm", api.ApiSessionRequired(confirmCustomerPayment)).Methods("POST") // GET /api/v4/cloud/customer + // PUT /api/v4/cloud/customer + // PUT /api/v4/cloud/customer/address api.BaseRoutes.Cloud.Handle("/customer", api.ApiSessionRequired(getCloudCustomer)).Methods("GET") + api.BaseRoutes.Cloud.Handle("/customer", api.ApiSessionRequired(updateCloudCustomer)).Methods("PUT") + api.BaseRoutes.Cloud.Handle("/customer/address", api.ApiSessionRequired(updateCloudCustomerAddress)).Methods("PUT") // GET /api/v4/cloud/subscription api.BaseRoutes.Cloud.Handle("/subscription", api.ApiSessionRequired(getSubscription)).Methods("GET") @@ -107,6 +111,82 @@ func getCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) { w.Write(json) } +func updateCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) { + if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.Cloud { + c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.license_error", nil, "", http.StatusNotImplemented) + return + } + + if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + bodyBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + var customerInfo *model.CloudCustomerInfo + if err = json.Unmarshal(bodyBytes, &customerInfo); err != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + customer, appErr := c.App.Cloud().UpdateCloudCustomer(customerInfo) + if appErr != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.request_error", nil, appErr.Error(), http.StatusInternalServerError) + return + } + + json, err := json.Marshal(customer) + if err != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + w.Write(json) +} + +func updateCloudCustomerAddress(c *Context, w http.ResponseWriter, r *http.Request) { + if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.Cloud { + c.Err = model.NewAppError("Api4.updateCloudCustomerAddress", "api.cloud.license_error", nil, "", http.StatusNotImplemented) + return + } + + if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + bodyBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomerAddress", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + var address *model.Address + if err = json.Unmarshal(bodyBytes, &address); err != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomerAddress", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + customer, appErr := c.App.Cloud().UpdateCloudCustomerAddress(address) + if appErr != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomerAddress", "api.cloud.request_error", nil, appErr.Error(), http.StatusInternalServerError) + return + } + + json, err := json.Marshal(customer) + if err != nil { + c.Err = model.NewAppError("Api4.updateCloudCustomerAddress", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + w.Write(json) +} + func createCustomerPayment(c *Context, w http.ResponseWriter, r *http.Request) { if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.Cloud { c.Err = model.NewAppError("Api4.createCustomerPayment", "api.cloud.license_error", nil, "", http.StatusNotImplemented) diff --git a/einterfaces/cloud.go b/einterfaces/cloud.go index 963067c10f..ed4563b25e 100644 --- a/einterfaces/cloud.go +++ b/einterfaces/cloud.go @@ -14,6 +14,8 @@ type CloudInterface interface { ConfirmCustomerPayment(*model.ConfirmPaymentMethodRequest) *model.AppError GetCloudCustomer() (*model.CloudCustomer, *model.AppError) + UpdateCloudCustomer(customerInfo *model.CloudCustomerInfo) (*model.CloudCustomer, *model.AppError) + UpdateCloudCustomerAddress(address *model.Address) (*model.CloudCustomer, *model.AppError) GetSubscription() (*model.Subscription, *model.AppError) } diff --git a/model/client4.go b/model/client4.go index aac6d59fb1..8e1c7a0fbb 100644 --- a/model/client4.go +++ b/model/client4.go @@ -5686,3 +5686,33 @@ func (c *Client4) GetSubscription() (*Subscription, *Response) { return subscription, BuildResponse(r) } + +func (c *Client4) UpdateCloudCustomer(customerInfo *CloudCustomerInfo) (*CloudCustomer, *Response) { + customerBytes, _ := json.Marshal(customerInfo) + + r, appErr := c.doApiPutBytes(c.GetCloudRoute()+"/customer", customerBytes) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) + } + defer closeBody(r) + + var customer *CloudCustomer + json.NewDecoder(r.Body).Decode(&customer) + + return customer, BuildResponse(r) +} + +func (c *Client4) UpdateCloudCustomerAddress(address *Address) (*CloudCustomer, *Response) { + addressBytes, _ := json.Marshal(address) + + r, appErr := c.doApiPutBytes(c.GetCloudRoute()+"/customer/address", addressBytes) + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) + } + defer closeBody(r) + + var customer *CloudCustomer + json.NewDecoder(r.Body).Decode(&customer) + + return customer, BuildResponse(r) +} diff --git a/model/cloud.go b/model/cloud.go index e4f9bf04c9..b08a4b5167 100644 --- a/model/cloud.go +++ b/model/cloud.go @@ -33,17 +33,22 @@ type ConfirmPaymentMethodRequest struct { // Customer model represents a customer on the system. type CloudCustomer struct { - ID string `json:"id"` - CreatorID string `json:"creator_id"` - CreateAt int64 `json:"create_at"` - Email string `json:"email"` - Name string `json:"name"` - NumEmployees int `json:"num_employees"` - ContactFirstName string `json:"contact_first_name"` - ContactLastName string `json:"contact_last_name"` - BillingAddress *Address `json:"billing_address"` - CompanyAddress *Address `json:"company_address"` - PaymentMethod *PaymentMethod `json:"payment_method"` + CloudCustomerInfo + ID string `json:"id"` + CreatorID string `json:"creator_id"` + CreateAt int64 `json:"create_at"` + BillingAddress *Address `json:"billing_address"` + CompanyAddress *Address `json:"company_address"` + PaymentMethod *PaymentMethod `json:"payment_method"` +} + +// CloudCustomerInfo represents editable info of a customer. +type CloudCustomerInfo struct { + Name string `json:"name"` + Email string `json:"email"` + ContactFirstName string `json:"contact_first_name"` + ContactLastName string `json:"contact_last_name"` + NumEmployees int `json:"num_employees"` } // Address model represents a customer's address.