usermngmt/db.go

75 lines
1.5 KiB
Go
Raw Normal View History

2021-11-08 08:34:20 +00:00
package usermngmt
import (
"context"
"fmt"
"github.com/Selly-Modules/logger"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// getUserCollection ...
func (s Service) getUserCollection() *mongo.Collection {
2021-11-08 10:04:01 +00:00
if s.TablePrefix != "" {
return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableUser))
}
return s.DB.Collection(tableUser)
2021-11-08 08:34:20 +00:00
}
// getRoleCollection ...
func (s Service) getRoleCollection() *mongo.Collection {
2021-11-08 10:04:01 +00:00
if s.TablePrefix != "" {
s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableRole))
}
return s.DB.Collection(tableRole)
2021-11-08 08:34:20 +00:00
}
2021-11-08 10:04:01 +00:00
func (s Service) isPhoneNumberOrEmailExisted(ctx context.Context, phone, email string) bool {
2021-11-08 08:34:20 +00:00
var (
2021-11-08 10:04:01 +00:00
col = s.getUserCollection()
2021-11-08 08:34:20 +00:00
)
// Find
cond := bson.M{
"$or": []bson.M{
{
"phone": phone,
},
{
"email": email,
},
},
}
2021-11-08 10:04:01 +00:00
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - countUserByCondition", logger.LogData{
2021-11-08 08:34:20 +00:00
"condition": cond,
"err": err.Error(),
})
return true
}
2021-11-08 10:04:01 +00:00
return total != 0
2021-11-08 08:34:20 +00:00
}
2021-11-08 10:04:01 +00:00
func (s Service) isRoleIDExisted(ctx context.Context, roleID primitive.ObjectID) bool {
2021-11-08 08:34:20 +00:00
var (
2021-11-08 10:04:01 +00:00
col = s.getRoleCollection()
2021-11-08 08:34:20 +00:00
)
// Find
cond := bson.M{
"_id": roleID,
}
2021-11-08 10:04:01 +00:00
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - countRoleByCondition", logger.LogData{
2021-11-08 08:34:20 +00:00
"condition": cond,
"err": err.Error(),
})
return false
}
2021-11-08 10:04:01 +00:00
return total != 0
2021-11-08 08:34:20 +00:00
}