feat/user: auth reworking
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
|
||||
ed "simrs-vx/internal/domain/main-entities/doctor"
|
||||
ee "simrs-vx/internal/domain/main-entities/employee"
|
||||
en "simrs-vx/internal/domain/main-entities/nurse"
|
||||
erc "simrs-vx/internal/domain/references/common"
|
||||
erg "simrs-vx/internal/domain/references/organization"
|
||||
|
||||
@@ -77,11 +78,6 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-login-unverified", Message: el.GenMessage("auth-login-unverified")}}
|
||||
}
|
||||
|
||||
userDivisionPositions, err := getDivisionPosition(user.Id)
|
||||
if err != nil {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: el.GenMessage("auth-getData-failed")}}
|
||||
}
|
||||
|
||||
// Access token prep
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
@@ -100,7 +96,6 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
atClaims["user_id"] = user.Id
|
||||
atClaims["user_name"] = user.Name
|
||||
atClaims["user_contractPosition_code"] = user.ContractPosition_Code
|
||||
atClaims["division_positions"] = userDivisionPositions
|
||||
atClaims["uuid"] = aUuid
|
||||
atClaims["exp"] = atExpires
|
||||
|
||||
@@ -118,21 +113,56 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
// employee
|
||||
employee := ee.Employee{}
|
||||
dg.I.Where("\"User_Id\" = ?", user.Id).First(&employee)
|
||||
if employee.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noEmployee", Message: el.GenMessage("auth-noEmployee")}}
|
||||
}
|
||||
atClaims["employee_id"] = employee.Id
|
||||
outputData["employee_id"] = employee.Id
|
||||
role = append(role, "emp-"+string(*employee.Position_Code))
|
||||
atClaims["employee_division_code"] = employee.Division_Code
|
||||
outputData["employee_division_code"] = employee.Division_Code
|
||||
// doctor
|
||||
if employee.Id > 0 && employee.Position_Code != nil && *employee.Position_Code == erg.EPCDoc {
|
||||
doctor := ed.Doctor{}
|
||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&doctor)
|
||||
if doctor.Specialist_Id != nil {
|
||||
atClaims["specialist_id"] = doctor.Specialist_Id
|
||||
outputData["specialist_id"] = doctor.Specialist_Id
|
||||
|
||||
if employee.Division_Code != nil {
|
||||
atClaims["employee_division_code"] = employee.Division_Code
|
||||
outputData["employee_division_code"] = employee.Division_Code
|
||||
}
|
||||
|
||||
// employee position
|
||||
if employee.Id > 0 && employee.Position_Code != nil {
|
||||
switch *employee.Position_Code {
|
||||
case erg.EPCDoc:
|
||||
doctor := ed.Doctor{}
|
||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&doctor)
|
||||
if doctor.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noDoctor", Message: el.GenMessage("auth-noDoctor")}}
|
||||
}
|
||||
atClaims["doctor_id"] = doctor.Id
|
||||
outputData["doctor_id"] = doctor.Id
|
||||
|
||||
// specialist
|
||||
if doctor.Specialist_Id != nil {
|
||||
atClaims["specialist_id"] = doctor.Specialist_Id
|
||||
outputData["specialist_id"] = doctor.Specialist_Id
|
||||
}
|
||||
if doctor.Subspecialist_Id != nil {
|
||||
atClaims["subspecialist_id"] = doctor.Subspecialist_Id
|
||||
outputData["subspecialist_id"] = doctor.Subspecialist_Id
|
||||
}
|
||||
case erg.EPCNur:
|
||||
nurse := en.Nurse{}
|
||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&nurse)
|
||||
if nurse.Id == 0 {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: el.GenMessage("auth-noNurse")}}
|
||||
}
|
||||
atClaims["nurse_id"] = nurse.Id
|
||||
outputData["nurse_id"] = nurse.Id
|
||||
}
|
||||
if doctor.Subspecialist_Id != nil {
|
||||
atClaims["subspecialist_id"] = doctor.Subspecialist_Id
|
||||
outputData["subspecialist_id"] = doctor.Subspecialist_Id
|
||||
// division position
|
||||
divsionPositions, err := getDivisionPosition(employee.Id)
|
||||
if err != nil {
|
||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: el.GenMessage("auth-getData-failed")}}
|
||||
}
|
||||
role = append(role, divsionPositions...)
|
||||
// atClaims["division_positions"] = divsionPositions
|
||||
// outputData["division_positions"] = divsionPositions
|
||||
}
|
||||
case erg.CSCInt:
|
||||
intern := intern.Intern{}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
package authentication
|
||||
|
||||
import (
|
||||
"errors"
|
||||
edp "simrs-vx/internal/domain/main-entities/division-position"
|
||||
ee "simrs-vx/internal/domain/main-entities/employee"
|
||||
|
||||
pa "simrs-vx/pkg/auth-helper"
|
||||
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
"gorm.io/gorm"
|
||||
@@ -27,21 +23,21 @@ func getDocName(id uint) string {
|
||||
return "authentication"
|
||||
}
|
||||
|
||||
func getDivisionPosition(user_id uint) ([]pa.DivisionPosition, error) {
|
||||
var result []pa.DivisionPosition
|
||||
func getDivisionPosition(employee_id uint) ([]string, error) {
|
||||
var result []string
|
||||
|
||||
var employee ee.Employee
|
||||
if err := dg.I.Where("\"User_Id\" = ?", user_id).First(&employee).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return result, nil
|
||||
}
|
||||
return result, errors.New("no employee found")
|
||||
}
|
||||
// var employee ee.Employee
|
||||
// if err := dg.I.Where("\"Employee_Id\" = ?", employee_id).First(&employee).Error; err != nil {
|
||||
// if err == gorm.ErrRecordNotFound {
|
||||
// return result, nil
|
||||
// }
|
||||
// return result, errors.New("no employee found")
|
||||
// }
|
||||
|
||||
var divisionPositions []edp.DivisionPosition
|
||||
err := dg.I.
|
||||
Preload("Division").
|
||||
Where("\"Employee_Id\" = ?", employee.Id).
|
||||
Where("\"Employee_Id\" = ?", employee_id).
|
||||
Find(&divisionPositions).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
@@ -51,15 +47,9 @@ func getDivisionPosition(user_id uint) ([]pa.DivisionPosition, error) {
|
||||
}
|
||||
|
||||
for _, dp := range divisionPositions {
|
||||
result = append(result, pa.DivisionPosition{
|
||||
Division_Code: func() string {
|
||||
if dp.Division != nil {
|
||||
return "div-" + dp.Division.Code
|
||||
}
|
||||
return ""
|
||||
}(),
|
||||
DivisionPosition_Code: dp.Code,
|
||||
})
|
||||
if dp.Division != nil {
|
||||
result = append(result, "div-"+dp.Division.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
Reference in New Issue
Block a user