feat: customer list
This commit is contained in:
@@ -123,6 +123,54 @@ func (b *UserRepository) GetAllUsers(ctx context.Context, req entity.UserSearch)
|
||||
|
||||
return users, int(total), nil
|
||||
}
|
||||
func (b *UserRepository) GetAllCustomer(ctx context.Context, req entity.CustomerSearch) (entity.CustomerList, int, error) {
|
||||
var users []*entity.UserDB
|
||||
var total int64
|
||||
|
||||
query := b.db.Table("users").
|
||||
Select("users.id, users.email, users.name, users.phone_number, users.status, users.created_at, users.updated_at, ur.role_id, r.role_name, ur.partner_id, b.name as partner_name").
|
||||
Joins("LEFT JOIN user_roles ur ON users.id = ur.user_id").
|
||||
Joins("LEFT JOIN roles r ON ur.role_id = r.role_id").
|
||||
Joins("LEFT JOIN partners b ON ur.partner_id = b.id").
|
||||
Where("users.deleted_at is null").
|
||||
Where("users.user_type = ?", "CUSTOMER")
|
||||
|
||||
if req.Search != "" {
|
||||
query = query.Where("users.name ILIKE ? or users.email ILIKE ? or r.role_name ILIKE ? or b.name ILIKE ? ", "%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%")
|
||||
}
|
||||
|
||||
if req.Name != "" {
|
||||
query = query.Where("users.name ILIKE ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
if req.RoleID > 0 {
|
||||
query = query.Where("ur.role_id = ? ", req.RoleID)
|
||||
}
|
||||
|
||||
if req.PartnerID > 0 {
|
||||
query = query.Where("ur.partner_id = ? ", req.PartnerID)
|
||||
}
|
||||
|
||||
if req.SiteID > 0 {
|
||||
query = query.Where("ur.site_id = ? ", req.SiteID)
|
||||
}
|
||||
|
||||
// Get the total count without applying the limit and offset.
|
||||
if err := query.Model(&entity.UserDB{}).Count(&total).Error; err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when count users", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Apply pagination.
|
||||
query = query.Offset(req.Offset).Limit(req.Limit)
|
||||
|
||||
if err := query.Scan(&users).Error; err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when get all users", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return users, int(total), nil
|
||||
}
|
||||
|
||||
func (b *UserRepository) GetUserByID(ctx context.Context, id int64) (*entity.UserDB, error) {
|
||||
var user *entity.UserDB
|
||||
|
||||
Reference in New Issue
Block a user