validate date as a 4 digit input value

This commit is contained in:
Carlosokumu 2024-08-27 14:57:26 +03:00
parent 2e9b96d3a2
commit 674fd5773a
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -4,8 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"regexp" "strconv"
"time"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/lang" "git.defalsify.org/vise.git/lang"
@ -189,7 +188,7 @@ func (h *Handlers) SaveYob(cxt context.Context, sym string, input []byte) (resou
} }
yob := string(input) yob := string(input)
if len(yob) > 4 { if len(yob) == 4 {
yob := string(input) yob := string(input)
accountData["YOB"] = yob accountData["YOB"] = yob
@ -380,13 +379,17 @@ func (h *Handlers) Quit(ctx context.Context, sym string, input []byte) (resource
func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} res := resource.Result{}
date := string(input) date := string(input)
_, err := strconv.Atoi(date)
dateRegex := regexp.MustCompile(`^\d{2}/\d{2}/\d{4}$`) if err != nil {
isCorrectFormat := dateRegex.MatchString(date) // If conversion fails, input is not numeric
if !isCorrectFormat {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT) res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
} else { return res, nil
}
if len(date) == 4 {
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT) res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
} else {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
} }
return res, nil return res, nil
@ -558,21 +561,14 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
yob := accountData["YOB"] yob := accountData["YOB"]
location := accountData["Location"] location := accountData["Location"]
offerings := accountData["Offerings"] offerings := accountData["Offerings"]
layout := "02/01/2006"
birthdate, err := time.Parse(layout, yob)
if err != nil {
return res, err
}
if yob == "Not provided" { if yob == "Not provided" {
age = "Not provided" age = "Not provided"
} else { } else {
currentDate := time.Now() ageInt, err := strconv.Atoi(yob)
formattedDate := currentDate.Format(layout)
today, err := time.Parse(layout, formattedDate)
if err != nil { if err != nil {
return res, nil return res, nil
} }
age = string(utils.CalculateAge(birthdate, today)) age = strconv.Itoa(utils.CalculateAgeWithYOB(ageInt))
} }
formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", name, gender, age, location, offerings) formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", name, gender, age, location, offerings)
res.Content = formattedData res.Content = formattedData