Skip to content

Commit

Permalink
feat(admin): Adding support APIs For Admin (#44)
Browse files Browse the repository at this point in the history
* feat: Get All Inactive User API Added

* Two Tests Added

* Some Test Written

* Some Chnages
  • Loading branch information
keyurboss authored Dec 22, 2024
1 parent 3efa8af commit 2dcbb7c
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 2 deletions.
206 changes: 206 additions & 0 deletions coverage.out

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/apis/data/trade-user/get-in-active-trade-user.api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tradeuser

import (
"github.com/gofiber/fiber/v2"
"github.com/rpsoftech/bullion-server/src/interfaces"
"github.com/rpsoftech/bullion-server/src/services"
)

func apiGetInActiveTradeUsers(c *fiber.Ctx) error {
bullionId, err := interfaces.ExtractBullionIdFromCtx(c)
if err != nil {
return err
}
entity, err := services.TradeUserService.FindAndReturnAllInActiveTradeUsers(bullionId)
if err != nil {
return err
} else {
return c.JSON(entity)
}

}
1 change: 1 addition & 0 deletions src/apis/data/trade-user/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func AddTradeUserAPIs(router fiber.Router) {
adminGroup := router.Use(middleware.AllowOnlyBigAdmins.Validate)
adminGroup.Get("/getInActiveTradeUsers", apiGetInActiveTradeUsers)
adminGroup.Get("/getTradeUser", apiGetTradeUserDetails)
adminGroup.Patch("/updateTradeUserDetails", apiUpdateTradeUserDetails)
adminGroup.Patch("/updateTradeUserStatus", apiChangeTradeUserStatus)
Expand Down
47 changes: 47 additions & 0 deletions src/interfaces/admin-user.entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package interfaces

import (
"testing"
)

func TestAdminUserEntity(t *testing.T) {
e := &AdminUserEntity{}
// // c.createNewId()
// Admin()
t.Run("Create New Admin Entity", func(t *testing.T) {
if e.BaseEntity != nil {
t.Fatalf("BaseEntity is not nil")
}
if e.UserRolesInterface != nil {
t.Fatalf("UserRolesInterface is not nil")
}
e.CreateNewEntity("admin", "password", "nickName", "bullionId")
if e.BaseEntity == nil {
t.Fatalf("BaseEntity is nil")
}
if e.UserName != "admin" {
t.Fatalf("UserName is not admin")
}
if e.Password != "password" {
t.Fatalf("Password is not password")
}
if e.NickName != "nickName" {
t.Fatalf("NickName is not nickName")
}
if e.BullionId != "bullionId" {
t.Fatalf("BullionId is not bullionId")
}
if e.UserRolesInterface == nil {
t.Fatalf("UserRolesInterface is nil")
}
if e.UserRolesInterface.Role != ROLE_ADMIN {
t.Fatalf("UserRolesInterface Role is not ROLE_ADMIN")
}
testBaseEntityCreateNewId(t, e.BaseEntity)
t.Run("password match", func(t *testing.T) {
if !e.MatchPassword("password") {
t.Fatalf("Password does not match")
}
})
})
}
2 changes: 1 addition & 1 deletion src/interfaces/base-entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (b *BaseEntity) createNewId() *BaseEntity {
id := uuid.New().String()
b.ID = id
b.CreatedAt = time.Now()
b.ModifiedAt = time.Now()
b.ModifiedAt = b.CreatedAt
return b
}

Expand Down
71 changes: 71 additions & 0 deletions src/interfaces/base-entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package interfaces

import (
"testing"
"time"
)

func testBaseEntityCreateNewId(t *testing.T, c *BaseEntity) {

if c.ID == "" {
t.Fatalf("Id is empty")
}

if c.CreatedAt.IsZero() {
t.Fatalf("CreatedAt is empty")
}
if c.ModifiedAt.IsZero() {
t.Fatalf("ModifiedAt is empty")
}
if !c.CreatedAtExported.IsZero() {
t.Fatalf("CreatedAtExported is not empty %d", c.CreatedAtExported.Unix())
}
if !c.ModifiedAtExported.IsZero() {
t.Fatalf("ModifiedAtExported is not empty")
}
}

func TestBaseEntity(t *testing.T) {
c := &BaseEntity{}
c.createNewId()
t.Run("Create New ID", func(t *testing.T) {
id := c.ID
c.createNewId()
if c.ID == id {
t.Fatalf("Id Should Be Different")
}
testBaseEntityCreateNewId(t, c)
})
t.Run("AddTimeStamps", func(t *testing.T) {
now := time.Now()
c.CreatedAt = now
c.ModifiedAt = now
c.AddTimeStamps()
if c.CreatedAtExported != now {
t.Fatalf("CreatedAt and CreatedAtExported Should be same")
}
if c.ModifiedAtExported != now {
t.Fatalf("ModifiedAt and ModifiedAtExported Should be same")
}
})
t.Run("RestoreTimeStamp", func(t *testing.T) {
now := time.Now()
c.CreatedAtExported = now
c.ModifiedAtExported = now
c.RestoreTimeStamp()
if c.CreatedAt != now {
t.Fatalf("CreatedAt and CreatedAtExported Should be same")
}
if c.ModifiedAt != now {
t.Fatalf("ModifiedAt and ModifiedAtExported Should be same")
}
})
t.Run("Updated", func(t *testing.T) {
now := time.Now()
c.ModifiedAt = now
c.Updated()
if c.ModifiedAt.Before(now) {
t.Fatalf("ModifiedAt Should be greater than now")
}
})
}
2 changes: 2 additions & 0 deletions src/mongodb/repos/bank-rate-calc.repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
}

func (repo *BankRateCalcRepoStruct) cacheDataToRedis(entity *interfaces.BankRateCalcEntity) {
entity.AddTimeStamps()
if entityStringBytes, err := json.Marshal(entity); err == nil {
entityString := string(entityStringBytes)
repo.redis.SetStringDataWithExpiry(fmt.Sprintf("%s/%s", bankRateRedisCollection, entity.BullionId), entityString, time.Duration(24)*time.Hour)
Expand Down Expand Up @@ -73,6 +74,7 @@ func (repo *BankRateCalcRepoStruct) FindOneByBullionId(id string) (*interfaces.B
result := new(interfaces.BankRateCalcEntity)
if redisData := repo.redis.GetStringData(fmt.Sprintf("%s/%s", bankRateRedisCollection, id)); redisData != "" {
if err := json.Unmarshal([]byte(redisData), result); err == nil {
result.RestoreTimeStamp()
return result, err
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/mongodb/repos/trade-user.repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,25 @@ func (repo *TradeUserRepoStruct) findByFilter(filter *mongoDbFilter) (*[]interfa
return &result, err
}

func (repo *TradeUserRepoStruct) FindAllInActiveUser(bullionId string) (*[]interfaces.TradeUserEntity, error) {
return repo.findByFilter(&mongoDbFilter{
conditions: &bson.D{
{
Key: "$and",
Value: bson.A{
bson.D{{Key: "bullionId", Value: bullionId}},
bson.D{{Key: "isActive", Value: false}},
},
},
},
})
}

func (repo *TradeUserRepoStruct) FindOne(id string) (*interfaces.TradeUserEntity, error) {
result := new(interfaces.TradeUserEntity)
if redisData := repo.redis.GetStringData(fmt.Sprintf("tradeUser/%s", id)); redisData != "" {
if err := json.Unmarshal([]byte(redisData), result); err == nil {
result.RestoreTimeStamp()
return result, err
}
}
Expand Down Expand Up @@ -167,6 +182,7 @@ func (repo *TradeUserRepoStruct) FindOne(id string) (*interfaces.TradeUserEntity
}

func (repo *TradeUserRepoStruct) cacheDataToRedis(entity *interfaces.TradeUserEntity) {
entity.AddTimeStamps()
if entityStringBytes, err := json.Marshal(entity); err == nil {
entityString := string(entityStringBytes)
repo.redis.SetStringDataWithExpiry(fmt.Sprintf("tradeUser/%s", entity.ID), entityString, time.Duration(24)*time.Hour)
Expand Down
7 changes: 6 additions & 1 deletion src/services/trade-user.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ func (service *tradeUserServiceStruct) UpdateTradeUser(entity *interfaces.TradeU
user.TradeUserBase = entity.TradeUserBase
user.TradeUserAdvanced.IsActive = entity.TradeUserAdvanced.IsActive
user.TradeUserMargins = entity.TradeUserMargins
if _, err := service.tradeUserRepo.Save(entity); err != nil {
// TODO: Password Entity
// user.Password = entity.Password
if _, err := service.tradeUserRepo.Save(user); err != nil {
return err
}
service.eventBus.Publish(events.CreateTradeUserUpdated(entity.BullionId, user, adminId))
Expand All @@ -327,6 +329,9 @@ func (service *tradeUserServiceStruct) generateTokensForTradeUserWithPasswordMat
return service.generateTokensForTradeUser(tradeUser)
}

func (service *tradeUserServiceStruct) FindAndReturnAllInActiveTradeUsers(bullionId string) (*[]interfaces.TradeUserEntity, error) {
return service.tradeUserRepo.FindAllInActiveUser(bullionId)
}
func (service *tradeUserServiceStruct) FindOneUserById(id string) (*interfaces.TradeUserEntity, error) {
return service.tradeUserRepo.FindOne(id)
}
Expand Down

0 comments on commit 2dcbb7c

Please sign in to comment.