Skip to content

Commit fe75122

Browse files
committed
Change event type ID handling to distinguish between "leave alone" and "clear" values
1 parent e0bd546 commit fe75122

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

‎frontend/src/routes/availability/+page.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
block.saving = true;
101101
try {
102102
await api.patch(`/v1/availability-rules/${block.id}`, {
103-
event_type_id: block.event_type_id === null ? "null" : block.event_type_id,
103+
event_type_id: block.event_type_id === "null" ? null : block.event_type_id,
104104
start_time: block.start_time,
105105
end_time: block.end_time
106106
});

‎internal/handler/availability.go‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
123123
r.Body = http.MaxBytesReader(w, r.Body, 32<<10)
124124

125125
var req struct {
126-
EventTypeId *string `json:"event_type_id"`
127-
DayOfWeek *int `json:"day_of_week"`
128-
StartTime *string `json:"start_time"`
129-
EndTime *string `json:"end_time"`
126+
EventTypeId json.RawMessage `json:"event_type_id"` // `nil` = leave alone, `null` = clear
127+
DayOfWeek *int `json:"day_of_week"`
128+
StartTime *string `json:"start_time"`
129+
EndTime *string `json:"end_time"`
130130
}
131131
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
132132
h.writeError(w, http.StatusBadRequest, "invalid JSON")
@@ -152,15 +152,14 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
152152
return
153153
}
154154

155-
// As there's no good way to distinguish between "set etId to null" and "don't set etId" we're using a string of
156-
// `null` to represent the former.
157155
if req.EventTypeId != nil {
158-
etID := req.EventTypeId
159-
if *etID == "null" {
160-
current.EventTypeID = nil
161-
} else {
162-
current.EventTypeID = req.EventTypeId
156+
var etId *string
157+
if err := json.Unmarshal(req.EventTypeId, &etId); err != nil {
158+
h.writeError(w, http.StatusBadRequest, "event_type_id must be a string")
159+
return
163160
}
161+
162+
current.EventTypeID = etId
164163
}
165164

166165
if req.DayOfWeek != nil {

0 commit comments

Comments
 (0)