Files
alfred-be/alfred/utils/vaildations.go
2026-03-08 16:14:42 +05:30

117 lines
3.6 KiB
Go

package utils
import (
"alfred/model/common"
"alfred/pkg/log"
"errors"
"fmt"
"github.com/google/uuid"
"go.uber.org/zap"
"regexp"
"strconv"
"strings"
"time"
)
func ValidateTimestampsForWeb(startTimestamp, endTimestamp string, lagTime time.Duration) (int64, int64, error) {
startTimeInteger, err := strconv.ParseInt(startTimestamp, 0, 64)
//introducing lag of 5 min to avoid the case where events are present but zip is still pending to be uploaded
if err != nil {
return 0, GetTimeWithOffsetInMillis(-lagTime), nil
}
endTimeInteger, err := strconv.ParseInt(endTimestamp, 0, 64)
if err != nil {
return startTimeInteger, GetTimeWithOffsetInMillis(-lagTime), nil
}
if startTimeInteger > endTimeInteger {
return 0, 0, errors.New("start time should be less than end time")
}
return startTimeInteger, endTimeInteger, nil
}
func ValidateTimestamps(startTimestamp, endTimestamp string) (int64, int64, error) {
startTimeInteger := GetInt64FromString(startTimestamp)
endTimeInteger := GetInt64FromString(endTimestamp)
if startTimeInteger > endTimeInteger {
return 0, 0, errors.New("start time should be less than end time")
}
return startTimeInteger, endTimeInteger, nil
}
func ValidatePage(pageSize, pageNumber, sortDirection string) (int64, int64, common.SortDirection, error) {
pageSizeInteger, err := strconv.ParseInt(pageSize, 0, 64)
if err != nil {
pageSizeInteger = 10
}
pageNumberInteger, err := strconv.ParseInt(pageNumber, 0, 64)
if err != nil {
pageNumberInteger = 0
}
sortEnum := common.SortDirection(EMPTY)
if sortDirection == "asc" {
sortEnum = common.ASC
} else {
sortEnum = common.DESC
}
return pageSizeInteger, pageNumberInteger, sortEnum, nil
}
func ValidateFileExtension(filename, fileExtension string) (bool, error) {
if !strings.HasSuffix(filename, fileExtension) {
return false, errors.New(fmt.Sprintf("filename %s should be of extension: %s", filename, fileExtension))
}
return true, nil
}
func ValidateDirectoryName(directoryName string) bool {
if directoryName == EMPTY || strings.Contains(directoryName, FORWARD_SLASH) {
return false
}
return true
}
func ValidateId(id, suffix string) bool {
trimmedId := strings.TrimSuffix(id, suffix)
_, err := uuid.Parse(trimmedId)
return err == nil
}
func ValidateZipName(id, suffix string) bool {
trimmedBody := strings.TrimSuffix(id, suffix)
if trimmedBody == EMPTY {
return false
}
return true
}
func CheckFilterEnabled(labelFilters, customerId, phoneNumber, appName, screenName, vertical, appVersion string, deviceIds []string, screenTags string, codePushVersion string, agentEmailId string, fragmentNames string, snapshotPerSecond string) bool {
return labelFilters != EMPTY || customerId != EMPTY || phoneNumber != EMPTY || appName != EMPTY || screenName != EMPTY || vertical != EMPTY ||
appVersion != EMPTY || deviceIds != nil || screenTags != EMPTY || codePushVersion != EMPTY || agentEmailId != EMPTY || fragmentNames != EMPTY || snapshotPerSecond != EMPTY
}
func ValidatePresentTime(timeStamp int64, allowedPastTimestamp, allowedFutureTimestamp time.Duration) bool {
presentTime := GetCurrentTimeInMillis()
futureTimeAllowed := presentTime + (allowedFutureTimestamp * time.Hour).Milliseconds()
pastTimeAllowed := presentTime - (allowedPastTimestamp * time.Hour).Milliseconds()
return timeStamp <= futureTimeAllowed && timeStamp >= pastTimeAllowed
}
func ValidatePhoneNumber(phoneNumber string) bool {
// Check if the phoneNumber is empty
if phoneNumber == EMPTY {
return true
}
match, err := regexp.MatchString(`^\+?(\d{1,2})?\d{10}$`, phoneNumber)
if err != nil {
log.Error("Error: Phone Number Validation Failed", zap.Error(err))
return false
}
return match
}