discount/internal/utils/expression/bson_condition.go
2023-07-04 04:04:31 +00:00

45 lines
1.0 KiB
Go

package expression
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/exp/constraints"
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/internal/models"
)
type PeriodFieldPaths struct {
From string
To string
}
func GetAscRangeConditionBSON[T constraints.Ordered](key string, value T) []bson.D {
return []bson.D{
{{Key: key, Value: bson.M{"$lte": value}}},
{{Key: key, Value: nil}},
}
}
func GetValueConditionBSON(key string, value any) []bson.D {
return []bson.D{
{{Key: key, Value: value}},
{{Key: key, Value: nil}},
}
}
func GetPerionConditionBSON(period models.PeriodCondition, paths PeriodFieldPaths) []bson.M {
return []bson.M{
{
"$and": []bson.D{
{{Key: paths.From, Value: bson.M{"$lte": primitive.NewDateTimeFromTime(period.From)}}},
{{Key: paths.To, Value: bson.M{"$gte": primitive.NewDateTimeFromTime(period.To)}}},
},
},
{
"$and": []bson.D{
{{Key: paths.From, Value: nil}},
{{Key: paths.To, Value: nil}},
},
},
}
}