| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/identity" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | "github.com/go-chi/chi/v5" |
| 13 | ) |
| 14 | |
| 15 | func (a *application) answerQuestion(w http.ResponseWriter, r *http.Request) { |
| 16 | r.Body = http.MaxBytesReader(w, r.Body, 32<<10) |
| 17 | if r.ParseForm() != nil || len(r.URL.Query()) != 0 { |
| 18 | a.failure(w, r, ErrInvalid) |
| 19 | return |
| 20 | } |
| 21 | for k, v := range r.PostForm { |
| 22 | if len(v) != 1 || (k != "revision" && k != "behavior" && k != "updated_input" && !strings.HasPrefix(k, "answer_")) { |
| 23 | a.failure(w, r, ErrInvalid) |
| 24 | return |
| 25 | } |
| 26 | } |
| 27 | actor, ok := identity.From(r) |
| 28 | if !ok { |
| 29 | a.failure(w, r, ErrForbidden) |
| 30 | return |
| 31 | } |
| 32 | rev, err := strconv.ParseInt(r.PostForm.Get("revision"), 10, 64) |
| 33 | if err != nil { |
| 34 | a.failure(w, r, ErrInvalid) |
| 35 | return |
| 36 | } |
| 37 | answer := runnerproto.QuestionAnswer{Behavior: r.PostForm.Get("behavior")} |
| 38 | if answer.Behavior == "deny" { |
| 39 | for key, values := range r.PostForm { |
| 40 | if strings.HasPrefix(key, "answer_") && values[0] != "" { |
| 41 | a.failure(w, r, ErrInvalid) |
| 42 | return |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | if raw := r.PostForm.Get("updated_input"); raw != "" { |
| 47 | answer.UpdatedInput = json.RawMessage(raw) |
| 48 | } |
| 49 | if answer.Behavior == "allow" && len(answer.UpdatedInput) == 0 { |
| 50 | view, e := a.store.TaskView(r.Context(), Actor{Address: actor.Address}, chi.URLParam(r, "id")) |
| 51 | if e != nil { |
| 52 | a.failure(w, r, e) |
| 53 | return |
| 54 | } |
| 55 | var fields []QuestionField |
| 56 | canAllow := false |
| 57 | for _, q := range view.Questions { |
| 58 | if q.ID == chi.URLParam(r, "question") { |
| 59 | fields = q.Fields |
| 60 | canAllow = q.CanAllow |
| 61 | } |
| 62 | } |
| 63 | if len(fields) == 0 && !canAllow { |
| 64 | a.failure(w, r, ErrInvalid) |
| 65 | return |
| 66 | } |
| 67 | answers := map[string]string{} |
| 68 | permitted := map[string]bool{} |
| 69 | for i, f := range fields { |
| 70 | key := fmt.Sprintf("answer_%d", i) |
| 71 | permitted[key] = true |
| 72 | answers[f.Key] = r.PostForm.Get(key) |
| 73 | } |
| 74 | for key := range r.PostForm { |
| 75 | if strings.HasPrefix(key, "answer_") && !permitted[key] { |
| 76 | a.failure(w, r, ErrInvalid) |
| 77 | return |
| 78 | } |
| 79 | } |
| 80 | if len(fields) > 0 { |
| 81 | answer.UpdatedInput, _ = json.Marshal(struct { |
| 82 | Answers map[string]string `json:"answers"` |
| 83 | }{answers}) |
| 84 | } |
| 85 | } |
| 86 | if err := a.store.answerQuestion(r.Context(), Actor{Address: actor.Address}, chi.URLParam(r, "id"), chi.URLParam(r, "question"), rev, answer); err != nil { |
| 87 | a.failure(w, r, err) |
| 88 | return |
| 89 | } |
| 90 | http.Redirect(w, r, "/needs-you", http.StatusSeeOther) |
| 91 | } |
| 92 | |