Skip to content

Commit

Permalink
add search support
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-enebeli committed Apr 8, 2024
1 parent e9c3f3c commit 0c52430
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 36 deletions.
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,5 @@ Every financial product is made up of different transactions happening concurren
Blnk gives you, the developer, a starting point to improve how you build, operate and perfectly manage money movement and storage in a reliable, scaleable and secure way.

## Get Started and Core Features
- [Install Blnk](https://docs.blnkserver.xyz)
- [Managing ledgers](https://docs.blnkserver.xyz/ledger/get-started)
- [Managing balances](https://docs.blnkserver.xyz/balance/get-started)
- [Balance monitoring](https://docs.blnkserver.xyz/balance/monitoring)
- [Accounts](https://docs.blnkserver.xyz/account/get-started)
- [External account generation](https://docs.blnkserver.xyz/account/external-generation)
- [Queuing transactions](https://docs.blnkserver.xyz/transaction/queuing-transactions)
- [Scheduling transactions](https://docs.blnkserver.xyz/transaction/scheduling-transactions)
- [Fraud detection](https://docs.blnkserver.xyz/fraud)
- [Notifications and webhooks](https://docs.blnkserver.xyz/notifications)
- [More Controls and configurations](https://docs.blnkserver.xyz/config)
- [API Reference](https://docs.blnkserver.xyz/misc/create-ledger)

- [Documentation](https://docs.blnkserver.xyz)

25 changes: 24 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"net/http"

"github.com/jerry-enebeli/blnk/config"

Expand Down Expand Up @@ -49,6 +50,8 @@ func (a Api) Router() *gin.Engine {

router.GET("/backup", a.BackupDB)
router.GET("/backup-s3", a.BackupDBS3)

router.POST("/search/:collection", a.Search)
return a.router
}

Expand All @@ -66,7 +69,6 @@ func NewAPI(b *blnk.Blnk) *Api {
r.GET("/", func(c *gin.Context) {
c.JSON(200, "server running...")
})

r.POST("/webhook", func(c *gin.Context) {
var payload map[string]interface{}
err := c.Bind(&payload)
Expand All @@ -78,3 +80,24 @@ func NewAPI(b *blnk.Blnk) *Api {

return &Api{blnk: b, router: r}
}

func (a Api) Search(c *gin.Context) {
collection, passed := c.Params.Get("collection")
if !passed {
c.JSON(http.StatusBadRequest, gin.H{"error": "collection is required. pass id in the route /:collection"})
return
}

var query map[string]interface{}

c.BindJSON(&query)

resp, err := a.blnk.Search(collection, query)

if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusCreated, resp)
}
7 changes: 6 additions & 1 deletion blnk.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package blnk

import (
"context"
"embed"
"fmt"

Expand Down Expand Up @@ -37,7 +38,11 @@ func NewBlnk(db database.IDataSource) (*Blnk, error) {
}
bt := NewBalanceTracker()
newQueue := NewQueue(configuration)
newSearch := NewTypesenseClient("", []string{""})
newSearch := NewTypesenseClient("blnk-api-key", []string{"http://typesense:8108"})
newBlnk := &Blnk{datasource: db, bt: bt, queue: newQueue, redis: redis.Client(), search: newSearch}
return newBlnk, nil
}

func (b Blnk) Search(collection string, query map[string]interface{}) (interface{}, error) {
return b.search.Search(context.Background(), collection, query)
}
2 changes: 1 addition & 1 deletion docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ services:

typesense:
image: typesense/typesense:0.23.1 # Use the latest version suitable for your needs
command: ["--data-dir", "/data", "--api-key=your_typesense_api_key", "--listen-port", "8108"]
command: ["--data-dir", "/data", "--api-key=blnk-api-key", "--listen-port", "8108"]
volumes:
- typesense_data:/data
ports:
Expand Down
17 changes: 0 additions & 17 deletions docs/blnk-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
},
"/balances": {
"post": {
"tags": ["Misc"],
"summary": "Create Balance",
"operationId": "CreateBalance",
"parameters": [],
Expand Down Expand Up @@ -204,7 +203,6 @@
},
"/transactions": {
"post": {
"tags": ["Misc"],
"summary": "Record Transaction",
"operationId": "RecordTransaction",
"parameters": [],
Expand Down Expand Up @@ -388,7 +386,6 @@
},
"/transactions/inflight/{txID}/{status}": {
"put": {
"tags": ["Misc"],
"summary": "Update Inflight Transaction",
"operationId": "UpdateInflightTransaction",
"parameters": [
Expand Down Expand Up @@ -492,7 +489,6 @@
},
"/refund-transaction/{id}": {
"post": {
"tags": ["Misc"],
"summary": "Refund Transaction",
"operationId": "RefundTransaction",
"parameters": [
Expand Down Expand Up @@ -607,7 +603,6 @@
},
"/balance-monitors": {
"post": {
"tags": ["Misc"],
"summary": "Create Balance Monitor",
"operationId": "CreateBalanceMonitor",
"parameters": [],
Expand Down Expand Up @@ -715,7 +710,6 @@
},
"/identities": {
"post": {
"tags": ["Misc"],
"summary": "Create Identity",
"operationId": "CreateIdentity",
"parameters": [],
Expand Down Expand Up @@ -859,7 +853,6 @@
},
"/accounts": {
"post": {
"tags": ["Misc"],
"summary": "Create Account",
"operationId": "CreateAccount",
"parameters": [],
Expand Down Expand Up @@ -967,7 +960,6 @@
},
"/identities/{id}": {
"put": {
"tags": ["Misc"],
"summary": "Update Identity",
"operationId": "UpdateIdentity",
"parameters": [
Expand Down Expand Up @@ -1091,7 +1083,6 @@
"deprecated": false
},
"get": {
"tags": ["Misc"],
"summary": "Get Identity",
"operationId": "GetIdentity",
"parameters": [
Expand Down Expand Up @@ -1200,7 +1191,6 @@
},
"/accounts/{id}": {
"put": {
"tags": ["Misc"],
"summary": "Update Account",
"operationId": "UpdateAccount",
"parameters": [
Expand Down Expand Up @@ -1317,7 +1307,6 @@
"deprecated": false
},
"get": {
"tags": ["Misc"],
"summary": "Get Account",
"operationId": "GetAccount",
"parameters": [
Expand Down Expand Up @@ -1496,7 +1485,6 @@
},
"/balance-monitors/{id}": {
"put": {
"tags": ["Misc"],
"summary": "Update Balance Monitor",
"operationId": "UpdateBalanceMonitor",
"parameters": [
Expand Down Expand Up @@ -1554,7 +1542,6 @@
"deprecated": false
},
"get": {
"tags": ["Misc"],
"summary": "Get Balance Monitor",
"operationId": "GetBalanceMonitor",
"parameters": [
Expand Down Expand Up @@ -1639,7 +1626,6 @@
},
"/backup": {
"post": {
"tags": ["Misc"],
"summary": "Back DB",
"operationId": "BackDB",
"parameters": [],
Expand Down Expand Up @@ -1736,7 +1722,6 @@
},
"/backup/s3": {
"post": {
"tags": ["Misc"],
"summary": "Back DB To S3",
"operationId": "BackDBToS3",
"parameters": [],
Expand Down Expand Up @@ -1833,7 +1818,6 @@
},
"/ledgers/{id}": {
"get": {
"tags": ["Misc"],
"summary": "Get Ledger",
"operationId": "GetLedger",
"parameters": [
Expand Down Expand Up @@ -1904,7 +1888,6 @@
},
"/balances/{id}": {
"get": {
"tags": ["Misc"],
"summary": "Get Balance",
"operationId": "GetBalance",
"parameters": [
Expand Down
2 changes: 1 addition & 1 deletion model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Transaction struct {
Destination string `json:"destination,omitempty"`
Reference string `json:"reference"`
Currency string `json:"currency"`
Description string `json:"description"`
Description string `json:"description",omitempty`
Status string `json:"status"`
Hash string `json:"hash"`
GroupIds []string `json:"group_ids"`
Expand Down
11 changes: 9 additions & 2 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func (l Blnk) RecordTransaction(ctx context.Context, transaction *model.Transact
logrus.Errorf("ERROR saying transaction to db. %s", err)
}

l.search.IndexDocument(context.Background(), "transactions", transaction)
err = SendWebhook(NewWebhook{
Event: "transaction.applied",
Payload: transaction,
Expand Down Expand Up @@ -230,9 +231,13 @@ func (l Blnk) CommitInflightTransaction(ctx context.Context, transactionID strin
return nil, err
}

if amount == 0 {
amount = transaction.Amount
}

// Commit inflight balances
sourceBalance.CommitInflightDebit(transaction.Amount) // For the source
destinationBalance.CommitInflightCredit(transaction.Amount) // For the destination
sourceBalance.CommitInflightDebit(amount) // For the source
destinationBalance.CommitInflightCredit(amount) // For the destination

// Update balances in the database
if err = l.updateBalances(ctx, sourceBalance, destinationBalance); err != nil {
Expand All @@ -242,6 +247,8 @@ func (l Blnk) CommitInflightTransaction(ctx context.Context, transactionID strin
// create a new transaction with status applied
transaction.Status = StatusApplied
transaction.TransactionID = model.GenerateUUIDWithSuffix("txn")
transaction.Reference = model.GenerateUUIDWithSuffix("ref")
transaction.Hash = transaction.HashTxn()
transaction, err = l.datasource.RecordTransaction(cxt, transaction)
if err != nil {
return nil, logAndRecordError(span, "saving transaction to db error", err)
Expand Down

0 comments on commit 0c52430

Please sign in to comment.