@@ -0,0 +1,15 @@
|
||||
-- Rename a column from "Position_Code" to "ContractPosition_Code"
|
||||
ALTER TABLE "public"."User" RENAME COLUMN "Position_Code" TO "ContractPosition_Code";
|
||||
-- Create "Intern" table
|
||||
CREATE TABLE "public"."Intern" (
|
||||
"Id" bigserial NOT NULL,
|
||||
"CreatedAt" timestamptz NULL,
|
||||
"UpdatedAt" timestamptz NULL,
|
||||
"DeletedAt" timestamptz NULL,
|
||||
"Person_Id" bigint NULL,
|
||||
"Position_Code" character varying(20) NULL,
|
||||
"User_Id" bigint NULL,
|
||||
PRIMARY KEY ("Id"),
|
||||
CONSTRAINT "fk_Intern_Person" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
|
||||
CONSTRAINT "fk_Intern_User" FOREIGN KEY ("User_Id") REFERENCES "public"."User" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
|
||||
);
|
||||
@@ -1,4 +1,4 @@
|
||||
h1:S3ggQSrIa2Lwhkx+yWmD9N2hZxRuVwCMZcEpsk83PJY=
|
||||
h1:eCBVp2yPQk7VrLdSk2mDps+FXavKA5OcJ1/QRBh+ovI=
|
||||
20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k=
|
||||
20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0=
|
||||
20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI=
|
||||
@@ -49,5 +49,4 @@ h1:S3ggQSrIa2Lwhkx+yWmD9N2hZxRuVwCMZcEpsk83PJY=
|
||||
20251016062912.sql h1:ACNn0fe+EMqUt3hoY+Dr3uqAV/QICBa1+mIW7fUc9Fk=
|
||||
20251017060617.sql h1:4T3t9ifWrEQTPMSM0XJ98pF7Qdt+UfgtMui17bhrnWI=
|
||||
20251017082207.sql h1:8vLG1l/saRRMHXkyA4nelJyjaSddhZd6r7R+Uo4JS/c=
|
||||
20251018032635.sql h1:UltiY1Jm1KjAyBx/oRbhKzwEl3Aqh3o+eaOIfWdroJ8=
|
||||
20251018040322.sql h1:24VgOPgO2pxfwTcAv7xCv8BHiRPqmIbHUCXNLyvrGfc=
|
||||
20251019093915.sql h1:wsJMh1r45ONaY7zIq2c3rTcuGXN4mI0irZK7a96HZW8=
|
||||
|
||||
@@ -12,7 +12,7 @@ type (
|
||||
|
||||
const (
|
||||
CSCEmp ContractPositionCode = "employee" // Pegawai
|
||||
CSCSpi ContractPositionCode = "intern" // PPDS
|
||||
CSCInt ContractPositionCode = "intern" // PPDS
|
||||
|
||||
EPCReg EmployeePosisitionCode = "registration" // Admisi/Pendaftaran
|
||||
EPCNur EmployeePosisitionCode = "nurse" // Perawat
|
||||
|
||||
@@ -10,14 +10,18 @@ import (
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"simrs-vx/internal/domain/main-entities/intern"
|
||||
eu "simrs-vx/internal/domain/main-entities/user"
|
||||
|
||||
pa "simrs-vx/pkg/auth-helper"
|
||||
el "simrs-vx/pkg/logger"
|
||||
p "simrs-vx/pkg/password"
|
||||
|
||||
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"
|
||||
|
||||
a "github.com/karincake/apem"
|
||||
dg "github.com/karincake/apem/db-gorm-pg"
|
||||
@@ -74,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 {
|
||||
@@ -92,43 +91,94 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
||||
atExpires := time.Now().Add(duration).Unix()
|
||||
atSecretKey := authCfg.AtSecretKey
|
||||
|
||||
// extra
|
||||
role := []string{}
|
||||
if user.ContractPosition_Code == "employee" {
|
||||
employee := ee.Employee{}
|
||||
dg.I.Where("user_id = ?", user.Id).First(&employee)
|
||||
role = append(role, "emp-"+string(*employee.Position_Code))
|
||||
} else if user.ContractPosition_Code == "intern" {
|
||||
// specialistIntern := esi.SpecialistIntern{}
|
||||
// dg.I.Where("user_id = ?", user.Id).First(&specialistIntern)
|
||||
role = append(role, "spi")
|
||||
}
|
||||
|
||||
// Creating Access Token
|
||||
// Create Claim
|
||||
atClaims := jwt.MapClaims{}
|
||||
atClaims["user_id"] = user.Id
|
||||
atClaims["user_name"] = user.Name
|
||||
// atClaims["user_email"] = user.Email
|
||||
// atClaims["user_position_code"] = user.Position_Code
|
||||
atClaims["user_employementStatus_code"] = user.ContractPosition_Code
|
||||
// atClaims["user_ref_id"] = user.Ref_Id
|
||||
atClaims["exp"] = atExpires
|
||||
atClaims["user_contractPosition_code"] = user.ContractPosition_Code
|
||||
atClaims["uuid"] = aUuid
|
||||
atClaims["user_division_positions"] = userDivisionPositions
|
||||
atClaims["exp"] = atExpires
|
||||
|
||||
// Create output
|
||||
outputData := d.II{
|
||||
"user_id": strconv.Itoa(int(user.Id)),
|
||||
"user_name": user.Name,
|
||||
"user_contractPosition_code": user.ContractPosition_Code,
|
||||
}
|
||||
|
||||
// extra
|
||||
role := []string{}
|
||||
switch user.ContractPosition_Code {
|
||||
case erg.CSCEmp:
|
||||
// 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))
|
||||
|
||||
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
|
||||
}
|
||||
// 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{}
|
||||
dg.I.Where("\"User_Id\" = ?", user.Id).First(&intern)
|
||||
role = append(role, "int-"+string(*intern.Position_Code))
|
||||
}
|
||||
atClaims["roles"] = role
|
||||
outputData["roles"] = role
|
||||
|
||||
// Generate jwt
|
||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||
ats, err := at.SignedString([]byte(atSecretKey))
|
||||
if err != nil {
|
||||
return nil, d.FieldErrors{"user": d.FieldError{Code: "token-sign-err", Message: el.GenMessage("token-sign-err")}}
|
||||
}
|
||||
|
||||
outputData := d.II{
|
||||
"user_id": strconv.Itoa(int(user.Id)),
|
||||
"user_name": user.Name,
|
||||
// "user_email": user.Email,
|
||||
"user_contractPosition_code": user.ContractPosition_Code,
|
||||
// "user_ref_id": user.Ref_Id,
|
||||
"accessToken": ats,
|
||||
}
|
||||
outputData["accessToken"] = ats
|
||||
|
||||
// Save to redis
|
||||
now := time.Now()
|
||||
|
||||
@@ -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 dp.Division.Code
|
||||
}
|
||||
return ""
|
||||
}(),
|
||||
DivisionPosition_Code: dp.Code,
|
||||
})
|
||||
if dp.Division != nil {
|
||||
result = append(result, "div-"+dp.Division.Code+"-"+dp.Code)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
@@ -83,7 +83,7 @@ func Create(input e.CreateDto) (*d.Data, error) {
|
||||
data = *resData
|
||||
}
|
||||
|
||||
if input.ContractPosition_Code == ero.CSCSpi {
|
||||
if input.ContractPosition_Code == ero.CSCInt {
|
||||
createInt := esi.CreateDto{
|
||||
Person_Id: input.Person_Id,
|
||||
Specialist_Id: input.Specialist_Id,
|
||||
@@ -336,7 +336,7 @@ func Update(input e.UpdateDto) (*d.Data, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if input.ContractPosition_Code == ero.CSCSpi {
|
||||
if input.ContractPosition_Code == ero.CSCInt {
|
||||
readInt := esi.ReadDetailDto{User_Id: &data.Id}
|
||||
readIntData, err := usi.ReadDetailData(readInt, &event, tx)
|
||||
if err != nil {
|
||||
|
||||
@@ -56,7 +56,7 @@ func setDataEmployeeUpdate(src e.EmployeUpdateDto) ee.UpdateDto {
|
||||
func getPersonIdByUserId(userId uint, positionCode erg.ContractPositionCode, event *pl.Event, tx *gorm.DB) (*uint, error) {
|
||||
pl.SetLogInfo(event, nil, "started", "DBGetPersonIdByUserId")
|
||||
if positionCode == erg.CSCEmp {
|
||||
} else if positionCode == erg.CSCSpi {
|
||||
} else if positionCode == erg.CSCInt {
|
||||
specInt, err := usi.ReadDetailData(esi.ReadDetailDto{User_Id: &userId}, event, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user