67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
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"
|
|
)
|
|
|
|
// just return the error code
|
|
func getAndCheck(input, condition any) (eCode string) {
|
|
result := dg.I.Where(condition).Find(&input)
|
|
if result.Error != nil {
|
|
return "fetch-fail"
|
|
} else if result.RowsAffected == 0 {
|
|
return "auth-login-incorrect"
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
func getDocName(id uint) string {
|
|
return "authentication"
|
|
}
|
|
|
|
func getDivisionPosition(user_id uint) ([]pa.DivisionPosition, error) {
|
|
var result []pa.DivisionPosition
|
|
|
|
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 divisionPositions []edp.DivisionPosition
|
|
err := dg.I.
|
|
Preload("Division").
|
|
Where("\"Employee_Id\" = ?", employee.Id).
|
|
Find(&divisionPositions).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return result, nil
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
for _, dp := range divisionPositions {
|
|
result = append(result, pa.DivisionPosition{
|
|
Division_Code: func() string {
|
|
if dp.Division != nil {
|
|
return dp.Division.Code
|
|
}
|
|
return ""
|
|
}(),
|
|
DivisionPosition_Code: dp.Code,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|