Compare commits

..

No commits in common. "2a5f7517f420dab359a07a1981caedab75be4c91" and "bf99ed84822121de076638a429acc4abb8c4ab51" have entirely different histories.

4 changed files with 19 additions and 30 deletions

View File

@ -4,7 +4,8 @@ import (
"bytes"
"context"
"fmt"
"strconv"
"regexp"
"time"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/lang"
@ -188,7 +189,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
@ -379,17 +380,13 @@ 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
}
if len(date) == 4 {
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
} else {
dateRegex := regexp.MustCompile(`^\d{2}/\d{2}/\d{4}$`)
isCorrectFormat := dateRegex.MatchString(date)
if !isCorrectFormat {
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
} else {
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
}
return res, nil
@ -561,14 +558,21 @@ 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 {
ageInt, err := strconv.Atoi(yob)
currentDate := time.Now()
formattedDate := currentDate.Format(layout)
today, err := time.Parse(layout, formattedDate)
if err != nil {
return res, nil
}
age = strconv.Itoa(utils.CalculateAgeWithYOB(ageInt))
age = string(utils.CalculateAge(birthdate, today))
}
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,16 +20,3 @@ 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 @@
The year of birth you entered is invalid.
Please try again.
Unsuported date format
Please provide date format as 12/01/1972

View File

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