Scopes, Table, UpdatedAt
} from 'sequelize-typescript'
import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared'
-import { User } from '../../../shared/models/users'
+import { User, UserRole } from '../../../shared/models/users'
import {
isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
isUserVideoQuotaValid
@HasOne(() => AccountModel, {
foreignKey: 'userId',
- onDelete: 'cascade'
+ onDelete: 'cascade',
+ hooks: true
})
Account: AccountModel
return this.count()
}
- static getByUsername (username: string) {
- const query = {
- where: {
- username: username
- },
- include: [ { model: AccountModel, required: true } ]
- }
-
- return UserModel.findOne(query)
- }
-
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
- order: [ getSort(sort) ]
+ order: getSort(sort)
}
return UserModel.findAndCountAll(query)
})
}
+ static listEmailsWithRight (right: UserRight) {
+ const roles = Object.keys(USER_ROLE_LABELS)
+ .map(k => parseInt(k, 10) as UserRole)
+ .filter(role => hasUserRight(role, right))
+
+ console.log(roles)
+
+ const query = {
+ attribute: [ 'email' ],
+ where: {
+ role: {
+ [Sequelize.Op.in]: roles
+ }
+ }
+ }
+
+ return UserModel.unscoped()
+ .findAll(query)
+ .then(u => u.map(u => u.email))
+ }
+
static loadById (id: number) {
return UserModel.findById(id)
}
return UserModel.scope('withVideoChannel').findOne(query)
}
- static loadByUsernameOrEmail (username: string, email: string) {
+ static loadByEmail (email: string) {
+ const query = {
+ where: {
+ email
+ }
+ }
+
+ return UserModel.findOne(query)
+ }
+
+ static loadByUsernameOrEmail (username: string, email?: string) {
+ if (!email) email = username
+
const query = {
where: {
[ Sequelize.Op.or ]: [ { username }, { email } ]