Add fake API endpoint for creating leads

This commit is contained in:
Eli Ribble 2026-03-05 14:18:10 +00:00
parent 0f4ef9d2f8
commit 89197df6b0
No known key found for this signature in database
7 changed files with 85 additions and 10 deletions

View file

@ -3,6 +3,7 @@ package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/auth"
@ -11,9 +12,12 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/html"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/Gleipnir-Technology/nidus-sync/platform"
"github.com/gorilla/schema"
"github.com/rs/zerolog/log"
)
var decoder = schema.NewDecoder()
type handlerFunctionGet[T any] func(context.Context, *http.Request, *models.Organization, *models.User) (*T, *nhttp.ErrorWithStatus)
type wrappedHandler func(http.ResponseWriter, *http.Request)
type contentAuthenticated[T any] struct {
@ -26,7 +30,6 @@ type ErrorAPI struct {
Message string `json:"message"`
}
// w http.ResponseWriter, r *http.Request, u *models.User) {
func authenticatedHandlerJSON[T any](f handlerFunctionGet[T]) http.Handler {
return auth.NewEnsureAuth(func(w http.ResponseWriter, r *http.Request, u *models.User) {
ctx := r.Context()
@ -62,3 +65,52 @@ func authenticatedHandlerJSON[T any](f handlerFunctionGet[T]) http.Handler {
w.Write(body)
})
}
type handlerFunctionPost[FormType any, ResponseType any] func(context.Context, *http.Request, *models.Organization, *models.User, FormType) (ResponseType, *nhttp.ErrorWithStatus)
func authenticatedHandlerJSONPost[FormType any, ResponseType any](f handlerFunctionPost[FormType, ResponseType]) http.Handler {
return auth.NewEnsureAuth(func(w http.ResponseWriter, r *http.Request, u *models.User) {
w.Header().Set("Content-Type", "application/json")
err := r.ParseForm()
if err != nil {
respondError(w, http.StatusBadRequest, "failed to parse form: %w", err)
return
}
var form FormType
err = decoder.Decode(&form, r.PostForm)
if err != nil {
respondError(w, http.StatusBadRequest, "Failed to decode form: %w", err)
return
}
ctx := r.Context()
org, err := u.Organization().One(ctx, db.PGInstance.BobDB)
if err != nil {
respondError(w, http.StatusInternalServerError, "Failed to get org: %w", err)
return
}
response, e := f(ctx, r, org, u, form)
if e != nil {
http.Error(w, e.Error(), e.Status)
return
}
resp_body, err := json.Marshal(response)
if err != nil {
respondError(w, http.StatusInternalServerError, "Failed to marshal json response: %w", err)
return
}
w.Write(resp_body)
})
}
func respondError(w http.ResponseWriter, status int, format string, args ...any) {
outer_err := fmt.Errorf(format, args...)
body, err := json.Marshal(ErrorAPI{
Message: outer_err.Error(),
})
if err != nil {
http.Error(w, "{\"message\": \"failed to marshal json\"}", http.StatusInternalServerError)
return
}
http.Error(w, string(body), status)
}

24
api/lead.go Normal file
View file

@ -0,0 +1,24 @@
package api
import (
"context"
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/rs/zerolog/log"
)
type formLeads struct {
SignalIDs []int `schema:"signal_ids"`
}
type createdLead struct {
ID int `json:"id"`
}
func postLeads(ctx context.Context, r *http.Request, org *models.Organization, user *models.User, f formLeads) (*createdLead, *nhttp.ErrorWithStatus) {
log.Info().Ints("signal ids", f.SignalIDs).Msg("fake post leads")
return &createdLead{
ID: 0,
}, nil
}

View file

@ -19,6 +19,7 @@ func AddRoutes(r chi.Router) {
r.Method("POST", "/audio/{uuid}/content", auth.NewEnsureAuth(apiAudioContentPost))
r.Method("POST", "/image/{uuid}", auth.NewEnsureAuth(apiImagePost))
r.Method("POST", "/image/{uuid}/content", auth.NewEnsureAuth(apiImageContentPost))
r.Method("POST", "/leads", authenticatedHandlerJSONPost(postLeads))
// Unauthenticated endpoints
r.Get("/district", apiGetDistrict)