Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement fetch customers list #90

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type User struct {
Name string `gorm:"column:name; type:varchar(255)" json:"name"`
Email string `gorm:"column:email; type:varchar(255)" json:"email"`
Password string `gorm:"column:password; type:text; not null" json:"-"`
Role string `gorm:"column:role; type:varchar(255)" json:"role"`
IsActive bool `gorm:"column:is_active; type:boolean" json:"is_active"`
Profile Profile `gorm:"foreignKey:Userid;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"profile"`
Organisations []Organisation `gorm:"many2many:user_organisations;;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"organisations" ` // many to many relationship
Products []Product `gorm:"foreignKey:OwnerID" json:"products"`
Expand All @@ -27,6 +29,7 @@ type CreateUserRequestModel struct {
LastName string `json:"last_name" validate:"required"`
UserName string `json:"username" validate:"required"`
PhoneNumber string `json:"phone_number"`
Role string `json:"role"`
}

type LoginRequestModel struct {
Expand Down Expand Up @@ -65,3 +68,19 @@ func (u *User) CreateUser(db *gorm.DB) error {

return nil
}

func (u *User) GetAllCustomers(db *gorm.DB, page int, limit int) ([]User, any, any, error) {
var totalItems int64
if err := db.Model(&User{}).Preload("Profile").Preload("Products").Preload("Organisations").Where("role = ?", "customer").Count(&totalItems).Error; err != nil {
return nil, nil, nil, err
}

totalPages := (totalItems + int64(limit) - 1) / int64(limit)

var users []User
if err := db.Preload("Profile").Preload("Products").Preload("Organisations").Where("role = ?", "customer").Limit(limit).Offset(page).Find(&users).Error; err != nil {
return users, nil, nil, err
}

return users, totalPages, totalItems, nil
}
49 changes: 49 additions & 0 deletions pkg/controller/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package user

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -92,3 +93,51 @@ func (base *Controller) LoginUser(c *gin.Context) {
rd := utility.BuildSuccessResponse(http.StatusOK, "user login successfully", respData)
c.JSON(http.StatusOK, rd)
}

func (base *Controller) GetAllCustomers(c *gin.Context) {

limitStr := c.Query("limit")
pageStr := c.Query("page")

if limitStr == "" {
c.JSON(http.StatusBadRequest, utility.BuildErrorResponse(400, "error", "Missing limit parameter", "Missing limit parameter", nil))
return
}
if pageStr == "" {
c.JSON(http.StatusBadRequest, utility.BuildErrorResponse(400, "error", "Missing page parameter", "Missing page parameter", nil))
return
}

limit, err := strconv.Atoi(limitStr)
if err != nil || limit <= 0 {
c.JSON(http.StatusBadRequest, utility.BuildErrorResponse(400, "error", "Invalid or missing limit parameter", err, nil))
return
}

page, err := strconv.Atoi(pageStr)
if err != nil || page <= 0 {
c.JSON(http.StatusBadRequest, utility.BuildErrorResponse(400, "error", "Invalid or missing page parameter", err, nil))
return
}


respData, totalPages, totalItems, err := user.GetAllCustomers(base.Db.Postgresql, page, limit)
if err != nil {
rd := utility.BuildErrorResponse(400, "error", err.Error(), err, nil)
c.JSON(http.StatusBadRequest, rd)
return
}

base.Logger.Info("All Customers fetched successfully")

response := gin.H {
"status_code": 200,
"current_page": page,
"total_pages": totalPages,
"limit": limit,
"total_items": totalItems,
"data": respData,
}

c.JSON(http.StatusOK, response)
}
19 changes: 19 additions & 0 deletions services/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func CreateUser(req models.CreateUserRequestModel, db *gorm.DB) (gin.H, int, err
username = strings.ToLower(req.UserName)
phoneNumber = req.PhoneNumber
password = req.Password
role = req.Role
responseData gin.H
)

Expand All @@ -79,11 +80,16 @@ func CreateUser(req models.CreateUserRequestModel, db *gorm.DB) (gin.H, int, err
return nil, http.StatusInternalServerError, err
}

if role == "" {
role = "customer"
}

user := models.User{
ID: utility.GenerateUUID(),
Name: username,
Email: email,
Password: password,
Role: role,
Profile: models.Profile{
ID: utility.GenerateUUID(),
FirstName: firstName,
Expand All @@ -108,6 +114,7 @@ func CreateUser(req models.CreateUserRequestModel, db *gorm.DB) (gin.H, int, err
"first_name": user.Profile.FirstName,
"last_name": user.Profile.LastName,
"phone": user.Profile.Phone,
"role": user.Role,
"expires_in": expiry,
"access_token": token,
}
Expand Down Expand Up @@ -149,9 +156,21 @@ func LoginUser(req models.LoginRequestModel, db *gorm.DB) (gin.H, int, error) {
"first_name": userData.Profile.FirstName,
"last_name": userData.Profile.LastName,
"phone": userData.Profile.Phone,
"role": userData.Role,
"expires_in": expiry,
"access_token": token,
}

return responseData, http.StatusCreated, nil
}

func GetAllCustomers(db *gorm.DB, page int, limit int) ([]models.User, any, any, error) {
var users models.User

userResp, totalPages, totalItems, err := users.GetAllCustomers(db, page, limit)
if err != nil {
return userResp, nil, nil, err
}

return userResp, totalPages, totalItems, err
}
Loading