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"
|
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2021-04-06 09:03:49 +12:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemberBulkUpdatePayload struct {
|
|
|
|
IDs []string `json:"ids"`
|
|
|
|
Profile types.MemberProfile `json:"profile"`
|
|
|
|
}
|
|
|
|
|
2021-09-17 10:58:50 +12:00
|
|
|
func (h *MembersHandler) membersBulkUpdate(w http.ResponseWriter, r *http.Request) error {
|
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 10:58:50 +12:00
|
|
|
return utils.HttpBadRequest("unable to read post body").WithInternalErr(err)
|
2021-04-06 09:03:49 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
header := &MemberBulkUpdatePayload{}
|
|
|
|
if err := json.Unmarshal(bytes, &header); err != nil {
|
2021-09-17 10:58:50 +12:00
|
|
|
return utils.HttpBadRequest("unable to unmarshal payload").WithInternalErr(err)
|
2021-04-06 09:03:49 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, memberId := range header.IDs {
|
|
|
|
// TODO: Bulk select?
|
|
|
|
profile, err := h.members.Select(memberId)
|
|
|
|
if err != nil {
|
2021-09-17 10:58:50 +12:00
|
|
|
return utils.HttpInternalServerError().
|
|
|
|
WithInternalErr(err).
|
|
|
|
WithInternalMsg("unable to select member profile").
|
|
|
|
Msgf("failed to update member %s", memberId)
|
2021-04-06 09:03:49 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
body := &MemberBulkUpdatePayload{
|
|
|
|
Profile: profile,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(bytes, &body); err != nil {
|
2021-09-17 10:58:50 +12:00
|
|
|
return utils.HttpBadRequest().
|
|
|
|
WithInternalErr(err).
|
|
|
|
Msgf("unable to unmarshal payload for member %s", memberId)
|
2021-04-06 09:03:49 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.members.UpdateProfile(memberId, body.Profile); err != nil {
|
2021-09-17 10:58:50 +12:00
|
|
|
return utils.HttpInternalServerError().
|
|
|
|
WithInternalErr(err).
|
|
|
|
WithInternalMsg("unable to update member profile").
|
|
|
|
Msgf("failed to update member %s", memberId)
|
2021-04-06 09:03:49 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 10:58:50 +12:00
|
|
|
return utils.HttpSuccess(w)
|
2021-04-06 09:03:49 +12:00
|
|
|
}
|
2023-11-20 03:19:47 +13:00
|
|
|
|
|
|
|
type MemberBulkDeletePayload struct {
|
|
|
|
IDs []string `json:"ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MembersHandler) membersBulkDelete(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
bytes, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return utils.HttpBadRequest("unable to read post body").WithInternalErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &MemberBulkDeletePayload{}
|
|
|
|
if err := json.Unmarshal(bytes, &data); err != nil {
|
|
|
|
return utils.HttpBadRequest("unable to unmarshal payload").WithInternalErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, memberId := range data.IDs {
|
|
|
|
if err := h.members.Delete(memberId); err != nil {
|
|
|
|
return utils.HttpInternalServerError().
|
|
|
|
WithInternalErr(err).
|
|
|
|
WithInternalMsg("unable to delete member").
|
|
|
|
Msgf("failed to delete member %s", memberId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.HttpSuccess(w)
|
|
|
|
}
|