41 lines
859 B
Go
41 lines
859 B
Go
package tiktok
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"ruben/inventory2/internal/domains/raw_events"
|
|
)
|
|
|
|
func NewWebhookHandler(db *raw_events.Store) http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("POST /test", func(w http.ResponseWriter, r *http.Request) {
|
|
var body json.RawMessage
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
http.Error(w, "Failed to decode body as json: "+err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
ts := time.Now().UTC()
|
|
|
|
err := db.Save(r.Context(), &raw_events.Event{
|
|
Platform: "tiktok",
|
|
StoreID: "test-store-1",
|
|
EventID: fmt.Sprint(ts.Unix()),
|
|
EventTimestamp: ts,
|
|
Payload: body,
|
|
})
|
|
if err != nil {
|
|
http.Error(w, "Error occurred saving the body as the event payload: "+err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(201)
|
|
})
|
|
|
|
return mux
|
|
}
|