init project
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"furtuna-be/internal/constants/role"
|
||||
"furtuna-be/internal/constants/userstatus"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
Status userstatus.UserStatus
|
||||
NIK string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
RoleID role.Role
|
||||
RoleName string
|
||||
PartnerID *int64
|
||||
BranchName string
|
||||
}
|
||||
|
||||
type AuthenticateUser struct {
|
||||
Token string
|
||||
Name string
|
||||
RoleID role.Role
|
||||
RoleName string
|
||||
BranchID *int64
|
||||
BranchName string
|
||||
}
|
||||
|
||||
type UserRoleDB struct {
|
||||
ID int64 `gorm:"primary_key;column:user_role_id" `
|
||||
UserID int64 `gorm:"column:user_id"`
|
||||
RoleID int64 `gorm:"column:role_id"`
|
||||
PartnerID *int64 `gorm:"column:partner_id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
}
|
||||
|
||||
func (UserRoleDB) TableName() string {
|
||||
return "user_roles"
|
||||
}
|
||||
|
||||
func (u *User) ToUserDB(createdBy int64) (*UserDB, error) {
|
||||
hashedPassword, err := u.HashedPassword(u.Password)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if u.RoleID == role.BranchAdmin && u.PartnerID == nil {
|
||||
return nil, errors.New("invalid request")
|
||||
}
|
||||
|
||||
return &UserDB{
|
||||
Name: u.Name,
|
||||
Email: u.Email,
|
||||
Password: hashedPassword,
|
||||
RoleID: int64(u.RoleID),
|
||||
BranchID: u.PartnerID,
|
||||
Status: userstatus.Active,
|
||||
CreatedBy: createdBy,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u User) HashedPassword(password string) (string, error) {
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(hashedPassword), nil
|
||||
}
|
||||
Reference in New Issue
Block a user