Untitled
unknown
plain_text
a year ago
2.4 kB
13
Indexable
func FirstContactBuyerToSeller(sendBirdHandler http.HandlerFunc, proxyPrefix string, reqBody CreateChannelRequestBody) (channel.ChannelResponse, error) {
for _, user := range reqBody.Users {
userURL := fmt.Sprintf("%s/v3/users/%s", proxyPrefix, user.UserID)
req, err := http.NewRequest(http.MethodGet, userURL, nil)
if err != nil {
return channel.ChannelResponse{}, err
}
w := httptest.NewRecorder()
sendBirdHandler(w, req)
resp := w.Result()
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode == http.StatusBadRequest {
userCreateURL := fmt.Sprintf("%s/v3/users", proxyPrefix)
userReqBodyBytes, err := json.Marshal(user)
if err != nil {
return channel.ChannelResponse{}, err
}
req, err = http.NewRequest(http.MethodPost, userCreateURL, bytes.NewBuffer(userReqBodyBytes))
if err != nil {
return channel.ChannelResponse{}, err
}
w = httptest.NewRecorder()
sendBirdHandler(w, req)
resp = w.Result()
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode != http.StatusOK {
return channel.ChannelResponse{}, fmt.Errorf("failed to create user, status: %s", resp.Status)
}
} else if resp.StatusCode != http.StatusOK {
return channel.ChannelResponse{}, fmt.Errorf("failed to check user, status: %s", resp.Status)
}
}
channelURL := fmt.Sprintf("%s/v3/group_channels", proxyPrefix)
reqBodyBytes, err := json.Marshal(reqBody)
if err != nil {
return channel.ChannelResponse{}, err
}
req, err := http.NewRequest(http.MethodPost, channelURL, bytes.NewBuffer(reqBodyBytes))
if err != nil {
return channel.ChannelResponse{}, err
}
w := httptest.NewRecorder()
sendBirdHandler(w, req)
resp := w.Result()
if resp.Body != nil {
defer resp.Body.Close()
}
if resp.StatusCode != http.StatusOK {
return channel.ChannelResponse{}, fmt.Errorf("failed to create channel, status: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return channel.ChannelResponse{}, err
}
var channelResponse channel.ChannelResponse
err = json.Unmarshal(body, &channelResponse)
if err != nil {
return channel.ChannelResponse{}, err
}
message := reqBody.DefaultMessage
channelResponse, err = channel.SendMessageToChannel(sendBirdHandler, proxyPrefix, channelResponse, message)
if err != nil {
return channel.ChannelResponse{}, err
}
return channelResponse, nil
}Editor is loading...
Leave a Comment