/
user.go
53 lines (43 loc) · 1.34 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dbmodels
import (
"github.com/fullstaq-labs/sqedule/server/dbutils"
"gorm.io/gorm"
)
//
// ******** Types, constants & variables ********
//
type User struct {
OrganizationMember
Email string `gorm:"type:citext; primaryKey; not null"`
FirstName string `gorm:"not null"`
LastName string `gorm:"not null"`
}
//
// ******** User methods ********
//
// Type returns a name of the concrete type. This name is short,
// suitable for machine use, not user display purposes.
func (user User) Type() OrganizationMemberType {
return UserType
}
// ID returns the primary key's value, i.e. that of `User.Email`
// or `ServiceAccount.Name`.
func (user User) ID() string {
return user.Email
}
// IDTypeDisplayName returns the primary key's type as a lowercase string suitable
// for user display, i.e. "email" (for User) and "service account name" (for ServiceAccount).
func (user User) IDTypeDisplayName() string {
return "email"
}
//
// ******** Find/load functions ********
//
// FindUserByEmail looks up a User by its email address.
// When not found, returns a `gorm.ErrRecordNotFound` error.
func FindUserByEmail(db *gorm.DB, organizationID string, email string) (User, error) {
var result User
tx := db.Where("organization_id = ? AND email = ?", organizationID, email)
tx.Take(&result)
return result, dbutils.CreateFindOperationError(tx)
}