Compare commits

...

3 Commits

4 changed files with 30 additions and 19 deletions

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)
if err != nil {
// If conversion fails, input is not numeric
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
return res, nil
}
dateRegex := regexp.MustCompile(`^\d{2}/\d{2}/\d{4}$`) if len(date) == 4 {
isCorrectFormat := dateRegex.MatchString(date)
if !isCorrectFormat {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
} else {
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

View File

@ -19,4 +19,17 @@ func CalculateAge(birthdate, today time.Time) int {
age-- age--
} }
return age return age
}
// CalculateAgeWithYOB calculates the age based on the given year of birth (YOB).
// It subtracts the YOB from the current year to determine the age.
//
// Parameters:
// yob: The year of birth as an integer.
//
// Returns:
// The calculated age as an integer.
func CalculateAgeWithYOB(yob int) int {
currentYear := time.Now().Year()
return currentYear - yob
} }

View File

@ -1,2 +1,2 @@
Unsuported date format The year of birth you entered is invalid.
Please provide date format as 12/01/1972 Please try again.

View File

@ -0,0 +1,2 @@
Mwaka wa kuzaliwa ulioweka sio sahihi.
Tafadhali jaribu tena.