65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package verificationimp
|
|
|
|
import (
|
|
ri "github.com/tech/sendico/pkg/db/repository/index"
|
|
"github.com/tech/sendico/pkg/db/template"
|
|
"github.com/tech/sendico/pkg/db/transaction"
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
"github.com/tech/sendico/pkg/model"
|
|
"github.com/tech/sendico/pkg/mservice"
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type verificationDB struct {
|
|
template.DBImp[*model.VerificationToken]
|
|
tf transaction.Factory
|
|
}
|
|
|
|
func Create(
|
|
logger mlogger.Logger,
|
|
db *mongo.Database,
|
|
tf transaction.Factory,
|
|
) (*verificationDB, error) {
|
|
p := &verificationDB{
|
|
DBImp: *template.Create[*model.VerificationToken](logger, mservice.VerificationTokens, db),
|
|
tf: tf,
|
|
}
|
|
|
|
if err := p.Repository.CreateIndex(&ri.Definition{
|
|
Keys: []ri.Key{{Field: "verifyTokenHash", Sort: ri.Asc}},
|
|
Unique: true,
|
|
Name: "unique_token_hash",
|
|
}); err != nil {
|
|
p.Logger.Error("Failed to create unique verifyTokenHash index", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
if err := p.Repository.CreateIndex(&ri.Definition{
|
|
Keys: []ri.Key{
|
|
{Field: "accountRef", Sort: ri.Asc},
|
|
{Field: "purpose", Sort: ri.Asc},
|
|
{Field: "target", Sort: ri.Asc},
|
|
{Field: "idempotencyKey", Sort: ri.Asc},
|
|
},
|
|
Unique: true,
|
|
Sparse: true,
|
|
Name: "uniq_verification_context_idempotency",
|
|
}); err != nil {
|
|
p.Logger.Error("Failed to create unique idempotency index on verification context", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
ttl := int32(2678400) // 30 days
|
|
if err := p.Repository.CreateIndex(&ri.Definition{
|
|
Keys: []ri.Key{{Field: "expiresAt", Sort: ri.Asc}},
|
|
TTL: &ttl,
|
|
Name: "ttl_expires_at",
|
|
}); err != nil {
|
|
p.Logger.Error("Failed to create TTL index on expiresAt", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return p, nil
|
|
}
|