fix qldt duyet phep
All checks were successful
Deploy on Master Change / deploy (push) Successful in 1m18s

This commit is contained in:
2026-07-06 11:09:02 +07:00
parent 6502531ad7
commit 6cc520a0c4
2 changed files with 29 additions and 24 deletions

View File

@@ -221,43 +221,48 @@ func (c *Client) GetLeaveRequests(ctx context.Context, token string, classID, co
return body, nil 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} payload := map[string]string{"status": status}
jsonData, _ := json.Marshal(payload) jsonData, _ := json.Marshal(payload)
u := fmt.Sprintf("%s/request-leave/%d/status", c.baseURL, leaveID) 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 { if err != nil {
return nil, err return nil, 0, err
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
c.setAuthHeaders(req, token) c.setAuthHeaders(req, token)
resp, err := c.http.Do(req) resp, err := c.http.Do(req)
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 { return body, resp.StatusCode, nil
// 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)) func (c *Client) UpdateLeaveStatus(ctx context.Context, token string, leaveID int64, status string) ([]byte, error) {
if err2 == nil { // Try PUT first, then POST, then PATCH
req2.Header.Set("Content-Type", "application/json") methods := []string{http.MethodPut, http.MethodPost, http.MethodPatch}
c.setAuthHeaders(req2, token) var lastErr error
resp2, errOr := c.http.Do(req2) var lastBody []byte
if errOr == nil { var lastCode int
defer resp2.Body.Close()
body2, _ := io.ReadAll(resp2.Body) for _, method := range methods {
if resp2.StatusCode >= 200 && resp2.StatusCode < 300 { body, code, err := c.updateLeaveStatusAttempt(ctx, token, leaveID, status, method)
return body2, nil if err == nil && code >= 200 && code < 300 {
} return body, nil
} }
} if err != nil {
} lastErr = err
return nil, fmt.Errorf("qldt request-leave/%d/status http %d: %s", leaveID, resp.StatusCode, string(body)) } else {
} lastErr = fmt.Errorf("HTTP %d: %s", code, string(body))
return body, nil }
lastBody = body
lastCode = code
}
return lastBody, fmt.Errorf("all methods (PUT, POST, PATCH) failed. Last error: %w (code %d)", lastErr, lastCode)
} }

Binary file not shown.