mirror of https://github.com/requarks/wiki
parent
cc506a086d
commit
5a4a9df43a
@ -0,0 +1,85 @@
|
||||
const graphHelper = require('../../helpers/graph')
|
||||
const _ = require('lodash')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
module.exports = {
|
||||
Query: {
|
||||
async hooks () {
|
||||
return WIKI.models.hooks.query().orderBy('name')
|
||||
},
|
||||
async hookById (obj, args) {
|
||||
return WIKI.models.hooks.query().findById(args.id)
|
||||
}
|
||||
},
|
||||
Mutation: {
|
||||
/**
|
||||
* CREATE HOOK
|
||||
*/
|
||||
async createHook (obj, args) {
|
||||
try {
|
||||
// -> Validate inputs
|
||||
if (!args.name || args.name.length < 1) {
|
||||
throw WIKI.ERROR(new Error('Invalid Hook Name'), 'HookCreateInvalidName')
|
||||
}
|
||||
if (!args.events || args.events.length < 1) {
|
||||
throw WIKI.ERROR(new Error('Invalid Hook Events'), 'HookCreateInvalidEvents')
|
||||
}
|
||||
if (!args.url || args.url.length < 8 || !args.url.startsWith('http')) {
|
||||
throw WIKI.ERROR(new Error('Invalid Hook URL'), 'HookCreateInvalidURL')
|
||||
}
|
||||
// -> Create hook
|
||||
const newHook = await WIKI.models.hooks.createHook(args)
|
||||
return {
|
||||
operation: graphHelper.generateSuccess('Hook created successfully'),
|
||||
hook: newHook
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* UPDATE HOOK
|
||||
*/
|
||||
async updateHook (obj, args) {
|
||||
try {
|
||||
// -> Load hook
|
||||
const hook = await WIKI.models.hooks.query().findById(args.id)
|
||||
if (!hook) {
|
||||
throw WIKI.ERROR(new Error('Invalid Hook ID'), 'HookInvalidId')
|
||||
}
|
||||
// -> Check for bad input
|
||||
if (_.has(args.patch, 'name') && args.patch.name.length < 1) {
|
||||
throw WIKI.ERROR(new Error('Invalid Hook Name'), 'HookCreateInvalidName')
|
||||
}
|
||||
if (_.has(args.patch, 'events') && args.patch.events.length < 1) {
|
||||
throw WIKI.ERROR(new Error('Invalid Hook Events'), 'HookCreateInvalidEvents')
|
||||
}
|
||||
if (_.has(args.patch, 'url') && (_.trim(args.patch.url).length < 8 || !args.patch.url.startsWith('http'))) {
|
||||
throw WIKI.ERROR(new Error('URL is invalid.'), 'HookInvalidURL')
|
||||
}
|
||||
// -> Update hook
|
||||
await WIKI.models.hooks.query().findById(args.id).patch(args.patch)
|
||||
|
||||
return {
|
||||
operation: graphHelper.generateSuccess('Hook updated successfully')
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* DELETE HOOK
|
||||
*/
|
||||
async deleteHook (obj, args) {
|
||||
try {
|
||||
await WIKI.models.hooks.deleteHook(args.id)
|
||||
return {
|
||||
operation: graphHelper.generateSuccess('Hook deleted successfully')
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
# ===============================================
|
||||
# WEBHOOKS
|
||||
# ===============================================
|
||||
|
||||
extend type Query {
|
||||
hooks: [Hook]
|
||||
|
||||
hookById(
|
||||
id: UUID!
|
||||
): Hook
|
||||
}
|
||||
|
||||
extend type Mutation {
|
||||
createHook(
|
||||
name: String!
|
||||
events: [String]!
|
||||
url: String!
|
||||
includeMetadata: Boolean!
|
||||
includeContent: Boolean!
|
||||
acceptUntrusted: Boolean!
|
||||
authHeader: String
|
||||
): HookCreateResponse
|
||||
|
||||
updateHook(
|
||||
id: UUID!
|
||||
patch: HookUpdateInput!
|
||||
): DefaultResponse
|
||||
|
||||
deleteHook (
|
||||
id: UUID!
|
||||
): DefaultResponse
|
||||
}
|
||||
|
||||
# -----------------------------------------------
|
||||
# TYPES
|
||||
# -----------------------------------------------
|
||||
|
||||
type Hook {
|
||||
id: UUID
|
||||
name: String
|
||||
events: [String]
|
||||
url: String
|
||||
includeMetadata: Boolean
|
||||
includeContent: Boolean
|
||||
acceptUntrusted: Boolean
|
||||
authHeader: String
|
||||
state: HookState
|
||||
lastErrorMessage: String
|
||||
}
|
||||
|
||||
input HookUpdateInput {
|
||||
name: String
|
||||
events: [String]
|
||||
url: String
|
||||
includeMetadata: Boolean
|
||||
includeContent: Boolean
|
||||
acceptUntrusted: Boolean
|
||||
authHeader: String
|
||||
}
|
||||
|
||||
enum HookState {
|
||||
pending
|
||||
error
|
||||
success
|
||||
}
|
||||
|
||||
type HookCreateResponse {
|
||||
operation: Operation
|
||||
hook: Hook
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
const Model = require('objection').Model
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
/**
|
||||
* Hook model
|
||||
*/
|
||||
module.exports = class Hook extends Model {
|
||||
static get tableName () { return 'hooks' }
|
||||
|
||||
static get jsonAttributes () {
|
||||
return ['events']
|
||||
}
|
||||
|
||||
$beforeUpdate () {
|
||||
this.updatedAt = new Date()
|
||||
}
|
||||
|
||||
static async createHook (data) {
|
||||
return WIKI.models.hooks.query().insertAndFetch({
|
||||
name: data.name,
|
||||
events: data.events,
|
||||
url: data.url,
|
||||
includeMetadata: data.includeMetadata,
|
||||
includeContent: data.includeContent,
|
||||
acceptUntrusted: data.acceptUntrusted,
|
||||
authHeader: data.authHeader,
|
||||
state: 'pending',
|
||||
lastErrorMessage: null
|
||||
})
|
||||
}
|
||||
|
||||
static async updateHook (id, patch) {
|
||||
return WIKI.models.hooks.query().findById(id).patch({
|
||||
...patch,
|
||||
state: 'pending',
|
||||
lastErrorMessage: null
|
||||
})
|
||||
}
|
||||
|
||||
static async deleteHook (id) {
|
||||
return WIKI.models.hooks.query().deleteById(id)
|
||||
}
|
||||
}
|
@ -1,92 +1,106 @@
|
||||
<template lang="pug">
|
||||
q-dialog(ref='dialog', @hide='onDialogHide')
|
||||
q-dialog(ref='dialogRef', @hide='onDialogHide')
|
||||
q-card(style='min-width: 350px; max-width: 450px;')
|
||||
q-card-section.card-header
|
||||
q-icon(name='img:/_assets/icons/fluent-delete-bin.svg', left, size='sm')
|
||||
span {{$t(`admin.webhooks.delete`)}}
|
||||
span {{t(`admin.webhooks.delete`)}}
|
||||
q-card-section
|
||||
.text-body2
|
||||
i18n-t(keypath='admin.webhooks.deleteConfirm')
|
||||
template(v-slot:name)
|
||||
strong {{hook.name}}
|
||||
.text-body2.q-mt-md
|
||||
strong.text-negative {{$t(`admin.webhooks.deleteConfirmWarn`)}}
|
||||
strong.text-negative {{t(`admin.webhooks.deleteConfirmWarn`)}}
|
||||
q-card-actions.card-actions
|
||||
q-space
|
||||
q-btn.acrylic-btn(
|
||||
flat
|
||||
:label='$t(`common.actions.cancel`)'
|
||||
:label='t(`common.actions.cancel`)'
|
||||
color='grey'
|
||||
padding='xs md'
|
||||
@click='hide'
|
||||
@click='onDialogCancel'
|
||||
)
|
||||
q-btn(
|
||||
unelevated
|
||||
:label='$t(`common.actions.delete`)'
|
||||
:label='t(`common.actions.delete`)'
|
||||
color='negative'
|
||||
padding='xs md'
|
||||
@click='confirm'
|
||||
:loading='state.isLoading'
|
||||
)
|
||||
</template>
|
||||
|
||||
<script>
|
||||
<script setup>
|
||||
import gql from 'graphql-tag'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useDialogPluginComponent, useQuasar } from 'quasar'
|
||||
import { reactive } from 'vue'
|
||||
|
||||
export default {
|
||||
props: {
|
||||
hook: {
|
||||
type: Object
|
||||
}
|
||||
},
|
||||
emits: ['ok', 'hide'],
|
||||
data () {
|
||||
return {
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
show () {
|
||||
this.$refs.dialog.show()
|
||||
},
|
||||
hide () {
|
||||
this.$refs.dialog.hide()
|
||||
},
|
||||
onDialogHide () {
|
||||
this.$emit('hide')
|
||||
},
|
||||
async confirm () {
|
||||
try {
|
||||
const resp = await this.$apollo.mutate({
|
||||
mutation: gql`
|
||||
mutation deleteHook ($id: UUID!) {
|
||||
deleteHook(id: $id) {
|
||||
status {
|
||||
succeeded
|
||||
message
|
||||
}
|
||||
}
|
||||
// PROPS
|
||||
|
||||
const props = defineProps({
|
||||
hook: {
|
||||
type: Object,
|
||||
required: true
|
||||
}
|
||||
})
|
||||
|
||||
// EMITS
|
||||
|
||||
defineEmits([
|
||||
...useDialogPluginComponent.emits
|
||||
])
|
||||
|
||||
// QUASAR
|
||||
|
||||
const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent()
|
||||
const $q = useQuasar()
|
||||
|
||||
// I18N
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
// DATA
|
||||
|
||||
const state = reactive({
|
||||
isLoading: false
|
||||
})
|
||||
|
||||
// METHODS
|
||||
|
||||
async function confirm () {
|
||||
state.isLoading = true
|
||||
try {
|
||||
const resp = await APOLLO_CLIENT.mutate({
|
||||
mutation: gql`
|
||||
mutation deleteHook ($id: UUID!) {
|
||||
deleteHook(id: $id) {
|
||||
operation {
|
||||
succeeded
|
||||
message
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
id: this.hook.id
|
||||
}
|
||||
})
|
||||
if (resp?.data?.deleteHook?.status?.succeeded) {
|
||||
this.$q.notify({
|
||||
type: 'positive',
|
||||
message: this.$t('admin.webhooks.deleteSuccess')
|
||||
})
|
||||
this.$emit('ok')
|
||||
this.hide()
|
||||
} else {
|
||||
throw new Error(resp?.data?.deleteHook?.status?.message || 'An unexpected error occured.')
|
||||
}
|
||||
} catch (err) {
|
||||
this.$q.notify({
|
||||
type: 'negative',
|
||||
message: err.message
|
||||
})
|
||||
`,
|
||||
variables: {
|
||||
id: props.hook.id
|
||||
}
|
||||
})
|
||||
if (resp?.data?.deleteHook?.operation?.succeeded) {
|
||||
$q.notify({
|
||||
type: 'positive',
|
||||
message: t('admin.webhooks.deleteSuccess')
|
||||
})
|
||||
onDialogOK()
|
||||
} else {
|
||||
throw new Error(resp?.data?.deleteHook?.operation?.message || 'An unexpected error occured.')
|
||||
}
|
||||
} catch (err) {
|
||||
$q.notify({
|
||||
type: 'negative',
|
||||
message: err.message
|
||||
})
|
||||
}
|
||||
state.isLoading = false
|
||||
}
|
||||
</script>
|
||||
|
Loading…
Reference in new issue