Skip to content

Commit

Permalink
feat: add payment fields to connection codes
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Dec 13, 2024
1 parent 13f580b commit 818be80
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ type ConnectionCodes struct {
ConnectionString string `json:"connection_string"`
IsUsed bool `json:"is_used"`
DateCreated *time.Time `json:"date_created"`
Pubkey string `json:"pubkey"`
RouteHint string `json:"route_hint"`
SatsAmount int64 `json:"sats_amount"`
}

type ConnectionCodesShort struct {
Expand Down
19 changes: 15 additions & 4 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,34 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque
body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println("ReadAll Error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body.Close()

err = json.Unmarshal(body, &codeBody)

if err != nil {
fmt.Println("Could not umarshal connection code body")
fmt.Println("Could not unmarshal connection code body")
w.WriteHeader(http.StatusNotAcceptable)
return
}

if codeBody.Pubkey != "" && codeBody.RouteHint == "" {
fmt.Println("route hint missing")
w.WriteHeader(http.StatusNotAcceptable)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode("Route hint is required when pubkey is provided")
return
}

if codeBody.RouteHint != "" && codeBody.Pubkey == "" {
fmt.Println("pubkey missing missing")
w.WriteHeader(http.StatusNotAcceptable)
json.NewEncoder(w).Encode("pubkey is required when Route hint is provided")
return
}

if codeBody.SatsAmount == 0 {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode("Sats amount must be greater than 0")
return
}

Expand All @@ -100,6 +108,9 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque
newCode := db.ConnectionCodes{
ConnectionString: code,
IsUsed: false,
Pubkey: codeBody.Pubkey,
RouteHint: codeBody.RouteHint,
SatsAmount: int64(codeBody.SatsAmount),
}
codeArr = append(codeArr, newCode)
}
Expand Down
5 changes: 4 additions & 1 deletion tribes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,8 @@ CREATE TABLE connectioncodes {
id SERIAL PRIMARY KEY,
connection_string TEXT,
is_used boolean,
date_created timestamptz
date_created timestamptz,
pubkey TEXT,
route_hint TEXT,
sats_amount bigint
}

0 comments on commit 818be80

Please sign in to comment.