2021-07-12 18:22:16 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-12-17 15:55:07 +00:00
|
|
|
"fmt"
|
2021-07-12 18:22:16 +00:00
|
|
|
"log"
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
"github.com/arangodb/go-driver"
|
2021-10-03 16:03:55 +00:00
|
|
|
"github.com/google/uuid"
|
2021-07-12 18:22:16 +00:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
2021-07-18 04:22:54 +00:00
|
|
|
type VerificationRequest struct {
|
2021-12-17 15:55:07 +00:00
|
|
|
Key string `json:"_key,omitempty"` // for arangodb
|
|
|
|
ObjectID string `json:"_id,omitempty"` // for arangodb & mongodb
|
|
|
|
ID string `gorm:"primaryKey;type:char(36)" json:"id"`
|
|
|
|
Token string `gorm:"type:text" json:"token"`
|
|
|
|
Identifier string `gorm:"uniqueIndex:idx_email_identifier" json:"identifier"`
|
|
|
|
ExpiresAt int64 `json:"expires_at"`
|
|
|
|
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
Email string `gorm:"uniqueIndex:idx_email_identifier" json:"email"`
|
2021-10-03 16:03:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:16 +00:00
|
|
|
// AddVerification function to add verification record
|
2021-07-18 04:22:54 +00:00
|
|
|
func (mgr *manager) AddVerification(verification VerificationRequest) (VerificationRequest, error) {
|
2021-12-17 15:55:07 +00:00
|
|
|
if verification.ID == "" {
|
|
|
|
verification.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
if IsSQL {
|
|
|
|
// copy id as value for fields required for mongodb & arangodb
|
|
|
|
verification.Key = verification.ID
|
|
|
|
verification.ObjectID = verification.ID
|
|
|
|
result := mgr.sqlDB.Clauses(clause.OnConflict{
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
|
|
|
|
}).Create(&verification)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error saving verification record`, result.Error)
|
|
|
|
return verification, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
verificationRequestCollection, _ := mgr.arangodb.Collection(nil, Collections.VerificationRequest)
|
|
|
|
meta, err := verificationRequestCollection.CreateDocument(nil, verification)
|
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
verification.Key = meta.Key
|
|
|
|
verification.ObjectID = meta.ID.String()
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
return verification, nil
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
// GetVerificationRequests function to get all verification requests
|
|
|
|
func (mgr *manager) GetVerificationRequests() ([]VerificationRequest, error) {
|
|
|
|
var verificationRequests []VerificationRequest
|
|
|
|
|
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Find(&verificationRequests)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error getting verification requests:", result.Error)
|
|
|
|
return verificationRequests, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s RETURN d", Collections.VerificationRequest)
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return verificationRequests, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var verificationRequest VerificationRequest
|
|
|
|
meta, err := cursor.ReadDocument(nil, &verificationRequest)
|
|
|
|
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return verificationRequests, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Key != "" {
|
|
|
|
verificationRequest.Key = meta.Key
|
|
|
|
verificationRequest.ObjectID = meta.ID.String()
|
|
|
|
verificationRequests = append(verificationRequests, verificationRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return verificationRequests, nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 04:22:54 +00:00
|
|
|
func (mgr *manager) GetVerificationByToken(token string) (VerificationRequest, error) {
|
|
|
|
var verification VerificationRequest
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Where("token = ?", token).First(&verification)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error getting verification request:`, result.Error)
|
|
|
|
return verification, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.token == @token LIMIT 1 RETURN d", Collections.VerificationRequest)
|
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := cursor.ReadDocument(nil, &verification)
|
|
|
|
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return verification, nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 07:26:17 +00:00
|
|
|
func (mgr *manager) GetVerificationByEmail(email string) (VerificationRequest, error) {
|
|
|
|
var verification VerificationRequest
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Where("email = ?", email).First(&verification)
|
2021-07-18 07:26:17 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error getting verification token:`, result.Error)
|
|
|
|
return verification, result.Error
|
|
|
|
}
|
2021-07-18 07:26:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.email == @email LIMIT 1 RETURN d", Collections.VerificationRequest)
|
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"email": email,
|
|
|
|
}
|
2021-07-18 07:26:17 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := cursor.ReadDocument(nil, &verification)
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
return verification, nil
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
2021-07-18 04:22:54 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
func (mgr *manager) DeleteVerificationRequest(verificationRequest VerificationRequest) error {
|
|
|
|
if IsSQL {
|
|
|
|
result := mgr.sqlDB.Delete(&verificationRequest)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error deleting verification request:`, result.Error)
|
|
|
|
return result.Error
|
|
|
|
}
|
2021-07-18 04:22:54 +00:00
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
collection, _ := mgr.arangodb.Collection(nil, Collections.VerificationRequest)
|
|
|
|
_, err := collection.RemoveDocument(nil, verificationRequest.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`error deleting verification request:`, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-07-18 04:22:54 +00:00
|
|
|
}
|