|
| 1 | +package repositories |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 10 | + "go.mongodb.org/mongo-driver/v2/mongo" |
| 11 | + |
| 12 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 13 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 14 | + "github.com/palantir/stacktrace" |
| 15 | +) |
| 16 | + |
| 17 | +// mongoHeartbeatMonitorRepository is responsible for persisting entities.HeartbeatMonitor in MongoDB |
| 18 | +type mongoHeartbeatMonitorRepository struct { |
| 19 | + logger telemetry.Logger |
| 20 | + tracer telemetry.Tracer |
| 21 | + collection *mongo.Collection |
| 22 | +} |
| 23 | + |
| 24 | +// NewMongoHeartbeatMonitorRepository creates the MongoDB version of the HeartbeatMonitorRepository |
| 25 | +func NewMongoHeartbeatMonitorRepository( |
| 26 | + logger telemetry.Logger, |
| 27 | + tracer telemetry.Tracer, |
| 28 | + client *mongo.Client, |
| 29 | +) HeartbeatMonitorRepository { |
| 30 | + return &mongoHeartbeatMonitorRepository{ |
| 31 | + logger: logger.WithService(fmt.Sprintf("%T", &mongoHeartbeatMonitorRepository{})), |
| 32 | + tracer: tracer, |
| 33 | + collection: client.Database(mongoDBName).Collection(collectionHeartbeatMonitors), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +type heartbeatMonitorDocument struct { |
| 38 | + ID string `bson:"_id"` |
| 39 | + PhoneID string `bson:"phone_id"` |
| 40 | + UserID string `bson:"user_id"` |
| 41 | + QueueID string `bson:"queue_id"` |
| 42 | + Owner string `bson:"owner"` |
| 43 | + PhoneOnline bool `bson:"phone_online"` |
| 44 | + CreatedAt time.Time `bson:"created_at"` |
| 45 | + UpdatedAt time.Time `bson:"updated_at"` |
| 46 | +} |
| 47 | + |
| 48 | +func (repository *mongoHeartbeatMonitorRepository) Store(ctx context.Context, monitor *entities.HeartbeatMonitor) error { |
| 49 | + ctx, span := repository.tracer.Start(ctx) |
| 50 | + defer span.End() |
| 51 | + |
| 52 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 53 | + defer cancel() |
| 54 | + |
| 55 | + doc := heartbeatMonitorDocument{ |
| 56 | + ID: monitor.ID.String(), |
| 57 | + PhoneID: monitor.PhoneID.String(), |
| 58 | + UserID: string(monitor.UserID), |
| 59 | + QueueID: monitor.QueueID, |
| 60 | + Owner: monitor.Owner, |
| 61 | + PhoneOnline: monitor.PhoneOnline, |
| 62 | + CreatedAt: monitor.CreatedAt.UTC(), |
| 63 | + UpdatedAt: monitor.UpdatedAt.UTC(), |
| 64 | + } |
| 65 | + |
| 66 | + _, err := repository.collection.InsertOne(ctx, doc) |
| 67 | + if err != nil { |
| 68 | + msg := fmt.Sprintf("cannot save heartbeat monitor with ID [%s]", monitor.ID) |
| 69 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (repository *mongoHeartbeatMonitorRepository) Load(ctx context.Context, userID entities.UserID, phoneNumber string) (*entities.HeartbeatMonitor, error) { |
| 76 | + ctx, span := repository.tracer.Start(ctx) |
| 77 | + defer span.End() |
| 78 | + |
| 79 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 80 | + defer cancel() |
| 81 | + |
| 82 | + filter := bson.D{ |
| 83 | + {"user_id", string(userID)}, |
| 84 | + {"owner", phoneNumber}, |
| 85 | + } |
| 86 | + |
| 87 | + var doc heartbeatMonitorDocument |
| 88 | + err := repository.collection.FindOne(ctx, filter).Decode(&doc) |
| 89 | + if err == mongo.ErrNoDocuments { |
| 90 | + msg := fmt.Sprintf("heartbeat monitor with userID [%s] and owner [%s] does not exist", userID, phoneNumber) |
| 91 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg)) |
| 92 | + } |
| 93 | + if err != nil { |
| 94 | + msg := fmt.Sprintf("cannot load heartbeat monitor with userID [%s] and owner [%s]", userID, phoneNumber) |
| 95 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 96 | + } |
| 97 | + |
| 98 | + monitor, err := docToHeartbeatMonitor(doc) |
| 99 | + if err != nil { |
| 100 | + return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, "cannot convert heartbeat monitor document")) |
| 101 | + } |
| 102 | + |
| 103 | + return monitor, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (repository *mongoHeartbeatMonitorRepository) Exists(ctx context.Context, userID entities.UserID, monitorID uuid.UUID) (bool, error) { |
| 107 | + ctx, span := repository.tracer.Start(ctx) |
| 108 | + defer span.End() |
| 109 | + |
| 110 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 111 | + defer cancel() |
| 112 | + |
| 113 | + filter := bson.D{ |
| 114 | + {"user_id", string(userID)}, |
| 115 | + {"_id", monitorID.String()}, |
| 116 | + } |
| 117 | + |
| 118 | + count, err := repository.collection.CountDocuments(ctx, filter) |
| 119 | + if err != nil { |
| 120 | + msg := fmt.Sprintf("cannot check if heartbeat monitor exists with userID [%s] and monitor ID [%s]", userID, monitorID) |
| 121 | + return false, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 122 | + } |
| 123 | + |
| 124 | + return count > 0, nil |
| 125 | +} |
| 126 | + |
| 127 | +func (repository *mongoHeartbeatMonitorRepository) UpdateQueueID(ctx context.Context, monitorID uuid.UUID, queueID string) error { |
| 128 | + ctx, span := repository.tracer.Start(ctx) |
| 129 | + defer span.End() |
| 130 | + |
| 131 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 132 | + defer cancel() |
| 133 | + |
| 134 | + filter := bson.D{{"_id", monitorID.String()}} |
| 135 | + update := bson.D{{"$set", bson.D{ |
| 136 | + {"queue_id", queueID}, |
| 137 | + {"updated_at", time.Now().UTC()}, |
| 138 | + }}} |
| 139 | + |
| 140 | + _, err := repository.collection.UpdateOne(ctx, filter, update) |
| 141 | + if err != nil { |
| 142 | + msg := fmt.Sprintf("cannot update heartbeat monitor ID [%s]", monitorID) |
| 143 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (repository *mongoHeartbeatMonitorRepository) Delete(ctx context.Context, userID entities.UserID, phoneNumber string) error { |
| 150 | + ctx, span := repository.tracer.Start(ctx) |
| 151 | + defer span.End() |
| 152 | + |
| 153 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 154 | + defer cancel() |
| 155 | + |
| 156 | + filter := bson.D{ |
| 157 | + {"user_id", string(userID)}, |
| 158 | + {"owner", phoneNumber}, |
| 159 | + } |
| 160 | + |
| 161 | + _, err := repository.collection.DeleteMany(ctx, filter) |
| 162 | + if err != nil { |
| 163 | + msg := fmt.Sprintf("cannot delete heartbeat monitor with owner [%s] and userID [%s]", phoneNumber, userID) |
| 164 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func (repository *mongoHeartbeatMonitorRepository) UpdatePhoneOnline(ctx context.Context, userID entities.UserID, monitorID uuid.UUID, online bool) error { |
| 171 | + ctx, span := repository.tracer.Start(ctx) |
| 172 | + defer span.End() |
| 173 | + |
| 174 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 175 | + defer cancel() |
| 176 | + |
| 177 | + filter := bson.D{ |
| 178 | + {"_id", monitorID.String()}, |
| 179 | + {"user_id", string(userID)}, |
| 180 | + } |
| 181 | + update := bson.D{{"$set", bson.D{ |
| 182 | + {"phone_online", online}, |
| 183 | + {"updated_at", time.Now().UTC()}, |
| 184 | + }}} |
| 185 | + |
| 186 | + _, err := repository.collection.UpdateOne(ctx, filter, update) |
| 187 | + if err != nil { |
| 188 | + msg := fmt.Sprintf("cannot update heartbeat monitor ID [%s] for user [%s]", monitorID, userID) |
| 189 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 190 | + } |
| 191 | + |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +func (repository *mongoHeartbeatMonitorRepository) DeleteAllForUser(ctx context.Context, userID entities.UserID) error { |
| 196 | + ctx, span := repository.tracer.Start(ctx) |
| 197 | + defer span.End() |
| 198 | + |
| 199 | + ctx, cancel := context.WithTimeout(ctx, dbOperationDuration) |
| 200 | + defer cancel() |
| 201 | + |
| 202 | + _, err := repository.collection.DeleteMany(ctx, bson.D{{"user_id", string(userID)}}) |
| 203 | + if err != nil { |
| 204 | + msg := fmt.Sprintf("cannot delete all [%T] for user with ID [%s]", &entities.HeartbeatMonitor{}, userID) |
| 205 | + return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | +} |
| 210 | + |
| 211 | +func docToHeartbeatMonitor(doc heartbeatMonitorDocument) (*entities.HeartbeatMonitor, error) { |
| 212 | + id, err := uuid.Parse(doc.ID) |
| 213 | + if err != nil { |
| 214 | + return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse heartbeat monitor ID [%s]", doc.ID)) |
| 215 | + } |
| 216 | + phoneID, err := uuid.Parse(doc.PhoneID) |
| 217 | + if err != nil { |
| 218 | + return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse heartbeat monitor phone ID [%s]", doc.PhoneID)) |
| 219 | + } |
| 220 | + return &entities.HeartbeatMonitor{ |
| 221 | + ID: id, |
| 222 | + PhoneID: phoneID, |
| 223 | + UserID: entities.UserID(doc.UserID), |
| 224 | + QueueID: doc.QueueID, |
| 225 | + Owner: doc.Owner, |
| 226 | + PhoneOnline: doc.PhoneOnline, |
| 227 | + CreatedAt: doc.CreatedAt, |
| 228 | + UpdatedAt: doc.UpdatedAt, |
| 229 | + }, nil |
| 230 | +} |
0 commit comments