Skip to content

Commit

Permalink
Merge pull request #207 from NetSepio/main
Browse files Browse the repository at this point in the history
Merging main with prod
  • Loading branch information
Rushikeshnimkar authored Oct 14, 2024
2 parents 46d1107 + 9c3c144 commit a4b2932
Show file tree
Hide file tree
Showing 15 changed files with 806 additions and 182 deletions.
10 changes: 7 additions & 3 deletions api/v1/authenticate/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ func authenticate(c *gin.Context) {
}
if isCorrect {
// update wallet address for that user_id
err = db.Model(&models.User{}).Where("user_id = ?", userId).Update("wallet_address", walletAddr).Error
err = db.Model(&models.User{}).Where("user_id = ?", userId).
Updates(map[string]interface{}{
"wallet_address": walletAddr,
"chain_name": req.ChainName,
}).Error
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").SendD(c)
logwrapper.Errorf("failed to update wallet address, error %v", err.Error())
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
logwrapper.Errorf("failed to update wallet address and chain name, error %v", err.Error())
return
}

Expand Down
1 change: 1 addition & 0 deletions api/v1/authenticate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type AuthenticateRequest struct {
Message string `json:"message" binding:"omitempty"`
AccessToken string `json:"accessToken" binding:"omitempty"`
IdToken string `json:"idToken" binding:"omitempty"` // to be pass in bearer token [ AUTHORIZATION KEY ]
ChainName string `json:"chainName" binding:"required"`
}
type AuthenticateRequestNoSign struct {
FlowId string `json:"flowId" binding:"required"`
Expand Down
152 changes: 152 additions & 0 deletions api/v1/leaderboard/cron.job.operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package leaderboard

import (
"fmt"
"log"
"strings"
"time"

"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/config/models"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

func CronJobLeaderBoardUpdate(column_name string, leaderboard Leaderboard) {
// Database connection setup (replace with your actual connection details)
db := dbconfig.GetDb()

// // Check if the user_id exists in the LeaderBoard table
// var leaderboard ScoreBoard

var scoreBoard ScoreBoard

data, err := GetActivityUnitXpByActivity(column_name)
if err != nil {
log.Printf("failed to get the ScoreBoard by ID: %v", err)
}
// leaderboard.UserId = user_id
err = db.Debug().Where("user_id = ?", leaderboard.UserId).First(&scoreBoard).Error

if err != nil {
if err == gorm.ErrRecordNotFound {

CreateScoreBoard(ScoreBoard{
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
})
log.Println("New record inserted and " + column_name + " count initialized successfully!")
return
}
log.Printf("failed to query the ScoreBoard: %v", err)
} else {

err = UpdateScoreBoard(leaderboard.ID, ScoreBoard{
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
}, column_name, data.XP)
if err != nil {
log.Printf("failed to update the Reviews count: %v", err)
return
}
log.Println(column_name + " count incremented successfully!")
return
}
}
func CronForReviewUpdate() {
db := dbconfig.GetDb()

var voters []string
db.Model(&models.Review{}).Select("voter").Find(&voters)

// fmt.Println("len voters : ", len(voters))
// fmt.Println("voters : ", voters)

if len(voters) > 0 {

for _, v := range voters {
var userIds []string
db := dbconfig.GetDb()
// Select only UserId column from the Leaderboard table
if err := db.Raw("SELECT user_id FROM users WHERE wallet_address = ?", v).Scan(&userIds).Error; err != nil {
if err == gorm.ErrRecordNotFound {
fmt.Println("This user does not exist in the user table: wallet address =", v)
} else {
log.Printf("Failed to get the Reviews: %v\n", err)
}
} else {
if len(userIds) > 0 {
for _, id := range userIds {
go DynamicLeaderBoardUpdate(id, "reviews")
}
}
}
}

}

}
func AutoCalculateScoreBoard() {

// fmt.Println("STARTING AUTO CALCULATE SCOREBOARD AT ", time.Now())
border := strings.Repeat("=", 50) // Creates a border line

func() {
fmt.Println(border)
fmt.Println("🚀 STARTING AUTO CALCULATE SCOREBOARD")
fmt.Println("📅 Date & Time:", time.Now().Format("02-Jan-2006 15:04:05 MST"))
fmt.Println("🔄 Status: In Progress")
fmt.Println(border)
}()

// CronForReviewUpdate()

// var leaderboard ScoreBoard
leaderboards, err := GetAllLeaderBoard()
if err != nil {
logrus.Error(err)
return
}

fmt.Println("leaderboards len : ", len(leaderboards))

for _, leaderboard := range leaderboards {
CronJobLeaderBoardUpdate("reviews", leaderboard)
CronJobLeaderBoardUpdate("domain", leaderboard)
CronJobLeaderBoardUpdate("nodes", leaderboard)
CronJobLeaderBoardUpdate("d_wifi", leaderboard)
CronJobLeaderBoardUpdate("discord", leaderboard)
CronJobLeaderBoardUpdate("twitter", leaderboard)
CronJobLeaderBoardUpdate("telegram", leaderboard)
}

func() {
// After the task completes, print the "Completed" status
// border := strings.Repeat("=", 50) // Creates a border line
fmt.Println(border)
fmt.Println("✅ SCOREBOARD CALCULATION COMPLETED")
fmt.Println("📅 Date & Time:", time.Now().Format("02-Jan-2006 15:04:05 MST"))
fmt.Println("✔️ Status: Completed")
fmt.Println(border)
}()

}
65 changes: 0 additions & 65 deletions api/v1/leaderboard/db.operations.go

This file was deleted.

Loading

0 comments on commit a4b2932

Please sign in to comment.