2021-04-06 09:03:49 +12:00
|
|
|
package members
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-08-30 03:12:37 +12:00
|
|
|
"io"
|
2021-04-06 09:03:49 +12:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MemberBulkUpdatePayload struct {
|
|
|
|
IDs []string `json:"ids"`
|
|
|
|
Profile types.MemberProfile `json:"profile"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MembersHandler) membersBulkUpdate(w http.ResponseWriter, r *http.Request) {
|
2021-08-30 03:12:37 +12:00
|
|
|
bytes, err := io.ReadAll(r.Body)
|
2021-04-06 09:03:49 +12:00
|
|
|
if err != nil {
|
2021-09-17 06:16:51 +12:00
|
|
|
utils.HttpBadRequest(w).WithInternalErr(err).Msg("unable to read post body")
|
2021-04-06 09:03:49 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
header := &MemberBulkUpdatePayload{}
|
|
|
|
if err := json.Unmarshal(bytes, &header); err != nil {
|
2021-09-17 06:16:51 +12:00
|
|
|
utils.HttpBadRequest(w).WithInternalErr(err).Msg("unable to unmarshal payload")
|
2021-04-06 09:03:49 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, memberId := range header.IDs {
|
|
|
|
// TODO: Bulk select?
|
|
|
|
profile, err := h.members.Select(memberId)
|
|
|
|
if err != nil {
|
2021-09-17 06:16:51 +12:00
|
|
|
utils.HttpInternalServerError(w, err).WithInternalMsg("unable to select member profile").Msgf("failed to update member %s", memberId)
|
2021-04-06 09:03:49 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body := &MemberBulkUpdatePayload{
|
|
|
|
Profile: profile,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(bytes, &body); err != nil {
|
2021-09-17 06:16:51 +12:00
|
|
|
utils.HttpBadRequest(w).WithInternalErr(err).Msgf("unable to unmarshal payload for member %s", memberId)
|
2021-04-06 09:03:49 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.members.UpdateProfile(memberId, body.Profile); err != nil {
|
2021-09-17 06:16:51 +12:00
|
|
|
utils.HttpInternalServerError(w, err).WithInternalMsg("unable to update member profile").Msgf("failed to update member %s", memberId)
|
2021-04-06 09:03:49 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.HttpSuccess(w)
|
|
|
|
}
|