Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into unit-test-SendTicketMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Jan 31, 2025
2 parents 02e3d6f + ac56533 commit 1c2fc94
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
24 changes: 24 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,30 @@ func (db database) GetConnectionCode() ConnectionCodesShort {
return c
}

func (db database) GetConnectionCodesList(page, limit int) ([]ConnectionCodesList, int64, error) {
var codes []ConnectionCodesList
var total int64

offset := (page - 1) * limit

if err := db.db.Model(&ConnectionCodes{}).
Where("is_used = ?", false).
Count(&total).Error; err != nil {
return nil, 0, err
}

if err := db.db.Model(&ConnectionCodes{}).
Where("is_used = ?", false).
Order("date_created DESC").
Limit(limit).
Offset(offset).
Find(&codes).Error; err != nil {
return nil, 0, err
}

return codes, total, nil
}

func (db database) GetLnUser(lnKey string) int64 {
var count int64

Expand Down
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Database interface {
CountBounties() uint64
GetPeopleListShort(count uint32) *[]PersonInShort
GetConnectionCode() ConnectionCodesShort
GetConnectionCodesList(page, limit int) ([]ConnectionCodesList, int64, error)
CreateConnectionCode(c []ConnectionCodes) ([]ConnectionCodes, error)
GetLnUser(lnKey string) int64
CreateLnUser(lnKey string) (Person, error)
Expand Down
8 changes: 8 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ type ConnectionCodesShort struct {
DateCreated *time.Time `json:"date_created"`
}

type ConnectionCodesList struct {
ConnectionString string `json:"connection_string"`
Pubkey string `json:"pubkey"`
SatsAmount uint64 `json:"sats_amount"`
DateCreated *time.Time `json:"date_created"`
IsUsed bool `json:"is_used"`
}

type InvoiceRequest struct {
Amount string `json:"amount"`
Memo string `json:"memo"`
Expand Down
1 change: 1 addition & 0 deletions db/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func InitTestDB() {
db.AutoMigrate(&BountyTiming{})
db.AutoMigrate(&FileAsset{})
db.AutoMigrate(&TextSnippet{})
db.AutoMigrate(&ConnectionCodesList{})

people := TestDB.GetAllPeople()
for _, p := range people {
Expand Down
52 changes: 52 additions & 0 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"net/http"
"strconv"

"github.com/form3tech-oss/jwt-go"
"github.com/stakwork/sphinx-tribes/auth"
Expand All @@ -32,6 +33,14 @@ func NewAuthHandler(db db.Database) *authHandler {
}
}

type ConnectionCodesListResponse struct {
Success bool `json:"success"`
Data struct {
Codes []db.ConnectionCodesList `json:"codes"`
Total int64 `json:"total"`
} `json:"data"`
}

func GetAdminPubkeys(w http.ResponseWriter, r *http.Request) {
type PubKeysReturn struct {
Pubkeys []string `json:"pubkeys"`
Expand Down Expand Up @@ -335,3 +344,46 @@ func returnUserMap(p db.Person) map[string]interface{} {
user["url"] = config.Host
return user
}

func (ah *authHandler) ListConnectionCodes(w http.ResponseWriter, r *http.Request) {

page := 1
limit := 20

if pageStr := r.URL.Query().Get("page"); pageStr != "" {
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
page = p
}
}

if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
if l, err := strconv.Atoi(limitStr); err == nil && l > 0 {
limit = l
}
}

codes, total, err := ah.db.GetConnectionCodesList(page, limit)
if err != nil {
logger.Log.Error("[auth] Failed to get connection codes: %v", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": "Failed to fetch connection codes",
})
return
}

response := ConnectionCodesListResponse{
Success: true,
Data: struct {
Codes []db.ConnectionCodesList `json:"codes"`
Total int64 `json:"total"`
}{
Codes: codes,
Total: total,
},
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
62 changes: 62 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions routes/connection_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func ConnectionCodesRoutes() chi.Router {
r.Group(func(r chi.Router) {
r.Use(auth.PubKeyContextSuperAdmin)
r.Post("/", authHandler.CreateConnectionCode)
r.Get("/list", authHandler.ListConnectionCodes)
})
return r
}

0 comments on commit 1c2fc94

Please sign in to comment.