diff --git a/review.go b/review.go index fb712fc..8cea46c 100644 --- a/review.go +++ b/review.go @@ -44,18 +44,25 @@ type Review struct { } type ReviewCreate struct { - LocationId *string `json:"locationId"` - ExternalId *string `json:"externalId"` // Must have v param >= 20220120 - AccountId *string `json:"accountId"` - Rating *float64 `json:"rating"` - Content *string `json:"content"` - AuthorName *string `json:"authorName"` + LocationId *string `json:"locationId,omitempty"` + ExternalId *string `json:"externalId,omitempty"` // Must have v param >= 20220120 + AccountId *string `json:"accountId,omitempty"` + Rating *float64 `json:"rating,omitempty"` + Content *string `json:"content,omitempty"` + AuthorName *string `json:"authorName,omitempty"` AuthorEmail *string `json:"authorEmail,omitempty"` Status *string `json:"status,omitempty"` FlagStatus *string `json:"flagStatus,omitempty"` ReviewLanguage *string `json:"reviewLanguage,omitempty"` TransactionId *string `json:"transactionId,omitempty"` Date *string `json:"date,omitempty"` + + //LiveAPI Specific Fields + ReviewEntity *ReviewEntity `json:"entity,omitempty"` + Title *string `json:"title,omitempty"` + ReviewLabelNames *[]string `json:"reviewLabelNames,omitempty"` + InvitationUID *string `json:"invitationUid"` + ReviewDate *string `json:"reviewDate"` } type Comment struct { diff --git a/review_service.go b/review_service.go index 1ff182c..9c69547 100644 --- a/review_service.go +++ b/review_service.go @@ -1,7 +1,10 @@ package yext import ( + "encoding/json" "fmt" + "io/ioutil" + "net/http" "net/url" "strconv" "strings" @@ -77,21 +80,20 @@ type ReviewUpdateResponse struct { } type ReviewCreateInvitationResponse struct { - InvitationUID string `json:"invitationUid"` - Id string `json:"id"` - LocationId string `json:"locationId"` - Entity *ReviewEntity `json:"entity,omitempty"` // Must have v param >= 20210728 - FirstName string `json:"firstName"` - LastName string `json:"lastName"` - Contact string `json:"contact"` - Image bool `json:"image"` - TemplateId int `json:"templateId"` - Status string `json:"status"` - Details string `json:"details"` + Id string `json:"id"` + LocationId string `json:"locationId"` + Entity *ReviewEntity `json:"entity,omitempty"` // Must have v param >= 20210728 + FirstName string `json:"firstName"` + LastName string `json:"lastName"` + Contact string `json:"contact"` + Image bool `json:"image"` + TemplateId int `json:"templateId"` + Status string `json:"status"` + Details string `json:"details"` } type ReviewCreateReviewResponse struct { - Id string `json:"id"` + Id string `json:"apiIdentifier"` } func (l *ReviewService) ListAllWithOptions(rlOpts *ReviewListOptions) ([]*Review, error) { @@ -249,6 +251,7 @@ func (l *ReviewService) CreateInvitation(jsonData []*Reviewer) ([]*ReviewCreateI return v, r, nil } +//CreateReview: Pls use the new liveAPI implementation below func (l *ReviewService) CreateReview(jsonData *ReviewCreate) (*ReviewCreateReviewResponse, *Response, error) { var v *ReviewCreateReviewResponse r, err := l.client.DoRequestJSON("POST", reviewsPath, jsonData, &v) @@ -259,6 +262,53 @@ func (l *ReviewService) CreateReview(jsonData *ReviewCreate) (*ReviewCreateRevie return v, r, nil } +//the new way to create reviews on the yext platform +//refer to https://yextops.slack.com/archives/C01269F1ZTL/p1634751884059700 +func (l *ReviewService) CreateReviewLiveAPI(jsonData *ReviewCreate) (*ReviewCreateReviewResponse, *Response, error) { + reviewCreateReviewResponse := &ReviewCreateReviewResponse{} + baseURL := "https://liveapi.yext.com" + if strings.Contains(l.client.Config.BaseUrl, "sandbox") { + baseURL = "https://liveapi-sandbox.yext.com" + } + + reviewSubmissionLiveAPIURL := fmt.Sprintf("%s/v2/accounts/%s/reviewSubmission?api_key=%s&v=%s", baseURL, l.client.Config.AccountId, l.client.Config.ApiKey, l.client.Config.Version) + + client := &http.Client{} + req, err := http.NewRequest(http.MethodPost, reviewSubmissionLiveAPIURL, strings.NewReader(jsonData.String())) + if err != nil { + fmt.Println(err) + return nil, nil, err + } + + req.Header.Add("Content-Type", "application/json") + + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + return nil, nil, err + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, nil, err + } + + reviewResponse := &Response{} + + err = json.Unmarshal(body, reviewResponse) + if err != nil { + return nil, nil, err + } + + err = json.Unmarshal(*reviewResponse.ResponseRaw, reviewCreateReviewResponse) + if err != nil { + return nil, nil, err + } + + return reviewCreateReviewResponse, reviewResponse, nil +} + type ReviewUpdateLabelOptions struct { LabelIds *[]int `json:"labelIds,omitempty"` }