2024-01-05 11:37:06 +00:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"codeword/internal/models"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-11 16:29:53 +00:00
|
|
|
|
type CodewordRepository struct {
|
2024-01-05 11:37:06 +00:00
|
|
|
|
mdb *mongo.Collection
|
|
|
|
|
rdb *redis.Client
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 16:29:53 +00:00
|
|
|
|
func NewCodewordRepository(deps Deps) *CodewordRepository {
|
2024-01-05 11:37:06 +00:00
|
|
|
|
|
2024-01-11 16:29:53 +00:00
|
|
|
|
return &CodewordRepository{mdb: deps.Mdb, rdb: deps.Rdb}
|
2024-01-05 11:37:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// сохраняем полученные данные о пользователе и подписи в бд
|
2024-01-11 16:29:53 +00:00
|
|
|
|
func (r *CodewordRepository) StoreRecoveryRecord(ctx context.Context, deps models.StoreRecDeps) (string, error) {
|
2024-01-05 11:37:06 +00:00
|
|
|
|
newID := primitive.NewObjectID()
|
2024-01-11 11:15:28 +00:00
|
|
|
|
signID := deps.Key + newID.Hex()
|
2024-01-05 11:37:06 +00:00
|
|
|
|
record := models.RestoreRequest{
|
|
|
|
|
ID: newID,
|
2024-01-11 11:15:28 +00:00
|
|
|
|
UserID: deps.UserID,
|
|
|
|
|
Email: deps.Email,
|
|
|
|
|
Sign: deps.Key,
|
|
|
|
|
SignUrl: deps.Url,
|
2024-01-07 11:13:07 +00:00
|
|
|
|
SignID: signID,
|
2024-01-05 11:37:06 +00:00
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := r.mdb.InsertOne(ctx, record)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newID.Hex(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// добавляем в очередь данные для отправки на почту в редис
|
2024-01-11 16:29:53 +00:00
|
|
|
|
func (r *CodewordRepository) InsertToQueue(ctx context.Context, deps models.RecEmailDeps) error {
|
2024-01-17 09:37:41 +00:00
|
|
|
|
sendLockKey := "email:sendLock:" + deps.Email
|
|
|
|
|
ttl := 5 * time.Minute
|
|
|
|
|
|
|
|
|
|
lockSuccess, err := r.rdb.SetNX(ctx, sendLockKey, "1", ttl).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !lockSuccess {
|
|
|
|
|
return ErrAlreadyReported
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 11:37:06 +00:00
|
|
|
|
task := models.RecoveryRecord{
|
2024-01-11 11:15:28 +00:00
|
|
|
|
ID: deps.ID,
|
|
|
|
|
UserID: deps.UserID,
|
|
|
|
|
Email: deps.Email,
|
|
|
|
|
Key: deps.SignWithID,
|
2024-01-05 11:37:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskBytes, err := json.Marshal(task)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 09:37:41 +00:00
|
|
|
|
return r.rdb.Set(ctx, "email:task:"+deps.Email, taskBytes, ttl).Err()
|
2024-01-05 11:37:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// получаем данные юзера по подписи
|
2024-01-11 16:29:53 +00:00
|
|
|
|
func (r *CodewordRepository) GetRecoveryRecord(ctx context.Context, key string) (*models.RestoreRequest, error) {
|
2024-01-05 11:37:06 +00:00
|
|
|
|
var restoreRequest models.RestoreRequest
|
|
|
|
|
|
2024-01-07 11:13:07 +00:00
|
|
|
|
filter := bson.M{"sign_id": key}
|
2024-01-05 11:37:06 +00:00
|
|
|
|
|
|
|
|
|
err := r.mdb.FindOne(ctx, filter).Decode(&restoreRequest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &restoreRequest, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// пингует в монгу чтобы проверить подключение
|
2024-01-11 16:29:53 +00:00
|
|
|
|
func (r *CodewordRepository) Ping(ctx context.Context) error {
|
2024-01-15 08:43:55 +00:00
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
2024-01-11 12:07:17 +00:00
|
|
|
|
if err := r.mdb.Database().Client().Ping(ctx, readpref.Primary()); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-01-18 17:49:02 +00:00
|
|
|
|
if err := r.rdb.Ping(ctx).Err(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-01-11 12:07:17 +00:00
|
|
|
|
return nil
|
2024-01-05 11:37:06 +00:00
|
|
|
|
}
|