Skip to content

Commit

Permalink
Merge pull request #18 from codeclubvn/mvp
Browse files Browse the repository at this point in the history
loại bỏ một số thứ ko cần thiết
  • Loading branch information
hieuhoccode099 authored Nov 28, 2023
2 parents a4328b6 + f9b39c1 commit 8824b1a
Show file tree
Hide file tree
Showing 26 changed files with 199 additions and 392 deletions.
3 changes: 2 additions & 1 deletion api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"erp/api/response"
"erp/api_errors"
"erp/utils"
"fmt"
"net/http"

"github.com/pkg/errors"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (b *BaseController) ResponseList(c *gin.Context, message string, total *int
}

func (b *BaseController) ResponseError(c *gin.Context, err error) {

fmt.Println(err.Error())
mas, ok := api_errors.MapErrorCodeMessage[err.Error()]
var status int
ginType := gin.ErrorTypePublic
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/erp/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (b *ERPProductController) Create(c *gin.Context) {
return
}

req.StoreId = utils.GetStoreIDFromContext(c.Request.Context())
//req.StoreId = utils.GetStoreIDFromContext(c.Request.Context())

res, err := b.productService.Create(c, req)
if err != nil {
Expand Down Expand Up @@ -83,5 +83,5 @@ func (b *ERPProductController) GetList(c *gin.Context) {
b.ResponseError(c, err)
return
}
b.ResponseList(c, "success", total, res)
b.ResponseList(c, "success", &total, res)
}
127 changes: 63 additions & 64 deletions api/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"erp/api/response"
"erp/api_errors"
dto "erp/dto/auth"
"erp/models"
"net/http"
"strings"

Expand Down Expand Up @@ -56,69 +55,69 @@ func (e *GinMiddleware) Auth(authorization bool) gin.HandlerFunc {
return
}

storeID := c.Request.Header.Get("x-store-id")
if storeID == "" {
c.Errors = append(c.Errors, &gin.Error{
Err: errors.New(api_errors.ErrMissingXStoreID),
})

mas := api_errors.MapErrorCodeMessage[api_errors.ErrMissingXStoreID]

c.AbortWithStatusJSON(mas.Status, response.ResponseError{
Message: mas.Message,
Code: api_errors.ErrMissingXStoreID,
})
return
}
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), "x-store-id", storeID))

ur := new(models.UserRole)
if err = e.db.Model(models.UserRole{}).Where("user_id = ? AND store_id = ?", claims.Subject, storeID).First(ur).Error; err != nil {
c.Errors = append(c.Errors, &gin.Error{
Err: errors.Wrap(err, "cannot find user role"),
})

mas := api_errors.MapErrorCodeMessage[api_errors.ErrUnauthorizedAccess]

c.AbortWithStatusJSON(mas.Status, response.ResponseError{
Message: mas.Message,
Code: api_errors.ErrUnauthorizedAccess,
})
return
}
if ur.IsStoreOwner {
c.Next()
return
}

role := new(models.Role)
if err = e.db.Model(models.Role{}).Where("id = ?", ur.RoleID).First(role).Error; err != nil {
c.Errors = append(c.Errors, &gin.Error{
Err: errors.Wrap(err, "cannot find role"),
})

mas := api_errors.MapErrorCodeMessage[api_errors.ErrUnauthorizedAccess]

c.AbortWithStatusJSON(mas.Status, response.ResponseError{
Message: mas.Message,
Code: api_errors.ErrUnauthorizedAccess,
})
return
}
up := new(models.Permission)
if err = e.db.Model(models.Permission{}).Where("role_id = ? AND route_path = ?", ur.RoleID, c.Request.URL.Path).First(up).Error; err != nil {
c.Errors = append(c.Errors, &gin.Error{
Err: errors.Wrap(err, "cannot find permission"),
})

mas := api_errors.MapErrorCodeMessage[api_errors.ErrUnauthorizedAccess]

c.AbortWithStatusJSON(mas.Status, response.ResponseError{
Message: mas.Message,
Code: api_errors.ErrUnauthorizedAccess,
})
return
}
//storeID := c.Request.Header.Get("x-store-id")
//if storeID == "" {
// c.Errors = append(c.Errors, &gin.Error{
// Err: errors.New(api_errors.ErrMissingXStoreID),
// })
//
// mas := api_errors.MapErrorCodeMessage[api_errors.ErrMissingXStoreID]
//
// c.AbortWithStatusJSON(mas.Status, response.ResponseError{
// Message: mas.Message,
// Code: api_errors.ErrMissingXStoreID,
// })
// return
//}
//c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), "x-store-id", storeID))

//ur := new(models.UserRole)
//if err = e.db.Model(models.UserRole{}).Where("user_id = ? AND store_id = ?", claims.Subject, storeID).First(ur).Error; err != nil {
// c.Errors = append(c.Errors, &gin.Error{
// Err: errors.Wrap(err, "cannot find user role"),
// })
//
// mas := api_errors.MapErrorCodeMessage[api_errors.ErrUnauthorizedAccess]
//
// c.AbortWithStatusJSON(mas.Status, response.ResponseError{
// Message: mas.Message,
// Code: api_errors.ErrUnauthorizedAccess,
// })
// return
//}
//if ur.IsStoreOwner {
// c.Next()
// return
//}

//role := new(models.Role)
//if err = e.db.Model(models.Role{}).Where("id = ?", ur.RoleID).First(role).Error; err != nil {
// c.Errors = append(c.Errors, &gin.Error{
// Err: errors.Wrap(err, "cannot find role"),
// })
//
// mas := api_errors.MapErrorCodeMessage[api_errors.ErrUnauthorizedAccess]
//
// c.AbortWithStatusJSON(mas.Status, response.ResponseError{
// Message: mas.Message,
// Code: api_errors.ErrUnauthorizedAccess,
// })
// return
//}
//up := new(models.Permission)
//if err = e.db.Model(models.Permission{}).Where("role_id = ? AND route_path = ?", ur.RoleID, c.Request.URL.Path).First(up).Error; err != nil {
// c.Errors = append(c.Errors, &gin.Error{
// Err: errors.Wrap(err, "cannot find permission"),
// })
//
// mas := api_errors.MapErrorCodeMessage[api_errors.ErrUnauthorizedAccess]
//
// c.AbortWithStatusJSON(mas.Status, response.ResponseError{
// Message: mas.Message,
// Code: api_errors.ErrUnauthorizedAccess,
// })
// return
//}

c.Next()
}
Expand Down
19 changes: 0 additions & 19 deletions api/route/auth.go

This file was deleted.

24 changes: 0 additions & 24 deletions api/route/erp/category.go

This file was deleted.

24 changes: 0 additions & 24 deletions api/route/erp/customer.go

This file was deleted.

27 changes: 0 additions & 27 deletions api/route/erp/employee_management.go

This file was deleted.

17 changes: 0 additions & 17 deletions api/route/erp/erp.go

This file was deleted.

13 changes: 0 additions & 13 deletions api/route/erp/module.go

This file was deleted.

21 changes: 0 additions & 21 deletions api/route/erp/order.go

This file was deleted.

24 changes: 0 additions & 24 deletions api/route/erp/product.go

This file was deleted.

20 changes: 0 additions & 20 deletions api/route/erp/promote.go

This file was deleted.

23 changes: 0 additions & 23 deletions api/route/erp/store.go

This file was deleted.

Loading

0 comments on commit 8824b1a

Please sign in to comment.