feat: use upsert for otp + implement otp methods for cassandradb

This commit is contained in:
Lakhan Samani
2022-07-23 16:39:35 +05:30
parent 22ae3bca54
commit f6029fb7bf
7 changed files with 86 additions and 82 deletions

View File

@@ -10,31 +10,20 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)
// AddOTP to add otp
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
// UpsertOTP to add or update otp
func (p *provider) UpsertOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
if otp.ID == "" {
otp.ID = uuid.New().String()
}
otp.Key = otp.ID
otp.CreatedAt = time.Now().Unix()
otp.UpdatedAt = time.Now().Unix()
otpCollection := p.db.Collection(models.Collections.OTP, options.Collection())
_, err := otpCollection.InsertOne(ctx, otp)
if err != nil {
return nil, err
if otp.CreatedAt <= 0 {
otp.CreatedAt = time.Now().Unix()
}
return otp, nil
}
// UpdateOTP to update otp for a given email address
func (p *provider) UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
otp.UpdatedAt = time.Now().Unix()
otpCollection := p.db.Collection(models.Collections.OTP, options.Collection())
_, err := otpCollection.UpdateOne(ctx, bson.M{"_id": bson.M{"$eq": otp.ID}}, bson.M{"$set": otp}, options.MergeUpdateOptions())
_, err := otpCollection.UpdateOne(ctx, bson.M{"_id": bson.M{"$eq": otp.ID}}, bson.M{"$set": otp}, options.MergeUpdateOptions().SetUpsert(true))
if err != nil {
return nil, err
}