feat: admin groups - list + create, gql refactoring

This commit is contained in:
NGPixel
2018-03-24 22:35:47 -04:00
parent cb253f7bfa
commit 7793df9bd4
28 changed files with 445 additions and 183 deletions

View File

@@ -11,11 +11,13 @@ defaults:
repo: ./repo
data: ./data
db:
type: postgres
host: localhost
port: 5432
user: wikijs
pass: wikijsrocks
db: wiki
storage: ./db.sqlite
redis:
host: localhost
port: 6379

View File

@@ -64,7 +64,8 @@ module.exports = {
this.inst = new this.Sequelize(WIKI.config.db.db, WIKI.config.db.user, WIKI.config.db.pass, {
host: WIKI.config.db.host,
port: WIKI.config.db.port,
dialect: 'postgres',
dialect: WIKI.config.db.type,
storage: WIKI.config.db.storage,
pool: {
max: 10,
min: 0,
@@ -77,9 +78,9 @@ module.exports = {
// Attempt to connect and authenticate to DB
this.inst.authenticate().then(() => {
WIKI.logger.info('Database (PostgreSQL) connection: [ OK ]')
WIKI.logger.info(`Database (${WIKI.config.db.type}) connection: [ OK ]`)
}).catch(err => {
WIKI.logger.error('Failed to connect to PostgreSQL instance.')
WIKI.logger.error(`Failed to connect to ${WIKI.config.db.type} instance.`)
WIKI.logger.error(err)
process.exit(1)
})
@@ -112,7 +113,10 @@ module.exports = {
},
// -> Set Connection App Name
setAppName() {
return self.inst.query(`set application_name = 'WIKI.js'`, { raw: true })
switch (WIKI.config.db.type) {
case 'postgres':
return self.inst.query(`set application_name = 'WIKI.js'`, { raw: true })
}
}
}

View File

@@ -35,7 +35,7 @@ module.exports = {
let authResult = await WIKI.db.User.login(args, context)
return {
...authResult,
operation: graphHelper.generateSuccess('Login success')
responseResult: graphHelper.generateSuccess('Login success')
}
} catch (err) {
return graphHelper.generateError(err)
@@ -46,7 +46,7 @@ module.exports = {
let authResult = await WIKI.db.User.loginTFA(args, context)
return {
...authResult,
operation: graphHelper.generateSuccess('TFA success')
responseResult: graphHelper.generateSuccess('TFA success')
}
} catch (err) {
return graphHelper.generateError(err)

View File

@@ -1,3 +1,4 @@
const graphHelper = require('../../helpers/graph')
/* global WIKI */
@@ -11,8 +12,22 @@ module.exports = {
async groups() { return {} }
},
GroupQuery: {
list(obj, args, context, info) {
return WIKI.db.Group.findAll({ where: args })
async list(obj, args, context, info) {
return WIKI.db.Group.findAll({
attributes: {
include: [[WIKI.db.inst.fn('COUNT', WIKI.db.inst.col('users.id')), 'userCount']]
},
include: [{
model: WIKI.db.User,
attributes: [],
through: {
attributes: []
}
}],
raw: true,
// TODO: Figure out how to exclude these extra fields...
group: ['group.id', 'users->userGroups.createdAt', 'users->userGroups.updatedAt', 'users->userGroups.version', 'users->userGroups.userId', 'users->userGroups.groupId']
})
}
},
GroupMutation: {
@@ -29,8 +44,15 @@ module.exports = {
})
})
},
create(obj, args) {
return WIKI.db.Group.create(args)
async create(obj, args) {
const group = await WIKI.db.Group.create({
name: args.name
})
console.info(group)
return {
responseResult: graphHelper.generateSuccess('Group created successfully.'),
group
}
},
delete(obj, args) {
return WIKI.db.Group.destroy({

View File

@@ -59,7 +59,7 @@ type AuthenticationProvider {
}
type AuthenticationLoginResponse {
operation: ResponseStatus
responseResult: ResponseStatus
tfaRequired: Boolean
tfaLoginToken: String
}

View File

@@ -39,12 +39,12 @@ input KeyValuePairInput {
}
type DefaultResponse {
operation: ResponseStatus
responseResult: ResponseStatus
}
type ResponseStatus {
succeeded: Boolean!
code: Int!
errorCode: Int!
slug: String!
message: String
}

View File

@@ -55,7 +55,7 @@ type GroupMutation {
# -----------------------------------------------
type GroupResponse {
operation: ResponseStatus!
responseResult: ResponseStatus!
group: Group
}
@@ -64,6 +64,7 @@ type Group {
name: String!
rights: [String]
users: [User]
userCount: Int
createdAt: Date!
updatedAt: Date!
}

View File

@@ -5,7 +5,7 @@ module.exports = {
generateSuccess (msg) {
return {
succeeded: true,
code: 0,
errorCode: 0,
slug: 'ok',
message: _.defaultTo(msg, 'Operation succeeded.')
}
@@ -13,11 +13,11 @@ module.exports = {
generateError (err, complete = true) {
const error = {
succeeded: false,
code: err.code || 1,
errorCode: err.code || 1,
slug: err.name,
message: err.message || 'An unexpected error occured.'
}
return (complete) ? { operation: error } : error
return (complete) ? { responseResult: error } : error
},
filter (arr, filterString) {
const prvFilter = new Filter(_.toString(filterString).replace(/'/g, `"`))

View File

@@ -14,7 +14,7 @@ module.exports = (sequelize, DataTypes) => {
defaultValue: 'application/octet-stream'
},
extra: {
type: DataTypes.JSONB,
type: DataTypes.JSON,
allowNull: true
},
filename: {

View File

@@ -8,7 +8,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false
},
config: {
type: DataTypes.JSONB,
type: DataTypes.JSON,
allowNull: false
}
}, {