diff --git a/server/internal/qldt/client.go b/server/internal/qldt/client.go index b5922ae..b5905bd 100644 --- a/server/internal/qldt/client.go +++ b/server/internal/qldt/client.go @@ -221,43 +221,48 @@ func (c *Client) GetLeaveRequests(ctx context.Context, token string, classID, co return body, nil } -func (c *Client) UpdateLeaveStatus(ctx context.Context, token string, leaveID int64, status string) ([]byte, error) { +func (c *Client) updateLeaveStatusAttempt(ctx context.Context, token string, leaveID int64, status string, method string) ([]byte, int, error) { payload := map[string]string{"status": status} jsonData, _ := json.Marshal(payload) u := fmt.Sprintf("%s/request-leave/%d/status", c.baseURL, leaveID) - req, err := http.NewRequestWithContext(ctx, http.MethodPatch, u, bytes.NewBuffer(jsonData)) + req, err := http.NewRequestWithContext(ctx, method, u, bytes.NewBuffer(jsonData)) if err != nil { - return nil, err + return nil, 0, err } req.Header.Set("Content-Type", "application/json") c.setAuthHeaders(req, token) resp, err := c.http.Do(req) if err != nil { - return nil, err + return nil, 0, err } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - // Fallback to POST method if PATCH is Method Not Allowed or Not Found - if resp.StatusCode == http.StatusMethodNotAllowed || resp.StatusCode == http.StatusNotFound { - req2, err2 := http.NewRequestWithContext(ctx, http.MethodPost, u, bytes.NewBuffer(jsonData)) - if err2 == nil { - req2.Header.Set("Content-Type", "application/json") - c.setAuthHeaders(req2, token) - resp2, errOr := c.http.Do(req2) - if errOr == nil { - defer resp2.Body.Close() - body2, _ := io.ReadAll(resp2.Body) - if resp2.StatusCode >= 200 && resp2.StatusCode < 300 { - return body2, nil - } - } - } - } - return nil, fmt.Errorf("qldt request-leave/%d/status http %d: %s", leaveID, resp.StatusCode, string(body)) - } - return body, nil + return body, resp.StatusCode, nil +} + +func (c *Client) UpdateLeaveStatus(ctx context.Context, token string, leaveID int64, status string) ([]byte, error) { + // Try PUT first, then POST, then PATCH + methods := []string{http.MethodPut, http.MethodPost, http.MethodPatch} + var lastErr error + var lastBody []byte + var lastCode int + + for _, method := range methods { + body, code, err := c.updateLeaveStatusAttempt(ctx, token, leaveID, status, method) + if err == nil && code >= 200 && code < 300 { + return body, nil + } + if err != nil { + lastErr = err + } else { + lastErr = fmt.Errorf("HTTP %d: %s", code, string(body)) + } + lastBody = body + lastCode = code + } + + return lastBody, fmt.Errorf("all methods (PUT, POST, PATCH) failed. Last error: %w (code %d)", lastErr, lastCode) } diff --git a/server/server.exe b/server/server.exe index 5238448..1252330 100644 Binary files a/server/server.exe and b/server/server.exe differ