mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 12:50:05 +01:00
initial rebuild
This commit is contained in:
13
internal/db/models/common.go
Normal file
13
internal/db/models/common.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Timestamped struct {
|
||||
CreatedAt time.Time `gorm:"column:created_at;not null;default:now()" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime;column:updated_at;not null;default:now()" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
21
internal/db/models/email-verification.go
Normal file
21
internal/db/models/email-verification.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type EmailVerification struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
|
||||
UserID uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
|
||||
User User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"-"`
|
||||
Token string `gorm:"type:char(43);uniqueIndex;not null" json:"-"`
|
||||
ExpiresAt time.Time `gorm:"not null;index" json:"expires_at"`
|
||||
Used bool `gorm:"not null;default:false;index" json:"used"`
|
||||
Timestamped
|
||||
}
|
||||
|
||||
func (e EmailVerification) IsActive(now time.Time) bool {
|
||||
return !e.Used && now.Before(e.ExpiresAt)
|
||||
}
|
||||
19
internal/db/models/invitation.go
Normal file
19
internal/db/models/invitation.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Invitation struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey"`
|
||||
OrganizationID uuid.UUID `gorm:"type:uuid;not null"`
|
||||
Organization Organization `gorm:"foreignKey:OrganizationID;constraint:OnDelete:CASCADE" json:"organization"`
|
||||
Email string `gorm:"type:text;not null"`
|
||||
Role string `gorm:"type:text;default:'member';not null"`
|
||||
Status string `gorm:"type:text;default:'pending';not null"` // pending, accepted, revoked
|
||||
ExpiresAt time.Time `gorm:"not null"`
|
||||
InviterID uuid.UUID `gorm:"type:uuid;not null"`
|
||||
Timestamped
|
||||
}
|
||||
21
internal/db/models/member.go
Normal file
21
internal/db/models/member.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type MemberRole string
|
||||
|
||||
const (
|
||||
MemberRoleAdmin MemberRole = "admin"
|
||||
MemberRoleMember MemberRole = "member"
|
||||
MemberRoleUser MemberRole = "user"
|
||||
)
|
||||
|
||||
type Member struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
|
||||
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id"`
|
||||
User User `gorm:"foreignKey:UserID" json:"user"`
|
||||
OrganizationID uuid.UUID `gorm:"type:uuid;not null" json:"organization_id"`
|
||||
Organization Organization `gorm:"foreignKey:OrganizationID;constraint:OnDelete:CASCADE" json:"organization"`
|
||||
Role MemberRole `gorm:"not null;default:member" json:"role"` // e.g. admin, member
|
||||
Timestamped
|
||||
}
|
||||
12
internal/db/models/organization.go
Normal file
12
internal/db/models/organization.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Organization struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Slug string `gorm:"unique" json:"slug"`
|
||||
Logo string `json:"logo"`
|
||||
Metadata string `json:"metadata"`
|
||||
Timestamped
|
||||
}
|
||||
21
internal/db/models/password-reset.go
Normal file
21
internal/db/models/password-reset.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PasswordReset struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
|
||||
UserID uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
|
||||
User User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"-"`
|
||||
Token string `gorm:"type:char(43);uniqueIndex;not null" json:"-"`
|
||||
ExpiresAt time.Time `gorm:"not null;index" json:"expires_at"`
|
||||
Used bool `gorm:"not null;default:false;index" json:"used"`
|
||||
Timestamped
|
||||
}
|
||||
|
||||
func (p PasswordReset) IsActive(now time.Time) bool {
|
||||
return !p.Used && now.Before(p.ExpiresAt)
|
||||
}
|
||||
15
internal/db/models/refresh-token.go
Normal file
15
internal/db/models/refresh-token.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RefreshToken struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
|
||||
UserID uuid.UUID
|
||||
Token string `gorm:"uniqueIndex"`
|
||||
ExpiresAt time.Time
|
||||
Revoked bool
|
||||
}
|
||||
33
internal/db/models/user.go
Normal file
33
internal/db/models/user.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleAdmin Role = "admin"
|
||||
RoleUser Role = "user"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
|
||||
Name string `gorm:"type:varchar(255);not null" json:"name"`
|
||||
Email string `gorm:"uniqueIndex" json:"email"`
|
||||
EmailVerified bool `gorm:"default:false" json:"email_verified"`
|
||||
EmailVerifiedAt time.Time `gorm:"default:null" json:"email_verified_at"`
|
||||
Password string
|
||||
Role Role
|
||||
Timestamped
|
||||
}
|
||||
|
||||
func (r Role) IsValid() bool {
|
||||
switch r {
|
||||
case RoleAdmin, RoleUser:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user