Skip to content

Commit

Permalink
feat: cronjob sync icy tx
Browse files Browse the repository at this point in the history
fix: test

feat: cronjob sync icy tx

fix: cmt

fix: cmt

fix: cmt
  • Loading branch information
trkhoi authored and namnhce committed May 15, 2023
1 parent d203b54 commit ac48317
Show file tree
Hide file tree
Showing 18 changed files with 413 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,47 @@ const docTemplate = `{
}
}
},
"/cron-jobs/store-vault-transaction": {
"post": {
"description": "Store vault tx as icy tx from Mochi service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Vault"
],
"summary": "Store vault tx as icy tx from Mochi service",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/view.MessageResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
}
}
}
},
"/cron-jobs/sync-project-member-status": {
"put": {
"description": "Sync project member status",
Expand Down
41 changes: 41 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,47 @@
}
}
},
"/cron-jobs/store-vault-transaction": {
"post": {
"description": "Store vault tx as icy tx from Mochi service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Vault"
],
"summary": "Store vault tx as icy tx from Mochi service",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/view.MessageResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
}
}
}
},
"/cron-jobs/sync-project-member-status": {
"put": {
"description": "Sync project member status",
Expand Down
27 changes: 27 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4526,6 +4526,33 @@ paths:
summary: Update client by id
tags:
- Client
/cron-jobs/store-vault-transaction:
post:
consumes:
- application/json
description: Store vault tx as icy tx from Mochi service
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/view.MessageResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/view.ErrorResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/view.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/view.ErrorResponse'
summary: Store vault tx as icy tx from Mochi service
tags:
- Vault
/cron-jobs/sync-project-member-status:
put:
consumes:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up
CREATE TABLE IF NOT EXISTS icy_transactions (
id uuid PRIMARY KEY DEFAULT (uuid()),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ,
vault TEXT NOT NULL,
amount TEXT NOT NULL,
token TEXT NOT NULL,
sender_discord_id TEXT NOT NULL,
recipient_address TEXT NOT NULL,
recipient_discord_id TEXT NOT NULL
);
-- +migrate Down
DROP TABLE IF EXISTS icy_transactions;
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
Discord Discord
Basecamp Basecamp
CurrencyLayer CurrencyLayer
Mochi Mochi

Invoice Invoice
Sendgrid Sendgrid
Expand Down Expand Up @@ -77,6 +78,10 @@ type Vault struct {
Path string
}

type Mochi struct {
BaseURL string
}

type Notion struct {
Secret string
Databases NotionDatabase
Expand Down Expand Up @@ -227,6 +232,9 @@ func Generate(v ENV) *Config {
Sendgrid: Sendgrid{
APIKey: v.GetString("SENDGRID_API_KEY"),
},
Mochi: Mochi{
BaseURL: v.GetString("MOCHI_BASE_URL"),
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/dwarvesf/fortress-api/pkg/handler/project"
"github.com/dwarvesf/fortress-api/pkg/handler/survey"
"github.com/dwarvesf/fortress-api/pkg/handler/valuation"
"github.com/dwarvesf/fortress-api/pkg/handler/vault"
"github.com/dwarvesf/fortress-api/pkg/handler/webhook"
"github.com/dwarvesf/fortress-api/pkg/logger"
"github.com/dwarvesf/fortress-api/pkg/service"
Expand Down Expand Up @@ -53,6 +54,7 @@ type Handler struct {
Survey survey.IHandler
Valuation valuation.IHandler
Webhook webhook.IHandler
Vault vault.IHandler
}

func New(store *store.Store, repo store.DBRepo, service *service.Service, ctrl *controller.Controller, worker *worker.Worker, logger logger.Logger, cfg *config.Config) *Handler {
Expand All @@ -78,5 +80,6 @@ func New(store *store.Store, repo store.DBRepo, service *service.Service, ctrl *
Survey: survey.New(store, repo, service, logger, cfg),
Valuation: valuation.New(store, repo, service, logger, cfg),
Webhook: webhook.New(ctrl, store, repo, service, logger, cfg),
Vault: vault.New(store, repo, service, logger, cfg),
}
}
7 changes: 7 additions & 0 deletions pkg/handler/vault/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package vault

import "github.com/gin-gonic/gin"

type IHandler interface {
StoreVaultTransaction(c *gin.Context)
}
91 changes: 91 additions & 0 deletions pkg/handler/vault/vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package vault

import (
"net/http"
"time"

"github.com/gin-gonic/gin"

"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/logger"
"github.com/dwarvesf/fortress-api/pkg/model"
"github.com/dwarvesf/fortress-api/pkg/service"
"github.com/dwarvesf/fortress-api/pkg/store"
"github.com/dwarvesf/fortress-api/pkg/utils/timeutil"
"github.com/dwarvesf/fortress-api/pkg/view"
)

type handler struct {
store *store.Store
service *service.Service
logger logger.Logger
repo store.DBRepo
config *config.Config
}

// New returns a handler
func New(store *store.Store, repo store.DBRepo, service *service.Service, logger logger.Logger, cfg *config.Config) IHandler {
return &handler{
store: store,
repo: repo,
service: service,
logger: logger,
config: cfg,
}
}

// StoreVaultTransaction godoc
// @Summary Store vault tx as icy tx from Mochi service
// @Description Store vault tx as icy tx from Mochi service
// @Tags Vault
// @Accept json
// @Produce json
// @Success 200 {object} view.MessageResponse
// @Failure 400 {object} view.ErrorResponse
// @Failure 404 {object} view.ErrorResponse
// @Failure 500 {object} view.ErrorResponse
// @Router /cron-jobs/store-vault-transaction [post]
func (h *handler) StoreVaultTransaction(c *gin.Context) {
l := h.logger.Fields(logger.Fields{
"handler": "vault",
"method": "StoreVaultTransaction",
})

// currently support
supportedVaults := []string{"18", "19", "20"}

startOfTheWeek := timeutil.FormatDateForCurl(timeutil.GetStartDayOfWeek(time.Now().Local()).Format(time.RFC3339))
endOfTheWeek := timeutil.FormatDateForCurl(timeutil.GetEndDayOfWeek(time.Now().Local()).Format(time.RFC3339))

for _, vaultId := range supportedVaults {
req := &model.VaultTransactionRequest{
VaultId: vaultId,
StartTime: startOfTheWeek,
EndTime: endOfTheWeek,
}
res, err := h.service.Mochi.GetVaultTransaction(req)
if err != nil {
l.Error(err, "GetVaultTransaction failed")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, ""))
return
}

for _, transaction := range res.Data {
err := h.store.IcyTransaction.Create(h.repo.DB(), &model.IcyTransaction{
Vault: transaction.VaultName,
Amount: transaction.Amount,
Token: transaction.Token,
SenderDiscordId: transaction.Sender,
RecipientAddress: transaction.ToAddress,
RecipientDiscordId: transaction.Target,
})
if err != nil {
l.Error(err, "Create IcyTransaction failed")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, ""))
return
}
}
}

c.JSON(http.StatusOK, view.CreateResponse[any](nil, nil, nil, nil, "ok"))
}
11 changes: 11 additions & 0 deletions pkg/model/icy_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

type IcyTransaction struct {
BaseModel
Vault string `json:"vault"`
Amount string `json:"amount"`
Token string `json:"token"`
SenderDiscordId string `json:"sender_discord_id"`
RecipientAddress string `json:"recipient_address"`
RecipientDiscordId string `json:"recipient_discord_id"`
}
28 changes: 28 additions & 0 deletions pkg/model/mochi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package model

type VaultTransactionRequest struct {
VaultId string `json:"vault_id"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
}

type VaultTransactionResponse struct {
Data []VaultTransaction `json:"data"`
}

type VaultTransaction struct {
Id int64 `json:"id"`
GuildId string `json:"guild_id"`
VaultId int64 `json:"vault_id"`
VaultName string `json:"vault_name"`
Action string `json:"action"`
FromAddress string `json:"from_address"`
ToAddress string `json:"to_address"`
Target string `json:"target"`
Sender string `json:"sender"`
Amount string `json:"amount"`
Token string `json:"token"`
Threshold string `json:"threshold"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
1 change: 1 addition & 0 deletions pkg/routes/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func loadV1Routes(r *gin.Engine, h *handler.Handler, repo store.DBRepo, s *store
cronjob.POST("/sync-discord-info", amw.WithAuth, pmw.WithPerm(model.PermissionCronjobExecute), h.Discord.SyncDiscordInfo)
cronjob.POST("/sync-monthly-accounting-todo", amw.WithAuth, pmw.WithPerm(model.PermissionCronjobExecute), h.Accounting.CreateAccountingTodo)
cronjob.PUT("/sync-project-member-status", amw.WithAuth, pmw.WithPerm(model.PermissionCronjobExecute), h.Project.SyncProjectMemberStatus)
cronjob.POST("/store-vault-transaction", amw.WithAuth, pmw.WithPerm(model.PermissionCronjobExecute), h.Vault.StoreVaultTransaction)
}

/////////////////
Expand Down
6 changes: 6 additions & 0 deletions pkg/routes/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ func Test_loadV1Routes(t *testing.T) {
Handler: "github.com/dwarvesf/fortress-api/pkg/handler/project.IHandler.SyncProjectMemberStatus-fm",
},
},
"/cronjobs/store-vault-transaction": {
"POST": {
Method: "POST",
Handler: "github.com/dwarvesf/fortress-api/pkg/handler/vault.IHandler.StoreVaultTransaction-fm",
},
},
"/webhooks/n8n": {
"POST": {
Method: "POST",
Expand Down
Loading

0 comments on commit ac48317

Please sign in to comment.