Compare commits

..

3 Commits

4 changed files with 30 additions and 19 deletions

View File

@ -4,8 +4,7 @@ import (
"bytes"
"context"
"fmt"
"regexp"
"time"
"strconv"
"git.defalsify.org/vise.git/engine"
"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)
if len(yob) > 4 {
if len(yob) == 4 {
yob := string(input)
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) {
res := resource.Result{}
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}$`)
isCorrectFormat := dateRegex.MatchString(date)
if !isCorrectFormat {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
} else {
if len(date) == 4 {
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
} else {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
}
return res, nil
@ -558,21 +561,14 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
yob := accountData["YOB"]
location := accountData["Location"]
offerings := accountData["Offerings"]
layout := "02/01/2006"
birthdate, err := time.Parse(layout, yob)
if err != nil {
return res, err
}
if yob == "Not provided" {
age = "Not provided"
} else {
currentDate := time.Now()
formattedDate := currentDate.Format(layout)
today, err := time.Parse(layout, formattedDate)
ageInt, err := strconv.Atoi(yob)
if err != 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)
res.Content = formattedData

View File

@ -20,3 +20,16 @@ func CalculateAge(birthdate, today time.Time) int {
}
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
Please provide date format as 12/01/1972
The year of birth you entered is invalid.
Please try again.

View File

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