crisp-im / go-crisp-api

:hamster: Crisp API Golang Wrapper
https://docs.crisp.chat/guides/rest-api/
Other
37 stars 9 forks source link

Can't delete last segment #11

Closed laduke closed 3 months ago

laduke commented 3 months ago

hey there. I'm probably just doing something wrong, but I can't delete the last segment from a PersonProfile. Modifying the segments otherwise is working. I'm doing this, basically:

...
var profile crisp.PeopleProfileUpdateCard
profile.Segments = []string{} 
res, err := client.Website.UpdatePeopleProfile(websiteId, userId, profile)
...

A go serialization empty array gotcha?

If I do :

        profile.Active = 1
        profile.Segments = make([]string, 0)

the patch request body looks like

{"active":1}
valeriansaliou commented 3 months ago

In the underlying data structure, we are using:

type PeopleProfileUpdateCard struct {
  Segments  []string  `json:"segments,omitempty"`
}

It sounds that the omitempty would kick in there and elude the Segments key from the generated JSON.

Unfortunately we cannot really move Segments to a reference, without breaking all implementations. I'd therefore advise trying to set a dummy segment in place.

laduke commented 3 months ago

ok sounds good. thanks for replying.

I can also just use the internals and avoid the dummy segment

url := fmt.Sprintf("website/%s/people/profile/%s", r.websiteId, userId)
empty := struct {Segments []string `json:"segments"`}{ Segments: make([]string, 0) }
req, err := r.client.NewRequest("PATCH", url, empty)
_, err = r.client.Do(req, nil)

🙃

valeriansaliou commented 3 months ago

Thanks for the tip! That looks much cleaner.