Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions internal/clients/myteam2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,26 @@ func (c *MyTeam2GoClocker) submitWorkAssistance(ctx context.Context, action work
}

// ── Extract Guardar button name ───────────────────────────────────────────
btnRegex := regexp.MustCompile(`name="(workAssistanceForm:j_idt\d+)"[^>]*><span[^>]*>Guardar</span>`)
btnMatches := btnRegex.FindStringSubmatch(menuHtml)
if len(btnMatches) < 2 {
btnMatches = btnRegex.FindStringSubmatch(html)
if len(btnMatches) < 2 {
return fmt.Errorf("could not find Guardar button in workAssistanceForm")
btnName := ""
// Match <button name="workAssistanceForm:...">...Guardar...</button> with possible nested tags
btnRegexText := regexp.MustCompile(`name="(workAssistanceForm:[^"]+)"[^>]*>(?:<[^>]+>)*\s*Guardar\s*<`)
// Match <input name="workAssistanceForm:..." value="Guardar">
btnRegexVal := regexp.MustCompile(`name="(workAssistanceForm:[^"]+)"[^>]*value="Guardar"`)

for _, htmlSource := range []string{menuHtml, html} {
if btnMatches := btnRegexText.FindStringSubmatch(htmlSource); len(btnMatches) >= 2 {
btnName = btnMatches[1]
break
}
if btnMatches := btnRegexVal.FindStringSubmatch(htmlSource); len(btnMatches) >= 2 {
btnName = btnMatches[1]
break
}
}

if btnName == "" {
return fmt.Errorf("could not find Guardar button in workAssistanceForm")
}
btnName := btnMatches[1]

// ── Extract option value ──────────────────────────────────────────────────
optRegex := regexp.MustCompile(`value="(\d+)"[^>]*>` + regexp.QuoteMeta(action.optionLabel) + `<`)
Expand Down Expand Up @@ -449,7 +460,6 @@ func (c *MyTeam2GoClocker) submitWorkAssistance(ctx context.Context, action work
}
changeBytes, _ := io.ReadAll(changeResp.Body)
changeHtml := string(changeBytes)
slog.Debug("🌐 Change event response", "body", changeHtml)
if err := changeResp.Body.Close(); err != nil {
slog.Warn("⚠️ submitWorkAssistance: failed to close change response body", "error", err)
}
Expand Down Expand Up @@ -504,7 +514,6 @@ func (c *MyTeam2GoClocker) submitWorkAssistance(ctx context.Context, action work

respBody, _ := io.ReadAll(postResp.Body)
respStr := string(respBody)
slog.Debug("🌐 Guardar response", "action", action.logVerb, "body", respStr)

if strings.Contains(respStr, "No se ha podido efectuar") {
return fmt.Errorf("%s rejected by server: no se ha podido efectuar el registro", action.logVerb)
Expand Down
1 change: 1 addition & 0 deletions internal/core/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestRandomizeHours(t *testing.T) {
cfg := &config.Config{
ClockIn: config.ClockTime{Time: clockInTime},
ClockOut: config.ClockTime{Time: clockOutTime},
FridayTimes: []config.ClockTime{{Time: clockInTime}, {Time: clockOutTime}},
Lunchtime: &config.ClockTime{Time: lunchTime},
MinTimeToLunch: 30,
MaxTimeToLunch: 60,
Expand Down