discount/internal/utils/expression/bson_condition.go
2024-12-11 15:14:39 +03:00

45 lines
1014 B
Go

package expression
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/exp/constraints"
"gitea.pena/PenaSide/discount/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: 0}},
}
}
func GetValueConditionBSON(key string, value any) []bson.D {
return []bson.D{
{{Key: key, Value: value}},
{{Key: key, Value: ""}},
}
}
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: 0}},
{{Key: paths.To, Value: 0}},
},
},
}
}