2022-01-21 07:23:30 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2022-01-31 06:05:24 +00:00
|
|
|
"time"
|
2022-01-21 07:23:30 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-01-25 05:27:40 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-01-21 07:23:30 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddVerification to save verification request in database
|
|
|
|
func (p *provider) AddVerificationRequest(verificationRequest models.VerificationRequest) (models.VerificationRequest, error) {
|
|
|
|
if verificationRequest.ID == "" {
|
|
|
|
verificationRequest.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationRequest.Key = verificationRequest.ID
|
2022-01-31 06:05:24 +00:00
|
|
|
verificationRequest.CreatedAt = time.Now().Unix()
|
|
|
|
verificationRequest.UpdatedAt = time.Now().Unix()
|
2022-01-21 07:23:30 +00:00
|
|
|
result := p.db.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
|
|
|
|
}).Create(&verificationRequest)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error saving verification request record`, result.Error)
|
|
|
|
return verificationRequest, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return verificationRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVerificationRequestByToken to get verification request from database using token
|
|
|
|
func (p *provider) GetVerificationRequestByToken(token string) (models.VerificationRequest, error) {
|
|
|
|
var verificationRequest models.VerificationRequest
|
|
|
|
result := p.db.Where("token = ?", token).First(&verificationRequest)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error getting verification request:`, result.Error)
|
|
|
|
return verificationRequest, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return verificationRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVerificationRequestByEmail to get verification request by email from database
|
|
|
|
func (p *provider) GetVerificationRequestByEmail(email string, identifier string) (models.VerificationRequest, error) {
|
|
|
|
var verificationRequest models.VerificationRequest
|
|
|
|
|
|
|
|
result := p.db.Where("email = ? AND identifier = ?", email, identifier).First(&verificationRequest)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error getting verification token:`, result.Error)
|
|
|
|
return verificationRequest, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return verificationRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListVerificationRequests to get list of verification requests from database
|
2022-01-25 05:27:40 +00:00
|
|
|
func (p *provider) ListVerificationRequests(pagination model.Pagination) (*model.VerificationRequests, error) {
|
2022-01-21 07:23:30 +00:00
|
|
|
var verificationRequests []models.VerificationRequest
|
|
|
|
|
2022-01-25 05:27:40 +00:00
|
|
|
result := p.db.Limit(int(pagination.Limit)).Offset(int(pagination.Offset)).Order("created_at DESC").Find(&verificationRequests)
|
2022-01-21 07:23:30 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error getting verification requests:", result.Error)
|
2022-01-25 05:27:40 +00:00
|
|
|
return nil, result.Error
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
2022-01-25 05:27:40 +00:00
|
|
|
|
|
|
|
responseVerificationRequests := []*model.VerificationRequest{}
|
|
|
|
for _, v := range verificationRequests {
|
|
|
|
responseVerificationRequests = append(responseVerificationRequests, v.AsAPIVerificationRequest())
|
|
|
|
}
|
|
|
|
|
|
|
|
var total int64
|
|
|
|
totalRes := p.db.Model(&models.VerificationRequest{}).Count(&total)
|
|
|
|
if totalRes.Error != nil {
|
|
|
|
return nil, totalRes.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
paginationClone := pagination
|
|
|
|
paginationClone.Total = total
|
|
|
|
|
|
|
|
return &model.VerificationRequests{
|
|
|
|
VerificationRequests: responseVerificationRequests,
|
|
|
|
Pagination: &paginationClone,
|
|
|
|
}, nil
|
2022-01-21 07:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVerificationRequest to delete verification request from database
|
|
|
|
func (p *provider) DeleteVerificationRequest(verificationRequest models.VerificationRequest) error {
|
|
|
|
result := p.db.Delete(&verificationRequest)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error deleting verification request:`, result.Error)
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|