2017-11-27 07:44:51 -06:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-11-27 10:30:46 -06:00
|
|
|
import { Response } from 'express'
|
2017-11-10 07:48:08 -06:00
|
|
|
import 'express-validator'
|
2017-11-23 11:04:48 -06:00
|
|
|
import * as validator from 'validator'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
2017-11-10 07:48:08 -06:00
|
|
|
import { isUserUsernameValid } from './users'
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
function isAccountNameValid (value: string) {
|
2017-11-10 07:48:08 -06:00
|
|
|
return isUserUsernameValid(value)
|
|
|
|
}
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
function isAccountIdExist (id: number | string, res: Response) {
|
2017-12-12 10:53:50 -06:00
|
|
|
let promise: Bluebird<AccountModel>
|
2017-11-27 07:44:51 -06:00
|
|
|
|
|
|
|
if (validator.isInt('' + id)) {
|
2017-12-12 10:53:50 -06:00
|
|
|
promise = AccountModel.load(+id)
|
2017-11-10 07:48:08 -06:00
|
|
|
} else { // UUID
|
2017-12-12 10:53:50 -06:00
|
|
|
promise = AccountModel.loadByUUID('' + id)
|
2017-11-10 07:48:08 -06:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
return isAccountExist(promise, res)
|
2017-11-27 07:44:51 -06:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
function isLocalAccountNameExist (name: string, res: Response) {
|
2017-12-12 10:53:50 -06:00
|
|
|
const promise = AccountModel.loadLocalByName(name)
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
return isAccountExist(promise, res)
|
2017-11-27 07:44:51 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function isAccountExist (p: Bluebird<AccountModel>, res: Response) {
|
2017-11-27 10:30:46 -06:00
|
|
|
const account = await p
|
|
|
|
|
|
|
|
if (!account) {
|
|
|
|
res.status(404)
|
|
|
|
.send({ error: 'Account not found' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.account = account
|
|
|
|
|
|
|
|
return true
|
2017-11-10 07:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-11-27 10:30:46 -06:00
|
|
|
isAccountIdExist,
|
|
|
|
isLocalAccountNameExist,
|
2017-11-14 10:31:26 -06:00
|
|
|
isAccountNameValid
|
2017-11-10 07:48:08 -06:00
|
|
|
}
|