diff --git a/internal/models/opt.go b/internal/models/opt.go new file mode 100644 index 0000000..f1b6a6f --- /dev/null +++ b/internal/models/opt.go @@ -0,0 +1,47 @@ +package models + +import ( + "github.com/google/uuid" + "gorm.io/gorm" +) + +func (u *OTP) BeforeCreate(tx *gorm.DB) error { + + uuid := uuid.New().String() + OTP := "should be Random six digit number" + + tx.Statement.SetColumn("ID", uuid) + tx.Statement.SetColumn("OPT", OTP) + return nil +} + +func (u *OTP) Create(opt OTP) (string, error) { + + // Expire all other existing user opts + result := db.Create(&opt) + + if result.Error != nil { + return "", result.Error + } + return opt.ID, nil +} + +func (u *OTP) FindOne(id string) (OTP, error) { + var opt OTP + db.First(&opt, "id = ?", id) + + return opt, nil +} + +func (u *OTP) FindByUser(userId string) (OTP, error) { + var otp OTP + db.First(&otp, "userId = ?", userId) + + return otp, nil +} + +func (u *OTP) ExpireUserOPT(userId string) error { + + //TODO: to expire opt here + return nil +} diff --git a/internal/models/schema.go b/internal/models/schema.go index 00365fb..e183e7d 100644 --- a/internal/models/schema.go +++ b/internal/models/schema.go @@ -22,7 +22,7 @@ type User struct { Role string `gorm:"column:role;default:'user';not null" json:"role"` App []App `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"apps"` Feedback []Feedback `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"feedbacks"` - OPT []OPT `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"OPT"` + OPT []OTP `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"OPT"` CreatedAt time.Time `gorm:"column:createdAt" json:"createdAt"` UpdatedAt time.Time `gorm:"column:updatedAt" json:"updatedAt"` DeletedAt gorm.DeletedAt `gorm:"column:deletedAt;index" json:"deletedAt"` @@ -73,10 +73,11 @@ type Feedback struct { UpdatedAt time.Time `gorm:"column:updatedAt;index" json:"updatedAt"` } -type OPT struct { +type OTP struct { ID string `gorm:"column:id;type:uuid;primaryKey" json:"id"` UserID string `gorm:"column:userId;not null;index" json:"userId"` - OPT string `gorm:"column:OPT;index" json:"OPT"` + OTP int `gorm:"column:OTP;index" json:"OTP"` + IsUsed bool `gorm:"column:isUsed;default:false" json:"isUsed"` ExpiresAt time.Time `gorm:"column:expiresAt;not null;index" json:"expiresAt"` CreatedAt time.Time `gorm:"column:createdAt" json:"createdAt"` UpdatedAt time.Time `gorm:"column:updatedAt" json:"updatedAt"`