github.com/resonatecoop/user-api@v1.0.0-13.0.20220915120639-05dc9c04014a/model/user.go (about) 1 package model 2 3 import ( 4 "database/sql" 5 "time" 6 7 uuid "github.com/google/uuid" 8 ) 9 10 // AuthUser represents data stored in session/context for a user 11 type AuthUser struct { 12 ID uuid.UUID 13 TenantID int32 14 Username string 15 Email string 16 Role AccessRole 17 } 18 19 // User basic definition of a User and its meta 20 type User struct { 21 IDRecord 22 LegacyID int32 `bun:",type:serial,notnull,unique"` 23 Username string `bun:",notnull,unique"` 24 FullName string 25 FirstName string 26 LastName string 27 EmailConfirmed bool `bun:"default:false"` 28 Country string `bun:"type:varchar(2)"` 29 Member bool `bun:"default:false,notnull"` 30 NewsletterNotification bool 31 FollowedGroups []uuid.UUID `bun:",type:uuid[],array"` 32 OwnerOfGroups []*UserGroup `bun:"rel:has-many"` 33 TenantID int32 34 RoleID int32 35 LastLogin time.Time 36 LastPasswordChange time.Time 37 Password sql.NullString `bun:"type:varchar(60)"` 38 Token string 39 // Email string `bun:",unique,notnull"` 40 // FavoriteTracks []uuid.UUID `bun:",type:uuid[]" pg:",array"` 41 // Playlists []uuid.UUID `bun:",type:uuid[]" pg:",array"` 42 // Plays []Track `pg:"many2many:plays"` Payment API 43 } 44 45 // UpdateLoginDetails updates login related fields 46 func (u *User) UpdateLoginDetails(token string) { 47 u.Token = token 48 t := time.Now() 49 u.LastLogin = t 50 } 51 52 //DeleteUser(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*Empty, error) 53 54 // Delete deletes a provided User 55 // func (u *User) Delete(db *bun.DB) (error, string) { 56 57 // // ctx context.Context 58 // pgerr := tx.Model(u). 59 // Column("user.followed_groups", "OwnerOfGroups"). 60 // WherePK(). 61 // Select() 62 // if pgerr != nil { 63 // return pgerr, "user" 64 // } 65 66 // // Column("user.favorite_tracks", "user.followed_groups", "user.playlists", "OwnerOfGroups"). 67 // // if len(u.FavoriteTracks) > 0 { 68 // // _, pgerr = tx.Exec(` 69 // // UPDATE tracks 70 // // SET favorite_of_users = array_remove(favorite_of_users, ?) 71 // // WHERE id IN (?) 72 // // `, u.Id, pg.In(u.FavoriteTracks)) 73 // // if pgerr != nil { 74 // // return pgerr, "track" 75 // // } 76 // // } 77 78 // if len(u.FollowedGroups) > 0 { 79 // _, pgerr = tx.Exec(` 80 // UPDATE user_groups 81 // SET followers = array_remove(followers, ?) 82 // WHERE id IN (?) 83 // `, u.ID, pg.In(u.FollowedGroups)) 84 // if pgerr != nil { 85 // return pgerr, "user_group" 86 // } 87 // } 88 89 // if len(u.OwnerOfGroups) > 0 { 90 // for _, group := range u.OwnerOfGroups { 91 // if pgerr, table := group.Delete(tx); pgerr != nil { 92 // return pgerr, table 93 // } 94 // } 95 // } 96 97 // // if len(u.Playlists) > 0 { 98 // // for _, playlistId := range u.Playlists { 99 // // playlist := &TrackGroup{Id: playlistId} 100 // // if pgerr, table := playlist.Delete(tx); pgerr != nil { 101 // // return pgerr, table 102 // // } 103 // // } 104 // // } 105 106 // pgerr = tx.Delete(u) 107 // if pgerr != nil { 108 // return pgerr, "user" 109 // } 110 111 // return nil, "" 112 // } 113 114 // FollowGroup causes a User to follow a UserGroup 115 // func (u *User) FollowGroup(db *pg.DB, userGroupID uuid.UUID) (error, string) { 116 // var table string 117 // tx, err := db.Begin() 118 // if err != nil { 119 // return err, table 120 // } 121 // defer tx.Rollback() 122 123 // // Add userGroupID to user FollowedGroups 124 // userGroupIDArr := []uuid.UUID{userGroupID} 125 // _, pgerr := tx.ExecOne(` 126 // UPDATE users 127 // SET followed_groups = (select array_agg(distinct e) from unnest(followed_groups || ?) e) 128 // WHERE id = ? 129 // `, pg.Array(userGroupIDArr), u.ID) 130 // if pgerr != nil { 131 // table = "user" 132 // return pgerr, table 133 // } 134 135 // // Add userID to userGroup Followers 136 // userIDArr := []uuid.UUID{u.ID} 137 // _, pgerr = tx.ExecOne(` 138 // UPDATE user_groups 139 // SET followers = (select array_agg(distinct e) from unnest(followers || ?) e) 140 // WHERE id = ? 141 // `, pg.Array(userIDArr), userGroupID) 142 // if pgerr != nil { 143 // table = "user_group" 144 // return pgerr, table 145 // } 146 // return tx.Commit(), table 147 // } 148 149 // // UnfollowGroup removes the follow state of the supplied User from the supplied userGroup via the supplied userGroupID 150 // func (u *User) UnfollowGroup(db *pg.DB, userGroupID uuid.UUID) (error, string) { 151 // var table string 152 // tx, err := db.Begin() 153 // if err != nil { 154 // return err, table 155 // } 156 // // Rollback tx on error. 157 // defer tx.Rollback() 158 159 // // Remove userGroupId from user FollowedGroups 160 // _, pgerr := tx.ExecOne(` 161 // UPDATE users 162 // SET followed_groups = array_remove(followed_groups, ?) 163 // WHERE id = ? 164 // `, userGroupID, u.ID) 165 // if pgerr != nil { 166 // table = "user" 167 // return pgerr, table 168 // } 169 170 // // Remove userId from track FavoriteOfUsers 171 // _, pgerr = tx.ExecOne(` 172 // UPDATE user_groups 173 // SET followers = array_remove(followers, ?) 174 // WHERE id = ? 175 // `, u.ID, userGroupID) 176 // if pgerr != nil { 177 // table = "user_group" 178 // return pgerr, table 179 // } 180 // return tx.Commit(), table 181 // } 182 183 // func (u *User) RemoveFavoriteTrack(db *pg.DB, trackId uuid.UUID) (error, string) { 184 // var table string 185 // tx, err := db.Begin() 186 // if err != nil { 187 // return err, table 188 // } 189 // // Rollback tx on error. 190 // defer tx.Rollback() 191 192 // // Remove trackId from user FavoriteTracks 193 // _, pgerr := tx.ExecOne(` 194 // UPDATE users 195 // SET favorite_tracks = array_remove(favorite_tracks, ?) 196 // WHERE id = ? 197 // `, trackId, u.ID) 198 // if pgerr != nil { 199 // table = "user" 200 // return pgerr, table 201 // } 202 203 // // Remove userId from track FavoriteOfUsers 204 // _, pgerr = tx.ExecOne(` 205 // UPDATE tracks 206 // SET favorite_of_users = array_remove(favorite_of_users, ?) 207 // WHERE id = ? 208 // `, u.Id, trackId) 209 // if pgerr != nil { 210 // table = "track" 211 // return pgerr, table 212 // } 213 // return tx.Commit(), table 214 // } 215 216 // func (u *User) AddFavoriteTrack(db *pg.DB, trackId uuid.UUID) (error, string) { 217 // var table string 218 // tx, err := db.Begin() 219 // if err != nil { 220 // return err, table 221 // } 222 // // Rollback tx on error. 223 // defer tx.Rollback() 224 225 // // Add trackId to user FavoriteTracks 226 // trackIdArr := []uuid.UUID{trackId} 227 // _, pgerr := tx.ExecOne(` 228 // UPDATE users 229 // SET favorite_tracks = (select array_agg(distinct e) from unnest(favorite_tracks || ?) e) 230 // WHERE id = ? 231 // `, pg.Array(trackIdArr), u.Id) 232 // // WHERE NOT favorite_tracks @> ? 233 // if pgerr != nil { 234 // table = "user" 235 // return pgerr, table 236 // } 237 238 // // Add userId to track FavoriteOfUsers 239 // userIdArr := []uuid.UUID{u.Id} 240 // _, pgerr = tx.ExecOne(` 241 // UPDATE tracks 242 // SET favorite_of_users = (select array_agg(distinct e) from unnest(favorite_of_users || ?) e) 243 // WHERE id = ? 244 // `, pg.Array(userIdArr), trackId) 245 // if pgerr != nil { 246 // table = "track" 247 // return pgerr, table 248 // } 249 // return tx.Commit(), table 250 // }