deliveries.go49 lines, 1.4 KB
| 1 | package webhook |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 7 | "github.com/fiston-user/payments/internal/platform/authn" |
| 8 | "github.com/fiston-user/payments/internal/platform/httpx" |
| 9 | "github.com/fiston-user/payments/internal/webhook/webhookdb" |
| 10 | "github.com/google/uuid" |
| 11 | ) |
| 12 | |
| 13 | // deliveries is the delivery log of one endpoint, newest first. |
| 14 | func (a *API) deliveries(w http.ResponseWriter, r *http.Request) error { |
| 15 | endpoint, err := uuid.Parse(r.URL.Query().Get("endpoint")) |
| 16 | if err != nil { |
| 17 | var v apperr.ValidationError |
| 18 | v.Add("endpoint", "must be the ID of one of your endpoints") |
| 19 | return v.Err() |
| 20 | } |
| 21 | user, _ := authn.UserID(r.Context()) |
| 22 | log, err := a.q.ListDeliveries(r.Context(), webhookdb.ListDeliveriesParams{ |
| 23 | UserID: user, EndpointID: endpoint, |
| 24 | }) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | httpx.WriteJSON(w, r, http.StatusOK, map[string]any{"deliveries": log}) |
| 29 | return nil |
| 30 | } |
| 31 | |
| 32 | // replay sends a delivery again, whatever happened to it before. |
| 33 | func (a *API) replay(w http.ResponseWriter, r *http.Request) error { |
| 34 | id, err := uuid.Parse(r.PathValue("id")) |
| 35 | if err != nil { |
| 36 | return ErrDeliveryNotFound |
| 37 | } |
| 38 | user, _ := authn.UserID(r.Context()) |
| 39 | n, err := a.q.Replay(r.Context(), webhookdb.ReplayParams{ID: id, UserID: user}) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | if n == 0 { |
| 44 | return ErrDeliveryNotFound |
| 45 | } |
| 46 | a.wake() |
| 47 | httpx.WriteJSON(w, r, http.StatusAccepted, map[string]string{"id": id.String(), "state": "pending"}) |
| 48 | return nil |
| 49 | } |