verification/internal/client/customer.go

56 lines
1.4 KiB
Go
Raw Normal View History

2023-06-12 14:19:10 +00:00
package client
import (
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/backend/verification/internal/models"
2023-06-12 14:19:10 +00:00
)
type Customer struct {
logger *zap.Logger
address string
}
func NewCustomer(logger *zap.Logger, address string) *Customer {
return &Customer{logger: logger, address: address}
}
func (c *Customer) UpdateAccountVerification(userId, status string) (*models.RespUpdateVerificationStatus, error) {
agent := fiber.Patch(fmt.Sprintf("%s/account/%s", c.address, userId))
agent.JSON(&models.ReqCreateVerification{Status: status})
err := agent.Parse()
if err != nil {
c.logger.Error("CustomerUpdateAccountVerification", zap.Error(err))
return nil, err
}
var resp models.RespUpdateVerificationStatus
statusCode, body, errs := agent.Struct(&resp)
if errs != nil || len(errs) > 0 {
for _, err := range errs {
c.logger.Error("CustomerUpdateAccountVerification", zap.Error(err))
}
return nil, errs[0]
}
if statusCode != 200 {
var respErr models.RespUpdateVerificationStatusError
err = json.Unmarshal(body, &respErr)
if err != nil {
c.logger.Error("CustomerUpdateAccountVerification", zap.Error(err))
return nil, err
}
err = fmt.Errorf("got bad status: %v | %v", respErr.StatusCode, respErr.Message)
c.logger.Error("CustomerUpdateAccountVerification", zap.Error(err))
return nil, err
2023-06-12 14:19:10 +00:00
}
return &resp, nil
}