Spaces:
Paused
Paused
File size: 7,705 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | package repositories
import (
"context"
"fmt"
"regexp"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/NdoleStudio/stacktrace"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// mongoContactRepository is responsible for persisting entities.Contact in MongoDB.
type mongoContactRepository struct {
logger telemetry.Logger
tracer telemetry.Tracer
collection *mongo.Collection
}
// NewMongoContactRepository creates the MongoDB version of the ContactRepository.
func NewMongoContactRepository(
logger telemetry.Logger,
tracer telemetry.Tracer,
db *mongo.Database,
) ContactRepository {
return &mongoContactRepository{
logger: logger.WithService(fmt.Sprintf("%T", &mongoContactRepository{})),
tracer: tracer,
collection: db.Collection(collectionContacts),
}
}
func (repository *mongoContactRepository) Store(ctx context.Context, contacts []*entities.Contact) error {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
if len(contacts) == 0 {
return nil
}
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
documents := make([]any, len(contacts))
for index, contact := range contacts {
documents[index] = contact
}
if _, err := repository.collection.InsertMany(ctx, documents); err != nil {
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot store [%d] contacts", len(contacts)))
}
return nil
}
func (repository *mongoContactRepository) Update(ctx context.Context, contact *entities.Contact) error {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
filter := mongoContactIDFilter(contact.UserID, contact.ID)
if _, err := repository.collection.ReplaceOne(ctx, filter, contact); err != nil {
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot update contact with ID [%s]", contact.ID))
}
return nil
}
func (repository *mongoContactRepository) Load(ctx context.Context, userID entities.UserID, contactID uuid.UUID) (*entities.Contact, error) {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
contact := new(entities.Contact)
err := repository.collection.FindOne(ctx, mongoContactIDFilter(userID, contactID)).Decode(contact)
if err == mongo.ErrNoDocuments {
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCodef(err, ErrCodeNotFound, "contact with ID [%s] for user [%s] does not exist", contactID, userID))
}
if err != nil {
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot load contact with ID [%s] for user [%s]", contactID, userID))
}
return contact, nil
}
func (repository *mongoContactRepository) Index(ctx context.Context, userID entities.UserID, params IndexParams) (*[]entities.Contact, error) {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
findOptions := options.Find().
SetSort(mongoContactSort(params)).
SetSkip(int64(params.Skip)).
SetLimit(int64(params.Limit))
cursor, err := repository.collection.Find(ctx, mongoContactFilter(userID, params.Query), findOptions)
if err != nil {
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot index contacts for user [%s] with params [%+#v]", userID, params))
}
defer cursor.Close(ctx)
contacts := make([]entities.Contact, 0)
if err = cursor.All(ctx, &contacts); err != nil {
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot decode contacts for user [%s]", userID))
}
return &contacts, nil
}
func (repository *mongoContactRepository) Count(ctx context.Context, userID entities.UserID, params IndexParams) (int64, error) {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
count, err := repository.collection.CountDocuments(ctx, mongoContactFilter(userID, params.Query))
if err != nil {
return 0, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot count contacts for user [%s] with query [%s]", userID, params.Query))
}
return count, nil
}
func (repository *mongoContactRepository) FetchByPhoneNumbers(ctx context.Context, userID entities.UserID, phoneNumbers []string) (*[]entities.Contact, error) {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
findOptions := options.Find().SetSort(bson.D{
{Key: "updated_at", Value: 1},
{Key: "_id", Value: 1},
})
cursor, err := repository.collection.Find(ctx, mongoContactPhoneNumbersFilter(userID, phoneNumbers), findOptions)
if err != nil {
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot fetch contacts for user [%s] by phone numbers [%v]", userID, phoneNumbers))
}
defer cursor.Close(ctx)
contacts := make([]entities.Contact, 0)
if err = cursor.All(ctx, &contacts); err != nil {
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot decode contacts for user [%s] by phone numbers", userID))
}
return &contacts, nil
}
func (repository *mongoContactRepository) Delete(ctx context.Context, userID entities.UserID, contactID uuid.UUID) error {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
if _, err := repository.collection.DeleteOne(ctx, mongoContactIDFilter(userID, contactID)); err != nil {
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot delete contact with ID [%s] for user [%s]", contactID, userID))
}
return nil
}
func (repository *mongoContactRepository) DeleteAllForUser(ctx context.Context, userID entities.UserID) error {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
defer cancel()
if _, err := repository.collection.DeleteMany(ctx, bson.D{{Key: "user_id", Value: string(userID)}}); err != nil {
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot delete all contacts for user [%s]", userID))
}
return nil
}
func mongoContactFilter(userID entities.UserID, query string) bson.D {
filter := bson.D{{Key: "user_id", Value: string(userID)}}
if query == "" {
return filter
}
expression := bson.Regex{Pattern: regexp.QuoteMeta(query), Options: "i"}
return append(filter, bson.E{Key: "$or", Value: bson.A{
bson.D{{Key: "name", Value: expression}},
bson.D{{Key: "emails", Value: expression}},
bson.D{{Key: "phone_numbers", Value: expression}},
}})
}
func mongoContactSort(params IndexParams) bson.D {
sortBy := "updated_at"
if params.SortBy == "name" {
sortBy = "name"
}
direction := 1
if params.SortBy == "" || params.SortDescending {
direction = -1
}
return bson.D{
{Key: sortBy, Value: direction},
{Key: "_id", Value: direction},
}
}
func mongoContactPhoneNumbersFilter(userID entities.UserID, phoneNumbers []string) bson.D {
return bson.D{
{Key: "user_id", Value: string(userID)},
{Key: "phone_numbers", Value: bson.D{{Key: "$in", Value: phoneNumbers}}},
}
}
func mongoContactIDFilter(userID entities.UserID, contactID uuid.UUID) bson.D {
return bson.D{
{Key: "user_id", Value: string(userID)},
{Key: "_id", Value: contactID.String()},
}
}
|