Files
speckle-server/packages/server/modules/webhooks/graph/resolvers/webhooks.js
T
andrewwallacespeckle 0045c353c6 Feature: FE2 - Project Webhooks Page (#1792)
* Layout Pages

* Add Multi Select with Badges

* Add MultiBadge

* Add prevent close on click outside

* Fix import issue

* Import Table

* Add Classnames to buttons

* Add Switch Component

* Update for webhooks

* skip precommit hooks

* Remove Infinite Load. Update Types

* Create Webhook Dialog

* Tidy Ups

* Edit Webhook dialog

* WIP Breadcurmbs

* Changes from calls with Fabians

* Breadcrumbs

* Reorders

* Fix Create Dialog

* Rename MultiBadge to BadgeSelected

* Fix and update Story file for Table

* Adjust Padding for Buttons in Table

* Add extra story, adjust padding for no buttons

* Fix bug with Edit Select

* fixed Webhook sorting + added Webhook.hasSecret and Webhook.projectId

* fixed hydration mismatch

* Changes from PR feedback

* Validation Rule for Select

* Reset Dialogs on Cancel. Conditionally render headers in Table

* stricter webhook gql types

* stricter webhook gql types

* Fix initial dialogs

* Quick Fixes

* Add projectWebhooksRoute

* Remove TableItemType

* Fixes from PR

* Fix broken Query

* Fixes from PR

* Fix based on PR

* Fix from PR

* Changes to index

* Fix in index

* Updates to Validation and Table

* Add "by" prop to FormSelectBadges and renamed component

* Use defineModel for Switch

* Revert "Use defineModel for Switch"

This reverts commit 6bc9e07a767cdc64f06c03b028150915e013ed4f.

* Replace breadcrumbs with projectWebhooksRoute

* Rename FormValues to WebhookFormValues

* Add target blank and simplify trigger mapping

* Fix casing of webhookFormValues

* Change webhookModel to prevent props mutation

* Remove unnecessary typecast

* Webhook deletion now uses fieldNameWhitelist.

* Use convertThrowIntoFetchResult and getFirstErrorMessage in Create

* Use defineModel for handling open state of Dialogs

* Optimise Switch component with defineModel

* Merge Create and Edit Dialogs

* Fix issue with Status Icons

* Remove console log

* WIP Merge of Edit and Create

* Add optional placeholder to SelectBase. Update Events placeholder.

* Add secret to Create webhook dialog

* Update Watch

* Rename Dialogs. Fix active select items

* Fix Select active items

* Simplify triggers, add secret to create call

* Remove $webhooksId: String

* fix: stale form state across edit/create webhook dialog sessions

* Fix from PR

* Swap t.text for t.id

* Use enum for historyStatus

* Use consistent story formatting

* More consistent create/edit mutations

* fix be linting errors

---------

Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
2023-09-26 15:41:29 +02:00

101 lines
2.8 KiB
JavaScript

const { ForbiddenError } = require('apollo-server-express')
const { authorizeResolver } = require('@/modules/shared')
const {
createWebhook,
getWebhook,
updateWebhook,
deleteWebhook,
getStreamWebhooks,
getLastWebhookEvents,
getWebhookEventsCount
} = require('../../services/webhooks')
const { Roles } = require('@speckle/shared')
const streamWebhooksResolver = async (parent, args, context) => {
await authorizeResolver(context.userId, parent.id, Roles.Stream.Owner)
if (args.id) {
const wh = await getWebhook({ id: args.id })
const items = wh ? [wh] : []
return { items, totalCount: items.length }
}
const items = await getStreamWebhooks({ streamId: parent.id })
return { items, totalCount: items.length }
}
module.exports = {
Stream: {
webhooks: streamWebhooksResolver
},
Project: {
webhooks: streamWebhooksResolver
},
Webhook: {
projectId: (parent) => parent.streamId,
hasSecret: (parent) => !!parent.secret?.length,
async history(parent, args) {
const items = await getLastWebhookEvents({
webhookId: parent.id,
limit: args.limit
})
const totalCount = await getWebhookEventsCount({ webhookId: parent.id })
return { items, totalCount }
}
},
Mutation: {
async webhookCreate(parent, args, context) {
await authorizeResolver(context.userId, args.webhook.streamId, Roles.Stream.Owner)
const id = await createWebhook({
streamId: args.webhook.streamId,
url: args.webhook.url,
description: args.webhook.description,
secret: args.webhook.secret,
enabled: args.webhook.enabled !== false,
triggers: args.webhook.triggers
})
return id
},
async webhookUpdate(parent, args, context) {
await authorizeResolver(context.userId, args.webhook.streamId, Roles.Stream.Owner)
const wh = await getWebhook({ id: args.webhook.id })
if (args.webhook.streamId !== wh.streamId)
throw new ForbiddenError(
'The webhook id and stream id do not match. Please check your inputs.'
)
const updated = await updateWebhook({
id: args.webhook.id,
url: args.webhook.url,
description: args.webhook.description,
secret: args.webhook.secret,
enabled: args.webhook.enabled !== false,
triggers: args.webhook.triggers
})
return !!updated
},
async webhookDelete(parent, args, context) {
await authorizeResolver(context.userId, args.webhook.streamId, Roles.Stream.Owner)
const wh = await getWebhook({ id: args.webhook.id })
if (args.webhook.streamId !== wh.streamId)
throw new ForbiddenError(
'The webhook id and stream id do not match. Please check your inputs.'
)
const deleted = await deleteWebhook({ id: args.webhook.id })
return !!deleted
}
}
}