53 lines
2.1 KiB
Go
53 lines
2.1 KiB
Go
package models
|
|
|
|
import (
|
|
"GoMembership/internal/constants"
|
|
"time"
|
|
|
|
"github.com/alexedwards/argon2id"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type User struct {
|
|
UpdatedAt time.Time
|
|
DateOfBirth time.Time `gorm:"not null" json:"date_of_birth" validate:"required,age"`
|
|
CreatedAt time.Time
|
|
Company string `json:"company" validate:"omitempty,omitnil"`
|
|
Phone string `json:"phone" validate:"omitempty,omitnil"`
|
|
Notes *string `json:"notes"`
|
|
FirstName string `gorm:"not null" json:"first_name" validate:"required"`
|
|
Password string `json:"password" required_unless=RoleID 0`
|
|
Email string `gorm:"unique;not null" json:"email" validate:"required,email"`
|
|
LastName string `gorm:"not null" json:"last_name" validate:"required"`
|
|
ProfilePicture string `json:"profile_picture" validate:"omitempty,omitnil,image"`
|
|
Address string `gorm:"not null" json:"address" validate:"required"`
|
|
ZipCode string `gorm:"not null" json:"zip_code" validate:"required,alphanum"`
|
|
City string `form:"not null" json:"city" validate:"required,alphaunicode"`
|
|
Consents []Consent `gorm:"constraint:OnUpdate:CASCADE"`
|
|
BankAccount BankAccount `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"bank_account"`
|
|
Verification Verification `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
|
|
Membership Membership `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"membership"`
|
|
ID int64 `gorm:"primaryKey"`
|
|
PaymentStatus int8 `json:"payment_status"`
|
|
Status int8 `json:"status"`
|
|
RoleID int8 `json:"role_id"`
|
|
}
|
|
|
|
// BeforeCreate sets the default value for Status
|
|
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
|
|
if u.Status == 0 { // Assuming 0 is an unset value
|
|
u.Status = constants.UnverifiedStatus
|
|
u.PaymentStatus = constants.AwaitingPaymentStatus
|
|
}
|
|
return
|
|
}
|
|
|
|
func (u *User) PasswordMatches(plaintextPassword string) (bool, error) {
|
|
match, err := argon2id.ComparePasswordAndHash(plaintextPassword, u.Password)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return match, nil
|
|
}
|