Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
najamuslim committed May 19, 2024
2 parents ba65c66 + 1f33348 commit ef2274f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion db/migrations/20240501171902_add_users_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE users (
user_id VARCHAR(255) PRIMARY KEY,
nip NUMERIC(13) UNIQUE NOT NULL,
nip NUMERIC(15) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
identity_card_scan_img VARCHAR(2083),
password VARCHAR(255) NOT NULL,
Expand Down
22 changes: 18 additions & 4 deletions src/handler/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (h *AuthHandler) Register(c *gin.Context) {
}

// Validate request payload
if(request.Nip == 0) {
log.Println("Register bad request > invalid NIP")
c.JSON(400, gin.H{"status": "bad request", "message": "invalid NIP"})
return
}

err = ValidateRegisterRequest(request.Nip, request.Name, request.Password)
if err != nil {
log.Println("Register bad request ", err)
Expand Down Expand Up @@ -72,6 +78,12 @@ func (h *AuthHandler) Login(c *gin.Context) {
c.JSON(400, gin.H{"status": "bad request", "message": err})
return
}

if(request.Nip == 0) {
log.Println("Register bad request > invalid NIP")
c.JSON(400, gin.H{"status": "bad request", "message": "invalid NIP"})
return
}

nStr := strconv.FormatInt(request.Nip, 10)
if !strings.HasPrefix(nStr, "615") {
Expand Down Expand Up @@ -105,7 +117,8 @@ func (h *AuthHandler) Login(c *gin.Context) {
"data": gin.H{
"nip": userData.Nip,
"name": userData.Name,
"accessToken": token,
"accessToken": token,
"userId": userData.Id,
},
})
}
Expand Down Expand Up @@ -158,10 +171,10 @@ func (h *AuthHandler) LoginNurse(c *gin.Context) {
}

// ValidateRegisterRequest validates the register user request payload
func ValidateRegisterRequest(nip int64, name, password string) error {
func ValidateRegisterRequest(nip int64, name string, password string) error {
// Validate email format
if !isValidNip(nip) {
return errors.New("nip must be in valid email format")
return errors.New("nip invalid")
}

// Validate name length
Expand Down Expand Up @@ -210,7 +223,8 @@ func ValidateLoginNurseRequest(nip int64, password string) error {
// TODO fix to the correct nip validation
func isValidNip(nip int64) bool {
// Regular expression pattern for email format
nipRegex := `^615[12][2-9][0-9]{3}(0[1-9]|1[0-2])[0-9]{3}$`
nipRegex := `^615[12](200[0-9]|201[0-9]|202[0-4])(0[1-9]|1[0-2])\d{3,5}$`

match, _ := regexp.MatchString(nipRegex, strconv.FormatInt(nip, 10))
return match
}
4 changes: 1 addition & 3 deletions src/handler/nurse_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -247,8 +246,7 @@ func ValidateRegisterNurseRequest(nip int64, name string) error {
}

func isValidNipNurse(nip int64) bool {
currentYear := time.Now().Year()
nipRegex := fmt.Sprintf(`^303[12](200[0-%d]|20[01][0-9]|202[0-%d])(0[1-9]|1[0-2])[0-9]{3}$`, currentYear%10, currentYear%10)
nipRegex := fmt.Sprintf(`^303[12](200[0-9]|201[0-9]|202[0-4])(0[1-9]|1[0-2])\d{3,5}$`)
// Convert the nip int64 to a string
nipStr := strconv.FormatInt(nip, 10)
// Match the string with the regex
Expand Down

0 comments on commit ef2274f

Please sign in to comment.