nfp_sites/old/staff/model.mjs

83 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-02-05 11:50:01 +00:00
import { createPrototype, safeColumns } from '../knex.mjs'
2022-04-05 16:47:24 +00:00
// import bcrypt from 'bcrypt'
2021-02-05 11:50:01 +00:00
/*import config from '../config.mjs'*/
2019-02-19 11:34:52 +00:00
/* Staff model:
{
id,
username,
password,
fullname,
is_deleted,
level,
created_at,
updated_at,
}
*/
2021-02-05 11:50:01 +00:00
function StaffItem(data) {
Object.assign(this, data)
}
2021-02-05 11:50:01 +00:00
function Staff() {
this.tableName = 'staff'
this.Model = StaffItem
this.privateFields = safeColumns(['fullname','email','level',])
this.publicFields = ['id', 'fullname']
this.init()
}
2021-02-05 11:50:01 +00:00
Staff.prototype = createPrototype({
hash(password) {
return new Promise((resolve, reject) =>
bcrypt.hash(password, config.get('bcrypt'), (err, hashed) => {
if (err) return reject(err)
resolve(hashed)
})
)
},
compare(password, hashed) {
return new Promise((resolve, reject) =>
bcrypt.compare(password, hashed, (err, res) => {
if (err || !res) return reject(new Error('PasswordMismatch'))
resolve()
})
)
},
2021-02-05 11:50:01 +00:00
_getSingle(subq, includes = [], require = true, ctx = null) {
return this.getSingleQuery(this.query(qb => {
return qb
.where(qb => {
if (subq) subq(qb)
})
}, includes, this.privateFields), require)
},
/* getAll(ctx, where = {}, withRelated = [], orderBy = 'id') {
return this.query(qb => {
this.baseQueryAll(ctx, qb, where, orderBy)
qb.select(bookshelf.safeColumns([
'fullname',
'email',
'level',
]))
})
.fetchPage({
pageSize: ctx.state.pagination.perPage,
page: ctx.state.pagination.page,
withRelated,
ctx: ctx,
})
.then(result => {
ctx.state.pagination.total = result.pagination.rowCount
return result
})
2021-02-05 11:50:01 +00:00
}, */
2019-02-19 11:34:52 +00:00
})
2021-02-05 11:50:01 +00:00
export default new Staff()