mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.7 KiB
88 lines
2.7 KiB
2 years ago
|
import { generateError, generateSuccess } from '../../helpers/graph.mjs'
|
||
|
import _ from 'lodash-es'
|
||
3 years ago
|
|
||
2 years ago
|
export default {
|
||
3 years ago
|
Query: {
|
||
|
async hooks () {
|
||
2 years ago
|
return WIKI.db.hooks.query().orderBy('name')
|
||
3 years ago
|
},
|
||
|
async hookById (obj, args) {
|
||
2 years ago
|
return WIKI.db.hooks.query().findById(args.id)
|
||
3 years ago
|
}
|
||
|
},
|
||
|
Mutation: {
|
||
|
/**
|
||
|
* CREATE HOOK
|
||
|
*/
|
||
|
async createHook (obj, args) {
|
||
|
try {
|
||
|
// -> Validate inputs
|
||
|
if (!args.name || args.name.length < 1) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookCreateInvalidName', 'Invalid Hook Name')
|
||
3 years ago
|
}
|
||
|
if (!args.events || args.events.length < 1) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookCreateInvalidEvents', 'Invalid Hook Events')
|
||
3 years ago
|
}
|
||
|
if (!args.url || args.url.length < 8 || !args.url.startsWith('http')) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookCreateInvalidURL', 'Invalid Hook URL')
|
||
3 years ago
|
}
|
||
|
// -> Create hook
|
||
2 years ago
|
const newHook = await WIKI.db.hooks.createHook(args)
|
||
2 years ago
|
WIKI.logger.debug(`New Hook ${newHook.id} created successfully.`)
|
||
|
|
||
3 years ago
|
return {
|
||
2 years ago
|
operation: generateSuccess('Hook created successfully'),
|
||
3 years ago
|
hook: newHook
|
||
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
3 years ago
|
}
|
||
|
},
|
||
|
/**
|
||
|
* UPDATE HOOK
|
||
|
*/
|
||
|
async updateHook (obj, args) {
|
||
|
try {
|
||
|
// -> Load hook
|
||
2 years ago
|
const hook = await WIKI.db.hooks.query().findById(args.id)
|
||
3 years ago
|
if (!hook) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookInvalidId', 'Invalid Hook ID')
|
||
3 years ago
|
}
|
||
|
// -> Check for bad input
|
||
|
if (_.has(args.patch, 'name') && args.patch.name.length < 1) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookCreateInvalidName', 'Invalid Hook Name')
|
||
3 years ago
|
}
|
||
|
if (_.has(args.patch, 'events') && args.patch.events.length < 1) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookCreateInvalidEvents', 'Invalid Hook Events')
|
||
3 years ago
|
}
|
||
|
if (_.has(args.patch, 'url') && (_.trim(args.patch.url).length < 8 || !args.patch.url.startsWith('http'))) {
|
||
2 years ago
|
throw new WIKI.Error.Custom('HookInvalidURL', 'URL is invalid.')
|
||
3 years ago
|
}
|
||
|
// -> Update hook
|
||
2 years ago
|
await WIKI.db.hooks.query().findById(args.id).patch(args.patch)
|
||
2 years ago
|
WIKI.logger.debug(`Hook ${args.id} updated successfully.`)
|
||
3 years ago
|
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('Hook updated successfully')
|
||
3 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
3 years ago
|
}
|
||
|
},
|
||
|
/**
|
||
|
* DELETE HOOK
|
||
|
*/
|
||
|
async deleteHook (obj, args) {
|
||
|
try {
|
||
2 years ago
|
await WIKI.db.hooks.deleteHook(args.id)
|
||
2 years ago
|
WIKI.logger.debug(`Hook ${args.id} deleted successfully.`)
|
||
3 years ago
|
return {
|
||
2 years ago
|
operation: generateSuccess('Hook deleted successfully')
|
||
3 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|